diff --git a/FDRS_Gateway2000/FDRS_Gateway2000.ino b/FDRS_Gateway2000/FDRS_Gateway2000.ino index c76e848..6dc263f 100644 --- a/FDRS_Gateway2000/FDRS_Gateway2000.ino +++ b/FDRS_Gateway2000/FDRS_Gateway2000.ino @@ -28,13 +28,6 @@ const char* password = WIFI_PASS; const char* mqtt_server = MQTT_ADDR; #endif -void sendLoRa() { -#ifdef USE_LORA - LoRa.beginPacket(); - LoRa.write((uint8_t*)&theData, ln); - LoRa.endPacket(); -#endif -} void setup() { #if defined(ESP8266) @@ -62,6 +55,32 @@ void setup() { } void loop() { + if (millis() > timeESPNOWG) { + timeESPNOWG += ESPNOWG_DELAY; + if (lenESPNOWG > 0) releaseESPNOW(0); + } + if (millis() > timeESPNOW1) { + timeESPNOW1 += ESPNOW1_DELAY; + if (lenESPNOW1 > 0) releaseESPNOW(1); + } + if (millis() > timeESPNOW2) { + timeESPNOW2 += ESPNOW2_DELAY; + if (lenESPNOW2 > 0) releaseESPNOW(2); + } + if (millis() > timeSERIAL) { + //Serial.println("timeSERIAL tripped: " + String(lenSERIAL)); + timeSERIAL += SERIAL_DELAY; + if (lenSERIAL > 0) releaseSerial(); + } + if (millis() > timeMQTT) { + timeMQTT += MQTT_DELAY; + if (lenMQTT > 0) releaseMQTT(); + } + if (millis() > timeLORA) { + timeLORA += LORA_DELAY; + if (lenLORA > 0) releaseLoRa(); + } + while (Serial.available()) { getSerial(); } @@ -73,6 +92,12 @@ void loop() { ln = packetSize; newData = 6; } +#endif +#ifdef USE_WIFI + if (!client.connected()) { + reconnect(); + } + client.loop(); #endif if (newData) { switch (newData) { @@ -97,10 +122,4 @@ void loop() { } newData = 0; } -#ifdef USE_WIFI - if (!client.connected()) { - reconnect(); - } - client.loop(); -#endif } diff --git a/FDRS_Gateway2000/fdrs_config.h b/FDRS_Gateway2000/fdrs_config.h index 9d5266e..e4028ed 100644 --- a/FDRS_Gateway2000/fdrs_config.h +++ b/FDRS_Gateway2000/fdrs_config.h @@ -4,40 +4,51 @@ // This is still in progress. Stay tuned! #define RXD2 21 #define TXD2 22 -#define UNIT_MAC 0x00// THIS UNIT -#define PREV_MAC 0x01// ESPNOW1 Address -#define NEXT_MAC 0x02// ESPNOW2 Address +#define UNIT_MAC 0x01// THIS UNIT +#define PREV_MAC 0x00// ESPNOW1 Address +#define NEXT_MAC 0x03// ESPNOW2 Address //#define USE_WIFI //You cannot use ESP-NOW while WiFi is in use -//#define WIFI_NET "Your SSID" -//#define WIFI_PASS "Password" -//#define MQTT_ADDR "192.168.0.8" +#define WIFI_NET "Your SSID" +#define WIFI_PASS "Password" +#define MQTT_ADDR "192.168.0.8" //#define USE_LORA -//#define SCK 5 -//#define MISO 19 -//#define MOSI 27 -//#define SS 18 -//#define RST 14 -//#define DIO0 26 -////433E6 for Asia -////866E6 for Europe -////915E6 for North America -//#define BAND 915E6 +#define SCK 5 +#define MISO 19 +#define MOSI 27 +#define SS 18 +#define RST 14 +#define DIO0 26 +//433E6 for Asia +//866E6 for Europe +//915E6 for North America +#define BAND 915E6 //Actions -- Define what happens when a packet arrives at each interface: //Current function options are: sendESPNOW(interface), sendSerial(), sendMQTT(), and sendLoRa(). -#define ESPNOW1_ACT sendESPNOW(2); sendSerial(); -#define ESPNOW2_ACT sendESPNOW(1); sendSerial(); -#define ESPNOWG_ACT sendSerial(); sendLoRa(); -#define SERIAL_ACT sendESPNOW(0); sendLoRa(); -#define MQTT_ACT sendSerial(); -#define LORA_ACT sendSerial(); +#define ESPNOW1_DELAY 0 +#define ESPNOW2_DELAY 0 +#define ESPNOWG_DELAY 0 +#define SERIAL_DELAY 0 +#define MQTT_DELAY 0 +#define LORA_DELAY 0 -//#define ESPNOW1_ACTION -//#define ESPNOW2_ACTION -//#define ESPNOWG_ACTION +//Use these settings for a gateway that recieves ESP-NOW data and sends serial (UART). +#define ESPNOW1_ACT sendSerial(); +#define ESPNOW2_ACT sendSerial(); +#define ESPNOWG_ACT sendSerial(); +#define SERIAL_ACT sendESPNOW(0); +#define MQTT_ACT sendSerial(); +#define LORA_ACT sendSerial(); + +//Use these settings for a gateway that recieves serial (UART) data and sends MQTT. +//#define USE_WIFI +//#define ESPNOW1_ACTION +//#define ESPNOW2_ACTION +//#define ESPNOWG_ACTION //#define SERIAL_ACTION sendMQTT(); //#define MQTT_ACTION sendSerial(); +//#define LORA_ACT sendSerial(); diff --git a/FDRS_Gateway2000/fdrs_functions.h b/FDRS_Gateway2000/fdrs_functions.h index ed1d5fe..cda2fb6 100644 --- a/FDRS_Gateway2000/fdrs_functions.h +++ b/FDRS_Gateway2000/fdrs_functions.h @@ -1,13 +1,36 @@ +const uint8_t espnow_size = 250 / sizeof(DataReading); +const uint8_t lora_size = 256 / sizeof(DataReading); + uint8_t broadcast_mac[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; uint8_t prevAddress[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, PREV_MAC}; uint8_t selfAddress[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, UNIT_MAC}; uint8_t nextAddress[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, NEXT_MAC}; uint8_t incMAC[6]; -DataReading theData[31]; +DataReading theData[256]; uint8_t ln; uint8_t newData = 0; +DataReading bufferESPNOW1[256]; +uint8_t lenESPNOW1 = 0; +uint32_t timeESPNOW1 = 0; +DataReading bufferESPNOW2[256]; +uint8_t lenESPNOW2 = 0; +uint32_t timeESPNOW2 = 0; +DataReading bufferESPNOWG[256]; +uint8_t lenESPNOWG = 0; +uint32_t timeESPNOWG = 0; +DataReading bufferSERIAL[256]; +uint8_t lenSERIAL = 0; +uint32_t timeSERIAL = 0; +DataReading bufferMQTT[256]; +uint8_t lenMQTT = 0; +uint32_t timeMQTT = 0; +DataReading bufferLORA[256]; +uint8_t lenLORA = 0; +uint32_t timeLORA = 0; + + WiFiClient espClient; PubSubClient client(espClient); @@ -26,7 +49,219 @@ void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) { if (memcmp(&incMAC, &prevAddress, 6) == 0) newData = 1; else if (memcmp(&incMAC, &nextAddress, 6) == 0) newData = 2; else newData = 3; - ln = len; + ln = len / sizeof(DataReading); + Serial.println("RCV:" + String(ln)); +} +void getSerial() { + String incomingString = Serial.readStringUntil('\n'); + DynamicJsonDocument doc(24576); + DeserializationError error = deserializeJson(doc, incomingString); + if (error) { // Test if parsing succeeds. + Serial.println("parse err"); + return; + } else { + int s = doc.size(); + //Serial.println(s); + for (int i = 0; i < s; i++) { + theData[i].id = doc[i]["id"]; + theData[i].t = doc[i]["type"]; + theData[i].d = doc[i]["data"]; + } + ln = s; + newData = 4; + } +} +void mqtt_callback(char* topic, byte * message, unsigned int length) { + String incomingString; + for (int i = 0; i < length; i++) { + incomingString += (char)message[i]; + } + StaticJsonDocument<2048> doc; + DeserializationError error = deserializeJson(doc, incomingString); + if (error) { // Test if parsing succeeds. + //Serial.println("parse err"); + return; + } else { + int s = doc.size(); + //Serial.println(s); + for (int i = 0; i < s; i++) { + theData[i].id = doc[i]["id"]; + theData[i].t = doc[i]["type"]; + theData[i].d = doc[i]["data"]; + } + ln = s; + newData = 5; + } +} +#ifdef USE_LORA +void getLoRa() { + int packetSize = LoRa.parsePacket(); + if (packetSize) + { + LoRa.readBytes((uint8_t *)&theData, packetSize); + ln = packetSize / sizeof(DataReading); + newData = 6; + } +} +#endif + + +void sendESPNOW(uint8_t interface) { + switch (interface) { + case 0: + for (int i = 0; i < ln; i++) { + bufferESPNOWG[lenESPNOWG + i] = theData[i]; + } + lenESPNOWG += ln; + break; + case 1: + for (int i = 0; i < ln; i++) { + bufferESPNOW1[lenESPNOW1 + i] = theData[i]; + } + lenESPNOW1 += ln; + break; + case 2: + for (int i = 0; i < ln; i++) { + bufferESPNOW2[lenESPNOW2 + i] = theData[i]; + } + lenESPNOW2 += ln; + break; + } +} +void sendSerial() { + for (int i = 0; i < ln; i++) { + bufferSERIAL[lenSERIAL + i] = theData[i]; + } + lenSERIAL += ln; + Serial.println("SENDSERIAL:" + String(lenSERIAL)+" "); + +} +void sendMQTT() { + for (int i = 0; i < ln; i++) { + bufferMQTT[lenMQTT + i] = theData[i]; + } + lenMQTT += ln; +} +void sendLoRa() { + for (int i = 0; i < ln; i++) { + bufferLORA[lenLORA + i] = theData[i]; + } + lenLORA += ln; +} + +void releaseESPNOW(uint8_t interface) { + switch (interface) { + case 0: + { + DataReading thePacket[espnow_size]; + int j = 0; + for (int i = 0; i < lenESPNOWG; i++) { + if ( j > 250 / sizeof(DataReading)) { + j = 0; + esp_now_send(broadcast_mac, (uint8_t *) &thePacket, sizeof(thePacket)); + } + thePacket[j] = bufferESPNOWG[i]; + j++; + } + esp_now_send(broadcast_mac, (uint8_t *) &thePacket, j * sizeof(DataReading)); + lenESPNOWG = 0; + break; + } + case 1: + { + DataReading thePacket[espnow_size]; + int j = 0; + for (int i = 0; i < lenESPNOW1; i++) { + if ( j > 250 / sizeof(DataReading)) { + j = 0; + esp_now_send(prevAddress, (uint8_t *) &thePacket, sizeof(thePacket)); + } + thePacket[j] = bufferESPNOW1[i]; + j++; + } + esp_now_send(prevAddress, (uint8_t *) &thePacket, j * sizeof(DataReading)); + lenESPNOW1 = 0; + break; + } + case 2: + { + DataReading thePacket[espnow_size]; + int j = 0; + for (int i = 0; i < lenESPNOW2; i++) { + if ( j > 250 / sizeof(DataReading)) { + j = 0; + esp_now_send(nextAddress, (uint8_t *) &thePacket, sizeof(thePacket)); + } + thePacket[j] = bufferESPNOW2[i]; + j++; + } + esp_now_send(nextAddress, (uint8_t *) &thePacket, j * sizeof(DataReading)); + lenESPNOW2 = 0; + break; + } + } +} +void releaseSerial() { + //DynamicJsonDocument doc(24576); + StaticJsonDocument<2048> doc; + + for (int i = 0; i < lenSERIAL; i++) { + doc[i]["id"] = bufferSERIAL[i].id; + doc[i]["type"] = bufferSERIAL[i].t; + doc[i]["data"] = bufferSERIAL[i].d; + } + serializeJson(doc, Serial); + Serial.println(); + lenSERIAL = 0; +} +void releaseMQTT() { +#ifdef USE_WIFI + DynamicJsonDocument doc(24576); + for (int i = 0; i < lenMQTT; i++) { + doc[i]["id"] = bufferMQTT[i].id; + doc[i]["type"] = bufferMQTT[i].t; + doc[i]["data"] = bufferMQTT[i].d; + } + String outgoingString; + serializeJson(doc, outgoingString); + client.publish("esp/fdrs", (char*) outgoingString.c_str()); + lenMQTT = 0; +#endif +} +void releaseLoRa() { +#ifdef USE_LORA + DataReading thePacket[lora_size]; + int j = 0; + for (int i = 0; i < lenLORA); i++) { + if ( j > lora_size)) { + j = 0; + LoRa.beginPacket(); + LoRa.write((uint8_t*)&thePacket, j * sizeof(DataReading)); + LoRa.endPacket(); + } + thePacket[j] = bufferLORA[i]; + j++; + } + LoRa.beginPacket(); + LoRa.write((uint8_t*)&thePacket, j * sizeof(DataReading)); + LoRa.endPacket(); + lenLORA = 0; + +#endif +} + +void reconnect() { + // Loop until reconnected + while (!client.connected()) { + // Attempt to connect + if (client.connect("FDRS_GATEWAY")) { + // Subscribe + client.subscribe("esp/fdrs"); + } else { + // Wait 5 seconds before retrying + delay(5000); + } + } } void begin_espnow() { WiFi.mode(WIFI_STA); @@ -72,99 +307,3 @@ void begin_espnow() { } #endif } -void getSerial() { - String incomingString = Serial.readStringUntil('\n'); - StaticJsonDocument<2048> doc; - DeserializationError error = deserializeJson(doc, incomingString); - if (error) { // Test if parsing succeeds. - Serial.println("parse err"); - return; - } else { - int s = doc.size(); - //Serial.println(s); - for (int i = 0; i < s; i++) { - if (i > 31) break; - theData[i].id = doc[i]["id"]; - theData[i].t = doc[i]["type"]; - theData[i].d = doc[i]["data"]; - } - ln = s * sizeof(DataReading); - newData = 4; - } -} - -void sendESPNOW(uint8_t interface) { - switch (interface) { - case 0: - esp_now_send(broadcast_mac, (uint8_t *) &theData, ln); - break; - case 1: - esp_now_send(prevAddress, (uint8_t *) &theData, ln); - break; - case 2: - esp_now_send(nextAddress, (uint8_t *) &theData, ln); - break; - } - -} -void sendMQTT() { -#ifdef USE_WIFI - StaticJsonDocument<2048> doc; - for (int i = 0; i < ln / sizeof(DataReading); i++) { - doc[i]["id"] = theData[i].id; - doc[i]["type"] = theData[i].t; - doc[i]["data"] = theData[i].d; - } - String incomingString; - serializeJson(doc, incomingString); - client.publish("esp/fdrs", (char*) incomingString.c_str()); -#endif -} - -void sendSerial() { - StaticJsonDocument<2048> doc; - for (int i = 0; i < ln / sizeof(DataReading); i++) { - doc[i]["id"] = theData[i].id; - doc[i]["type"] = theData[i].t; - doc[i]["data"] = theData[i].d; - } - serializeJson(doc, Serial); - Serial.println(); -} - -void mqtt_callback(char* topic, byte * message, unsigned int length) { - String incomingString; - for (int i = 0; i < length; i++) { - incomingString += (char)message[i]; - } - StaticJsonDocument<2048> doc; - DeserializationError error = deserializeJson(doc, incomingString); - if (error) { // Test if parsing succeeds. - //Serial.println("parse err"); - return; - } else { - int s = doc.size(); - //Serial.println(s); - for (int i = 0; i < s; i++) { - if (i > 31) break; - theData[i].id = doc[i]["id"]; - theData[i].t = doc[i]["type"]; - theData[i].d = doc[i]["data"]; - } - ln = s * sizeof(DataReading); - newData = 5; - } -} -void reconnect() { - // Loop until reconnected - while (!client.connected()) { - // Attempt to connect - if (client.connect("FDRS_GATEWAY")) { - // Subscribe - client.subscribe("esp/fdrs"); - } else { - // Wait 5 seconds before retrying - delay(5000); - } - } -}