UDPApp out of the box

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

Re: UDPApp out of the box

Postby GregEigsti » Wed May 26, 2010 5:51 pm

I don't suppose you have one that tests UDP packets in the other direction?

Probly, but not with me right now. I'll try to look at adding the "other half" of the conversation to the code that I posted last night. No guarantees, I'm currently running on very little sleep and could pass out at any time ;)
my router doesn't list the Yellowjacket as having an IP address. It comes up as 0.0.0.0.

Maybe but I would guess that it is not a problem - since you are effectively giving your WiShield/etc. a static IP your router may be reporting that it has not given it a DHCP address. Dunno, just a guess.

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: UDPApp out of the box

Postby seanblink » Wed May 26, 2010 6:50 pm

You're correct. The 0.0.0.0 does not matter. I tested a PC-to-PC UDP client-server application suite, where one PC had been statically assigned an IP address. The router saw it as 0.0.0.0, though it was still able to receive UDP messages from the other PC.

I also tried sending UDP messages to 192.168.1.255 to see if the Yellowjacket would pick them up. Nothing there either.

I'm starting to get the feeling that there aren't many more places to look for this problem...
seanblink
 
Posts: 10
Joined: Sat May 22, 2010 6:55 pm

Re: UDPApp out of the box

Postby GregEigsti » Wed May 26, 2010 10:53 pm

Ok, here is an update to the code posted last night. The WiShield app initiates the conversation by sending a UDP packet to the Ubuntu machine, the Ubuntu machine immediately sends that data back to the WiShield, and then it all starts over. This example, where both the WiShield and Ubuntu apps both send and receive, works well for me (its been running for an hour or so and has TX'ed/RX'ed ~5K packets back and forth).

WiShield .pde sketch
Code: Select all
/*
* UDP endpoint
*
*/

extern "C" {
   #include "uip.h"
}
#include <WiShield.h>

// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[]       = {192, 168, 1, 20};  // IP address of WiShield
unsigned char gateway_ip[]     = {192, 168, 1, 1};   // router or gateway IP address
unsigned char subnet_mask[]    = {255, 255, 255, 0}; // subnet mask for the local network
const prog_char ssid[] PROGMEM = {"SSID"};       // max 32 bytes

unsigned char security_type = 0;   // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"12345678"};   // max 64 characters
// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = {
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 0
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00  // Key 3
};

// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;
//---------------------------------------------------------------------------

//global shared data
int packetCount;


void setup()
{
   //init global data
   packetCount = 0;

   Serial.begin(57600);
   WiFi.init();
}

void loop()
{
   WiFi.run();
}

extern "C"
{
   void udpapp_init(void)
   {
      uip_ipaddr_t addr;
      struct uip_udp_conn *c;

      //address of the Ubuntu machine   
      uip_ipaddr(&addr, 192, 168, 1, 122);
      c = uip_udp_new(&addr, HTONS(1234));
      if(c != NULL) {
         uip_udp_bind(c, HTONS(1234));
      }
   }

   static void send_data(void)
   {
      char str[64];
     
      sprintf(str, "udpsample: this is packet %d of UDP data.", ++packetCount);
      Serial.print("[SEND DATA] -> ");
      Serial.println((const char*)str);
      memcpy(uip_appdata, str, strlen(str));
      uip_send(uip_appdata, strlen(str));
   }

   void udpapp_appcall(void)
   {
      //send data on the poll timer timeout
      if(0 != uip_poll()) {
         send_data();
      }
     
      //receive incoming data
      if(0 != uip_newdata()) {
         //NULL terminate the incoming data buffer
         *((byte*)uip_appdata + uip_datalen()) = '\0';
         Serial.print("[RECV DATA] -> ");
         Serial.println((const char*)uip_appdata);
      }
   }   

   void dummy_app_appcall(void)
   {
   }   
}


Ubuntu app source
Code: Select all
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#define PACKET_SIZE 128

int openListener(uint16_t port)
{
   int s;
   struct sockaddr_in server;
   socklen_t namelen = sizeof(server);
   
   s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
   if(-1 == s) {
      printf("ERROR: creating socket\n");
      return 0;
   }
      
   memset(&server, 0, namelen);
   server.sin_family = AF_INET;
   server.sin_addr.s_addr = htonl(INADDR_ANY);
   server.sin_port = htons(port);

      if(-1 == bind(s, (struct sockaddr *)&server, namelen)) {
      printf("ERROR: bind socket\n");
      close(s);
      s = -1;
   }

   return s;
}

void closeListener(int s)
{
   close(s);
}

void readListener(int s)
{
   int retval;
   char *ptime;
   time_t ltime;
   struct sockaddr_in remote;
   socklen_t namelen = sizeof(remote);
   unsigned char data[PACKET_SIZE];
   
   //configure remote address sockaddr_in struct
   memset(&remote, 0, namelen);
   remote.sin_family = AF_INET;
   remote.sin_port = htons(1234);
   inet_pton(AF_INET, "192.168.1.20", &remote.sin_addr);

   while(1) {
      //receive data from the WiShield
      memset(data, 0 , PACKET_SIZE);
      retval = recv(s, data, PACKET_SIZE, 0); 
      if(-1 == retval) {
         printf("WARNING: recv failed\n");
         continue;
      }
      
      if(0 == retval) {
         printf("WARNING: server closed connection\n");
         continue;
      }

      ltime = time(NULL);
      ptime = asctime(localtime(<ime));
      ptime[strlen(ptime)-1] = '\0';
      printf("%s [size %02d]: %s\n", ptime, retval, data);

      //send received data back to the WiShield
      retval = sendto(s, data, retval, 0, (struct sockaddr*)&remote, sizeof(remote));
      if(-1 == retval)
         printf("Error sending received data back to the WiShield...\n");
      else      
         printf("Success sending received data back to the WiShield...\n");
   }
}

