Pages: 1 2 [3] 4   Go Down
Author Topic: Arduino WiFi Shield UDP Support  (Read 15923 times)
0 Members and 1 Guest are viewing this topic.
Antwerp, Belgium
Offline Offline
Jr. Member
**
Karma: 2
Posts: 64
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Srrichie,

We live in a finite world, so I assume everything has its limits :-)
The thing with UDP is that it doesn't guarantee packages being delivered in the right order or at all. It's just very fast. Here's some interesting stuff about it: http://stackoverflow.com/questions/47903/udp-vs-tcp-how-much-faster-is-it

Did you get my example to work?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thx for you super-fast reply
I'm running it, but I just noticed that your sketch is printing me that I don't have the correct IP address....
------
Connected to wifi
SSID: todonet
IP Address: 0.0.0.0
----

any hint?
Logged

Antwerp, Belgium
Offline Offline
Jr. Member
**
Karma: 2
Posts: 64
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So this is the IP address you are getting back from the example sketch I posted here?
well... http://en.wikipedia.org/wiki/0.0.0.0 :-)

it seems your router is not giving you an IP.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, I've setup a network for my test and now I have the IP address assigned.

Just to be clear: my setup is composed by an Arduino reading 4 sensor and sending their values via UDP packets to a Node.js server. Everything was working fine with my Ethernet shield of course smiley


I had 3 global variables declared:
IPAddress receiverIP(192, 168, 2, 34); // IP of udp packets receiver
unsigned int receiverPort = 9100;      // port to listen on my PC
EthernetUDP Udp;

In my setup I was initializing the EthernetUDP object:

Udp.begin(9100);


...and then I my loop I was reading my sensors and sending them like this:

