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 zach » Mon Sep 06, 2010 5:42 pm

Odd, i get the error "invalid conversion from 'int' to 'const void*" with that code in Arduino 19.
zach
 
Posts: 12
Joined: Sat Sep 04, 2010 3:25 pm

Re: UDPApp out of the box

Postby GregEigsti » Mon Sep 06, 2010 5:52 pm

I was trying from the WebServer sample which I had laying around and not your sources; different includes, etc., could make a difference. If you post your complete source I'd be more than happy to give it a try.

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 » Mon Sep 06, 2010 6:28 pm

These are the programs the way I got them to work converting between ints and chars. I commented out the way you were able to make it work.

Send:
Code: Select all
extern "C" {
   #include "uip.h"
}
#include <WiShield.h>

// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[]       = {169, 254, 209, 10};  // 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 = {"password"};   // 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
};

#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
unsigned char wireless_mode = WIRELESS_MODE_ADHOC;
unsigned char ssid_len;
unsigned char security_passphrase_len;
//---------------------------------------------------------------------------

int potValue = 0;


void setup()
{
   Serial.begin(57600);
   WiFi.init();
}

void loop()
{
   potValue = analogRead(0);
   WiFi.run();
}

extern "C"
{
   void udpapp_init(void)
   {
      uip_ipaddr_t addr;
      struct uip_udp_conn *c;
   
      uip_ipaddr(&addr, 255, 255, 255, 255);
      c = uip_udp_new(&addr, HTONS(1234));
      if(c != NULL) {
         uip_udp_bind(c, HTONS(1234));
      }
   }

   static void send_data(void)
   {
      char str[5];
      sprintf(str, "%d", (potValue-5));
      Serial.println((const char*)str); 
      memcpy(uip_appdata, str, 5);
      //memcpy(uip_appdata, potValue, 2);  <--error
      uip_send(uip_appdata, 5);
   }

   void udpapp_appcall(void)
   {
      //send data on the poll timer timeout
      if(0 != uip_poll()) {
         send_data();
      }   
   }   

   void dummy_app_appcall(void)
   {
   }
}


Receive:
Code: Select all
extern "C" {
   #include "uip.h"
}
#include <WiShield.h>

// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[]       = {169, 254, 209, 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 = {"password"};   // 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
};

#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
unsigned char wireless_mode = WIRELESS_MODE_ADHOC;
unsigned char ssid_len;
unsigned char security_passphrase_len;
//---------------------------------------------------------------------------

int ledValue = 0;


void setup()
{
   Serial.begin(57600);
   WiFi.init();
}

void loop()
{
   analogWrite(2, ledValue);   
   WiFi.run();

}

extern "C"
{
   void udpapp_init(void)
   {
      uip_ipaddr_t addr;
      struct uip_udp_conn *c;
 
      uip_ipaddr(&addr, 255, 255, 255, 255);
      c = uip_udp_new(&addr, HTONS(1234));
      if(c != NULL) {
         uip_udp_bind(c, HTONS(1234));
      }
   }

   void udpapp_appcall(void)
   {
      //receive incoming data
      if(0 != uip_newdata()) {
         char str[5];
         memcpy(str, uip_appdata, 5);
         //memcpy(ledValue, uip_appdata, 2);  <--error
         ledValue= atoi((const char*)str);
         Serial.println(ledValue);
      }
   }   

   void dummy_app_appcall(void)
   {
   }   
}
zach
 
Posts: 12
Joined: Sat Sep 04, 2010 3:25 pm

Re: UDPApp out of the box

Postby GregEigsti » Mon Sep 06, 2010 7:44 pm

Maybe this small sample program will will help. memcpy() takes pointers as the first two params and you/I/we were not passing pointers. BTW - I never claimed that it worked it just built ;) The following sample does work and should easily be applied to your code or my name is not Bob.

Code: Select all
void setup()
{
   Serial.begin(57600);

   int first;
   int second;
   unsigned char buffer[32];

   // small integer test case
   first = 8;
   second = 0;   
   memset(buffer, 0, 32 * sizeof(unsigned char));
   // your send case
   memcpy(buffer, &first, 2);
   // your receive case
   memcpy(&second, buffer, 2);
   
   Serial.print("first    : ");
   Serial.println(first, DEC);
   Serial.print("second   : ");
   Serial.println(second, DEC);
   Serial.print("buffer[0]: ");
   Serial.println(buffer[0], DEC);
   Serial.print("buffer[1]: ");
   Serial.println(buffer[1], DEC);

   // larger integer test case
   first = 888;
   second = 0;   
   memset(buffer, 0, 32 * sizeof(unsigned char));
   // your send case
   memcpy(buffer, &first, 2);
   // your receive case
   memcpy(&second, buffer, 2);
   
   Serial.print("first    : ");
   Serial.println(first, DEC);
   Serial.print("second   : ");
   Serial.println(second, DEC);
   Serial.print("buffer[0]: ");
   Serial.println(buffer[0], DEC);
   Serial.print("buffer[1]: ");
   Serial.println(buffer[1], DEC);
}

