Write initial beacon message parser, but it's throwing a runtime error.
This commit is contained in:
parent
f6946ddb27
commit
48d61d74c7
3 changed files with 52 additions and 2 deletions
|
@ -8,8 +8,10 @@
|
|||
#include <ESP8266WiFi.h>
|
||||
#include <WiFiUdp.h>
|
||||
|
||||
#include "Utilities.h"
|
||||
|
||||
char ssid[] = "ssid";
|
||||
char password[] = "password";
|
||||
char password[] = "pass";
|
||||
|
||||
// The address that the PixelHub beacon is broadcasting on.
|
||||
IPAddress beaconAddress(239, 62, 148, 30);
|
||||
|
@ -46,6 +48,7 @@ void setup()
|
|||
Serial.println("success!");
|
||||
|
||||
printWiFiInfoLocal();
|
||||
findServer();
|
||||
}
|
||||
|
||||
void loop()
|
||||
|
@ -94,6 +97,34 @@ void findServer()
|
|||
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 = 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
9
PixelHub-Client/Utilities.cpp
Normal file
9
PixelHub-Client/Utilities.cpp
Normal file
|
@ -0,0 +1,9 @@
|
|||
#include <string.h>
|
||||
|
||||
int findChar(char* str, char targetChar)
|
||||
{
|
||||
for(int i = 0; strcmp(&str[i], "\0") == 0; i++) {
|
||||
if(str[i] == targetChar) return i;
|
||||
}
|
||||
}
|
||||
|
10
PixelHub-Client/Utilities.h
Normal file
10
PixelHub-Client/Utilities.h
Normal file
|
@ -0,0 +1,10 @@
|
|||
/**
|
||||
* Various useful utility functions for the Arduino
|
||||
*********************************************************
|
||||
* By Starbeamrainbowlabs <bugs@starbeamrainbowlabs.com> - https://starbeamrainbowlabs.com)
|
||||
* Originally written for PixelHub - http://git.starbeamrainbowlabs.com/sbrl/PixelHub
|
||||
*
|
||||
* Thanks to Rob Miles for getting me into this :-)
|
||||
*/
|
||||
|
||||
int findChar(char* str, char targetChar);
|
Reference in a new issue