Merge pull request #74 from Gulpman/dev

Clean up of #72
This commit is contained in:
Timm Bogner 2022-07-25 12:50:55 -05:00 committed by GitHub
commit 9ba1209c62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 478 additions and 226 deletions

View File

@ -45,12 +45,26 @@ void setup() {
Serial.begin(115200); Serial.begin(115200);
UART_IF.begin(115200, SERIAL_8N1, RXD2, TXD2); UART_IF.begin(115200, SERIAL_8N1, RXD2, TXD2);
#endif #endif
DBG("Address:" + String (UNIT_MAC, HEX)); 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 #ifdef USE_LED
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS); FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
leds[0] = CRGB::Blue; leds[0] = CRGB::Blue;
FastLED.show(); FastLED.show();
#endif #endif
#ifdef USE_LORA
begin_lora();
#endif
#ifdef USE_WIFI #ifdef USE_WIFI
delay(10); delay(10);
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
@ -69,9 +83,7 @@ void setup() {
#else #else
begin_espnow(); begin_espnow();
#endif #endif
#ifdef USE_LORA
begin_lora();
#endif
#ifdef USE_SD_LOG #ifdef USE_SD_LOG
begin_SD(); begin_SD();
#endif #endif
@ -153,7 +165,7 @@ void loop() {
client.loop(); // for recieving incoming messages and maintaining connection client.loop(); // for recieving incoming messages and maintaining connection
#endif #endif
if (newData) { if (newData != event_clear) {
switch (newData) { switch (newData) {
case event_espnowg: case event_espnowg:
ESPNOWG_ACT ESPNOWG_ACT

View File

@ -20,6 +20,11 @@ The UNIT_MAC is the ESP-NOW and LoRa address of the gateway. This is the address
This definition enables debug messages to be sent over the serial port. If disabled, the USB serial port is still used to echo data being sent via the sendSerial() command. This definition enables debug messages to be sent over the serial port. If disabled, the USB serial port is still used to echo data being sent via the sendSerial() command.
### ```#define RXD2 (pin)``` and ```TXD2 (pin)``` ### ```#define RXD2 (pin)``` and ```TXD2 (pin)```
These are the pins for inter-device serial communication. The single ESP8266 serial interface is not configurable, and thus these options only apply to ESP32 boards. These are the pins for inter-device serial communication. The single ESP8266 serial interface is not configurable, and thus these options only apply to ESP32 boards.
### ```#define USE_ESPNOW```
Enables ESP-NOW.
USE_ESPNOW and USE_WIFI must not be activated at the same time!
### ```#define USE_LORA``` ### ```#define USE_LORA```
Enables LoRa. Make sure that you set the LoRa module configuration parameters in the lines below. Enables LoRa. Make sure that you set the LoRa module configuration parameters in the lines below.

View File

@ -20,6 +20,7 @@ enum {
event_lora2 event_lora2
}; };
enum { enum {
cmd_clear, cmd_clear,
cmd_ping, cmd_ping,
@ -38,32 +39,99 @@ enum {
#define UART_IF Serial #define UART_IF Serial
#endif #endif
#ifdef FDRS_GLOBALS // enable to get detailed info from where single configuration macros have been taken
#define FDRS_WIFI_SSID GLOBAL_SSID #define DEBUG_NODE_CONFIG
#define FDRS_WIFI_PASS GLOBAL_PASS
#define FDRS_MQTT_ADDR GLOBAL_MQTT_ADDR #ifdef USE_WIFI
#define FDRS_MQTT_PORT GLOBAL_MQTT_PORT
#define FDRS_MQTT_USER GLOBAL_MQTT_USER // select WiFi SSID configuration
#define FDRS_MQTT_PASS GLOBAL_MQTT_PASS #if defined(WIFI_SSID)
#define FDRS_BAND GLOBAL_LORA_BAND
#define FDRS_SF GLOBAL_LORA_SF
#else
#define FDRS_WIFI_SSID 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 #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 #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 #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 #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_MQTT_PASS MQTT_PASS
#define FDRS_BAND LORA_BAND #elif defined (GLOBAL_MQTT_PASS)
#define FDRS_SF LORA_SF #define FDRS_MQTT_PASS GLOBAL_MQTT_PASS
#endif #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) #if defined (MQTT_AUTH) || defined (GLOBAL_MQTT_AUTH)
#define FDRS_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. #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 { typedef struct __attribute__((packed)) DataReading {
float d; float d;
uint16_t id; uint16_t id;
@ -117,6 +185,7 @@ uint8_t newData = event_clear;
uint8_t newCmd = cmd_clear; uint8_t newCmd = cmd_clear;
#ifdef USE_ESPNOW
DataReading ESPNOW1buffer[256]; DataReading ESPNOW1buffer[256];
uint8_t lenESPNOW1 = 0; uint8_t lenESPNOW1 = 0;
uint32_t timeESPNOW1 = 0; uint32_t timeESPNOW1 = 0;
@ -126,12 +195,16 @@ uint32_t timeESPNOW2 = 0;
DataReading ESPNOWGbuffer[256]; DataReading ESPNOWGbuffer[256];
uint8_t lenESPNOWG = 0; uint8_t lenESPNOWG = 0;
uint32_t timeESPNOWG = 0; uint32_t timeESPNOWG = 0;
#endif //USE_ESPNOW
DataReading SERIALbuffer[256]; DataReading SERIALbuffer[256];
uint8_t lenSERIAL = 0; uint8_t lenSERIAL = 0;
uint32_t timeSERIAL = 0; uint32_t timeSERIAL = 0;
DataReading MQTTbuffer[256]; DataReading MQTTbuffer[256];
uint8_t lenMQTT = 0; uint8_t lenMQTT = 0;
uint32_t timeMQTT = 0; uint32_t timeMQTT = 0;
#ifdef USE_LORA
DataReading LORAGbuffer[256]; DataReading LORAGbuffer[256];
uint8_t lenLORAG = 0; uint8_t lenLORAG = 0;
uint32_t timeLORAG = 0; uint32_t timeLORAG = 0;
@ -141,28 +214,31 @@ uint32_t timeLORA1 = 0;
DataReading LORA2buffer[256]; DataReading LORA2buffer[256];
uint8_t lenLORA2 = 0; uint8_t lenLORA2 = 0;
uint32_t timeLORA2 = 0; uint32_t timeLORA2 = 0;
#endif //USE_LORA
WiFiClient espClient;
#ifdef USE_LED #ifdef USE_LED
CRGB leds[NUM_LEDS]; CRGB leds[NUM_LEDS];
#endif #endif //USE_LED
#ifdef USE_WIFI #ifdef USE_WIFI
WiFiClient espClient;
PubSubClient client(espClient); PubSubClient client(espClient);
const char* ssid = FDRS_WIFI_SSID; const char* ssid = FDRS_WIFI_SSID;
const char* password = FDRS_WIFI_PASS; const char* password = FDRS_WIFI_PASS;
const char* mqtt_server = FDRS_MQTT_ADDR; const char* mqtt_server = FDRS_MQTT_ADDR;
const int mqtt_port = FDRS_MQTT_PORT; const int mqtt_port = FDRS_MQTT_PORT;
#endif
#ifdef FDRS_MQTT_AUTH #ifdef FDRS_MQTT_AUTH
const char* mqtt_user = FDRS_MQTT_USER; const char* mqtt_user = FDRS_MQTT_USER;
const char* mqtt_pass = FDRS_MQTT_PASS; const char* mqtt_pass = FDRS_MQTT_PASS;
#else #else
const char* mqtt_user = NULL; const char* mqtt_user = NULL;
const char* mqtt_pass = 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 // Set ESP-NOW send and receive callbacks for either ESP8266 or ESP32
#if defined(ESP8266) #if defined(ESP8266)
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) { void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
@ -193,6 +269,8 @@ void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
} }
newData = event_espnowg; newData = event_espnowg;
} }
#endif //USE_ESPNOW
void getSerial() { void getSerial() {
String incomingString = UART_IF.readStringUntil('\n'); String incomingString = UART_IF.readStringUntil('\n');
DynamicJsonDocument doc(24576); DynamicJsonDocument doc(24576);
@ -215,6 +293,7 @@ void getSerial() {
} }
} }
#if defined (USE_SD_LOG) || defined (USE_FS_LOG) #if defined (USE_SD_LOG) || defined (USE_FS_LOG)
void releaseLogBuffer() void releaseLogBuffer()
{ {
@ -234,6 +313,7 @@ void releaseLogBuffer()
logBufferPos = 0; logBufferPos = 0;
} }
#endif #endif
void sendLog() void sendLog()
{ {
#if defined (USE_SD_LOG) || defined (USE_FS_LOG) #if defined (USE_SD_LOG) || defined (USE_FS_LOG)
@ -250,8 +330,10 @@ void sendLog()
memcpy(&logBuffer[logBufferPos], linebuf, strlen(linebuf)); //append line to buffer memcpy(&logBuffer[logBufferPos], linebuf, strlen(linebuf)); //append line to buffer
logBufferPos += strlen(linebuf); logBufferPos += strlen(linebuf);
} }
#endif #endif //USE_xx_LOG
} }
void reconnect(short int attempts, bool silent) { void reconnect(short int attempts, bool silent) {
#ifdef USE_WIFI #ifdef USE_WIFI
@ -277,11 +359,13 @@ void reconnect(short int attempts, bool silent) {
} }
if (!silent) DBG(" Connecting MQTT failed."); if (!silent) DBG(" Connecting MQTT failed.");
#endif #endif //USE_WIFI
} }
void reconnect(int attempts) { void reconnect(int attempts) {
reconnect(attempts, false); reconnect(attempts, false);
} }
void mqtt_callback(char* topic, byte * message, unsigned int length) { void mqtt_callback(char* topic, byte * message, unsigned int length) {
String incomingString; String incomingString;
DBG(topic); DBG(topic);
@ -308,13 +392,14 @@ void mqtt_callback(char* topic, byte * message, unsigned int length) {
} }
} }
void mqtt_publish(const char* payload) { void mqtt_publish(const char* payload) {
#ifdef USE_WIFI #ifdef USE_WIFI
if (!client.publish(TOPIC_DATA, payload)) { if (!client.publish(TOPIC_DATA, payload)) {
DBG(" Error on sending MQTT"); DBG(" Error on sending MQTT");
sendLog(); sendLog();
} }
#endif #endif //USE_WIFI
} }
void getLoRa() { void getLoRa() {
@ -340,10 +425,11 @@ void getLoRa() {
newData = event_lorag; newData = event_lorag;
} }
} }
#endif #endif //USE_LORA
} }
void sendESPNOW(uint8_t address) { void sendESPNOW(uint8_t address) {
#ifdef USE_ESPNOW
DBG("Sending ESP-NOW."); DBG("Sending ESP-NOW.");
uint8_t temp_peer[] = {MAC_PREFIX, address}; uint8_t temp_peer[] = {MAC_PREFIX, address};
#if defined(ESP32) #if defined(ESP32)
@ -368,8 +454,11 @@ void sendESPNOW(uint8_t address) {
thePacket[j] = theData[i]; thePacket[j] = theData[i];
j++; j++;
} }
esp_now_send(temp_peer, (uint8_t *) &thePacket, j * sizeof(DataReading)); esp_now_send(temp_peer, (uint8_t *) &thePacket, j * sizeof(DataReading));
esp_now_del_peer(temp_peer); esp_now_del_peer(temp_peer);
} }
void sendSerial() { void sendSerial() {
@ -401,10 +490,11 @@ void sendMQTT() {
String outgoingString; String outgoingString;
serializeJson(doc, outgoingString); serializeJson(doc, outgoingString);
mqtt_publish((char*) outgoingString.c_str()); mqtt_publish((char*) outgoingString.c_str());
#endif #endif //USE_WIFI
} }
void bufferESPNOW(uint8_t interface) { void bufferESPNOW(uint8_t interface) {
#ifdef USE_ESPNOW
DBG("Buffering ESP-NOW."); DBG("Buffering ESP-NOW.");
switch (interface) { switch (interface) {
@ -427,7 +517,9 @@ void bufferESPNOW(uint8_t interface) {
lenESPNOW2 += ln; lenESPNOW2 += ln;
break; break;
} }
#endif USE_ESPNOW
} }
void bufferSerial() { void bufferSerial() {
DBG("Buffering Serial."); DBG("Buffering Serial.");
for (int i = 0; i < ln; i++) { for (int i = 0; i < ln; i++) {
@ -436,6 +528,7 @@ void bufferSerial() {
lenSERIAL += ln; lenSERIAL += ln;
//UART_IF.println("SENDSERIAL:" + String(lenSERIAL) + " "); //UART_IF.println("SENDSERIAL:" + String(lenSERIAL) + " ");
} }
void bufferMQTT() { void bufferMQTT() {
DBG("Buffering MQTT."); DBG("Buffering MQTT.");
for (int i = 0; i < ln; i++) { for (int i = 0; i < ln; i++) {
@ -443,13 +536,16 @@ void bufferMQTT() {
} }
lenMQTT += ln; lenMQTT += ln;
} }
//void bufferLoRa() { //void bufferLoRa() {
// for (int i = 0; i < ln; i++) { // for (int i = 0; i < ln; i++) {
// LORAbuffer[lenLORA + i] = theData[i]; // LORAbuffer[lenLORA + i] = theData[i];
// } // }
// lenLORA += ln; // lenLORA += ln;
//} //}
void bufferLoRa(uint8_t interface) { void bufferLoRa(uint8_t interface) {
#ifdef USE_LORA
DBG("Buffering LoRa."); DBG("Buffering LoRa.");
switch (interface) { switch (interface) {
case 0: case 0:
@ -471,9 +567,11 @@ void bufferLoRa(uint8_t interface) {
lenLORA2 += ln; lenLORA2 += ln;
break; break;
} }
#endif //USE_LORA
} }
void releaseESPNOW(uint8_t interface) { void releaseESPNOW(uint8_t interface) {
#ifdef USE_ESPNOW
DBG("Releasing ESP-NOW."); DBG("Releasing ESP-NOW.");
switch (interface) { switch (interface) {
case 0: case 0:
@ -525,7 +623,9 @@ void releaseESPNOW(uint8_t interface) {
break; break;
} }
} }
#endif USE_ESPNOW
} }
#ifdef USE_LORA #ifdef USE_LORA
void transmitLoRa(uint8_t* mac, DataReading * packet, uint8_t len) { void transmitLoRa(uint8_t* mac, DataReading * packet, uint8_t len) {
DBG("Transmitting LoRa."); DBG("Transmitting LoRa.");
@ -598,6 +698,7 @@ void releaseLoRa(uint8_t interface) {
} }
#endif //USE_LORA #endif //USE_LORA
} }
void releaseSerial() { void releaseSerial() {
DBG("Releasing Serial."); DBG("Releasing Serial.");
DynamicJsonDocument doc(24576); DynamicJsonDocument doc(24576);
@ -610,6 +711,7 @@ void releaseSerial() {
UART_IF.println(); UART_IF.println();
lenSERIAL = 0; lenSERIAL = 0;
} }
void releaseMQTT() { void releaseMQTT() {
#ifdef USE_WIFI #ifdef USE_WIFI
DBG("Releasing MQTT."); DBG("Releasing MQTT.");
@ -623,9 +725,11 @@ void releaseMQTT() {
serializeJson(doc, outgoingString); serializeJson(doc, outgoingString);
mqtt_publish((char*) outgoingString.c_str()); mqtt_publish((char*) outgoingString.c_str());
lenMQTT = 0; lenMQTT = 0;
#endif #endif //USE_WIFI
} }
void begin_espnow() { void begin_espnow() {
#ifdef USE_ESPNOW
DBG("Initializing ESP-NOW!"); DBG("Initializing ESP-NOW!");
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
WiFi.disconnect(); WiFi.disconnect();
@ -679,6 +783,7 @@ void begin_espnow() {
//#endif //#endif
#endif //ESP8266 #endif //ESP8266
DBG(" ESP-NOW Initialized."); DBG(" ESP-NOW Initialized.");
#endif //USE_ESPNOW
} }
void begin_lora() { void begin_lora() {
@ -693,10 +798,9 @@ void begin_lora() {
while (1); while (1);
} }
LoRa.setSpreadingFactor(FDRS_SF); LoRa.setSpreadingFactor(FDRS_SF);
DBG(" LoRa initialized.");
DBG("LoRa Band: " + String(FDRS_BAND)); DBG("LoRa Band: " + String(FDRS_BAND));
DBG("LoRa SF : " + String(FDRS_SF)); DBG("LoRa SF : " + String(FDRS_SF));
#endif //USE_LORA #endif // USE_LORA
} }
void begin_SD() { void begin_SD() {
@ -727,7 +831,7 @@ void begin_FS() {
{ {
DBG(" LittleFS initialized"); DBG(" LittleFS initialized");
} }
#endif #endif // USE_FS_LOG
} }
void handleCommands() { void handleCommands() {

View File

@ -16,8 +16,11 @@
#define MQTT_ACT #define MQTT_ACT
#define LORAG_ACT sendSerial(); #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_LORA
//#define USE_WIFI //Used only for MQTT gateway #define USE_ESPNOW
//#define USE_WIFI //Used only for MQTT gateway
// Peer addresses // Peer addresses
#define ESPNOW1_PEER 0x0E // ESPNOW1 Address #define ESPNOW1_PEER 0x0E // ESPNOW1 Address
@ -77,14 +80,14 @@
#define NUM_LEDS 4 #define NUM_LEDS 4
// WiFi and MQTT Credentials -- Needed for MQTT only if "fdrs_globals.h" is not included // WiFi and MQTT Credentials -- Needed for MQTT only if "fdrs_globals.h" is not included
#define WIFI_SSID "Your SSID" //#define WIFI_SSID "Your SSID"
#define WIFI_PASS "Your Password" //#define WIFI_PASS "Your Password"
#define MQTT_ADDR "192.168.0.8" //#define MQTT_ADDR "192.168.0.8"
#define MQTT_PORT 1883 // Default MQTT port is 1883 //#define MQTT_PORT 1883 // Default MQTT port is 1883
//#define MQTT_AUTH //Enable MQTT authentication //#define MQTT_AUTH //Enable MQTT authentication
#define MQTT_USER "Your MQTT Username" //#define MQTT_USER "Your MQTT Username"
#define MQTT_PASS "Your MQTT Password" //#define MQTT_PASS "Your MQTT Password"
// MQTT Topics // MQTT Topics
#define TOPIC_DATA "fdrs/data" #define TOPIC_DATA "fdrs/data"

View File

@ -5,6 +5,7 @@
// Developed by Timm Bogner (timmbogner@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". // An example of how to send data using "fdrs_sensor.h".
// //
#include "fdrs_sensor_config.h" #include "fdrs_sensor_config.h"
//#include <fdrs_sensor.h> //Use global functions file //#include <fdrs_sensor.h> //Use global functions file

View File

@ -18,7 +18,7 @@
#endif #endif
// enable to get detailed info from where single configuration macros have been taken // enable to get detailed info from where single configuration macros have been taken
//#define DEBUG_NODE_CONFIG #define DEBUG_NODE_CONFIG
#ifdef USE_LORA #ifdef USE_LORA
@ -50,6 +50,10 @@
#define MAC_PREFIX 0xAA, 0xBB, 0xCC, 0xDD, 0xEE // Should only be changed if implementing multiple FDRS systems. #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 { typedef struct __attribute__((packed)) DataReading {
float d; float d;
uint16_t id; uint16_t id;
@ -99,39 +103,12 @@ void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
} }
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() { void beginFDRS() {
#ifdef FDRS_DEBUG #ifdef FDRS_DEBUG
Serial.begin(115200); Serial.begin(115200);
// find out the reset reason
esp_reset_reason_t resetReason;
resetReason = esp_reset_reason();
#endif #endif
DBG("FDRS Sensor ID " + String(READING_ID) + " initializing..."); DBG("FDRS Sensor ID " + String(READING_ID) + " initializing...");
DBG(" Gateway: " + String (GTWY_MAC, HEX)); DBG(" Gateway: " + String (GTWY_MAC, HEX));
@ -174,7 +151,7 @@ void beginFDRS() {
} }
#endif #endif
DBG(" ESP-NOW Initialized."); DBG(" ESP-NOW Initialized.");
#endif #endif //USE_ESPNOW
#ifdef USE_LORA #ifdef USE_LORA
DBG("Initializing LoRa!"); DBG("Initializing LoRa!");
#ifdef ESP32 #ifdef ESP32
@ -187,13 +164,16 @@ void beginFDRS() {
} }
LoRa.setSpreadingFactor(FDRS_SF); LoRa.setSpreadingFactor(FDRS_SF);
DBG(" LoRa Initialized."); DBG(" LoRa Initialized.");
#ifdef DEBUG_NODE_CONFIG
debugConfig();
#else
DBG("LoRa Band: " + String(FDRS_BAND)); DBG("LoRa Band: " + String(FDRS_BAND));
DBG("LoRa SF : " + String(FDRS_SF)); DBG("LoRa SF : " + String(FDRS_SF));
#endif //DEBUG_NODE_CONFIG
#endif // USE_LORA #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) { void transmitLoRa(uint8_t* mac, DataReading * packet, uint8_t len) {

View File

@ -2,7 +2,7 @@
// //
// Basic Sensor Example // 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". // An example of how to send data using "fdrs_sensor.h".
// //

View File

@ -16,7 +16,10 @@
#define MQTT_ACT #define MQTT_ACT
#define LORAG_ACT #define LORAG_ACT
// protocols -- Define which protocols the gateways should handle.
// Warning: ESP-NOW and WiFi are mutual exclusive!
//#define USE_LORA //#define USE_LORA
#define USE_ESPNOW
//#define USE_WIFI //Used only for MQTT gateway //#define USE_WIFI //Used only for MQTT gateway
// Peer addresses // Peer addresses

View File

@ -44,7 +44,18 @@ void setup() {
Serial.begin(115200); Serial.begin(115200);
UART_IF.begin(115200, SERIAL_8N1, RXD2, TXD2); UART_IF.begin(115200, SERIAL_8N1, RXD2, TXD2);
#endif #endif
DBG("Address:" + String (UNIT_MAC, HEX)); 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 #ifdef USE_LED
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS); FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
leds[0] = CRGB::Blue; leds[0] = CRGB::Blue;

View File

@ -16,7 +16,10 @@
#define MQTT_ACT #define MQTT_ACT
#define LORAG_ACT sendSerial(); #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_LORA
#define USE_ESPNOW
//#define USE_WIFI //Used only for MQTT gateway //#define USE_WIFI //Used only for MQTT gateway
// Peer addresses // Peer addresses

View File

@ -16,8 +16,11 @@
#define MQTT_ACT #define MQTT_ACT
#define LORAG_ACT #define LORAG_ACT
// protocols -- Define which protocols the gateways should handle.
// Warning: ESP-NOW and WiFi are mutual exclusive!
//#define USE_LORA //#define USE_LORA
#define USE_WIFI //Used only for MQTT gateway //#define USE_ESPNOW
#define USE_WIFI //Used only for MQTT gateway
// Peer addresses // Peer addresses
#define ESPNOW1_PEER 0x0E // ESPNOW1 Address #define ESPNOW1_PEER 0x0E // ESPNOW1 Address

219
fdrs_checkConfig.h Normal file
View File

@ -0,0 +1,219 @@
#ifndef __FDRS_CHECKCONFIG_h__
#define __FDRS_CHECKCONFIG_h__
char* separatorLine2 = "----------------------------------------------------";
// helper function for a nice little header above each section
void printSmallSectionHeader(char* headerText) {
char * separatorLine = "----------------------------------------------------";
DBG(separatorLine);
DBG(headerText);
//DBG(separatorLine);
}
// helper function for a nice little header above each section
void printSectionHeader(char* headerText) {
char * separatorLine = "----------------------------------------------------";
DBG(separatorLine);
DBG(headerText);
DBG(separatorLine);
}
// helper function for a nice little header above each section
void printConfigHeader(char* headerText) {
char * headerAndFooter = "====================================================";
DBG(headerAndFooter);
DBG(headerText);
DBG(headerAndFooter);
}
// check which protocols are activated and which are deactivated
void printActivatedProtocols() {
// current candidates are: WIFI, ESPNOW, LORA, MQTT, ???
printSectionHeader("ACTIVATED PROTOCOLS");
#ifdef USE_LORA
DBG("LoRa : ENABLED");
#else
DBG("LoRa : DISABLED");
#endif
#ifdef USE_ESPNOW
DBG("ESPNow: ENABLED");
#else
DBG("ESPNow: DISABLED");
#endif
#ifdef USE_WIFI
DBG("WiFi : ENABLED");
#else
DBG("WiFi : DISABLED");
#endif
}
void printEspnowDetails() {
#ifdef USE_ESPNOW
#ifdef UNIT_MAC
printSmallSectionHeader("ESP-Now Details:");
DBG("Peer 1 address: " + String(ESPNOW1_PEER, HEX));
DBG("Peer 2 address: " + String(ESPNOW2_PEER, HEX));
#endif //UNIT_MAC
#endif //USE_ESPNOW
}
void printWifiDetails() {
#ifdef USE_WIFI
printSmallSectionHeader("WiFi Details:");
#if defined(WIFI_SSID)
DBG("WiFi SSID used from WIFI_SSID : " + String(FDRS_WIFI_SSID));
#elif defined (GLOBAL_SSID)
DBG("WiFi SSID used from GLOBAL_SSID : " + String(FDRS_WIFI_SSID));
#else
DBG("NO WiFi SSID defined! Please define in fdrs_globals.h (recommended) or in fdrs_sensor_config.h");
//exit(0);
#endif //WIFI_SSID
#if defined(WIFI_PASS)
DBG("WiFi password used from WIFI_PASS : " + String(FDRS_WIFI_PASS));
#elif defined (GLOBAL_SSID)
DBG("WiFi password used from GLOBAL_PASS : " + String(FDRS_WIFI_PASS));
#else
DBG("NO WiFi password defined! Please define in fdrs_globals.h (recommended) or in fdrs_sensor_config.h");
//exit(0);
#endif //WIFI_PASS
printSmallSectionHeader("MQTT BROKER CONFIG:");
#if defined(MQTT_ADDR)
DBG("MQTT address used from MQTT_ADDR : " + String(FDRS_MQTT_ADDR));
#elif defined (GLOBAL_MQTT_ADDR)
DBG("MQTT address used from GLOBAL_MQTT_ADDR : " + String(FDRS_MQTT_ADDR));
#else
DBG("NO MQTT address defined! Please define in fdrs_globals.h (recommended) or in fdrs_sensor_config.h");
//exit(0);
#endif //MQTT_ADDR
#if defined(MQTT_PORT)
DBG("MQTT port used from MQTT_PORT : " + String(FDRS_MQTT_PORT));
#elif defined (GLOBAL_MQTT_PORT)
DBG("MQTT port used from GLOBAL_MQTT_ADDR : " + String(FDRS_MQTT_PORT));
#else
DBG("Using default MQTT port : " + String(FDRS_MQTT_PORT));
#endif //MQTT_PORT
#ifdef FDRS_MQTT_AUTH
printSmallSectionHeader("MQTT AUTHENTIFICATION CONFIG:");
//GLOBAL_MQTT_AUTH
#if defined(MQTT_USER)
DBG("MQTT username used from MQTT_USER : " + String(FDRS_MQTT_USER));
#elif defined (GLOBAL_MQTT_USER)
DBG("MQTT username used from GLOBAL_MQTT_USER : " + String(FDRS_MQTT_USER));
#else
DBG("NO MQTT username defined! Please define in fdrs_globals.h (recommended) or in fdrs_sensor_config.h");
//exit(0);
#endif //MQTT_USER
#if defined(MQTT_PASS)
DBG("MQTT password used from MQTT_PASS : " + String(FDRS_MQTT_PASS));
#elif defined (GLOBAL_MQTT_PASS)
DBG("MQTT password used from GLOBAL_MQTT_PASS : " + String(FDRS_MQTT_PASS));
#else
DBG("NO MQTT password defined! Please define in fdrs_globals.h (recommended) or in fdrs_sensor_config.h");
//exit(0);
#endif //MQTT_PASS
#endif //FDRS_MQTT_AUTH
DBG("----------------------------------------------------");
DBG(separatorLine2);
#endif //USE_WIFI
}
void printLoraDetails() {
#ifdef USE_LORA
printSmallSectionHeader("LoRa Details:");
#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-SF
#ifdef UNIT_MAC
DBG("LoRa peers");
DBG("Peer 1 address: " + String(LORA1_PEER, HEX));
DBG("Peer 2 address: " + String(LORA2_PEER, HEX));
#endif //UNIT_MAC
#endif //USE_LORA
}
void checkConfig() {
printConfigHeader("NODE CONFIGURATION OVERVIEW");
#ifdef UNIT_MAC
DBG("Node Type : Gateway");
DBG("Gateway ID : " + String(UNIT_MAC, HEX));
#elif defined (READING_ID)
DBG("Node Type : Sensor");
DBG("Reading ID : " + String(READING_ID));
DBG("Sensor's Gateway: " + String(GTWY_MAC, HEX));
#else
DBG("Node Type : UNKNOWN!");
DBG("Please check config!");
DBG("If you have just created a new node type,");
DBG("please add it's config check to:");
DGB("fdrs_checkConfig.h");
#endif
//printConfigHeader("FULL CONFIG OVERVIEW");
printActivatedProtocols();
printSmallSectionHeader("PROTOCOL DETAILS");
#ifdef USE_LORA
printLoraDetails();
#endif
// why is USE_ESPNOW not defined for gateways? This should be implemented as not every gateway has to be an ESP-NOW gateway!
#ifdef USE_ESPNOW
printEspnowDetails();
#endif
#ifdef USE_WIFI
printWifiDetails();
#endif
DBG("----------------------------------------------------");
DBG("");
}
#endif //__FDRS_CHECKCONFIG_h__

View File

@ -91,6 +91,10 @@ enum {
// ASSERT("NO MQTT password defined! Please define in fdrs_globals.h (recommended) or in fdrs_sensor_config.h"); // ASSERT("NO MQTT password defined! Please define in fdrs_globals.h (recommended) or in fdrs_sensor_config.h");
#endif //MQTT_PASS #endif //MQTT_PASS
#if defined (MQTT_AUTH) || defined (GLOBAL_MQTT_AUTH)
#define FDRS_MQTT_AUTH
#endif //MQTT_AUTH
#endif //USE_WIFI #endif //USE_WIFI
#ifdef USE_LORA #ifdef USE_LORA
@ -115,12 +119,12 @@ enum {
#endif //USE_LORA #endif //USE_LORA
#if defined (MQTT_AUTH) || defined (GLOBAL_MQTT_AUTH)
#define FDRS_MQTT_AUTH
#endif
#define MAC_PREFIX 0xAA, 0xBB, 0xCC, 0xDD, 0xEE // Should only be changed if implementing multiple FDRS systems. #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 { typedef struct __attribute__((packed)) DataReading {
float d; float d;
uint16_t id; uint16_t id;
@ -167,6 +171,7 @@ DataReading theData[256];
uint8_t ln; uint8_t ln;
uint8_t newData = event_clear; uint8_t newData = event_clear;
#ifdef USE_ESPNOW
DataReading ESPNOW1buffer[256]; DataReading ESPNOW1buffer[256];
uint8_t lenESPNOW1 = 0; uint8_t lenESPNOW1 = 0;
uint32_t timeESPNOW1 = 0; uint32_t timeESPNOW1 = 0;
@ -176,12 +181,16 @@ uint32_t timeESPNOW2 = 0;
DataReading ESPNOWGbuffer[256]; DataReading ESPNOWGbuffer[256];
uint8_t lenESPNOWG = 0; uint8_t lenESPNOWG = 0;
uint32_t timeESPNOWG = 0; uint32_t timeESPNOWG = 0;
#endif //USE_ESPNOW
DataReading SERIALbuffer[256]; DataReading SERIALbuffer[256];
uint8_t lenSERIAL = 0; uint8_t lenSERIAL = 0;
uint32_t timeSERIAL = 0; uint32_t timeSERIAL = 0;
DataReading MQTTbuffer[256]; DataReading MQTTbuffer[256];
uint8_t lenMQTT = 0; uint8_t lenMQTT = 0;
uint32_t timeMQTT = 0; uint32_t timeMQTT = 0;
#ifdef USE_LORA
DataReading LORAGbuffer[256]; DataReading LORAGbuffer[256];
uint8_t lenLORAG = 0; uint8_t lenLORAG = 0;
uint32_t timeLORAG = 0; uint32_t timeLORAG = 0;
@ -191,123 +200,31 @@ uint32_t timeLORA1 = 0;
DataReading LORA2buffer[256]; DataReading LORA2buffer[256];
uint8_t lenLORA2 = 0; uint8_t lenLORA2 = 0;
uint32_t timeLORA2 = 0; uint32_t timeLORA2 = 0;
#endif //USE_LORA
WiFiClient espClient;
#ifdef USE_LED #ifdef USE_LED
CRGB leds[NUM_LEDS]; CRGB leds[NUM_LEDS];
#endif #endif //USE_LED
#ifdef USE_WIFI #ifdef USE_WIFI
WiFiClient espClient;
PubSubClient client(espClient); PubSubClient client(espClient);
const char* ssid = FDRS_WIFI_SSID; const char* ssid = FDRS_WIFI_SSID;
const char* password = FDRS_WIFI_PASS; const char* password = FDRS_WIFI_PASS;
const char* mqtt_server = FDRS_MQTT_ADDR; const char* mqtt_server = FDRS_MQTT_ADDR;
const int mqtt_port = FDRS_MQTT_PORT; const int mqtt_port = FDRS_MQTT_PORT;
#endif
#ifdef FDRS_MQTT_AUTH #ifdef FDRS_MQTT_AUTH
const char* mqtt_user = FDRS_MQTT_USER; const char* mqtt_user = FDRS_MQTT_USER;
const char* mqtt_pass = FDRS_MQTT_PASS; const char* mqtt_pass = FDRS_MQTT_PASS;
#else #else
const char* mqtt_user = NULL; const char* mqtt_user = NULL;
const char* mqtt_pass = NULL; const char* mqtt_pass = NULL;
#endif
void debugConfig() {
#ifdef USE_WIFI
DBG("----------------------------------------------------");
DBG("SENSOR WIFI CONFIG:");
#if defined(WIFI_SSID)
DBG("WiFi SSID used from WIFI_SSID : " + String(FDRS_WIFI_SSID));
#elif defined (GLOBAL_SSID)
DBG("WiFi SSID used from GLOBAL_SSID : " + String(FDRS_WIFI_SSID));
#else
DBG("NO WiFi SSID defined! Please define in fdrs_globals.h (recommended) or in fdrs_sensor_config.h");
//exit(0);
#endif //WIFI_SSID
#if defined(WIFI_PASS)
DBG("WiFi password used from WIFI_PASS : " + String(FDRS_WIFI_PASS));
#elif defined (GLOBAL_SSID)
DBG("WiFi password used from GLOBAL_PASS : " + String(FDRS_WIFI_PASS));
#else
DBG("NO WiFi password defined! Please define in fdrs_globals.h (recommended) or in fdrs_sensor_config.h");
//exit(0);
#endif //WIFI_PASS
#if defined(MQTT_ADDR)
DBG("MQTT address used from MQTT_ADDR : " + String(FDRS_MQTT_ADDR));
#elif defined (GLOBAL_MQTT_ADDR)
DBG("MQTT address used from GLOBAL_MQTT_ADDR : " + String(FDRS_MQTT_ADDR));
#else
DBG("NO MQTT address defined! Please define in fdrs_globals.h (recommended) or in fdrs_sensor_config.h");
//exit(0);
#endif //MQTT_ADDR
#if defined(MQTT_PORT)
DBG("MQTT port used from MQTT_PORT : " + String(FDRS_MQTT_PORT));
#elif defined (GLOBAL_MQTT_PORT)
DBG("MQTT port used from GLOBAL_MQTT_ADDR : " + String(FDRS_MQTT_PORT));
#else
DBG("Using default MQTT port : " + String(FDRS_MQTT_PORT));
#endif //MQTT_PORT
#ifdef FDRS_MQTT_AUTH
DBG("MQTT AUTHENTIFICATION CONFIG:");
//GLOBAL_MQTT_AUTH
#if defined(MQTT_USER)
DBG("MQTT username used from MQTT_USER : " + String(FDRS_MQTT_USER));
#elif defined (GLOBAL_MQTT_USER)
DBG("MQTT username used from GLOBAL_MQTT_USER : " + String(FDRS_MQTT_USER));
#else
DBG("NO MQTT username defined! Please define in fdrs_globals.h (recommended) or in fdrs_sensor_config.h");
//exit(0);
#endif //MQTT_USER
#if defined(MQTT_PASS)
DBG("MQTT password used from MQTT_PASS : " + String(FDRS_MQTT_PASS));
#elif defined (GLOBAL_MQTT_PASS)
DBG("MQTT password used from GLOBAL_MQTT_PASS : " + String(FDRS_MQTT_PASS));
#else
DBG("NO MQTT password defined! Please define in fdrs_globals.h (recommended) or in fdrs_sensor_config.h");
//exit(0);
#endif //MQTT_PASS
#endif //FDRS_MQTT_AUTH #endif //FDRS_MQTT_AUTH
DBG("----------------------------------------------------");
#endif //USE_WIFI #endif //USE_WIFI
#ifdef USE_LORA #ifdef USE_ESPNOW
DBG("----------------------------------------------------");
DBG("SENSOR LORA CONFIG:");
#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
#endif //USE_LORA
DBG("----------------------------------------------------");
DBG("");
}
// Set ESP-NOW send and receive callbacks for either ESP8266 or ESP32 // Set ESP-NOW send and receive callbacks for either ESP8266 or ESP32
#if defined(ESP8266) #if defined(ESP8266)
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) { void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
@ -332,6 +249,7 @@ void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
} }
newData = event_espnowg; newData = event_espnowg;
} }
#endif //USE_ESPNOW
void getSerial() { void getSerial() {
String incomingString = UART_IF.readStringUntil('\n'); String incomingString = UART_IF.readStringUntil('\n');
@ -355,6 +273,7 @@ void getSerial() {
} }
} }
#if defined (USE_SD_LOG) || defined (USE_FS_LOG) #if defined (USE_SD_LOG) || defined (USE_FS_LOG)
void releaseLogBuffer() void releaseLogBuffer()
{ {
@ -391,7 +310,7 @@ void sendLog()
memcpy(&logBuffer[logBufferPos], linebuf, strlen(linebuf)); //append line to buffer memcpy(&logBuffer[logBufferPos], linebuf, strlen(linebuf)); //append line to buffer
logBufferPos+=strlen(linebuf); logBufferPos+=strlen(linebuf);
} }
#endif #endif //USE_xx_LOG
} }
void reconnect(short int attempts, bool silent) { void reconnect(short int attempts, bool silent) {
@ -419,7 +338,7 @@ void reconnect(short int attempts, bool silent) {
} }
if (!silent) DBG(" Connecting MQTT failed."); if (!silent) DBG(" Connecting MQTT failed.");
#endif #endif //USE_WIFI
} }
void reconnect(int attempts) { void reconnect(int attempts) {
@ -459,7 +378,7 @@ void mqtt_publish(const char* payload) {
DBG(" Error on sending MQTT"); DBG(" Error on sending MQTT");
sendLog(); sendLog();
} }
#endif #endif //USE_WIFI
} }
void getLoRa() { void getLoRa() {
@ -485,10 +404,11 @@ void getLoRa() {
newData = event_lorag; newData = event_lorag;
} }
} }
#endif #endif //USE_LORA
} }
void sendESPNOW(uint8_t address) { void sendESPNOW(uint8_t address) {
#ifdef USE_ESPNOW
DBG("Sending ESP-NOW."); DBG("Sending ESP-NOW.");
uint8_t NEWPEER[] = {MAC_PREFIX, address}; uint8_t NEWPEER[] = {MAC_PREFIX, address};
#if defined(ESP32) #if defined(ESP32)
@ -515,6 +435,7 @@ void sendESPNOW(uint8_t address) {
} }
esp_now_send(NEWPEER, (uint8_t *) &thePacket, j * sizeof(DataReading)); esp_now_send(NEWPEER, (uint8_t *) &thePacket, j * sizeof(DataReading));
esp_now_del_peer(NEWPEER); esp_now_del_peer(NEWPEER);
#endif //USE_ESPNOW
} }
void sendSerial() { void sendSerial() {
@ -546,10 +467,11 @@ void sendMQTT() {
String outgoingString; String outgoingString;
serializeJson(doc, outgoingString); serializeJson(doc, outgoingString);
mqtt_publish((char*) outgoingString.c_str()); mqtt_publish((char*) outgoingString.c_str());
#endif #endif //USE_WIFI
} }
void bufferESPNOW(uint8_t interface) { void bufferESPNOW(uint8_t interface) {
#ifdef USE_ESPNOW
DBG("Buffering ESP-NOW."); DBG("Buffering ESP-NOW.");
switch (interface) { switch (interface) {
@ -572,7 +494,9 @@ void bufferESPNOW(uint8_t interface) {
lenESPNOW2 += ln; lenESPNOW2 += ln;
break; break;
} }
#endif USE_ESPNOW
} }
void bufferSerial() { void bufferSerial() {
DBG("Buffering Serial."); DBG("Buffering Serial.");
for (int i = 0; i < ln; i++) { for (int i = 0; i < ln; i++) {
@ -581,6 +505,7 @@ void bufferSerial() {
lenSERIAL += ln; lenSERIAL += ln;
//UART_IF.println("SENDSERIAL:" + String(lenSERIAL) + " "); //UART_IF.println("SENDSERIAL:" + String(lenSERIAL) + " ");
} }
void bufferMQTT() { void bufferMQTT() {
DBG("Buffering MQTT."); DBG("Buffering MQTT.");
for (int i = 0; i < ln; i++) { for (int i = 0; i < ln; i++) {
@ -588,6 +513,7 @@ void bufferMQTT() {
} }
lenMQTT += ln; lenMQTT += ln;
} }
//void bufferLoRa() { //void bufferLoRa() {
// for (int i = 0; i < ln; i++) { // for (int i = 0; i < ln; i++) {
// LORAbuffer[lenLORA + i] = theData[i]; // LORAbuffer[lenLORA + i] = theData[i];
@ -596,6 +522,7 @@ void bufferMQTT() {
//} //}
void bufferLoRa(uint8_t interface) { void bufferLoRa(uint8_t interface) {
#ifdef USE_LORA
DBG("Buffering LoRa."); DBG("Buffering LoRa.");
switch (interface) { switch (interface) {
case 0: case 0:
@ -617,9 +544,11 @@ void bufferLoRa(uint8_t interface) {
lenLORA2 += ln; lenLORA2 += ln;
break; break;
} }
#endif //USE_LORA
} }
void releaseESPNOW(uint8_t interface) { void releaseESPNOW(uint8_t interface) {
#ifdef USE_ESPNOW
DBG("Releasing ESP-NOW."); DBG("Releasing ESP-NOW.");
switch (interface) { switch (interface) {
case 0: case 0:
@ -671,6 +600,7 @@ void releaseESPNOW(uint8_t interface) {
break; break;
} }
} }
#endif USE_ESPNOW
} }
#ifdef USE_LORA #ifdef USE_LORA
@ -772,10 +702,11 @@ void releaseMQTT() {
serializeJson(doc, outgoingString); serializeJson(doc, outgoingString);
mqtt_publish((char*) outgoingString.c_str()); mqtt_publish((char*) outgoingString.c_str());
lenMQTT = 0; lenMQTT = 0;
#endif #endif //USE_WIFI
} }
void begin_espnow() { void begin_espnow() {
#ifdef USE_ESPNOW
DBG("Initializing ESP-NOW!"); DBG("Initializing ESP-NOW!");
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
WiFi.disconnect(); WiFi.disconnect();
@ -829,6 +760,7 @@ void begin_espnow() {
#endif #endif
#endif //ESP8266 #endif //ESP8266
DBG(" ESP-NOW Initialized."); DBG(" ESP-NOW Initialized.");
#endif //USE_ESPNOW
} }
void begin_lora() { void begin_lora() {
@ -843,12 +775,8 @@ void begin_lora() {
while (1); while (1);
} }
LoRa.setSpreadingFactor(FDRS_SF); LoRa.setSpreadingFactor(FDRS_SF);
#ifdef DEBUG_NODE_CONFIG
debugConfig();
#else
DBG("LoRa Band: " + String(FDRS_BAND)); DBG("LoRa Band: " + String(FDRS_BAND));
DBG("LoRa SF : " + String(FDRS_SF)); DBG("LoRa SF : " + String(FDRS_SF));
#endif //DEBUG_NODE_CONFIG
#endif // USE_LORA #endif // USE_LORA
} }
@ -880,7 +808,7 @@ void begin_FS() {
{ {
DBG(" LittleFS initialized"); DBG(" LittleFS initialized");
} }
#endif #endif // USE_FS_LOG
} }
#endif //__FDRS_FUNCTIONS_H__ #endif //__FDRS_FUNCTIONS_H__

View File

@ -18,7 +18,7 @@
#endif #endif
// enable to get detailed info from where single configuration macros have been taken // enable to get detailed info from where single configuration macros have been taken
//#define DEBUG_NODE_CONFIG #define DEBUG_NODE_CONFIG
#ifdef USE_LORA #ifdef USE_LORA
@ -50,6 +50,10 @@
#define MAC_PREFIX 0xAA, 0xBB, 0xCC, 0xDD, 0xEE // Should only be changed if implementing multiple FDRS systems. #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 { typedef struct __attribute__((packed)) DataReading {
float d; float d;
uint16_t id; uint16_t id;
@ -67,39 +71,12 @@ DataReading fdrsData[espnow_size];
uint8_t data_count = 0; 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() { void beginFDRS() {
#ifdef FDRS_DEBUG #ifdef FDRS_DEBUG
Serial.begin(115200); Serial.begin(115200);
// find out the reset reason
esp_reset_reason_t resetReason;
resetReason = esp_reset_reason();
#endif #endif
DBG("FDRS Sensor ID " + String(READING_ID) + " initializing..."); DBG("FDRS Sensor ID " + String(READING_ID) + " initializing...");
DBG(" Gateway: " + String (GTWY_MAC, HEX)); DBG(" Gateway: " + String (GTWY_MAC, HEX));
@ -137,7 +114,7 @@ void beginFDRS() {
} }
#endif #endif
DBG(" ESP-NOW Initialized."); DBG(" ESP-NOW Initialized.");
#endif #endif //USE_ESPNOW
#ifdef USE_LORA #ifdef USE_LORA
DBG("Initializing LoRa!"); DBG("Initializing LoRa!");
#ifdef ESP32 #ifdef ESP32
@ -150,13 +127,16 @@ void beginFDRS() {
} }
LoRa.setSpreadingFactor(FDRS_SF); LoRa.setSpreadingFactor(FDRS_SF);
DBG(" LoRa Initialized."); DBG(" LoRa Initialized.");
#ifdef DEBUG_NODE_CONFIG
debugConfig();
#else
DBG("LoRa Band: " + String(FDRS_BAND)); DBG("LoRa Band: " + String(FDRS_BAND));
DBG("LoRa SF : " + String(FDRS_SF)); DBG("LoRa SF : " + String(FDRS_SF));
#endif //DEBUG_NODE_CONFIG
#endif // USE_LORA #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) { void transmitLoRa(uint8_t* mac, DataReading * packet, uint8_t len) {