This repository has been archived on 2019-06-21. You can view files and clone it, but cannot push or open issues or pull requests.
PixelHub/PixelHub-Client/PixelHub-Client.ino

159 lines
4.1 KiB
Arduino
Raw Normal View History

/*
* Connecting to a WiFi network with WPA
************************************************
* Originally adapted from https://www.arduino.cc/en/Tutorial/ConnectWithWPA
* by Starbeamrainbowlabs
*/
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include "Utilities.h"
2016-10-20 17:46:28 +00:00
char ssid[] = "ZimWeaver";
2016-10-16 14:22:05 +00:00
char password[] = "***REMOVED***";
// 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.
WiFiUDP UdpClient;
// The size of the datagram buffer that is used to buffer incoming messages.
int datagramBufferSize = 256;
WiFiClient tcpClient;
char pixelHubServerIp[16];
int pixelHubPortNumber;
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 a second for the connection to be established
delay(1000);
}
Serial.println("success!");
printWiFiInfoLocal();
findServer();
}
void loop()
{
}
// ~~~ WiFi Diagnostics ~~~ //
/// <summary>
/// Prints the local IP address to the serial connection.
/// </summary>
void printWiFiInfoLocal()
{
Serial.print("IP Address: ");
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()
{
Serial.print("Initialising PixelHub auto detection system - ");
byte datagramBuffer[datagramBufferSize];
memset(datagramBuffer, '\0', datagramBufferSize); // Prefill the buffer with zeros for protection later
UdpClient.beginMulticast(WiFi.localIP(), beaconAddress, beaconPort);
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 ");
Serial.print(UdpClient.remoteIP());
Serial.print(":");
Serial.print(UdpClient.remotePort());
if(datagramSize > datagramBufferSize) {
Serial.println(", but the message is larger than the datagram buffer size.");
continue;
}
Serial.println(".");
UdpClient.read(datagramBuffer, datagramSize);
Serial.print("Content as hex: ");
for(int i = 0; i < datagramSize; i++) {
Serial.print(datagramBuffer[i], HEX);
Serial.print(" ");
}
Serial.println();
Serial.print("Raw content: ");
Serial.write(datagramBuffer, datagramSize);
Serial.println();
Serial.print("Parsing datagram - ");
// Parse the recieved message
char* datagramStr = (char*)datagramBuffer;
// Find the positions of the key characters
//int atPos = getPosition(datagramStr, datagramSize, '@');
//int colonPos = getPosition(datagramStr, datagramSize, ':');
int atPos = findChar(datagramStr, '@');
int colonPos = findChar(datagramStr, ':');
char role[7];
char portNumberText[7];
memset(role, '\0', 7);
memset(pixelHubServerIp, '\0', 16);
memset(portNumberText, '\0', 7);
strncpy(role, datagramStr, atPos);
strncpy(pixelHubServerIp, datagramStr + atPos + 1, colonPos - atPos - 1);
strncpy(portNumberText, datagramStr + colonPos + 1, datagramSize - colonPos - 1);
Serial.println("complete.");
Serial.print("atPos: "); Serial.println(atPos);
Serial.print("colonPos: "); Serial.println(colonPos);
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;
pixelHubPortNumber = atoi(portNumberText);
return;
}
}