added #define USE_ESPNOW to gateway code

Including sections which are ESPNOW specific only if #define USE_ESPNOW is enabled.
This commit is contained in:
Sascha 2022-07-23 01:05:01 +02:00
parent 5e85269373
commit ae85886e65
4 changed files with 46 additions and 13 deletions

View File

@ -16,7 +16,10 @@
#define MQTT_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_ESPNOW
//#define USE_WIFI //Used only for MQTT gateway
// Peer addresses

View File

@ -16,8 +16,10 @@
#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_ESPNOW // Missing! Gateways do not have to be ESPNOW activated. It also is making config much easier. Used it there.
#define USE_ESPNOW
//#define USE_WIFI //Used only for MQTT gateway
// Peer addresses

View File

@ -16,8 +16,11 @@
#define MQTT_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_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

View File

@ -171,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;
@ -180,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;
@ -195,26 +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) {
@ -239,6 +249,7 @@ 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');
@ -262,6 +273,7 @@ void getSerial() {
}
}
#if defined (USE_SD_LOG) || defined (USE_FS_LOG)
void releaseLogBuffer()
{
@ -298,7 +310,7 @@ 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) {
@ -326,7 +338,7 @@ void reconnect(short int attempts, bool silent) {
}
if (!silent) DBG(" Connecting MQTT failed.");
#endif
#endif //USE_WIFI
}
void reconnect(int attempts) {
@ -366,7 +378,7 @@ void mqtt_publish(const char* payload) {
DBG(" Error on sending MQTT");
sendLog();
}
#endif
#endif //USE_WIFI
}
void getLoRa() {
@ -392,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)
@ -422,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() {
@ -453,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) {
@ -479,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++) {
@ -488,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++) {
@ -495,6 +513,7 @@ void bufferMQTT() {
}
lenMQTT += ln;
}
//void bufferLoRa() {
// for (int i = 0; i < ln; i++) {
// LORAbuffer[lenLORA + i] = theData[i];
@ -503,6 +522,7 @@ void bufferMQTT() {
//}
void bufferLoRa(uint8_t interface) {
#ifdef USE_LORA
DBG("Buffering LoRa.");
switch (interface) {
case 0:
@ -524,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:
@ -578,6 +600,7 @@ void releaseESPNOW(uint8_t interface) {
break;
}
}
#endif USE_ESPNOW
}
#ifdef USE_LORA
@ -679,10 +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();
@ -736,6 +760,7 @@ void begin_espnow() {
#endif
#endif //ESP8266
DBG(" ESP-NOW Initialized.");
#endif //USE_ESPNOW
}
void begin_lora() {
@ -783,7 +808,7 @@ void begin_FS() {
{
DBG(" LittleFS initialized");
}
#endif
#endif // USE_FS_LOG
}
#endif //__FDRS_FUNCTIONS_H__