Skip to content

Instantly share code, notes, and snippets.

Created November 13, 2014 22:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/22f806f356681cd993fe to your computer and use it in GitHub Desktop.
Save anonymous/22f806f356681cd993fe to your computer and use it in GitHub Desktop.
// SYSTEM_MODE(MANUAL);
// //Disables connection to Spark Cloud without additional API query
// // UDP Port used for two way communication
// unsigned int localPort = 1900;
// const char *message = "A";
// #define MAX_SIZE 1024
// char buffer[MAX_SIZE];
// unsigned int once = 0;
// unsigned int ssdpport = 1900;
// IPAddress ip( 239, 255, 255, 250 );
// // IPAddress ip( 192,168, 8, 1 );
// // An UDP instance to let us send and receive packets over UDP
// UDP Udp;
// void setup() {
// Serial.begin(9600);
// WiFi.on();
// // Connects to a network with a specified authentication procedure.
// // Options are WPA2, WPA, or WEP.
// WiFi.clearCredentials();
// WiFi.setCredentials("hack_the_planet", "crashoverride", WPA);
// WiFi.connect();
// }
// void loop() {
// Spark.process();
// if (WiFi.ready() == true)
// {
// if ((ip[0] != 0 ||ip[1] != 0 || ip[2] != 0 || ip[3] != 0) && (once == 0)) {
// Serial.println("UDP has begun");
// once = 1;
// }
// Udp.begin(localPort);
// Serial.println(WiFi.localIP());
// //Module to send SSDP packet
// if (Udp.parsePacket() > 0) {
// // Read first char of data received
// Udp.read(buffer, MAX_SIZE);
// Serial.println(buffer);
// }
// message = "M-SEARCH * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\nMAN: \"ssdp:discover\"\r\nMX: 5\r\nST: ssdp:all\r\n\r\n";
// // Udp.flush();
// //Sends SSDP Packet to multicast address
// Udp.beginPacket(ip, ssdpport);
// Udp.write("M-SEARCH * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\nMAN: \"ssdp:discover\"\r\nMX: 5\r\nST: ssdp:all\r\n\r\n");
// Udp.endPacket();
// Udp.stop();
// // delay(100); //Needed to prevent the Core from self destructing
// }
// }
SYSTEM_MODE(MANUAL);
// UDP Port used for two way communication
unsigned int localPort = 1900;
#define MAX_SIZE 1024
char buffer[MAX_SIZE];
unsigned int once = 0;
unsigned int ssdpport = 1900;
IPAddress ip( 239, 255, 255, 250 );
// An UDP instance to let us send and receive packets over UDP
UDP Udp;
void setup() {
Spark.disconnect();
Serial.begin(9600);
WiFi.on();
// Connects to a network with a specified authentication procedure.
// Options are WPA2, WPA, or WEP.
WiFi.clearCredentials();
WiFi.setCredentials("ObiWanKenonbi", "youremyonlyhope", WPA);
WiFi.connect();
// while(!Serial.available()) Spark.process();
while (!WiFi.ready()) {
Serial.println("Waiting for WiFi...");
Spark.process();
delay(1000);
}
}
void loop() {
Spark.process();
if ((!once) && (WiFi.ready() == true)) {
IPAddress addr = WiFi.localIP();
if ((addr[0] != 0 ||addr[1] != 0 || addr[2] != 0 || addr[3] != 0) && (once == 0)) {
Serial.println("UDP has begun");
once = 1;
Udp.begin(localPort);
Spark.process();
Serial.println(WiFi.localIP());
//Sends SSDP Packet to multicast address
Udp.beginPacket(ip, ssdpport);
Spark.process();
Udp.write("M-SEARCH * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\nMAN: \"ssdp:discover\"\r\nMX: 5\r\nST: ssdp:all\r\n\r\n");
Spark.process();
Udp.endPacket();
Spark.process();
}
}
//Module to parse received SSDP packet
Spark.process();
int rcvd = Udp.parsePacket();
if (rcvd > 0) {
Serial.print("Received UPD packet, length: ");
Serial.println(rcvd);
Serial.println(buffer);
// Read first char of data received
Udp.read(buffer, MAX_SIZE);
Spark.process();
if (rcvd > MAX_SIZE) {
Serial.println("Too large packet");
while (Udp.available())
Udp.read();
Spark.process();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment