Problems with UDP on Arduino

Hello all,

I am working on my Home Control System and my aim is to have everything communicate to an old computer running a Visual Basic.Net application I have written as the server. I have run some tests by having the input devices (such as light switches) send data to the computer via UDP and it worked like a charm. However, sending UDP to arduinos and Sonoffs seems to be a problem. Using the serial monitor I have tested the program and the UDP packets are being received, but I can’t seem to actually analyse any data received.

For now I am running the outputs as web servers and my computer is calling them, acting as a client, so the system is operating, but it would be capable of far more if I had more options with the UDP.

Thanks in advance.

Josh

I have only used TCP on the microcontrollers. Perhaps the packet is arriving as a char array? Can you have it echo out the serial port to see what is actually received? Or you could use the SoftSerial library to add an additional serial port for debugging.

Thanks for the reply,

I have used the serial port to echo the char array and it does come out, however the arduino doesn’t want to act on any changes.

Josh

Hi Josh,

Here is the relevant portion of the code I used with an EtherTen. It uses the MQTT library which calls the function below to parse out the response. The MQTT Topic is returned as a char array and the payload is a byte array. You can see below, I convert the payload first to a string, then I use ‘strcmp’ (stringCompare) compare to a number, and ‘==’ to compare against another string. Perhaps the code could be cleaned up, but I found this worked for me.

void callback(char* topic, byte* payload, unsigned int length) {
payload[length] = '\0'; //set terminator for payload length (or part of old messages could show up)

//Command will be either "ON" or "OFF" 
String s = String((char*)payload); //convert payload to string


if(strcmp(topic, "home/kitchen/stringLights/command")==0){
//Note - don't publish a new command before reading in the current one - the library uses the same buffer for send & rec. This will confuse it.
// https://community.openhab.org/t/rgb-led-strip-control-using-arduino-and-mqtt/4366 
// https://community.openhab.org/t/oh2-how-to-convert-colorpicker-to-rgb-values/16305/5


if(s == "OFF") {
  run = false;
  client.publish("home/kitchen/stringLights/status", "OFF");
}

else if(s == "ON") {
  run = true;
  client.publish("home/kitchen/stringLights/status", "ON");
  }
} //end of the command callback


else if(strcmp(topic, "home/kitchen/stringLights/color")==0){    
    if(s == "OFF") {
  run = false;
  client.publish("home/kitchen/stringLights/status", "OFF");
}

else if(s == "ON") {
//if(strcmp( (char *) payload, "ON") == 0) {
  run = true;
  client.publish("home/kitchen/stringLights/status", "ON");
  }
  
//    else if(strcmp(s.substring(0,5), "color")==0){
    // set the color
//      }  

} //end of the color callback

}

Take a look through that sample code and try some of the methods. If you could post some of your code as well, we could all have a peek.