ETA of UDP library?

Is there an eta on when the UDP library will be available?
I’ve ordered 10 spark cores for a specific project that requires udp comms, so hoping it wont be too long?

2 Likes

Hi @Dermotos - even though it’s not actually documented on the site, I think UDP has already been implemented. Try it and let us know if it works :slight_smile:

http://docs.spark.io/#/firmware/communication-udp

I have used UDP for NTP and it worked for me!

I have a Spark and an Arduino (Ethernet) sending UDP messages back and forth:

    ////
//// SPARK CODE
////

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress ip(10, 0, 1, 170);         // IP of device we're communicating with
unsigned int remotePort = 8888;      // local port to listen on

int RunningLED = 3;
int intervalLED = 5;

#define UDP_TX_PACKET_MAX_SIZE 26

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];         //buffer to hold incoming packet,
char RequestBuffer[] = "Hello World\0";
char WeatherBuffer[30];

const unsigned int updateInterval = 30000;        // Time interval in milliseconds to send requests   
long lastConnectionTime = 0; 

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

void setup() {
  // start the Ethernet and UDP:
  
  pinMode(RunningLED, OUTPUT);
  digitalWrite(RunningLED, HIGH);

  pinMode(intervalLED, OUTPUT);
  digitalWrite(intervalLED, LOW);

  Udp.begin(remotePort);

  Serial.begin(9600);
  Serial.println("Spark UDP test started");
  lastConnectionTime = millis();

}


void sendUDPRequest(){

    Udp.beginPacket(ip, remotePort);
    Udp.write(RequestBuffer);
    Udp.endPacket();

}

void loop() {

  if ((millis() - lastConnectionTime) >  updateInterval){

    lastConnectionTime = millis();

    Serial.println("Spark Sending Packet");
      
    digitalWrite(intervalLED, HIGH);
    sendUDPRequest();
    delay(250);                         

    int packetSize = Udp.parsePacket();
      
    if(packetSize){
        Serial.print("Received packet of size ");
        Serial.println(packetSize);
        Serial.print("From ");
        IPAddress remote = Udp.remoteIP();
        for (int i =0; i < 4; i++){
            Serial.print(remote[i], DEC);
            if (i < 3){
                Serial.print(".");
            }
        }
        Serial.print(", port ");
        Serial.println(Udp.remotePort());
    
        // read the packet into packetBufffer
        Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
        Serial.print("Contents: ");
        Serial.println(packetBuffer);
    
    }
    delay(10);
  }
  digitalWrite(intervalLED, LOW);
}

////
//// ARDUINO CODE
////


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress ip(10, 0, 1, 170);         // IP of device we're communicating with
unsigned int remotePort = 8888;      // local port to listen on

int RunningLED = 3;
int intervalLED = 5;

#define UDP_TX_PACKET_MAX_SIZE 26

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];         //buffer to hold incoming packet,
char RequestBuffer[] = "Hello World\0";
char WeatherBuffer[30];

const unsigned int updateInterval = 30000;        // Time interval in milliseconds to send requests   
long lastConnectionTime = 0; 

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

void setup() {
  // start the Ethernet and UDP:
  
  pinMode(RunningLED, OUTPUT);
  digitalWrite(RunningLED, HIGH);

  pinMode(intervalLED, OUTPUT);
  digitalWrite(intervalLED, LOW);

  Udp.begin(remotePort);

  Serial.begin(9600);
  Serial.println("Spark UDP test started");
  lastConnectionTime = millis();

}


void sendUDPRequest(){

    Udp.beginPacket(ip, remotePort);
    Udp.write(RequestBuffer);
    Udp.endPacket();

}

void loop() {

  if ((millis() - lastConnectionTime) >  updateInterval){

    lastConnectionTime = millis();

    Serial.println("Spark Sending Packet");
      
    digitalWrite(intervalLED, HIGH);
    sendUDPRequest();
    delay(250);                         

    int packetSize = Udp.parsePacket();
      
    if(packetSize){
        Serial.print("Received packet of size ");
        Serial.println(packetSize);
        Serial.print("From ");
        IPAddress remote = Udp.remoteIP();
        for (int i =0; i < 4; i++){
            Serial.print(remote[i], DEC);
            if (i < 3){
                Serial.print(".");
            }
        }
        Serial.print(", port ");
        Serial.println(Udp.remotePort());
    
        // read the packet into packetBufffer
        Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
        Serial.print("Contents: ");
        Serial.println(packetBuffer);
    
    }
    delay(10);
  }
  digitalWrite(intervalLED, LOW);
}
3 Likes

@ClintonKeith Thanks for putting this code up here. The Spark code worked instantly. I think something like this should be in the standard sketches.
Maybe the Spark Core guys would benefit from also picking a standard piece of free software like Puredata and using that as a way to set up a sketch to receive packets, just so people can get to UDP hello world easily?