2016-10-16 09:16:54 +00:00
|
|
|
/*
|
|
|
|
* Connecting to a WiFi network with WPA
|
|
|
|
************************************************
|
|
|
|
* Originally adapted from https://www.arduino.cc/en/Tutorial/ConnectWithWPA
|
|
|
|
* by Starbeamrainbowlabs
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ESP8266WiFi.h>
|
2016-10-16 10:02:31 +00:00
|
|
|
#include <WiFiUdp.h>
|
2016-10-16 09:16:54 +00:00
|
|
|
|
2016-10-16 14:08:43 +00:00
|
|
|
#include "Utilities.h"
|
|
|
|
|
2016-10-16 14:22:05 +00:00
|
|
|
char ssid[] = "evenstar-2.4";
|
|
|
|
char password[] = "***REMOVED***";
|
2016-10-16 09:16:54 +00:00
|
|
|
|
|
|
|
// The address that the PixelHub beacon is broadcasting on.
|
|
|
|
IPAddress beaconAddress(239, 62, 148, 30);
|
|
|
|
// The port number that the PixelHub beacon is broadcasting on.
|
|
|
|
unsigned int beaconPort = 5050;
|
|
|
|
|
|
|
|
// A UDP Client to allow us to recieve UDP datagrams.
|
2016-10-16 10:02:31 +00:00
|
|
|
WiFiUDP UdpClient;
|
|
|
|
// The size of the datagram buffer that is used to buffer incoming messages.
|
2016-10-16 09:16:54 +00:00
|
|
|
int datagramBufferSize = 256;
|
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
// Setup the serial connection
|
|
|
|
Serial.begin(4800);
|
|
|
|
|
|
|
|
Serial.println("Hello, world!");
|
|
|
|
|
|
|
|
Serial.println("Beginning connection sequence.");
|
|
|
|
Serial.print("Attempting to connect to ");
|
|
|
|
Serial.print(ssid);
|
|
|
|
Serial.print(" - ");
|
|
|
|
|
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
|
|
|
|
while(WiFi.status() != WL_CONNECTED)
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
// Wait 10 seconds for the connection to start
|
|
|
|
delay(10000);
|
|
|
|
}
|
|
|
|
|
|
|
|
Serial.println("success!");
|
|
|
|
|
|
|
|
printWiFiInfoLocal();
|
2016-10-16 14:08:43 +00:00
|
|
|
findServer();
|
2016-10-16 09:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void printWiFiInfoLocal()
|
|
|
|
{
|
|
|
|
Serial.print("IP Address: ");
|
|
|
|
Serial.println(WiFi.localIP());
|
|
|
|
}
|
|
|
|
|
|
|
|
void findServer()
|
|
|
|
{
|
|
|
|
Serial.print("Initialising PixelHub auto detection system - ");
|
2016-10-16 10:02:31 +00:00
|
|
|
byte datagramBuffer[datagramBufferSize];
|
|
|
|
UdpClient.beginMulticast(WiFi.localIP(), beaconAddress, beaconPort);
|
2016-10-16 09:16:54 +00:00
|
|
|
Serial.println("success!");
|
|
|
|
|
|
|
|
Serial.println("Listening for PixelHub beacon pings.");
|
|
|
|
while(true) {
|
|
|
|
int datagramSize = UdpClient.parsePacket();
|
|
|
|
if(!datagramSize) continue;
|
|
|
|
|
|
|
|
Serial.print("Received datagram #");
|
|
|
|
Serial.print(datagramSize);
|
|
|
|
Serial.print(" bytes in size from ");
|
2016-10-16 10:02:31 +00:00
|
|
|
Serial.print(UdpClient.remoteIP());
|
2016-10-16 09:16:54 +00:00
|
|
|
Serial.print(":");
|
2016-10-16 10:02:31 +00:00
|
|
|
Serial.print(UdpClient.remotePort());
|
2016-10-16 09:16:54 +00:00
|
|
|
if(datagramSize > datagramBufferSize) {
|
|
|
|
Serial.println(", but the message is larger than the datagram buffer size.");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Serial.println(".");
|
|
|
|
|
2016-10-16 10:02:31 +00:00
|
|
|
UdpClient.read(datagramBuffer, datagramSize);
|
2016-10-16 09:16:54 +00:00
|
|
|
|
2016-10-16 10:02:31 +00:00
|
|
|
Serial.print("Content as hex: ");
|
2016-10-16 09:16:54 +00:00
|
|
|
for(int i = 0; i < datagramSize; i++) {
|
|
|
|
Serial.print(datagramBuffer[i], HEX);
|
|
|
|
Serial.print(" ");
|
|
|
|
}
|
2016-10-16 10:02:31 +00:00
|
|
|
Serial.println();
|
|
|
|
Serial.print("Raw content: ");
|
|
|
|
Serial.write(datagramBuffer, datagramSize);
|
|
|
|
Serial.println();
|
2016-10-16 14:08:43 +00:00
|
|
|
|
|
|
|
Serial.print("Parsing datagram - ");
|
|
|
|
|
|
|
|
// Parse the recieved message
|
|
|
|
char* datagramStr = (char*)datagramBuffer;
|
|
|
|
|
|
|
|
// Find the positions of the key characters
|
|
|
|
int atPos = findChar(datagramStr, '@');
|
|
|
|
int colonPos = findChar(datagramStr, ':');
|
|
|
|
|
|
|
|
char* role;
|
|
|
|
char* serverIp;
|
|
|
|
char* portNumberText;
|
|
|
|
|
|
|
|
strncpy(role, datagramStr, atPos);
|
|
|
|
strncpy(serverIp, datagramStr + atPos + 1, colonPos);
|
|
|
|
strncpy(portNumberText, datagramStr + colonPos + 1, datagramSize - 1);
|
|
|
|
|
|
|
|
Serial.println("complete.");
|
|
|
|
|
|
|
|
Serial.print("Role: "); Serial.print(role); Serial.print(" ");
|
|
|
|
Serial.print("Remote IP: "); Serial.print(serverIp); Serial.print(" ");
|
|
|
|
Serial.print("Port number: "); Serial.print(portNumberText);
|
|
|
|
Serial.println();
|
|
|
|
|
|
|
|
// If the advertiser isn't playing the role of a server, then we're not interested
|
|
|
|
if(role != "server") continue;
|
|
|
|
|
|
|
|
int portNumber = atoi(portNumberText);
|
2016-10-16 09:16:54 +00:00
|
|
|
}
|
|
|
|
}
|