Author Topic: Receive UDP packages  (Read 4176 times)

whity

  • Newbie
  • *
  • Posts: 1
Receive UDP packages
« on: September 01, 2014, 03:29:09 pm »
Hi,

Finally found some time to try out my Kickstarter reward today.

I'm trying to receive UDP packages, but completely failed  :-\. Can someone may provide me a short example how to do it? Simply how to set it up to listen on port 49000 and write the received data to serial. Couldn't find anything about it.

EDIT: Some more informations:
I set up the WiFi using the web interface as describes on the website and connected it to my access point. Also tried out the WebServer example which worked fine.


Thank you very much
Urs
« Last Edit: September 01, 2014, 03:37:38 pm by whity »

Lami

  • Newbie
  • *
  • Posts: 13
Re: Receive UDP packages
« Reply #1 on: November 10, 2014, 01:17:32 pm »
Let me kind of hijack this thread :)

Partial answer - I was able to send UDP and receive response.

Code: [Select]
    wifi.begin(9600);
    while (!wifi.ready()) {
        delay(1000);
    }
    wifi.setMode(UDP);
    // here I am creating "UDP connection"
    wifi.connect(IP_ADDRESS, PORT);
    // send the data
    wifi.write(packetData, packetLength);
    // wait for response
    while(1) {
        int avail = wifi.available();
        int rcvd = 0;
        if (avail) {
            rcvd = wifi.read(buffer, BUFFER_SIZE);
            if (avail > BUFFER_SIZE) {
                debug("Packet larger than buffer size, discarding rest of data");
                while(wifi.available())
                    wifi.read();
            }
            // now data are in the buffer
        }
    }

So I can send packet and read possible response to the packet. UDP connection is kind of nonsense, but in context of WiFi module it seems to say "Hey, I am talking with this IP:port".

For server, I did not try anything of it, but, well... looking into DigiFi code, server() command sets IP to 127.0.0.1. That might be Ok, module is quite stupid and might listen to anything arrives. So you might be OK with just:
Code: [Select]
wifi.server(PORT);
while(wifi.available()) {
    // ... process data as above
}

If not, you might try to parse IP address from WiFi settings. Command wifi.getSTANetwork() returns something like:
Code: [Select]
+ok=DHCP,192.168.1.107,255.255.255.0,192.168.1.1192.168.1.107 is IP address of WiFi module. And feed this into wifi.setNetParams, ie:
Code: [Select]
wifi.setNetParams("UDP", "SERVER", PORT, ipAddress);
And now, my question - I use code above for UDP broadcast, sending UPD packet to 255.255.255.255 and waiting for response. Which I get, but... How do I get IP address of the device which replied?

In docs for *-G module, there is nothing about that. *-G2 module documentation mentions AT+NETP command returning IP address of last incomming packet, if running in SERVER mode. Which is not case above (as I am client contacting server). Obviously AT+NETP returns whatever I set in connect. And I suspect DigiX comes with *-G module anyway (AT+H hints of that), although a comment in DigiFi library mentions *-G2 on one place.

Any ideas, undocumented API, black magic?
« Last Edit: November 10, 2014, 01:28:47 pm by Lami »