From e10315bc28ec23dca4dae765d30123c68034138a Mon Sep 17 00:00:00 2001 From: Timm Bogner <64260873+timmbogner@users.noreply.github.com> Date: Sat, 9 Jul 2022 20:05:34 -0500 Subject: [PATCH 1/3] used enum to make newData more expressive --- FDRS_Gateway/FDRS_Gateway.ino | 24 +++---- FDRS_Gateway/fdrs_functions.h | 119 ++++++++++++++++++---------------- 2 files changed, 76 insertions(+), 67 deletions(-) diff --git a/FDRS_Gateway/FDRS_Gateway.ino b/FDRS_Gateway/FDRS_Gateway.ino index 690236f..fb9226b 100644 --- a/FDRS_Gateway/FDRS_Gateway.ino +++ b/FDRS_Gateway/FDRS_Gateway.ino @@ -154,28 +154,28 @@ if(current_millis-last_millis >= 1000){ #endif if (newData) { switch (newData) { - case 1: //ESP-NOW #1 - ESPNOW1_ACT - break; - case 2: //ESP-NOW #2 - ESPNOW2_ACT - break; - case 3: //ESP-NOW General + case event_espnowg: //ESP-NOW General ESPNOWG_ACT break; - case 4: //Serial + case event_espnow1: //ESP-NOW #1 + ESPNOW1_ACT + break; + case event_espnow2: //ESP-NOW #2 + ESPNOW2_ACT + break; + case event_serial: //Serial SERIAL_ACT break; - case 5: //MQTT + case event_mqtt: //MQTT MQTT_ACT break; - case 6: //LoRa General + case event_lorag: //LoRa General LORAG_ACT break; - case 7: //LoRa #1 + case event_lora1: //LoRa #1 LORA1_ACT break; - case 8: //LoRa #2 + case event_lora2: //LoRa #2 LORA2_ACT break; } diff --git a/FDRS_Gateway/fdrs_functions.h b/FDRS_Gateway/fdrs_functions.h index 283965e..ab71e22 100644 --- a/FDRS_Gateway/fdrs_functions.h +++ b/FDRS_Gateway/fdrs_functions.h @@ -1,9 +1,19 @@ // FARM DATA RELAY SYSTEM // // GATEWAY 2.000 Functions -// This is the 'meat and potatoes' of FDRS, and should not be fooled with unless improving/adding features. -// Developed by Timm Bogner (timmbogner@gmail.com) - +// This is the 'meat and potatoes' of FDRS, and should not be fooled with unless improving/adding features. +// Developed by Timm Bogner (timmbogner@gmail.com) +enum { + event_clear, + event_espnowg, + event_espnow1, + event_espnow2, + event_serial, + event_mqtt, + event_lorag, + event_lora1, + event_lora2 +}; #ifdef FDRS_DEBUG #define DBG(a) (Serial.println(a)) #else @@ -85,7 +95,7 @@ unsigned long seconds_since_reset = 0; DataReading theData[256]; uint8_t ln; -uint8_t newData = 0; +uint8_t newData = event_clear; DataReading ESPNOW1buffer[256]; uint8_t lenESPNOW1 = 0; @@ -149,9 +159,9 @@ void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) { memcpy(&incMAC, mac, sizeof(incMAC)); DBG("Incoming ESP-NOW."); ln = len / sizeof(DataReading); - if (memcmp(&incMAC, &ESPNOW1, 6) == 0) newData = 1; - else if (memcmp(&incMAC, &ESPNOW2, 6) == 0) newData = 2; - else newData = 3; + if (memcmp(&incMAC, &ESPNOW1, 6) == 0) newData = event_espnow1; + else if (memcmp(&incMAC, &ESPNOW2, 6) == 0) newData = event_espnow2; + else newData = event_espnowg; } void getSerial() { String incomingString = UART_IF.readStringUntil('\n'); @@ -170,71 +180,71 @@ void getSerial() { theData[i].d = doc[i]["data"]; } ln = s; - newData = 4; + newData = event_serial; DBG("Incoming Serial."); } } void sendSD(const char filename[32]) { - #ifdef USE_SD_LOG +#ifdef USE_SD_LOG DBG("Logging to SD card."); File logfile = SD.open(filename, FILE_WRITE); for (int i = 0; i < ln; i++) { char linebuf[32]; - #ifdef USE_WIFI - sprintf(linebuf, "%ld,%d,%d,%g",timeClient.getEpochTime(),theData[i].id,theData[i].t,theData[i].d); - #else - sprintf(linebuf, "%ld,%d,%d,%g",seconds_since_reset,theData[i].id,theData[i].t,theData[i].d); - #endif +#ifdef USE_WIFI + sprintf(linebuf, "%ld,%d,%d,%g", timeClient.getEpochTime(), theData[i].id, theData[i].t, theData[i].d); +#else + sprintf(linebuf, "%ld,%d,%d,%g", seconds_since_reset, theData[i].id, theData[i].t, theData[i].d); +#endif logfile.println(linebuf); } logfile.close(); - #endif +#endif } void sendFS(const char filename[32]) { - #ifdef USE_FS_LOG +#ifdef USE_FS_LOG DBG("Logging to internal flash."); File logfile = LittleFS.open(filename, "a"); for (int i = 0; i < ln; i++) { char linebuf[32]; - #ifdef USE_WIFI - sprintf(linebuf, "%ld,%d,%d,%g",timeClient.getEpochTime(),theData[i].id,theData[i].t,theData[i].d); - #else - sprintf(linebuf, "%ld,%d,%d,%g",seconds_since_reset,theData[i].id,theData[i].t,theData[i].d); - #endif +#ifdef USE_WIFI + sprintf(linebuf, "%ld,%d,%d,%g", timeClient.getEpochTime(), theData[i].id, theData[i].t, theData[i].d); +#else + sprintf(linebuf, "%ld,%d,%d,%g", seconds_since_reset, theData[i].id, theData[i].t, theData[i].d); +#endif logfile.println(linebuf); } logfile.close(); - #endif +#endif } void reconnect(short int attempts, bool silent) { #ifdef USE_WIFI - if(!silent) DBG("Connecting MQTT..."); - - for (short int i = 1; i<=attempts; i++) { + if (!silent) DBG("Connecting MQTT..."); + + for (short int i = 1; i <= attempts; i++) { // Attempt to connect if (client.connect("FDRS_GATEWAY", mqtt_user, mqtt_pass)) { // Subscribe client.subscribe(TOPIC_COMMAND); - if(!silent) DBG(" MQTT Connected"); + if (!silent) DBG(" MQTT Connected"); return; } else { - if(!silent) { + if (!silent) { char msg[23]; - sprintf(msg, " Attempt %d/%d",i,attempts); + sprintf(msg, " Attempt %d/%d", i, attempts); DBG(msg); } - if((attempts=!1)){ + if ((attempts = !1)) { delay(3000); } } } - if(!silent) DBG(" Connecting MQTT failed."); + if (!silent) DBG(" Connecting MQTT failed."); #endif } -void reconnect(int attempts){ +void reconnect(int attempts) { reconnect(attempts, false); } void mqtt_callback(char* topic, byte * message, unsigned int length) { @@ -258,19 +268,19 @@ void mqtt_callback(char* topic, byte * message, unsigned int length) { theData[i].d = doc[i]["data"]; } ln = s; - newData = 5; + newData = event_mqtt; DBG("Incoming MQTT."); } } -void mqtt_publish(const char* payload){ - #ifdef USE_WIFI - if(!client.publish(TOPIC_DATA, payload)){ +void mqtt_publish(const char* payload) { +#ifdef USE_WIFI + if (!client.publish(TOPIC_DATA, payload)) { DBG(" Error on sending MQTT"); sendSD(SD_FILENAME); sendFS(FS_FILENAME); } - #endif +#endif } void getLoRa() { @@ -286,11 +296,10 @@ void getLoRa() { if (memcmp(&packet, &selfAddress[3], 3) == 0) { //Check if addressed to this device memcpy(&incLORAMAC, &packet[3], 2); //Split off address portion of packet memcpy(&theData, &packet[5], packetSize - 5); //Split off data portion of packet - if (memcmp(&incLORAMAC, &LoRa1, 2) == 0) newData = 7; //Check if it is from a registered sender - else if (memcmp(&incLORAMAC, &LoRa2, 2) == 0) newData = 8; - else newData = 6; + if (memcmp(&incLORAMAC, &LoRa1, 2) == 0) newData = event_lora1; //Check if it is from a registered sender + else if (memcmp(&incLORAMAC, &LoRa2, 2) == 0) newData = event_lora2; + else newData = event_lorag; ln = (packetSize - 5) / sizeof(DataReading); - newData = 6; DBG("Incoming LoRa."); } @@ -635,8 +644,8 @@ void begin_espnow() { #endif DBG(" ESP-NOW Initialized."); } -void begin_lora(){ - #ifdef USE_LORA +void begin_lora() { +#ifdef USE_LORA DBG("Initializing LoRa!"); #ifdef ESP32 SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI); @@ -648,34 +657,34 @@ void begin_lora(){ } LoRa.setSpreadingFactor(FDRS_SF); DBG(" LoRa initialized."); - #endif +#endif } -void begin_SD(){ - #ifdef USE_SD_LOG +void begin_SD() { +#ifdef USE_SD_LOG DBG("Initializing SD card..."); - #ifdef ESP32 +#ifdef ESP32 SPI.begin(SCK, MISO, MOSI); - #endif +#endif if (!SD.begin(SD_SS)) { DBG(" Initialization failed!"); while (1); - }else{ + } else { DBG(" SD initialized."); } - #endif +#endif } -void begin_FS(){ - #ifdef USE_FS_LOG +void begin_FS() { +#ifdef USE_FS_LOG DBG("Initializing LittleFS..."); - if(!LittleFS.begin()) + if (!LittleFS.begin()) { - Serial.println(" initialization failed"); + DBG(" initialization failed"); while (1); } else { - Serial.println(" LittleFS initialized"); + DBG(" LittleFS initialized"); } - #endif +#endif } From 59196b78294ebd34defb0c30cfcac4ef0e256d3d Mon Sep 17 00:00:00 2001 From: Timm Bogner <64260873+timmbogner@users.noreply.github.com> Date: Sat, 9 Jul 2022 20:22:33 -0500 Subject: [PATCH 2/3] fixed if/else --- FDRS_Gateway/FDRS_Gateway.ino | 16 +++++++-------- FDRS_Gateway/fdrs_functions.h | 38 +++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/FDRS_Gateway/FDRS_Gateway.ino b/FDRS_Gateway/FDRS_Gateway.ino index fb9226b..e398de0 100644 --- a/FDRS_Gateway/FDRS_Gateway.ino +++ b/FDRS_Gateway/FDRS_Gateway.ino @@ -154,28 +154,28 @@ if(current_millis-last_millis >= 1000){ #endif if (newData) { switch (newData) { - case event_espnowg: //ESP-NOW General + case event_espnowg: ESPNOWG_ACT break; - case event_espnow1: //ESP-NOW #1 + case event_espnow1: ESPNOW1_ACT break; - case event_espnow2: //ESP-NOW #2 + case event_espnow2: ESPNOW2_ACT break; - case event_serial: //Serial + case event_serial: SERIAL_ACT break; - case event_mqtt: //MQTT + case event_mqtt: MQTT_ACT break; - case event_lorag: //LoRa General + case event_lorag: LORAG_ACT break; - case event_lora1: //LoRa #1 + case event_lora1: LORA1_ACT break; - case event_lora2: //LoRa #2 + case event_lora2: LORA2_ACT break; } diff --git a/FDRS_Gateway/fdrs_functions.h b/FDRS_Gateway/fdrs_functions.h index ab71e22..f541475 100644 --- a/FDRS_Gateway/fdrs_functions.h +++ b/FDRS_Gateway/fdrs_functions.h @@ -159,9 +159,15 @@ void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) { memcpy(&incMAC, mac, sizeof(incMAC)); DBG("Incoming ESP-NOW."); ln = len / sizeof(DataReading); - if (memcmp(&incMAC, &ESPNOW1, 6) == 0) newData = event_espnow1; - else if (memcmp(&incMAC, &ESPNOW2, 6) == 0) newData = event_espnow2; - else newData = event_espnowg; + if (memcmp(&incMAC, &ESPNOW1, 6) == 0) { + newData = event_espnow1; + return; + } + if (memcmp(&incMAC, &ESPNOW2, 6) == 0) { + newData = event_espnow2; + return; + } + newData = event_espnowg; } void getSerial() { String incomingString = UART_IF.readStringUntil('\n'); @@ -290,18 +296,20 @@ void getLoRa() { uint8_t packet[packetSize]; uint8_t incLORAMAC[2]; LoRa.readBytes((uint8_t *)&packet, packetSize); - // for (int i = 0; i < packetSize; i++) { - // UART_IF.println(packet[i], HEX); - // } - if (memcmp(&packet, &selfAddress[3], 3) == 0) { //Check if addressed to this device - memcpy(&incLORAMAC, &packet[3], 2); //Split off address portion of packet - memcpy(&theData, &packet[5], packetSize - 5); //Split off data portion of packet - if (memcmp(&incLORAMAC, &LoRa1, 2) == 0) newData = event_lora1; //Check if it is from a registered sender - else if (memcmp(&incLORAMAC, &LoRa2, 2) == 0) newData = event_lora2; - else newData = event_lorag; - ln = (packetSize - 5) / sizeof(DataReading); - DBG("Incoming LoRa."); - + ln = (packetSize - 5) / sizeof(DataReading); + DBG("Incoming LoRa."); + if (memcmp(&packet, &selfAddress[3], 3) == 0) { //Check if addressed to this device + memcpy(&incLORAMAC, &packet[3], 2); //Split off address portion of packet + memcpy(&theData, &packet[5], packetSize - 5); //Split off data portion of packet + if (memcmp(&incLORAMAC, &LoRa1, 2) == 0) { //Check if it is from a registered sender + newData = event_lora1; + return; + } + if (memcmp(&incLORAMAC, &LoRa2, 2) == 0) { + newData = event_lora2; + return; + } + newData = event_lorag; } } #endif From 3979c09dd73641a7b91fbfc6c238d17cc8694083 Mon Sep 17 00:00:00 2001 From: Timm Bogner <64260873+timmbogner@users.noreply.github.com> Date: Sun, 10 Jul 2022 20:38:43 -0500 Subject: [PATCH 3/3] changed reading_id to uint16_t --- examples/Universal_Sensor_beta/fdrs_sensor.cpp | 6 +++--- examples/Universal_Sensor_beta/fdrs_sensor.h | 18 +++++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/examples/Universal_Sensor_beta/fdrs_sensor.cpp b/examples/Universal_Sensor_beta/fdrs_sensor.cpp index 89ccc8b..3d19843 100644 --- a/examples/Universal_Sensor_beta/fdrs_sensor.cpp +++ b/examples/Universal_Sensor_beta/fdrs_sensor.cpp @@ -8,7 +8,7 @@ uint8_t LoRaAddress[] = {0x42, 0x00}; uint32_t wait_time = 0; -FDRSBase::FDRSBase(uint8_t gtwy_mac,uint8_t reading_id): +FDRSBase::FDRSBase(uint8_t gtwy_mac,uint16_t reading_id): _gtwy_mac(gtwy_mac), _espnow_size(250 / sizeof(DataReading_t)), _reading_id(reading_id), @@ -77,7 +77,7 @@ void FDRSBase::sleep(int seconds){ } -FDRS_EspNow::FDRS_EspNow(uint8_t gtwy_mac,uint8_t reading_id): +FDRS_EspNow::FDRS_EspNow(uint8_t gtwy_mac,uint16_t reading_id): FDRSBase(gtwy_mac,reading_id) { @@ -129,7 +129,7 @@ void FDRS_EspNow::transmit(DataReading_t *fdrsData, uint8_t _data_count){ #ifdef USE_LORA FDRSLoRa::FDRSLoRa(uint8_t gtwy_mac, - uint8_t reading_id,uint8_t miso,uint8_t mosi,uint8_t sck, + uint16_t reading_id,uint8_t miso,uint8_t mosi,uint8_t sck, uint8_t ss,uint8_t rst,uint8_t dio0,uint32_t band,uint8_t sf): FDRSBase(gtwy_mac,reading_id), _miso(miso), diff --git a/examples/Universal_Sensor_beta/fdrs_sensor.h b/examples/Universal_Sensor_beta/fdrs_sensor.h index 6c7eba8..ff29a6a 100644 --- a/examples/Universal_Sensor_beta/fdrs_sensor.h +++ b/examples/Universal_Sensor_beta/fdrs_sensor.h @@ -63,10 +63,10 @@ public: * @brief Construct a new FDRSBase object * * @param gtwy_mac The network MAC - * @param reading_id The network identifier for the sensor + * @param reading_id The identifier for the sensor */ - FDRSBase(uint8_t gtwy_mac,uint8_t reading_id); + FDRSBase(uint8_t gtwy_mac,uint16_t reading_id); ~FDRSBase(); /** @@ -101,7 +101,7 @@ private: uint8_t _gtwy_mac; const uint16_t _espnow_size; - uint8_t _reading_id; + uint16_t _reading_id; uint8_t _data_count; DataReading_t *fdrsData; @@ -111,7 +111,7 @@ private: virtual void init(void) = 0; /** - * @brief Required impalamnetation of how a sensor will send its data out on the network. + * @brief Required implementation of how a sensor will send its data out on the network. * * @param fdrsData Pointer to the data that the sensor will be seding. * @param _data_count The number of data packets the sensor will be sending. @@ -127,10 +127,10 @@ public: * @brief Construct a new fdrs espnow object * * @param gtwy_mac The network MAC. - * @param reading_id The network identifier for the sensor. + * @param reading_id The identifier for the sensor. */ - FDRS_EspNow(uint8_t gtwy_mac, uint8_t reading_id); + FDRS_EspNow(uint8_t gtwy_mac, uint16_t reading_id); private: @@ -159,7 +159,7 @@ public: * @brief Construct a new FDRSLoRa object * * @param gtwy_mac The network MAC. - * @param reading_id The network identifier for the sensor. + * @param reading_id The identifier for the sensor. * @param miso Master in slave out pin. * @param mosi Master out slave in pin. * @param sck Master clock pin. @@ -170,7 +170,7 @@ public: * @param sf LoRa spread factor */ - FDRSLoRa(uint8_t gtwy_mac, uint8_t reading_id,uint8_t miso,uint8_t mosi,uint8_t sck, uint8_t ss,uint8_t rst,uint8_t dio0,uint32_t band,uint8_t sf); + FDRSLoRa(uint8_t gtwy_mac, uint16_t reading_id,uint8_t miso,uint8_t mosi,uint8_t sck, uint8_t ss,uint8_t rst,uint8_t dio0,uint32_t band,uint8_t sf); private: @@ -208,4 +208,4 @@ private: }; -#endif \ No newline at end of file +#endif