Sending Simple UDP Commands

I am working on a home project with Lightwave RF and Arduino.

I am tying (desperately) to find a simple example of sending a single UDP command as a UDP packet to the Lightwave Hub to control lighting etc.

To make it clearer (hopefully!), I need to send a command: 101,!R1D1F1 to run on lights connected to the hub.

I have successfully connected the Arduino to the network via the Ethernet Shield, assigned the MAC and IP, had it communicating, running as a web server/client etc. so I am happy with the config in this way but I cannot find an example of how to open the UDP client and fuire off a UDP broadcast to an IP on the network.

Example use:

Send 101,!R1D1F1 to 102.168.1.20 to port 9760

Can anybody help? Please.

Thanks in advance!

Seems to me like the UDPSendReceiveString example in the Ethernet library should be a good starting point.

If you want to send a packet without receiving one first, take a look at the NTP example sketch. It sends a packet and waits for a response packet.

Thanks guys! Much appreciated!! :slight_smile:
I'll dig those out and give it a blast tonight.

I tried - based on the exampled suggested - to send UDP data, but something is failing.

Can anybody see what I’m doing wrong from my code snippet?

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008

byte ethernetMac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ethernetIp(192, 168, 0, 200);
IPAddress lightwaveIp(192,168,0,2);
unsigned int lightwavePort = 9760;

EthernetUDP Udp;

void setup() 
{
  Ethernet.begin(ethernetMac, ethernetIp);
  Udp.begin(ethernetIp);
}

void loop() 
{
    Udp.beginPacket(lightwaveIp, lightwavePort); //initiate UDP packet
    Udp.write("090,!R1D1F0");  // turn light off
    Udp.endPacket();  // send packet

    delay(5000);  // wait 5 seconds

    Udp.beginPacket(lightwaveIp, lightwavePort); //initiate UDP packet
    Udp.write("091,!R1D1F1");  // turn light on again
    Udp.endPacket();  // send packet

    delay(5000);  // wait 5 seconds
}

Did the NTP example sketch work?

aarg:
Did the NTP example sketch work?

I couldn't find that in my examples so I looked at the previous one and removed the READ code, stipped it back to the SEND code and ended up with the above.

jonny2016:
I couldn't find that in my examples so I looked at the previous one and removed the READ code, stipped it back to the SEND code and ended up with the above.

It's in the Ethernet library. Standard issue.

Got it!

I'll have a look at this now. Thank you!

No joy - this looks like it's not sending NTP requests, or I'm not receiving any data back.

I am beginning to wonder if this is a problem with the LAN. UDP packets are being sent over WiFi but maybe not over wired LAN??

This sends a packet 10 times per second. It listens for a return packet. I have C code for a linux box that I use as the “server” if you need it.

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};

IPAddress ip(192, 168, 2, 2);
IPAddress gateway(192, 168, 2, 1);
IPAddress subnet(255, 255, 255, 0);

unsigned int UDPport = 5005;// local port to listen for UDP packets

IPAddress UDPServer(192, 168, 1, 253); // destination device server

const int UDP_PACKET_SIZE = 64;

byte packetBuffer[ UDP_PACKET_SIZE] = "0";

unsigned int noChange = 0;

// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

unsigned long currentTime;
unsigned long secondTime;

unsigned long msPerSecond = 100UL;

int UDPCount = 0;

void setup()
{
  Serial.begin(115200);
  
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);

  Serial.print(F("Starting w5100..."));

  //  Ethernet.begin(mac,ip,gateway,gateway,subnet);

  while (!Ethernet.begin(mac)) {
    Serial.println(F("failed. Retrying in 5 seconds."));
    delay(5000);
    Serial.print(F("Starting w5100..."));
  }

  Serial.println(Ethernet.localIP());

  Udp.begin(UDPport);

  delay(2000);

  currentTime = millis();
  secondTime = currentTime;

  Serial.println("Ready");
}

boolean packetOut = false;

void loop()
{
  currentTime = millis();

  getUDPpacket();

  if (currentTime - secondTime > msPerSecond) {
    byte rtnVal = Ethernet.maintain();
    switch (rtnVal) {
      case 1: Serial.println(F("\r\nDHCP renew fail"));
        break;
      case 2: Serial.println(F("\r\nDHCP renew ok"));
        break;
      case 3: Serial.println(F("\r\nDHCP rebind fail"));
        break;
      case 4: Serial.println(F("\r\nDHCP rebind ok"));
        break;

    }


    Serial.println(F("\r\nUDP send"));
    sendUDPpacket(UDPServer); // send an NTP packet to a time server
    packetOut = true;
    secondTime += msPerSecond;
  }
}

unsigned int udpCount = 0;

// send an NTP request to the time server at the given address
unsigned long sendUDPpacket(IPAddress& address)
{
  if(packetOut) Serial.println("Missed packet");
  
  udpCount++;

  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, UDP_PACKET_SIZE);


  sprintf((char*)packetBuffer, "Arduino count %u", udpCount);

  Udp.beginPacket(address, UDPport); //NTP requests are to port 123
  Udp.write(packetBuffer, UDP_PACKET_SIZE);
  Udp.endPacket();

  packetOut = true;
}


void getUDPpacket() {
  if ( Udp.parsePacket() ) {
    // We've received a packet, read the data from it

    
    if (Udp.remoteIP() == UDPServer) {
      Serial.print(F("UDP IP OK  "));
    }
    else {
      Serial.println(F("UDP IP Bad"));
      return;
    }
    if (Udp.remotePort() == UDPport) {
      Serial.println(F("Port OK"));
    }
    else {
      Serial.println(F("Port Bad"));
      return;
    }

    Udp.read(packetBuffer, UDP_PACKET_SIZE); // read the packet into the buffer

    Serial.print(F("Received: "));
    Serial.println((char*)packetBuffer);

    packetOut = false;
  }
}

Thanks SurferTim. I am getting frustrated now. I feel like I am asking stupid questions now and wasting peoples time.

I tried your code but all I see in the Serial Monitor is:

C þ�8C þ�Fÿ�aMþ�a�CÿÙÿ þ�x‡þ þ�dmLYÿ�Fÿ8C’FÿmL þ�Fÿ
ÿ þ�FÿÛÿIþMþ=CmL-C�Fÿ�CÿmA�d�Fÿ=C þ þ�Gÿ’Fÿx‡þx‡þ þ8CmA�Cÿ�FÿmAmAmL8CmA=CYÿ�a

Junk!

This is the closest I've gotten to any sort of data being sent.

jonny2016:
Thanks SurferTim. I am getting frustrated now. I feel like I am asking stupid questions now and wasting peoples time.

That only happens when people don't read. I don't think we're on that page yet.

Insure the baud rate in your sketch match the baud rate in the serial monitor. Note I use 115200.

  Serial.begin(115200);

You my friend are a genius!
This is now working, I am getting data through to the Lightwave Link.

I’ve hacked your code to cut it down to the base SEND method, and it looks to be working briliantly!

Thank you so much for your help! :slight_smile:

1 Like