void loop()
{
}
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 » Tue Sep 07, 2010 1:29 pm

That works. Unfortunately, I just realized the openFrameworks UDP implementation (which I'm using for the iPhone app) only accepts chars...
zach
 
Posts: 12
Joined: Sat Sep 04, 2010 3:25 pm

Re: UDPApp out of the box

Postby GregEigsti » Tue Sep 07, 2010 11:54 pm

Looks like you will need to coerce your data into a value that fits into 0-255 then...
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 » Wed Sep 08, 2010 8:31 pm

Greg,

Now that I have it working, I'm trying to send multiple values back and forth. I figured the best way was to encode them as arrays of three chars, with the first being the pin number and the second two being the value. I have it set up with a for loop to cycle through an array of sensor values and send them out. Unfortunately, it is only sending the last value (pin 9). I figured this was because of the latency built into UDP so it doesn't crash networks, but adding a delay inside the loop did not fix the problem. Any idea what's going on here?

Code: Select all
extern "C" {
   #include "uip.h"
}
#include <WiShield.h>

// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[]       = {169, 254, 209, 10};  // 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 = {"horsies"};       // 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 = {"password"};   // 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
};

#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
unsigned char wireless_mode = WIRELESS_MODE_ADHOC;
unsigned char ssid_len;
unsigned char security_passphrase_len;
//---------------------------------------------------------------------------

int sensors[10];

void setup()
{
   Serial.begin(57600);
   WiFi.init();
}

void loop()

   for(int i = 0; i < 10; i++){
     sensors[i] = map(analogRead(i), 0, 1023, 0, 255);
   }
   WiFi.run();
}

extern "C"
{
   void udpapp_init(void)
   {
      uip_ipaddr_t addr;
      struct uip_udp_conn *c;
   
      uip_ipaddr(&addr, 255, 255, 255, 255);
      c = uip_udp_new(&addr, HTONS(1234));
      if(c != NULL) {
         uip_udp_bind(c, HTONS(1234));
      }
   }

   static void send_data(void)
   {
      for(int i = 0; i < 3; i++){
        char str[3];
        sprintf(str, "%i%i", i, sensors[i]);
        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();
      }   
   }   

   void dummy_app_appcall(void)
   {
   }
}


I also don't think the conversion back to ints on the receive side is working (I tested it with just one value). Here is the code for that:

Code: Select all
extern "C" {
   #include "uip.h"
}
#include <WiShield.h>

// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[]       = {169, 254, 209, 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 = {"horsies"};       // 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 = {"password"};   // 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
};

#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
unsigned char wireless_mode = WIRELESS_MODE_ADHOC;
unsigned char ssid_len;
unsigned char security_passphrase_len;
//---------------------------------------------------------------------------

int motors[10];


void setup()
{
   Serial.begin(57600);
   WiFi.init();
}

void loop()
{
   for(int i = 0; i < 10; i++){
     analogWrite(i, motors[i]);
   }
   
   WiFi.run();
}

extern "C"
{
   void udpapp_init(void)
   {
      uip_ipaddr_t addr;
      struct uip_udp_conn *c;
 
      uip_ipaddr(&addr, 255, 255, 255, 255);
      c = uip_udp_new(&addr, HTONS(1234));
      if(c != NULL) {
         uip_udp_bind(c, HTONS(1234));
      }
   }

   void udpapp_appcall(void)
   {
     if(0 != uip_newdata()){
       char str[3];
       memcpy(str, uip_appdata, strlen(str));
       int index = str[0];
       str[0] = str[1];
       str[1] = str[2];
       str[2] = NULL;
       motors[index]= atoi((const char*)str);
       Serial.println(motors[index]);
     }
   }   

   void dummy_app_appcall(void)
   {
   }   
}


Thanks!

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

Re: UDPApp out of the box

Postby GregEigsti » Fri Sep 10, 2010 12:58 am

I think UDP is not the answer. You will find that it will cause more headaches than it is worth - TCP is going to be more robust and since it is connection oriented it will not (or should not) suffer from the kinds of problems that you are now fighting against.

Sorry, really busy, really tired and off to bed.

Just my $.02
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

Previous

Return to Sketches and Applications

Who is online

Users browsing this forum: No registered users and 1 guest