int main(int argc, const char * argv[])
{
   int s;
   
   printf("udpsample: opening port 1234 to send and receive UDP data\n");
   
   //open the UDP socket on the given port to listen for UDP data
   s = openListener(1234);
   if(-1 == s) {
      printf("ERROR: opening port 1234\n");
      return 1;
   }
   
   //listen for data...
   readListener(s);
   
   //close the UDP listenener socket
   closeListener(s);
   
   return 0;
}


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: UDPApp out of the box

Postby GregEigsti » Fri May 28, 2010 5:03 pm

Did the combined TX/RX code work for you?

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: UDPApp out of the box

Postby DrStu » Thu Aug 26, 2010 1:54 am

Just in case anybody decided to try this out,

I just tried it without any modifications (besides changing IP properties). It works.

One tip though, remember to check if your router model is listed in the "working routers" list! (its listed somewhere in this forum). Mine is a Belkin model and it doesn't support any wireless security configuration (WEP or WPA). Had me scratching my head for a while there.

(Credits to Greg for his help and code :D )

Cheers!
DrStu
 
Posts: 15
Joined: Wed Aug 25, 2010 9:01 am

Re: UDPApp out of the box

Postby zach » Sat Sep 04, 2010 3:32 pm

After trying and failing with the example UDP code, I am trying to adapt the sketch in this thread for an urgent project I need to complete this weekend. All I need to do is send integers between two WiShields over an adhoc network. I was able to send strings to my Mac using this code, but am having trouble modifying it for integers (my C for AVRs isn't the sharpest).

Here is my code for sending:
Code: Select all
   static void send_data(void)
   {
      char* str;
      str = itoa(potValue, str, 10);
      Serial.println((const char*)str); 
      memcpy(uip_appdata, str, 1);
      uip_send(uip_appdata, 1);
   }


and for receiving:
Code: Select all
      if(0 != uip_newdata()) {
         //NULL terminate the incoming data buffer
         *((byte*)uip_appdata + uip_datalen()) = '\0';
         ledValue= atoi((const char*)uip_appdata);
         Serial.println((const char*)uip_appdata);
      }


Any idea what I'm doing wrong here?

Thanks,
Zach
zach
 
Posts: 12
Joined: Sat Sep 04, 2010 3:25 pm

Re: UDPApp out of the box

Postby GregEigsti » Sat Sep 04, 2010 10:24 pm

Without a description of what the error is and more supporting source its near impossible to say what is going wrong or why.

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: UDPApp out of the box

Postby zach » Sat Sep 04, 2010 10:39 pm

The portions I posted are the only parts of your C code I modified. PotValue and ledValue are global integers used in the loop() function- all standard Arduino code that works fine on it's own.

I have the print functions set up on each side and the sender says something to the effect of "AAVR I256" where I think 256 is potValue. It prints this only once and the receiving side prints nothing. I've checked all the IP settings and gotten this to work with strings, so I think the issue is just with how I'm converting the integers.

I would greatly appreciate any suggestions you can offer.
zach
 
Posts: 12
Joined: Sat Sep 04, 2010 3:25 pm

Re: UDPApp out of the box

Postby GregEigsti » Sun Sep 05, 2010 1:52 am

>>the issue is just with how I'm converting the integers
So potValue and ledValue are ints?

If you take a look at the AVR/Arduino reference for an int you will see that it an int is a two byte value - however you are only copying/sending a single byte - which could be a 0 which would terminate the string. So My guess is that could be the problem - but that is only a guess because the details are pretty slim.

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: UDPApp out of the box

Postby zach » Sun Sep 05, 2010 12:06 pm

I think that was actually it. I changed the send code and it works fine:

Code: Select all
   static void send_data(void)
   {
      char str[5];
      sprintf(str, "%d", potValue);
      Serial.println((const char*)str); 
      memcpy(uip_appdata, str, 5);
      uip_send(uip_appdata, 5);
   }


However, I am still having trouble on the receiving side. When potValue reaches its max (1023) the data flowing through the Serial Monitor halts. I think this could be causing a buffer overflow due to my use of atoi. Unfortunately, I do not know how to convert a string to int using something like sprintf. The following code samples yield the same result (halting on receiving 1023):

Code: Select all
   void udpapp_appcall(void)
   {
      //receive incoming data
      if(0 != uip_newdata()) {
         char str[5];
         memcpy(str, uip_appdata, 5);
         ledValue= atoi((const char*)str);
         Serial.println(ledValue);
      }
   }


Code: Select all
   void udpapp_appcall(void)
   {
      //receive incoming data
      if(0 != uip_newdata()) {
         ledValue= atoi((const char*)uip_appdata);
         Serial.println(ledValue);
      }
   }


If I get rid of the if statement, it keeps receiving 1023, but will not recognize any changes in potValue after that.

On another note, I also need the data on the receiving end to be picked up by an iPad app I am writing. Even if I could broadcast to two IP addresses, I cannot expect the user to manually set their IP address in the iPad settings. Is there a way to either broadcast to the entire network or set the IP address of the iPad from the Arduino and broadcast to both that and the other Arduino?

Thanks!

-Zach
zach
 
Posts: 12
Joined: Sat Sep 04, 2010 3:25 pm

PreviousNext

Return to Sketches and Applications

Who is online

Users browsing this forum: No registered users and 2 guests

cron