|
|
|
@ -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__
|
|
|
|
|