From 62c247275ac13245dfac35e308652cedb25e0914 Mon Sep 17 00:00:00 2001 From: Sascha Date: Sat, 23 Jul 2022 01:09:58 +0200 Subject: [PATCH] Applied changes to FDRS_Sensor and FDRS_Gateway folders --- FDRS_Gateway/FDRS_Gateway.ino | 20 ++- FDRS_Gateway/fdrs_functions.h | 157 +++++++++++++++++++---- FDRS_Gateway/fdrs_gateway_config.h | 17 ++- FDRS_Sensor/FDRS_Sensor.ino | 1 + FDRS_Sensor/fdrs_sensor.h | 52 +++----- examples/1_LoRa_Sensor/1_LoRa_Sensor.ino | 2 +- fdrs_sensor.h | 1 - 7 files changed, 173 insertions(+), 77 deletions(-) diff --git a/FDRS_Gateway/FDRS_Gateway.ino b/FDRS_Gateway/FDRS_Gateway.ino index 121dbb8..453efae 100644 --- a/FDRS_Gateway/FDRS_Gateway.ino +++ b/FDRS_Gateway/FDRS_Gateway.ino @@ -45,12 +45,26 @@ void setup() { Serial.begin(115200); UART_IF.begin(115200, SERIAL_8N1, RXD2, TXD2); #endif + DBG("Address:" + String (UNIT_MAC, HEX)); + +#ifdef DEBUG_NODE_CONFIG + // find out the reset reason + esp_reset_reason_t resetReason; + resetReason = esp_reset_reason(); + if (resetReason != ESP_RST_DEEPSLEEP) { + checkConfig(); + } +#endif //DEBUG_NODE_CONFIG + #ifdef USE_LED FastLED.addLeds(leds, NUM_LEDS); leds[0] = CRGB::Blue; FastLED.show(); #endif +#ifdef USE_LORA + begin_lora(); +#endif #ifdef USE_WIFI delay(10); WiFi.begin(ssid, password); @@ -69,9 +83,7 @@ void setup() { #else begin_espnow(); #endif -#ifdef USE_LORA - begin_lora(); -#endif + #ifdef USE_SD_LOG begin_SD(); #endif @@ -152,7 +164,7 @@ void loop() { client.loop(); // for recieving incoming messages and maintaining connection #endif - if (newData) { + if (newData != event_clear) { switch (newData) { case event_espnowg: ESPNOWG_ACT diff --git a/FDRS_Gateway/fdrs_functions.h b/FDRS_Gateway/fdrs_functions.h index 8d008a0..e1eed7c 100644 --- a/FDRS_Gateway/fdrs_functions.h +++ b/FDRS_Gateway/fdrs_functions.h @@ -19,6 +19,7 @@ enum { event_lora1, event_lora2 }; + #ifdef FDRS_DEBUG #define DBG(a) (Serial.println(a)) #else @@ -31,32 +32,99 @@ enum { #define UART_IF Serial #endif -#ifdef FDRS_GLOBALS -#define FDRS_WIFI_SSID GLOBAL_SSID -#define FDRS_WIFI_PASS GLOBAL_PASS -#define FDRS_MQTT_ADDR GLOBAL_MQTT_ADDR -#define FDRS_MQTT_PORT GLOBAL_MQTT_PORT -#define FDRS_MQTT_USER GLOBAL_MQTT_USER -#define FDRS_MQTT_PASS GLOBAL_MQTT_PASS -#define FDRS_BAND GLOBAL_LORA_BAND -#define FDRS_SF GLOBAL_LORA_SF -#else +// enable to get detailed info from where single configuration macros have been taken +#define DEBUG_NODE_CONFIG + +#ifdef USE_WIFI + +// select WiFi SSID configuration +#if defined(WIFI_SSID) #define FDRS_WIFI_SSID WIFI_SSID +#elif defined (GLOBAL_SSID) +#define FDRS_WIFI_SSID GLOBAL_SSID +#else +// ASSERT("NO WiFi SSID defined! Please define in fdrs_globals.h (recommended) or in fdrs_sensor_config.h"); +#endif //WIFI_SSID + +// select WiFi password +#if defined(WIFI_PASS) #define FDRS_WIFI_PASS WIFI_PASS +#elif defined (GLOBAL_PASS) +#define FDRS_WIFI_PASS GLOBAL_PASS +#else +// ASSERT("NO WiFi password defined! Please define in fdrs_globals.h (recommended) or in fdrs_sensor_config.h"); +#endif //WIFI_PASS + +// select MQTT server address +#if defined(MQTT_ADDR) #define FDRS_MQTT_ADDR MQTT_ADDR +#elif defined (GLOBAL_MQTT_ADDR) +#define FDRS_MQTT_ADDR GLOBAL_MQTT_ADDR +#else +// ASSERT("NO MQTT address defined! Please define in fdrs_globals.h (recommended) or in fdrs_sensor_config.h"); +#endif //MQTT_ADDR + +// select MQTT server port +#if defined(MQTT_PORT) #define FDRS_MQTT_PORT MQTT_PORT +#elif defined (GLOBAL_MQTT_PORT) +#define FDRS_MQTT_PORT GLOBAL_MQTT_PORT +#else +#define FDRS_MQTT_PORT 1883 +#endif //MQTT_PORT + +// select MQTT user name +#if defined(MQTT_USER) #define FDRS_MQTT_USER MQTT_USER +#elif defined (GLOBAL_MQTT_USER) +#define FDRS_MQTT_USER GLOBAL_MQTT_USER +#else +// ASSERT("NO MQTT user defined! Please define in fdrs_globals.h (recommended) or in fdrs_sensor_config.h"); +#endif //MQTT_USER + +// select MQTT user password +#if defined(MQTT_PASS) #define FDRS_MQTT_PASS MQTT_PASS -#define FDRS_BAND LORA_BAND -#define FDRS_SF LORA_SF -#endif +#elif defined (GLOBAL_MQTT_PASS) +#define FDRS_MQTT_PASS GLOBAL_MQTT_PASS +#else +// ASSERT("NO MQTT password defined! Please define in fdrs_globals.h (recommended) or in fdrs_sensor_config.h"); +#endif //MQTT_PASS #if defined (MQTT_AUTH) || defined (GLOBAL_MQTT_AUTH) #define FDRS_MQTT_AUTH -#endif +#endif //MQTT_AUTH + +#endif //USE_WIFI + +#ifdef USE_LORA + +// select LoRa band configuration +#if defined(LORA_BAND) +#define FDRS_BAND LORA_BAND +#elif defined (GLOBAL_LORA_BAND) +#define FDRS_BAND GLOBAL_LORA_BAND +#else +// ASSERT("NO LORA-BAND defined! Please define in fdrs_globals.h (recommended) or in fdrs_sensor_config.h"); +#endif //LORA_BAND + +// select LoRa SF configuration +#if defined(LORA_SF) +#define FDRS_SF LORA_SF +#elif defined (GLOBAL_LORA_SF) +#define FDRS_SF GLOBAL_LORA_SF +#else +// ASSERT("NO LORA-SF defined! Please define in fdrs_globals.h (recommended) or in fdrs_sensor_config.h"); +#endif //LORA_SF + +#endif //USE_LORA #define MAC_PREFIX 0xAA, 0xBB, 0xCC, 0xDD, 0xEE // Should only be changed if implementing multiple FDRS systems. +#ifdef DEBUG_NODE_CONFIG +#include "fdrs_checkConfig.h" +#endif + typedef struct __attribute__((packed)) DataReading { float d; uint16_t id; @@ -103,6 +171,7 @@ DataReading theData[256]; uint8_t ln; uint8_t newData = event_clear; +#ifdef USE_ESPNOW DataReading ESPNOW1buffer[256]; uint8_t lenESPNOW1 = 0; uint32_t timeESPNOW1 = 0; @@ -112,12 +181,16 @@ uint32_t timeESPNOW2 = 0; DataReading ESPNOWGbuffer[256]; uint8_t lenESPNOWG = 0; uint32_t timeESPNOWG = 0; +#endif //USE_ESPNOW + DataReading SERIALbuffer[256]; uint8_t lenSERIAL = 0; uint32_t timeSERIAL = 0; DataReading MQTTbuffer[256]; uint8_t lenMQTT = 0; uint32_t timeMQTT = 0; + +#ifdef USE_LORA DataReading LORAGbuffer[256]; uint8_t lenLORAG = 0; uint32_t timeLORAG = 0; @@ -127,28 +200,31 @@ uint32_t timeLORA1 = 0; DataReading LORA2buffer[256]; uint8_t lenLORA2 = 0; uint32_t timeLORA2 = 0; +#endif //USE_LORA -WiFiClient espClient; #ifdef USE_LED CRGB leds[NUM_LEDS]; -#endif +#endif //USE_LED + #ifdef USE_WIFI +WiFiClient espClient; PubSubClient client(espClient); const char* ssid = FDRS_WIFI_SSID; const char* password = FDRS_WIFI_PASS; const char* mqtt_server = FDRS_MQTT_ADDR; const int mqtt_port = FDRS_MQTT_PORT; -#endif + #ifdef FDRS_MQTT_AUTH const char* mqtt_user = FDRS_MQTT_USER; const char* mqtt_pass = FDRS_MQTT_PASS; #else const char* mqtt_user = NULL; const char* mqtt_pass = NULL; -#endif - +#endif //FDRS_MQTT_AUTH +#endif //USE_WIFI +#ifdef USE_ESPNOW // Set ESP-NOW send and receive callbacks for either ESP8266 or ESP32 #if defined(ESP8266) void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) { @@ -173,6 +249,8 @@ void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) { } newData = event_espnowg; } +#endif //USE_ESPNOW + void getSerial() { String incomingString = UART_IF.readStringUntil('\n'); DynamicJsonDocument doc(24576); @@ -195,6 +273,7 @@ void getSerial() { } } + #if defined (USE_SD_LOG) || defined (USE_FS_LOG) void releaseLogBuffer() { @@ -214,6 +293,7 @@ void releaseLogBuffer() logBufferPos = 0; } #endif + void sendLog() { #if defined (USE_SD_LOG) || defined (USE_FS_LOG) @@ -230,8 +310,9 @@ void sendLog() memcpy(&logBuffer[logBufferPos], linebuf, strlen(linebuf)); //append line to buffer logBufferPos+=strlen(linebuf); } - #endif + #endif //USE_xx_LOG } + void reconnect(short int attempts, bool silent) { #ifdef USE_WIFI @@ -257,11 +338,13 @@ void reconnect(short int attempts, bool silent) { } if (!silent) DBG(" Connecting MQTT failed."); -#endif +#endif //USE_WIFI } + void reconnect(int attempts) { reconnect(attempts, false); } + void mqtt_callback(char* topic, byte * message, unsigned int length) { String incomingString; DBG(topic); @@ -288,13 +371,14 @@ void mqtt_callback(char* topic, byte * message, unsigned int length) { } } + void mqtt_publish(const char* payload) { #ifdef USE_WIFI if (!client.publish(TOPIC_DATA, payload)) { DBG(" Error on sending MQTT"); sendLog(); } -#endif +#endif //USE_WIFI } void getLoRa() { @@ -320,10 +404,11 @@ void getLoRa() { newData = event_lorag; } } -#endif +#endif //USE_LORA } void sendESPNOW(uint8_t address) { +#ifdef USE_ESPNOW DBG("Sending ESP-NOW."); uint8_t NEWPEER[] = {MAC_PREFIX, address}; #if defined(ESP32) @@ -350,6 +435,7 @@ void sendESPNOW(uint8_t address) { } esp_now_send(NEWPEER, (uint8_t *) &thePacket, j * sizeof(DataReading)); esp_now_del_peer(NEWPEER); +#endif //USE_ESPNOW } void sendSerial() { @@ -381,10 +467,11 @@ void sendMQTT() { String outgoingString; serializeJson(doc, outgoingString); mqtt_publish((char*) outgoingString.c_str()); -#endif +#endif //USE_WIFI } void bufferESPNOW(uint8_t interface) { +#ifdef USE_ESPNOW DBG("Buffering ESP-NOW."); switch (interface) { @@ -407,7 +494,9 @@ void bufferESPNOW(uint8_t interface) { lenESPNOW2 += ln; break; } +#endif USE_ESPNOW } + void bufferSerial() { DBG("Buffering Serial."); for (int i = 0; i < ln; i++) { @@ -416,6 +505,7 @@ void bufferSerial() { lenSERIAL += ln; //UART_IF.println("SENDSERIAL:" + String(lenSERIAL) + " "); } + void bufferMQTT() { DBG("Buffering MQTT."); for (int i = 0; i < ln; i++) { @@ -423,13 +513,16 @@ void bufferMQTT() { } lenMQTT += ln; } + //void bufferLoRa() { // for (int i = 0; i < ln; i++) { // LORAbuffer[lenLORA + i] = theData[i]; // } // lenLORA += ln; //} + void bufferLoRa(uint8_t interface) { +#ifdef USE_LORA DBG("Buffering LoRa."); switch (interface) { case 0: @@ -451,9 +544,11 @@ void bufferLoRa(uint8_t interface) { lenLORA2 += ln; break; } +#endif //USE_LORA } void releaseESPNOW(uint8_t interface) { +#ifdef USE_ESPNOW DBG("Releasing ESP-NOW."); switch (interface) { case 0: @@ -505,7 +600,9 @@ void releaseESPNOW(uint8_t interface) { break; } } +#endif USE_ESPNOW } + #ifdef USE_LORA void transmitLoRa(uint8_t* mac, DataReading * packet, uint8_t len) { DBG("Transmitting LoRa."); @@ -578,6 +675,7 @@ void releaseLoRa(uint8_t interface) { } #endif //USE_LORA } + void releaseSerial() { DBG("Releasing Serial."); DynamicJsonDocument doc(24576); @@ -590,6 +688,7 @@ void releaseSerial() { UART_IF.println(); lenSERIAL = 0; } + void releaseMQTT() { #ifdef USE_WIFI DBG("Releasing MQTT."); @@ -603,9 +702,11 @@ void releaseMQTT() { serializeJson(doc, outgoingString); mqtt_publish((char*) outgoingString.c_str()); lenMQTT = 0; -#endif +#endif //USE_WIFI } + void begin_espnow() { +#ifdef USE_ESPNOW DBG("Initializing ESP-NOW!"); WiFi.mode(WIFI_STA); WiFi.disconnect(); @@ -659,6 +760,7 @@ void begin_espnow() { #endif #endif //ESP8266 DBG(" ESP-NOW Initialized."); +#endif //USE_ESPNOW } void begin_lora() { @@ -673,10 +775,9 @@ void begin_lora() { while (1); } LoRa.setSpreadingFactor(FDRS_SF); - DBG(" LoRa initialized."); DBG("LoRa Band: " + String(FDRS_BAND)); DBG("LoRa SF : " + String(FDRS_SF)); -#endif //USE_LORA +#endif // USE_LORA } void begin_SD() { @@ -707,7 +808,7 @@ void begin_FS() { { DBG(" LittleFS initialized"); } -#endif +#endif // USE_FS_LOG } #endif //__FDRS_FUNCTIONS_H__ diff --git a/FDRS_Gateway/fdrs_gateway_config.h b/FDRS_Gateway/fdrs_gateway_config.h index 976d2aa..000ebf7 100644 --- a/FDRS_Gateway/fdrs_gateway_config.h +++ b/FDRS_Gateway/fdrs_gateway_config.h @@ -16,8 +16,11 @@ #define MQTT_ACT #define LORAG_ACT sendSerial(); +// protocols -- Define which protocols the gateways should handle. +// Warning: ESP-NOW and WiFi are mutual exclusive! //#define USE_LORA -//#define USE_WIFI //Used only for MQTT gateway +#define USE_ESPNOW +//#define USE_WIFI //Used only for MQTT gateway // Peer addresses #define ESPNOW1_PEER 0x0E // ESPNOW1 Address @@ -77,14 +80,14 @@ #define NUM_LEDS 4 // WiFi and MQTT Credentials -- Needed for MQTT only if "fdrs_globals.h" is not included -#define WIFI_SSID "Your SSID" -#define WIFI_PASS "Your Password" -#define MQTT_ADDR "192.168.0.8" -#define MQTT_PORT 1883 // Default MQTT port is 1883 +//#define WIFI_SSID "Your SSID" +//#define WIFI_PASS "Your Password" +//#define MQTT_ADDR "192.168.0.8" +//#define MQTT_PORT 1883 // Default MQTT port is 1883 //#define MQTT_AUTH //Enable MQTT authentication -#define MQTT_USER "Your MQTT Username" -#define MQTT_PASS "Your MQTT Password" +//#define MQTT_USER "Your MQTT Username" +//#define MQTT_PASS "Your MQTT Password" // MQTT Topics #define TOPIC_DATA "fdrs/data" diff --git a/FDRS_Sensor/FDRS_Sensor.ino b/FDRS_Sensor/FDRS_Sensor.ino index f034294..982c972 100644 --- a/FDRS_Sensor/FDRS_Sensor.ino +++ b/FDRS_Sensor/FDRS_Sensor.ino @@ -5,6 +5,7 @@ // Developed by Timm Bogner (timmbogner@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA. // An example of how to send data using "fdrs_sensor.h". // + #include "fdrs_sensor_config.h" //#include //Use global functions file diff --git a/FDRS_Sensor/fdrs_sensor.h b/FDRS_Sensor/fdrs_sensor.h index 48c2d32..9ac1024 100644 --- a/FDRS_Sensor/fdrs_sensor.h +++ b/FDRS_Sensor/fdrs_sensor.h @@ -18,7 +18,7 @@ #endif // enable to get detailed info from where single configuration macros have been taken -//#define DEBUG_NODE_CONFIG +#define DEBUG_NODE_CONFIG #ifdef USE_LORA @@ -50,6 +50,10 @@ #define MAC_PREFIX 0xAA, 0xBB, 0xCC, 0xDD, 0xEE // Should only be changed if implementing multiple FDRS systems. +#ifdef DEBUG_NODE_CONFIG +#include "fdrs_checkConfig.h" +#endif + typedef struct __attribute__((packed)) DataReading { float d; uint16_t id; @@ -67,39 +71,12 @@ DataReading fdrsData[espnow_size]; uint8_t data_count = 0; -void debugConfig() { -#ifdef USE_LORA - - DBG("----------------------------------------------------"); - DBG("SENSOR LORA CONFIG"); - DBG("----------------------------------------------------"); -#if defined(LORA_BAND) - DBG("LoRa Band used from LORA_BAND : " + String(FDRS_BAND)); -#elif defined (GLOBAL_LORA_BAND) - DBG("LoRa Band used from GLOBAL_LORA_BAND: " + String(FDRS_BAND)); -#else - DBG("NO LORA-BAND defined! Please define in fdrs_globals.h (recommended) or in fdrs_sensor_config.h"); - //exit(0); -#endif //LORA-BAND - -#if defined(LORA_SF) - DBG("LoRa SF used from LORA_SF : " + String(FDRS_SF)); -#elif defined (GLOBAL_LORA_SF) - DBG("LoRa SF used from GLOBAL_LORA_SF : " + String(FDRS_SF)); -#else -// ASSERT("NO LORA-SF defined! Please define in fdrs_globals.h (recommended) or in fdrs_sensor_config.h"); - DBG("NO LORA-SF defined! Please define in fdrs_globals.h (recommended) or in fdrs_sensor_config.h"); - //exit(0); -#endif //LORA-BAND - DBG("----------------------------------------------------"); - DBG(""); -#endif //USE_LORA -} - - void beginFDRS() { #ifdef FDRS_DEBUG Serial.begin(115200); + // find out the reset reason + esp_reset_reason_t resetReason; + resetReason = esp_reset_reason(); #endif DBG("FDRS Sensor ID " + String(READING_ID) + " initializing..."); DBG(" Gateway: " + String (GTWY_MAC, HEX)); @@ -137,7 +114,7 @@ void beginFDRS() { } #endif DBG(" ESP-NOW Initialized."); -#endif +#endif //USE_ESPNOW #ifdef USE_LORA DBG("Initializing LoRa!"); #ifdef ESP32 @@ -150,13 +127,16 @@ void beginFDRS() { } LoRa.setSpreadingFactor(FDRS_SF); DBG(" LoRa Initialized."); -#ifdef DEBUG_NODE_CONFIG - debugConfig(); -#else + DBG("LoRa Band: " + String(FDRS_BAND)); DBG("LoRa SF : " + String(FDRS_SF)); -#endif //DEBUG_NODE_CONFIG #endif // USE_LORA +#ifdef DEBUG_NODE_CONFIG + if (resetReason != ESP_RST_DEEPSLEEP) { + checkConfig(); + } +#endif //DEBUG_NODE_CONFIG + } void transmitLoRa(uint8_t* mac, DataReading * packet, uint8_t len) { diff --git a/examples/1_LoRa_Sensor/1_LoRa_Sensor.ino b/examples/1_LoRa_Sensor/1_LoRa_Sensor.ino index a8a3af5..b84f43b 100644 --- a/examples/1_LoRa_Sensor/1_LoRa_Sensor.ino +++ b/examples/1_LoRa_Sensor/1_LoRa_Sensor.ino @@ -2,7 +2,7 @@ // // Basic Sensor Example // -// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA. +// Developed by Timm Bogner (timmbogner@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA. // An example of how to send data using "fdrs_sensor.h". // diff --git a/fdrs_sensor.h b/fdrs_sensor.h index 6e191b1..9ac1024 100644 --- a/fdrs_sensor.h +++ b/fdrs_sensor.h @@ -183,7 +183,6 @@ void sleepFDRS(int sleep_time) { #ifdef ESP32 esp_sleep_enable_timer_wakeup(sleep_time * 1000000); esp_deep_sleep_start(); - #endif #ifdef ESP8266 ESP.deepSleep(sleep_time * 1000000);