Created
August 27, 2014 00:46
-
-
Save m-mcgowan/3195ac229d672593e0eb to your computer and use it in GitHub Desktop.
Spark UDP Write test harness
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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