Tweak a few things & replace spaces with tabs
This commit is contained in:
parent
70ef418a7a
commit
82f0b9620c
2 changed files with 116 additions and 96 deletions
|
@ -23,116 +23,136 @@ 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
|
||||||
Serial.begin(4800);
|
Serial.begin(4800);
|
||||||
|
|
||||||
Serial.println("Hello, world!");
|
Serial.println("Hello, world!");
|
||||||
|
|
||||||
Serial.println("Beginning connection sequence.");
|
Serial.println("Beginning connection sequence.");
|
||||||
Serial.print("Attempting to connect to ");
|
Serial.print("Attempting to connect to ");
|
||||||
Serial.print(ssid);
|
Serial.print(ssid);
|
||||||
Serial.print(" - ");
|
Serial.print(" - ");
|
||||||
|
|
||||||
WiFi.begin(ssid, password);
|
WiFi.begin(ssid, password);
|
||||||
|
|
||||||
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!");
|
printWiFiInfoLocal();
|
||||||
|
findServer();
|
||||||
printWiFiInfoLocal();
|
|
||||||
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];
|
||||||
UdpClient.beginMulticast(WiFi.localIP(), beaconAddress, beaconPort);
|
memset(datagramBuffer, '\0', datagramBufferSize); // Prefill the buffer with zeros for protection later
|
||||||
Serial.println("success!");
|
UdpClient.beginMulticast(WiFi.localIP(), beaconAddress, beaconPort);
|
||||||
|
Serial.println("success!");
|
||||||
|
|
||||||
Serial.println("Listening for PixelHub beacon pings.");
|
Serial.println("Listening for PixelHub beacon pings.");
|
||||||
while(true) {
|
while(true) {
|
||||||
int datagramSize = UdpClient.parsePacket();
|
int datagramSize = UdpClient.parsePacket();
|
||||||
if(!datagramSize) continue;
|
if(!datagramSize) continue;
|
||||||
|
|
||||||
Serial.print("Received datagram #");
|
Serial.print("Received datagram #");
|
||||||
Serial.print(datagramSize);
|
Serial.print(datagramSize);
|
||||||
Serial.print(" bytes in size from ");
|
Serial.print(" bytes in size from ");
|
||||||
Serial.print(UdpClient.remoteIP());
|
Serial.print(UdpClient.remoteIP());
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(UdpClient.remotePort());
|
Serial.print(UdpClient.remotePort());
|
||||||
if(datagramSize > datagramBufferSize) {
|
if(datagramSize > datagramBufferSize) {
|
||||||
Serial.println(", but the message is larger than the datagram buffer size.");
|
Serial.println(", but the message is larger than the datagram buffer size.");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Serial.println(".");
|
Serial.println(".");
|
||||||
|
|
||||||
UdpClient.read(datagramBuffer, datagramSize);
|
UdpClient.read(datagramBuffer, datagramSize);
|
||||||
|
|
||||||
Serial.print("Content as hex: ");
|
Serial.print("Content as hex: ");
|
||||||
for(int i = 0; i < datagramSize; i++) {
|
for(int i = 0; i < datagramSize; i++) {
|
||||||
Serial.print(datagramBuffer[i], HEX);
|
Serial.print(datagramBuffer[i], HEX);
|
||||||
Serial.print(" ");
|
Serial.print(" ");
|
||||||
}
|
}
|
||||||
Serial.println();
|
Serial.println();
|
||||||
Serial.print("Raw content: ");
|
Serial.print("Raw content: ");
|
||||||
Serial.write(datagramBuffer, datagramSize);
|
Serial.write(datagramBuffer, datagramSize);
|
||||||
Serial.println();
|
Serial.println();
|
||||||
|
|
||||||
Serial.print("Parsing datagram - ");
|
Serial.print("Parsing datagram - ");
|
||||||
|
|
||||||
// Parse the recieved message
|
// Parse the recieved message
|
||||||
char* datagramStr = (char*)datagramBuffer;
|
char* datagramStr = (char*)datagramBuffer;
|
||||||
|
|
||||||
// Find the positions of the key characters
|
// Find the positions of the key characters
|
||||||
//int atPos = getPosition(datagramStr, datagramSize, '@');
|
//int atPos = getPosition(datagramStr, datagramSize, '@');
|
||||||
//int colonPos = getPosition(datagramStr, datagramSize, ':');
|
//int colonPos = getPosition(datagramStr, datagramSize, ':');
|
||||||
int atPos = findChar(datagramStr, '@');
|
int atPos = findChar(datagramStr, '@');
|
||||||
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(pixelHubServerIp, '\0', 16);
|
||||||
memset(serverIp, '\0', 16);
|
memset(portNumberText, '\0', 7);
|
||||||
memset(portNumberText, '\0', 7);
|
|
||||||
|
strncpy(role, datagramStr, atPos);
|
||||||
strncpy(role, datagramStr, atPos); Serial.print("R: "); Serial.println(role);
|
strncpy(pixelHubServerIp, datagramStr + atPos + 1, colonPos - atPos - 1);
|
||||||
strncpy(serverIp, 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.");
|
|
||||||
|
Serial.print("atPos: "); Serial.println(atPos);
|
||||||
Serial.print("atPos: "); Serial.println(atPos);
|
Serial.print("colonPos: "); Serial.println(colonPos);
|
||||||
Serial.print("colonPos: "); Serial.println(colonPos);
|
|
||||||
|
Serial.print("Role: "); Serial.print(role); Serial.print(" ");
|
||||||
Serial.print("Role: "); Serial.print(role); Serial.print(" ");
|
Serial.print("Remote IP: "); Serial.print(serverIp); Serial.print(" ");
|
||||||
Serial.print("Remote IP: "); Serial.print(serverIp); Serial.print(" ");
|
Serial.print("Port number: "); Serial.print(portNumberText);
|
||||||
Serial.print("Port number: "); Serial.print(portNumberText);
|
Serial.println();
|
||||||
Serial.println();
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,9 +4,9 @@
|
||||||
|
|
||||||
int findChar(char* str, char targetChar)
|
int findChar(char* str, char targetChar)
|
||||||
{
|
{
|
||||||
for(int i = 0; str[i] != '\0'; i++) {
|
for(int i = 0; str[i] != '\0'; i++) {
|
||||||
if(str[i] == targetChar) return i;
|
if(str[i] == targetChar) return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
Reference in a new issue