Skip to content

Instantly share code, notes, and snippets.

@blakejakopovic
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blakejakopovic/cbb389998776033c2135 to your computer and use it in GitHub Desktop.
Save blakejakopovic/cbb389998776033c2135 to your computer and use it in GitHub Desktop.
Spark Core UDP Echo

UDP Echo Sketch for Spark Core

A simple UDP echo program for the Spark Core Arduino based device. You can send a single byte (ie. 'a') of data and have it sent back. This program serves no real practical application, but can be used to learn and tinker with UDP and inter-device communication.

Testing UDP

You can test sending and receiving UDP packets using the Packet Sender application, available for Windows, Mac, Ubuntu and Andriod.

// UDP Port used for two way communication
unsigned int localPort = 8888;
// An UDP instance to let us send and receive packets over UDP
UDP Udp;
void setup() {
// Initialise UDP
Udp.begin(localPort);
// Print your device IP Address via serial
Serial.begin(9600);
Serial.println(Network.localIP());
}
void loop() {
// Check if data has been received
if (Udp.parsePacket() > 0) {
// Read first char of data received
char c = Udp.read();
// Ignore other chars
Udp.flush();
// Store sender ip and port
IPAddress ipAddress = Udp.remoteIP();
int port = Udp.remotePort();
// Echo back data to sender
Udp.beginPacket(ipAddress, port);
Udp.write(c);
Udp.endPacket();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment