Skip to content

Instantly share code, notes, and snippets.

@m-mcgowan
Created August 27, 2014 00:46
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 m-mcgowan/3195ac229d672593e0eb to your computer and use it in GitHub Desktop.
Save m-mcgowan/3195ac229d672593e0eb to your computer and use it in GitHub Desktop.
Spark UDP Write test harness
#include "application.h"
SYSTEM_MODE(SEMI_AUTOMATIC);
UDP Udp;
unsigned char TxMsg[12] = { 1, 254, 1, 254, 1, 254, 43, 212, 71, 184, 3, 252 };
unsigned char recbuf[12];
IPAddress addr(192,168,1,199);
bool bound = false;
bool read = false;
void bindUDP() {
bound = Udp.begin(9000);
if (!bound)
Serial.println("UDP.begin not bound");
}
void setup() {
Serial.begin(9600);
pinMode(D7, OUTPUT);
digitalWrite(D7, LOW);
}
void loop() {
if (Serial.available()) {
char c = Serial.read();
switch (c) {
case 'b' : bindUDP(); break;
case 'B' : bound = false; break;
case 'r' : read = true; break;
case 'R' : read = false; break;
case 'c' : Spark.connect(); break;
case 'C' : Spark.disconnect(); break;
case 'w' : WiFi.on(); WiFi.connect(); break;
case 'W' : WiFi.off(); break;
}
}
if (!bound)
return;
if (read) {
int32_t packetLen = Udp.parsePacket();
Serial.print("read packet: ");
Serial.println(packetLen);
}
Udp.beginPacket(addr, 9000);
int wrBytes = 0;
wrBytes = Udp.write(TxMsg, 12);
Udp.endPacket();
if (wrBytes==-1) {
Serial.println("Data not written, resetting UDP");
Udp.stop();
Udp.begin(9000);
}
digitalWrite(D7, HIGH);
delay(200);
digitalWrite(D7, LOW);
delay(200);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment