Tweak a few things & replace spaces with tabs

This commit is contained in:
Starbeamrainbowlabs 2016-10-21 21:16:15 +01:00
parent 70ef418a7a
commit 82f0b9620c
2 changed files with 116 additions and 96 deletions

View file

@ -23,6 +23,10 @@ WiFiUDP UdpClient;
// The size of the datagram buffer that is used to buffer incoming messages. // The size of the datagram buffer that is used to buffer incoming messages.
int datagramBufferSize = 256; int datagramBufferSize = 256;
WiFiClient tcpClient;
char pixelHubServerIp[16];
int pixelHubPortNumber;
void setup() void setup()
{ {
// Setup the serial connection // Setup the serial connection
@ -39,10 +43,8 @@ void setup()
while(WiFi.status() != WL_CONNECTED) while(WiFi.status() != WL_CONNECTED)
{ {
// Wait a second for the connection to be established
delay(1000);
// Wait 10 seconds for the connection to start
delay(10000);
} }
Serial.println("success!"); Serial.println("success!");
@ -51,21 +53,37 @@ void setup()
findServer(); findServer();
} }
void loop() void loop()
{ {
} }
// ~~~ WiFi Diagnostics ~~~ //
/// <summary>
/// Prints the local IP address to the serial connection.
/// </summary>
void printWiFiInfoLocal() void printWiFiInfoLocal()
{ {
Serial.print("IP Address: "); Serial.print("IP Address: ");
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
} }
// ~~~ PixelHub auto discovery system ~~~ //
/// <summary>
/// Listens for PixelHub beacon pings in order to determine the location of the PixelHub server.
/// </summary>
/// <description>
/// Puts the details it finds into the `pixelHubServerIp` and `pixelHubPortNumber`
/// </description>
void findServer() void findServer()
{ {
Serial.print("Initialising PixelHub auto detection system - "); Serial.print("Initialising PixelHub auto detection system - ");
byte datagramBuffer[datagramBufferSize]; byte datagramBuffer[datagramBufferSize];
memset(datagramBuffer, '\0', datagramBufferSize); // Prefill the buffer with zeros for protection later
UdpClient.beginMulticast(WiFi.localIP(), beaconAddress, beaconPort); UdpClient.beginMulticast(WiFi.localIP(), beaconAddress, beaconPort);
Serial.println("success!"); Serial.println("success!");
@ -110,14 +128,13 @@ void findServer()
int colonPos = findChar(datagramStr, ':'); int colonPos = findChar(datagramStr, ':');
char role[7]; char role[7];
char serverIp[16];
char portNumberText[7]; char portNumberText[7];
memset(role, '\0', 7); memset(role, '\0', 7);
memset(serverIp, '\0', 16); memset(pixelHubServerIp, '\0', 16);
memset(portNumberText, '\0', 7); memset(portNumberText, '\0', 7);
strncpy(role, datagramStr, atPos); Serial.print("R: "); Serial.println(role); strncpy(role, datagramStr, atPos);
strncpy(serverIp, datagramStr + atPos + 1, colonPos - atPos - 1); strncpy(pixelHubServerIp, datagramStr + atPos + 1, colonPos - atPos - 1);
strncpy(portNumberText, datagramStr + colonPos + 1, datagramSize - colonPos - 1); strncpy(portNumberText, datagramStr + colonPos + 1, datagramSize - colonPos - 1);
Serial.println("complete."); Serial.println("complete.");
@ -133,6 +150,9 @@ void findServer()
// If the advertiser isn't playing the role of a server, then we're not interested // If the advertiser isn't playing the role of a server, then we're not interested
if(role != "server") continue; if(role != "server") continue;
int portNumber = atoi(portNumberText); pixelHubPortNumber = atoi(portNumberText);
return;
} }
} }