byte valueInBytes[8] = {lowByte(firstSensorValue), highByte(firstSensorValue),
                          lowByte(secondSensorValue), highByte(secondSensorValue),
                          lowByte(thirdSensorValue), highByte(thirdSensorValue),
                          lowByte(forthSensorValue), highByte(forthSensorValue)
                         }; //convert it to byte array
 
  Udp.beginPacket(receiverIP, receiverPort); //start udp packet
  Udp.write(valueInBytes, smiley-cool; //write sensor data to udp packet
  Udp.endPacket(); // end packet


Now, I'm a n00b and the setup it's getting quite complex for me, but since I switch to your library I don't know how to adapt my code to have everything working.
Any help would be appreciated smiley
Logged

Antwerp, Belgium
Offline Offline
Jr. Member
**
Karma: 2
Posts: 64
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

srrichie,

just strip your sketch from all the calls to ethernetUDP and replace it with the appropriate parts of WifiUDP calls in the example I posted above in this thread.

so
Code:
EthernetUDP Udp;
becomes
Code:
WiFiUDP Udp;

etc...
and if something doesn't work, post your complete code then we can take a look at it and quickly see any mistakes.
I would also suggest to always compile with verbose output. You can switch verbose output on in Arduino > Preferences 
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks again for your help.
Your suggestion was already implemented in my code (I'm not THAT noob  smiley-razz )

here goes my code


Code:

#include <WiFi.h>
#include <WiFiUdp.h>
#include <SPI.h>


char ssid[] = "customNet";   // SSID of your network
char pass[] = "customPassword";  // password of your  Network
int status = WL_IDLE_STATUS;     // the Wifi radio's status

IPAddress receiverIP(192, 168, 1, 212); // IP of udp packets receiver
unsigned int receiverPort = 9100;      // port to listen on receiver machine

WiFiUDP Udp;

int firstSensorPin = A0; //define sensor pin
int secondSensorPin = A1;
int thirdSensorPin = A2;
int forthSensorPin = A3;

int firstSensorValue;
int secondSensorValue;
int thirdSensorValue;
int forthSensorValue;

void setup() {
 
  Serial.begin (9600);
 
 // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);      // Connect to WPA/WPA2 network:   
    delay(5000);      // wait 5 seconds for connection:
  }
  delay (2000);

  Serial.print("You're connected to the network");
  printCurrentNet(); 
  Udp.begin(receiverPort);
}



void loop() {
 
  firstSensorValue = analogRead(firstSensorPin);
  secondSensorValue = analogRead (secondSensorPin);
  thirdSensorValue = analogRead (thirdSensorPin);
  forthSensorValue = analogRead (forthSensorPin);
 
  byte valueInBytes[8] = {lowByte(firstSensorValue), highByte(firstSensorValue),
                          lowByte(secondSensorValue), highByte(secondSensorValue),
                          lowByte(thirdSensorValue), highByte(thirdSensorValue),
                          lowByte(forthSensorValue), highByte(forthSensorValue)
                         }; //convert it to byte array
 
  Udp.beginPacket(receiverIP, receiverPort); //start udp packet
  Udp.write(valueInBytes, 8); //write sensor data to udp packet
  Udp.endPacket(); // end packet

  delay(10000);
 
}




void printCurrentNet() {
  // print the SSID:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print the MAC address of the router you're attached to:
  byte bssid[6];
  WiFi.BSSID(bssid);   
  Serial.print("BSSID: ");
  Serial.print(bssid[5],HEX);
  Serial.print(":");
  Serial.print(bssid[4],HEX);
  Serial.print(":");
  Serial.print(bssid[3],HEX);
  Serial.print(":");
  Serial.print(bssid[2],HEX);
  Serial.print(":");
  Serial.print(bssid[1],HEX);
  Serial.print(":");
  Serial.println(bssid[0],HEX);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.println(rssi);

  // print the encryption type:
  byte encryption = WiFi.encryptionType();
  Serial.print("Encryption Type:");
  Serial.println(encryption,HEX);
  Serial.println();
 
  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

Logged

Antwerp, Belgium
Offline Offline
Jr. Member
**
Karma: 2
Posts: 64
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

srrichie,

Quote
Your suggestion was already implemented in my code (I'm not THAT noob   )
Sorry about that, I didn't mean to call you a noob, it's just difficult to hunt down bugs when you don't post full code.

the sketch you posted compiles just fine. Could you provide more information about the errors you get?

best,
tim.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hey don't worry it was a joke smiley

About my code: actually I don't have any error, it compiles and run without any issue, but the problem is that the server on the listening machine isn't receiving anything....
Is the initialization of your class correct? Is it right I'm initializing it like this:

Code:
Udp.begin(receiverPort);

...using receiverPort as argument?

Is the composition of the UDP message correct?
Thank you again
Logged

Antwerp, Belgium
Offline Offline
Jr. Member
**
Karma: 2
Posts: 64
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Is the initialization of your class correct? Is it right I'm initializing it like this:

Code:
Udp.begin(receiverPort);

...using receiverPort as argument?

Is the composition of the UDP message correct?
Well yes and no.
If you look at the header file of WifiUDP ( https://github.com/mlafauci/Arduino/blob/wifishield-bugfix/libraries/WiFi/WiFiUdp.h ), you'll see that the method is commented like this:
Code:
  virtual uint8_t begin(uint16_t); // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
so it's expecting you to pass the port the Arduino should listen on, not the one to send to. But since you are not listening on the Arduino and the local and remote port can be the same, this shouldn't be a problem.

The construction of your UDP message
Code:
Udp.beginPacket(receiverIP, receiverPort); //start udp packet
  Udp.write(valueInBytes, 8); //write sensor data to udp packet
  Udp.endPacket(); // end packet
looks right to me.
It's difficult to debug these kind of things from a distance. What I would do in such a case is try with the simplest setup that works and then change the code until it breaks. Step by step, compiling and testing with every change. At a certain point it will stop working.
So I would suggest you start with my example, start changing things and see how far you can get.
I would start by checking these:
- do you really want delay(10000); // 10sec delay?
- is the receiving computer really at IP 192.168.1.212 ? and is it listening at port 9100 ?
- more really obvious mistakes that are easy to overlook :-)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, I'm trying to broke apart the problems.

The first thing that looks strange is that I've tried to connect to different routers/networks, and my Arduino always Seruial.prints this networks info:

Code:
Attempting to connect to WPA SSID: phwifi
You're connected to the networkSSID: phwifi
BSSID: 0:0:0:0:0:0
signal strength (RSSI):0
Encryption Type:0

IP Address: 192.168.2.6


where the IP obviosly changes, but the other info are always zero.....
Logged

Antwerp, Belgium
Offline Offline
Jr. Member
**
Karma: 2
Posts: 64
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
IP Address: 192.168.2.6
Just a wild guess: is it possible the receiving ip should be something like 192.168.2.something... instead of the 192.168.1.212 you were using? usually when you are on the same network, the range of IPs is limited so only the last byte is different.

I have no idea why you get this weird output like
Quote
signal strength (RSSI): 0
Encryption Type: 0
did you correctly upgrade the WifiShield firmware? Are you using the correct library?
« Last Edit: April 15, 2013, 05:56:25 pm by timKnapen » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The ip class has changed from the previously code because I'm trying different networks, sorry about the confusion
About the firmware upgrade, I'm pretty sure I did follow your instructions smiley-sad

I was just reading this interesting post and trying to understand if there are any differences:
http://flashgamer.com/arduino/comments/how-to-update-the-official-arduino-wifi-shield
 
Logged

Antwerp, Belgium
Offline Offline
Jr. Member
**
Karma: 2
Posts: 64
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I also read that page when I was still trying with the old buggy firmware.
You can ignore everything on that page except the part about updating ports. That's the only thing I didn't document in my post I think.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Tim - Thanks.  Looks like I may have to do some research on DFU programming from Windows and then try the firmware update.  Derek
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Tim,
about your instructions: when you say to update /Applications/Arduino_104_UDP.app/Contents/Resources/Java/hardware/arduino/firmwares/wifishield/scripts/ArduinoWifiShield_upgrade.sh

should I also update the file
/Applications/Arduino_104_UDP.app/Contents/Resources/Java/hardware/arduino/firmwares/wifishield/scripts/ArduinoWifiShield_upgrade_mac.sh
?

Thanks

Logged

Pages: 1 2 [3] 4   Go Up
Jump to: