android , arduino wifi and udp protocol

Hi guys,

Thanks in advance for reading and helping.

Ok, so I am trying to send data from an app on my android phone to my arduino wifi shield using the home wifi network.

I am using an app called packet sender that can send info if destination IP and destination port are known. ( https://play.google.com/store/apps/details?id=com.grayson.packetsender )

I am using this as my arduino code that I found on the website and forums etc.

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

int status = WL_IDLE_STATUS;
char ssid[] = "NETGEAR"; // your network SSID (name)
char pass[] = "harvey08"; // your network password (use for WPA, or use as key for WEP)

unsigned int localPort = 50505; // local port to listen on
char packetBuffer[255]; //buffer to hold incoming packet

IPAddress remote_ip(192, 168, 0, 4);
unsigned int remote_port = 50505;

char ReplyBuffer[] = "acknowledged"; // a string to send back

WiFiUDP Udp;

void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}

// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);

// wait 10 seconds for connection:
delay(10000);
}

// you're connected now, so print out the data:
Serial.print("You're connected to the network");
delay(10000);
printWifiStatus();

Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
Udp.begin(localPort);

}

void loop() {

// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if (packetSize)
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remoteIp = Udp.remoteIP();
Serial.print(remoteIp);
Serial.print(", port ");
Serial.println(Udp.remotePort());

// read the packet into packetBufffer
int len = Udp.read(packetBuffer, 255);
if (len > 0) packetBuffer[len] = 0;
Serial.println("Contents:");
Serial.println(packetBuffer);

// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(ReplyBuffer);
Udp.endPacket();
}
}

void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

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

WHEN , I open my serial monitor I see this.

Attempting to connect to WPA SSID: NETGEAR
You're connected to the networkSSID: NETGEAR
IP Address: 192.168.0.7
signal strength (RSSI):-82 dBm

Starting connection to server...

and that's where it all stops..

Following are the problems.

  1. When I use the android app to send info to my shield , the app says verify if source socket is open .. So I looked into this and changed my laptop's incoming connections settings to allow all connections, this still didnt make any difference. I used these instructions Windows help & learning

  2. I dont know what happens to trying to connect to server .. it just gets stuck there. I am not making any claims on programming , I can see the logic of the code but not necessarily how to build or debug it. Can anyone also explain how do you choose what port to receive information on? like how do you know its 2390 or 50505 ? sorry I read articles on google but no one explained how to choose one for UDP send or receive.

Many thanks :slight_smile:

Have you upgraded the wifi shield's firmware?
Are you sending the packet from the Android to the correct ip and port on the Arduino?

You should not set the remote ip and port unless you are certain those are correct. Take a look at the wifi shield example WiFiUdpSendReceiveString to see how to determine the sender's remote ip and port.

Hi Tim,

Thanks for taking interest as always.

  1. Yes, the wifi shield's firmware is updated.
  2. I am sending packet to correct IP but not sure about the port, that was one of the questions in my original post.. I dont understand how to choose a particular port on arduino wifi.. I just ended up saying UDP port 50505 which may not mean anything ..
  3. I think by remote in my case that would be the android and local would be wifi shield. I do know the ip address for these as I can log in to the modem and see all ip.
  4. for some reason using the Wifiudpsendreceivestring didnt not much to my help, it got stuck at trying to connect but I ended by using this code can't send/recieve UDP packets via Wi-Fi shield - Networking, Protocols, and Devices - Arduino Forum which got me to as far as I got as per my first post.

Thanks , would you be able to explain how to chose a port on arduino , I think I need to understand this first.

Can you ping the Arduino's ip on that localnet? Are you connected to the same wireless network with your Android?
I use port 5005 on my udp sketches, but that is up to you.

Are you certain you have the firmware upgraded successfully?

edit: Here is the test. Add the firmwareVersion call in setup.

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present"); 
    // don't continue:
    while(true);
  } 

// check firmware version
Serial.print(F("Firmware version: "));
Serial.println(WiFi.firmwareVersion());

// rest of your setup code

It should show "Firmware version: 1.1.0" if upgraded ok.

Hi Tim,

  1. If you mean am I connected to the local network and can see IP and signal strength than yes.
  2. Yes, my android, wifi module, laptop are all connected to the same wifi network.
  3. Yes , the firmware showed 1.1.0

I dont know what the problem is ..

Can you ping the wifi module from the laptop? Can you ping the android from the laptop? Are you certain the android is using the wifi network and not the cell provider's connection?

Hi Tim,

No, I cant ping the wifi module from the laptop. I have the wifi scan running in the IDE(not sure if it matters what program runs in IDE), I ping my android from the laptop and it sends and receives packet.

I then ping my wifi shield from the laptop and it says "destination host unreachable.

Does this means there is no comms between the shield and laptop? I mean it does connect to the local network but also yesterday when I was trying to send UDP through my android it kept saying "verify source socket is open and ready" - did this mean that the android wasnt connecting to wifi shield either ?

Thanks for enlightening me once again ..

That is odd. You are using the ip that is reported when the shield starts?

Until you get the ping straightened out, there is no sense trying to connect with another protocol/port.

edit: How far is the Arduino from the wireless router? Your signal strength is not that good. -82db is weak.

Hi Tim,

Yes,when I start the wifi shield, I check the IP address of the shield it picks up, I then used this IP .

I am sitting a room away from the router, If I have to take a guess it would be a 5-7m away.

I will try doing it all over again tonight and report .. Thanks Tim :slight_smile:

Some routers have a section that reports wifi connection states and signal strengths. If your router has that capability, you should log in to the router and check that. Maybe your wifi shield see the router radio ok, but the router is losing the wifi shield's signal after the initial connection.

edit: Now a warning about UDP and the wifi shield. If you try the WiFiUdpSendReceiveString example in the library, it will probably not work as you expect. My tests showed the wifi shield does not respond with the same port that it receives on. Normally, UDP will respond to the UDP packet from the same port it received the packet on. Mine does not.

My tests with Netcat shows the wifi shield listens on the port used in the UDP.begin() call, but responds with a different port. I used port 5005 for the UDP.begin() call, and it receives the packets ok, but Netcat never received a response like it did with my ethernet shield. That is because the wifi shield was responding using port 4097. I found that by starting two instances of Netcat, one as a client sending to port 5005 on the wifi shield, and one as a listener on port 5005. The client did not get the response, but the listener got it on port 5005 from port 4097 on the wifi shield.

Hi Tim,

Sorry for taking time and not coming back on this..

I managed to get it working, I believe the wifi I was connected to had some issues that I couldn't get working, I ended up using my own router and was able to send udp, also on the same port .. thanks again!

attached is the screen!