SocketApp: Using inputbuffer within the loop() function

Discussion about any of the included sketches (i.e. WebServer), or user-generated applications.

SocketApp: Using inputbuffer within the loop() function

Postby Hackfrag » Sat Oct 17, 2009 5:51 am

Hey there,

im relative new to arduino / wishield. I read everything in the forum but nothing could help me.

I have the SocketApp example and i want to parse the input in the main loop function.

Here is my code in the socketapp.c
Code: Select all
extern char*message;
static int handle_connection(struct socket_app_state *s)
{
  PSOCK_BEGIN(&s->p);
  PSOCK_READTO(&s->p, '\n');
  PSOCK_SEND_STR(&s->p, s->inputbuffer);
  // put the inputbuffer in 'message'
  message =  s->inputbuffer;
  memset(s->inputbuffer, 0x00, sizeof(s->inputbuffer));
  PSOCK_SEND_STR(&s->p, "\n");
  PSOCK_END(&s->p);
}


SocketApp.pde
Code: Select all
char*message;
void setup()
{
  Serial.begin(9600);
  WiFi.init();     
}

void loop()
{
  WiFi.run();
  Serial.println(message);
}


But the Serial output prints nothing.

I tried memcpy, strcpy but nothing works :(.

Someone has a clue what im doing wrong?

Thanks for the replies !
Hackfrag
 
Posts: 5
Joined: Sat Oct 17, 2009 5:40 am

Re: SocketApp: Using inputbuffer within the loop() function

Postby GregEigsti » Sat Oct 17, 2009 8:01 am

Can't comment on your use of memcpy/strcpy. In this case you are assigning the pointer "message" to "s->inputbuffer" and then immediately doing a memset on "s->inputbuffer" and clearing it out. By the time that you try to print out "message" the memory that it points to (s->inputbuffer) contains nothing printable due to the previous memset.

The line "message = s->inputbuffer;" does not copy the data from s->inputbuffer to message - it merely assigns message to point at s->inputbuffer (think of message being an "alias" to s->inputbuffer).

When using mem/strcpy to copy the data from s->inputbuffer into message make sure that message actually points at some memory; currently it is just a char pointer and does not point at any actual data.

Greg
Check out the wiki!
uIP Stack Docs
Compatible Access Point List
WiShield user contrib branch - DNS, DHCP, AP Scanning, bug fixes, etc.
SlackLab.org - My geek projects blog.
User avatar
GregEigsti
 
Posts: 1067
Joined: Sun Aug 02, 2009 5:23 pm
Location: Sammamish WA USA (near Seattle)
  • Website

Re: SocketApp: Using inputbuffer within the loop() function

Postby Hackfrag » Sat Oct 17, 2009 8:08 am

Hey thanks for your reply!

I think i figured it out:

socket.c:
Code: Select all
  extern char buffer[20];
static int handle_connection(struct socket_app_state *s)
{
  PSOCK_BEGIN(&s->p);
  PSOCK_READTO(&s->p, '\n');
  PSOCK_SEND_STR(&s->p, s->inputbuffer);
  memcpy(buffer,s->inputbuffer,20);

  memset(s->inputbuffer, 0x00, sizeof(s->inputbuffer));
  PSOCK_SEND_STR(&s->p, "\n");
  PSOCK_END(&s->p);
}

Code: Select all
void loop()
{
  WiFi.run();
  if(strncmp(buffer, "SET LED", 7) == 0) {
 
    substr(positionX, buffer, 8,1);
    substr(positionY, buffer, 10,1);
    substr(color, buffer, 12,3);
   
    matrix.set(atoi(positionX),atoi(positionY), atoi(color));
   
    matrix.send();
  }

  memset(buffer, 0x00, sizeof(buffer));
 
}


this is now working.

Is there any better way to do this?
Hackfrag
 
Posts: 5
Joined: Sat Oct 17, 2009 5:40 am

Re: SocketApp: Using inputbuffer within the loop() function

Postby GregEigsti » Sat Oct 17, 2009 8:31 am

That is about the simplest way to go. There are different ways but "better" is subjective ;)
You are currently limiting your memcy to 20 bytes - which is good because your message buffer is only 20 bytes! Just make sure that your incoming data is less <= 20 bytes and you should be fine.

Instead of converting your "matrix" values to strings to send them across the network you could just send them as real numbers and avoid the atoi(), etc. But if this is working for you then who cares? ;)

Greg
Check out the wiki!
uIP Stack Docs
Compatible Access Point List
WiShield user contrib branch - DNS, DHCP, AP Scanning, bug fixes, etc.
SlackLab.org - My geek projects blog.
User avatar
GregEigsti
 
Posts: 1067
Joined: Sun Aug 02, 2009 5:23 pm
Location: Sammamish WA USA (near Seattle)
  • Website

Re: SocketApp: Using inputbuffer within the loop() function

Postby Hackfrag » Sun Oct 18, 2009 1:25 pm

here is my weekend result:

http://www.floriansweb.com/arduino/ardu ... f-fun.html

arduino => wishield => socket server <= cocoa socket client
Hackfrag
 
Posts: 5
Joined: Sat Oct 17, 2009 5:40 am

Re: SocketApp: Using inputbuffer within the loop() function

Postby GregEigsti » Sun Oct 18, 2009 1:51 pm

Nice work! I did something similar with the same LED matrix to show my Mac Messenger presence status on my office window. No WiFi/network just a USB connection to the board; also used a PIC as it was during my "pre-Arduino" days. But I have thought that this project would benefit from a Arduino + WiShield overhaul ;)

http://www.eigsti.com/Eigsti.com/Geek/Entries/2009/2/9_MEG.html

Greg
Check out the wiki!
uIP Stack Docs
Compatible Access Point List
WiShield user contrib branch - DNS, DHCP, AP Scanning, bug fixes, etc.
SlackLab.org - My geek projects blog.
User avatar
GregEigsti
 
Posts: 1067
Joined: Sun Aug 02, 2009 5:23 pm
Location: Sammamish WA USA (near Seattle)
  • Website

Re: SocketApp: Using inputbuffer within the loop() function

Postby Hackfrag » Wed Oct 21, 2009 1:08 pm

I played again with the wishield and cocoa.

I ported the mac app to my iphone:

http://www.floriansweb.com/arduino/ardu ... phone.html

You can control the LED Matrix over Wifi and on your iphone with the accelerometer.

Its so much fun! :)
Hackfrag
 
Posts: 5
Joined: Sat Oct 17, 2009 5:40 am

Re: SocketApp: Using inputbuffer within the loop() function

Postby GregEigsti » Wed Oct 21, 2009 2:19 pm

Good stuff!
Check out the wiki!
uIP Stack Docs
Compatible Access Point List
WiShield user contrib branch - DNS, DHCP, AP Scanning, bug fixes, etc.
SlackLab.org - My geek projects blog.
User avatar
GregEigsti
 
Posts: 1067
Joined: Sun Aug 02, 2009 5:23 pm
Location: Sammamish WA USA (near Seattle)
  • Website

Re: SocketApp: Using inputbuffer within the loop() function

Postby dannyx » Thu Oct 07, 2010 9:05 pm

Hey Greg is it possible to increase the size of the incoming message buffer? or is it limited to 20bytes? Is there a way around it?

Cheers
Danny
dannyx
 
Posts: 1
Joined: Thu Oct 07, 2010 8:59 pm

Re: SocketApp: Using inputbuffer within the loop() function

Postby GregEigsti » Fri Oct 08, 2010 2:31 pm

There is a #define somewhere (look around UIP_BUF, UIP_BUFLEN???) that defines the incoming buffer for the stack. Sorry don't have the exact name location (search spidermonkey04's posts - he recently posted on it). Your incoming buffer should be much larger than 20 bytes by default though - somewhere around 250-300 bytes.

Sorry, at work and don't have the AsyncLabs stuff with me. Let me know if you don't find it.

Greg
Check out the wiki!
uIP Stack Docs
Compatible Access Point List
WiShield user contrib branch - DNS, DHCP, AP Scanning, bug fixes, etc.
SlackLab.org - My geek projects blog.
User avatar
GregEigsti
 
Posts: 1067
Joined: Sun Aug 02, 2009 5:23 pm
Location: Sammamish WA USA (near Seattle)
  • Website

Next

Return to Sketches and Applications

Who is online

Users browsing this forum: No registered users and 2 guests

cron