Help on how to use UDP in controlling an LED


I'm trying to create a basic UDP application with my arduino board just for my own fun (as I am new to this). What I want to do is send a UDP string such as "on" or "off", "high" or "low" to turn an LED on /off.
I was wondering how you can compare/ dissect what you read from the UDP packet received to the arduino so that the arduino can understand the message.
Would that be possible?

I've been trying to alter the SendReceiveString example, but I'm stuck on how to compare what is received.

If anyone could help me, that would be great!
And thanks for the help in advance. :slight_smile:

I can’t speak to UDP, but below is a simple tcp/ip http control setup.

//zoomkat 4-1-12
//simple button GET for servo and pin 5
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html, or use ' instead of " 
//address will look like when submited
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString; 


void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);

  myservo.write(90); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control
  //enable serial data print 
  Serial.println("server servo/pin 5 test 1.0"); // so I can keep track of what is loaded

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c =;

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 

        //if HTTP request has ended
        if (c == '\n') {

          Serial.println(readString); //print to serial monitor for debuging 

          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");

          client.println("<TITLE>Arduino GET test page</TITLE>");

          client.println("<H1>Zoomkat's simple Arduino button</H1>");
          client.println("<a href=\"/?on\">ON</a>"); 
          client.println("<a href=\"/?off\">OFF</a>"); 

          //stopping client

          ///////////////////// control arduino pin
          if(readString.indexOf("on") >0)//checks for on
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led On");
          if(readString.indexOf("off") >0)//checks for off
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led Off");
          //clearing string for next read


Thanks for your help zoomkat!

I was wondering if anyone knows how to flush the UDP message so it resets. That’s because every time when I type “high” and then type “low” it would give me “lowh” and if I type “on” after it would give me onwh.
It would just overwrite the old message.

I know using a for loop and re-initalizing the array would work. But is there a more efficient way to do it?

  for(int i=0;i<UDP_TX_PACKET_MAX_SIZE;i++)
     packetBuffer[i] = 0;

But is there a more efficient way to do it?

Yes. My crystal ball says that line 22 is the problem. Of course, it has been wrong before. Almost every time.

Sorry, I don't have time to test this myself.

You could try...

 char packetBuffer[UDP_TX_PACKET_MAX_SIZE +1]; //buffer to hold incoming packet,
 packetBuffer[, UDP_TX_PACKET_MAX_SIZE) ] = 0;

More verbosely. The data contained in a (network) packet is always an arbitrary array of bytes.
If you know the data will always be an array of printable characters which you will use as a string,
you must make sure the array is a well formed and correctly terminated string.

//ARTZ (always remember to terminate strings with a zero

//when a packet is max_size, we will need an extra byte to hold our zero terminator
char packetBuffer[UDP_TX_PACKET_MAX_SIZE +1]; //buffer to hold incoming packet,

 // returns the number of bytes which were read into the packet buffer
 int bytesRead =, UDP_TX_PACKET_MAX_SIZE);
 //the data occupies packetBuffer[0] to packetBuffer[bytesRead -1]
 //so we can add the zero terminator to the end with...
 packetBuffer[bytesRead] = 0;

 //now we can send a well formed string to print