136 lines
4.7 KiB
C++
136 lines
4.7 KiB
C++
//#define LOG(fmt, ...)
|
|
#define LOG(fmt, ...) (Serial.printf("%09llu: " fmt "\n", GetTimestamp(), ##__VA_ARGS__)); Serial.flush();
|
|
//#define DEBUGLOG(fmt, ...)
|
|
#define DEBUGLOG(fmt, ...) (Serial.printf("%09llu: [DEBUG] " fmt "\n", GetTimestamp(), ##__VA_ARGS__));
|
|
|
|
#define globalDelay 20000
|
|
const int MAX_PEERS = 20; //Limited due espNOW
|
|
//static const int WIFIESPNOW_KEYLEN = 16; //Limited due espNOW
|
|
const uint8_t key[16] = { 0 };
|
|
|
|
#if defined(ESP8266)
|
|
#include <ESP8266WiFi.h>
|
|
#include <Hash.h>
|
|
#elif defined(ESP32)
|
|
#include <WiFi.h>
|
|
#endif
|
|
#include <WifiEspNowBroadcast.h> //https://github.com/yoursunny/WifiEspNow
|
|
#include <Ticker.h> //Ticker Library
|
|
|
|
//version 1.1 - added originMAC
|
|
typedef struct {
|
|
char PREAMBLE1 = 'R';
|
|
char PREAMBLE2 = 'M';
|
|
uint8_t messageID[20];
|
|
char code = 'g'; // [g, y, r, X]
|
|
bool batteryWarning = false; //ToDo not implemented yet
|
|
uint8_t originMAC[6] = {0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
|
} rm370;
|
|
|
|
//version 1.0
|
|
typedef struct node {
|
|
uint8_t mac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
|
char identifier[20] = { ' ', 'e', 's', ' ', 'd', 'e', 'v', 'i', 'c', 'e', '1', '2', '3', '4', ' ', ' ', ' ', ' ', ' ', '\0' };
|
|
};
|
|
|
|
//EA:DB:84:D1:21:66 Dashboard
|
|
//EA:DB:84:D0:AB:34
|
|
//EA:DB:84:D0:E3:5C
|
|
|
|
rm370 receiveBuffer[MAX_PEERS];
|
|
node nodeList[] = {
|
|
{ {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, { 'B', 'r', 'o', 'a', 'd', 'c', 'a', 's', 't', '\0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\0' } },
|
|
{ {0xEA, 0xDB, 0x84, 0xD1, 0x21, 0x66}, { 'D', 'a', 's', 'h', 'b', 'o', 'a', 'r', 'd', '\0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\0' } },
|
|
{ {0xEA, 0xDB, 0x84, 0xD0, 0xAB, 0x34}, { 'T', 'e', 's', 't', 'd', 'e', 'v', 'i', 'c', 'e', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\0' } },
|
|
{ {0xEA, 0xDB, 0x84, 0xD0, 0xE3, 0x5C}, { 'T', 'e', 's', 't', 'd', 'e', 'v', 'i', 'c', 'e', '3', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\0' } },
|
|
// { {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, { 'T', 'e', 's', 't', 'd', 'e', 'v', 'i', 'c', 'e', '4', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\0' } },
|
|
// { {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, { 'T', 'e', 's', 't', 'd', 'e', 'v', 'i', 'c', 'e', '5', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\0' } },
|
|
// { {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, { 'T', 'e', 's', 't', 'd', 'e', 'v', 'i', 'c', 'e', '6', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\0' } },
|
|
};
|
|
|
|
Ticker globalStatusTicker;
|
|
volatile unsigned long globalStart = 0; //real physics workaround
|
|
volatile unsigned long now = millis(); //real physics workaround
|
|
volatile byte rxCounter = 0;
|
|
volatile byte rxHandlePointer = 0;
|
|
volatile bool refreshGlobalStatus = false;
|
|
|
|
int64_t GetTimestamp() {
|
|
struct timeval tv;
|
|
gettimeofday(&tv, NULL);
|
|
return (tv.tv_sec * 1000LL + (tv.tv_usec / 1000LL));
|
|
}
|
|
|
|
void globalStatus() {
|
|
|
|
/*
|
|
print global overview
|
|
battery warning
|
|
ToDo:
|
|
last message time stamp --> see following task
|
|
show missing stations --> new structure needed
|
|
*/
|
|
if (!refreshGlobalStatus) {
|
|
return;
|
|
}
|
|
DEBUGLOG("available Peers:");
|
|
//WifiEspNowPeerInfo peers[MAX_PEERS];
|
|
//int nPeers = std::min(WifiEspNow.listPeers(peers, MAX_PEERS), MAX_PEERS);
|
|
int nPeers = sizeof(nodeList)/sizeof(node);
|
|
for (int i = 0; i < nPeers; ++i) {
|
|
byte loopPointer = (rxCounter - 1 >= 0) ? (rxCounter - 1) : MAX_PEERS - 1;
|
|
LOG(" %02X:%02X:%02X:%02X:%02X:%02X | %s", nodeList[i].mac[0], nodeList[i].mac[1], nodeList[i].mac[2], nodeList[i].mac[3], nodeList[i].mac[4], nodeList[i].mac[5], nodeList[i].identifier);
|
|
//walk backwards through the buffer to get the latest message
|
|
while (loopPointer != rxCounter)
|
|
{
|
|
//DEBUGLOG("rxCounter | loopPointer: %d|%d", rxCounter, loopPointer);
|
|
int ret = memcmp(&receiveBuffer[loopPointer].originMAC[0], &nodeList[i].mac, sizeof(receiveBuffer[loopPointer].originMAC));
|
|
if (ret == 0) {
|
|
LOG("\tStatus: %c | BatWarn: %c", receiveBuffer[loopPointer].code, (receiveBuffer[loopPointer].batteryWarning) ? '!' : '.');
|
|
loopPointer = rxCounter + 1;
|
|
}
|
|
if (loopPointer - 1 >= 0) {
|
|
loopPointer--;
|
|
}
|
|
else {
|
|
loopPointer = MAX_PEERS - 1;
|
|
}
|
|
}
|
|
refreshGlobalStatus = false;
|
|
}
|
|
}
|
|
|
|
void setup() {
|
|
//prefillNodeList();
|
|
Serial.begin(115200);
|
|
Serial.flush();
|
|
delay(1000);
|
|
LOG("\n\nDashboard with Serial.print");
|
|
|
|
if (!initCommunication()) {
|
|
ESP.restart();
|
|
}
|
|
globalStatusTicker.attach(1.0, globalStatus);
|
|
LOG("Setup completed.\n");
|
|
}
|
|
|
|
void loop() {
|
|
//loop things
|
|
now = millis();
|
|
WifiEspNowBroadcast.loop();
|
|
|
|
|
|
//process remaining rxBuffer
|
|
while (rxHandlePointer != rxCounter)
|
|
{
|
|
checkMessage();
|
|
//shift ring buffer working pointer
|
|
if (rxHandlePointer + 1 < MAX_PEERS) {
|
|
rxHandlePointer++;
|
|
}
|
|
else {
|
|
rxHandlePointer = 0;
|
|
}
|
|
}
|
|
}
|