[untested] Refactor pixelbot code into own class.

This commit is contained in:
Starbeamrainbowlabs 2016-10-21 21:47:49 +01:00
parent f9d6b7a882
commit 9f24dbe7f6
4 changed files with 130 additions and 110 deletions

View file

@ -0,0 +1,105 @@
#include "PixelBot.h"
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include "Utilities.h"
PixelBot::PixelBot()
{
memset(serverIp, '\0', 16);
}
PixelBot::~PixelBot()
{
}
/// <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 `serverIp` and `portNumber` data members.
/// </description>
void PixelBot::FindServer()
{
Serial.print("Initialising PixelHub auto detection system - ");
// The UDP Client for sending and recieving UDP datagrams.
WiFiUDP UdpClient;
byte datagramBuffer[datagramBufferSize];
memset(datagramBuffer, '\0', datagramBufferSize); // Prefill the datagram 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 serverPortText[7];
memset(role, '\0', 7);
memset(serverPortText, '\0', 7);
strncpy(role, datagramStr, atPos);
strncpy(serverIp, datagramStr + atPos + 1, colonPos - atPos - 1);
strncpy(serverPortText, 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(serverPortText);
Serial.println();
// If the advertiser isn't playing the role of a server, then we're not interested
if(role != "server") continue;
serverPort = atoi(serverPortText);
return;
}
}

View file

@ -1,12 +1,33 @@
#pragma once #pragma once
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
class PixelBot class PixelBot
{ {
public: public:
PixelBot(); PixelBot();
~PixelBot(); ~PixelBot();
private: void FindServer();
private:
char serverIp[16];
int serverPort = -1;
// ~~~ Auto Discovery System ~~~ //
// The address that the PixelHub beacon is broadcasting on.
IPAddress beaconAddress = IPAddress(239, 62, 148, 30);
// The port number that the PixelHub beacon is broadcasting on.
unsigned int beaconPort = 5050;
// The size of the datagram buffer that is used to buffer incoming messages.
int datagramBufferSize = 256;
// ~~~ ~~~ //
// The TCP client
WiFiClient tcpClient = WiFiClient();
}; };

View file

@ -1,10 +0,0 @@
#include "PixelBot.h"
PixelBot::PixelBot()
{
}
PixelBot::~PixelBot()
{
}

View file

@ -4,28 +4,14 @@
* Originally adapted from https://www.arduino.cc/en/Tutorial/ConnectWithWPA * Originally adapted from https://www.arduino.cc/en/Tutorial/ConnectWithWPA
* by Starbeamrainbowlabs * by Starbeamrainbowlabs
*/ */
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include "Utilities.h" #include "PixelBot.h"
char ssid[] = "ZimWeaver"; char ssid[] = "ZimWeaver";
char password[] = "***REMOVED***"; char password[] = "***REMOVED***";
// The address that the PixelHub beacon is broadcasting on. PixelBot pixelBot;
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() void setup()
{ {
@ -50,7 +36,7 @@ void setup()
Serial.println("success!"); Serial.println("success!");
printWiFiInfoLocal(); printWiFiInfoLocal();
findServer(); pixelBot.FindServer();
} }
@ -73,86 +59,4 @@ void printWiFiInfoLocal()
// ~~~ PixelHub auto discovery system ~~~ // // ~~~ 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;
}
}