From fc117c1f4241ab360a72b15ed413a91587674bc4 Mon Sep 17 00:00:00 2001 From: sensorsiot Date: Thu, 26 May 2022 10:56:51 +0200 Subject: [PATCH] On the way back to a working example --- Examples/1_LoRa_Sensor/fdrs_sensor.h | 52 +++------------ Examples/2_ESPNOW_Sensor/2_ESPNOW_Sensor.ino | 2 +- Examples/2_ESPNOW_Sensor/fdrs_sensor.h | 6 ++ .../3_ESPNOW_Gateway/3_ESPNOW_Gateway.ino | 3 +- Examples/3_ESPNOW_Gateway/fdrs_config.h | 10 +++ Examples/3_ESPNOW_Gateway/fdrs_functions.h | 14 ++++ Examples/4_UART_Gateway/4_UART_Gateway.ino | 5 +- Examples/4_UART_Gateway/fdrs_config.h | 64 +++++++++++++------ Examples/4_UART_Gateway/fdrs_functions.h | 3 +- 9 files changed, 90 insertions(+), 69 deletions(-) diff --git a/Examples/1_LoRa_Sensor/fdrs_sensor.h b/Examples/1_LoRa_Sensor/fdrs_sensor.h index 9aaaa08..146d0b0 100644 --- a/Examples/1_LoRa_Sensor/fdrs_sensor.h +++ b/Examples/1_LoRa_Sensor/fdrs_sensor.h @@ -4,7 +4,7 @@ // // Developed by Timm Bogner (timmbogner@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA. // -#include "sensor_config.h" +#include "sensor_setup.h" #if defined(ESP8266) #include #include @@ -13,47 +13,11 @@ #include #include #endif + #ifdef USE_LORA #include #endif -#ifdef GLOBALS -#define FDRS_BAND GLOBAL_BAND -#define FDRS_SF GLOBAL_SF -#else -#define FDRS_BAND BAND -#define FDRS_SF SF -#endif - -#ifdef DEBUG -#define DBG(a) (Serial.println(a)) -#else -#define DBG(a) -#endif - -#define STATUS_T 0 // Status -#define TEMP_T 1 // Temperature -#define TEMP2_T 2 // Temperature #2 -#define HUMIDITY_T 3 // Relative Humidity -#define PRESSURE_T 4 // Atmospheric Pressure -#define LIGHT_T 5 // Light (lux) -#define SOIL_T 6 // Soil Moisture -#define SOIL2_T 7 // Soil Moisture #2 -#define SOILR_T 8 // Soil Resistance -#define SOILR2_T 9 // Soil Resistance #2 -#define OXYGEN_T 10 // Oxygen -#define CO2_T 11 // Carbon Dioxide -#define WINDSPD_T 12 // Wind Speed -#define WINDHDG_T 13 // Wind Direction -#define RAINFALL_T 14 // Rainfall -#define MOTION_T 15 // Motion -#define VOLTAGE_T 16 // Voltage -#define VOLTAGE2_T 17 // Voltage #2 -#define CURRENT_T 18 // Current -#define CURRENT2_T 19 // Current #2 -#define IT_T 20 // Iterations -#define MAC_PREFIX 0xAA, 0xBB, 0xCC, 0xDD, 0xEE // Should only be changed if implementing multiple FDRS systems. - typedef struct __attribute__((packed)) DataReading { float d; uint16_t id; @@ -64,7 +28,7 @@ typedef struct __attribute__((packed)) DataReading { const uint16_t espnow_size = 250 / sizeof(DataReading); uint8_t gatewayAddress[] = {MAC_PREFIX, GTWY_MAC}; uint8_t gtwyAddress[] = {gatewayAddress[3], gatewayAddress[4], GTWY_MAC}; -uint8_t LoRaAddress[] = {0x42, 0x00}; + uint32_t wait_time = 0; DataReading fdrsData[espnow_size]; @@ -113,23 +77,23 @@ void beginFDRS() { #endif #ifdef USE_LORA DBG("Initializing LoRa!"); - DBG(BAND); - DBG(SF); + DBG(GLOBAL_BAND); + DBG(SF); #ifndef __AVR__ SPI.begin(SCK, MISO, MOSI, SS); #endif LoRa.setPins(SS, RST, DIO0); - if (!LoRa.begin(FDRS_BAND)) { + if (!LoRa.begin(BAND)) { while (1); } - LoRa.setSpreadingFactor(FDRS_SF); + LoRa.setSpreadingFactor(SF); DBG(" LoRa Initialized."); #endif } void transmitLoRa(uint8_t* mac, DataReading * packet, uint8_t len) { #ifdef USE_LORA uint8_t pkt[5 + (len * sizeof(DataReading))]; - memcpy(&pkt, mac, 3); // + memcpy(&pkt, mac, 3); // memcpy(&pkt[3], &LoRaAddress, 2); memcpy(&pkt[5], packet, len * sizeof(DataReading)); LoRa.beginPacket(); diff --git a/Examples/2_ESPNOW_Sensor/2_ESPNOW_Sensor.ino b/Examples/2_ESPNOW_Sensor/2_ESPNOW_Sensor.ino index 02b275d..607373c 100644 --- a/Examples/2_ESPNOW_Sensor/2_ESPNOW_Sensor.ino +++ b/Examples/2_ESPNOW_Sensor/2_ESPNOW_Sensor.ino @@ -9,7 +9,7 @@ #define DEBUG #define CREDENTIALS -#include +#include #include "fdrs_sensor.h" float data1; diff --git a/Examples/2_ESPNOW_Sensor/fdrs_sensor.h b/Examples/2_ESPNOW_Sensor/fdrs_sensor.h index 36333b0..a0e0f31 100644 --- a/Examples/2_ESPNOW_Sensor/fdrs_sensor.h +++ b/Examples/2_ESPNOW_Sensor/fdrs_sensor.h @@ -19,6 +19,12 @@ #include #endif +#ifdef DEBUG +#define DBG(a) (Serial.println(a)) +#else +#define DBG(a) +#endif + typedef struct __attribute__((packed)) DataReading { float d; uint16_t id; diff --git a/Examples/3_ESPNOW_Gateway/3_ESPNOW_Gateway.ino b/Examples/3_ESPNOW_Gateway/3_ESPNOW_Gateway.ino index 4cbd783..54455b2 100644 --- a/Examples/3_ESPNOW_Gateway/3_ESPNOW_Gateway.ino +++ b/Examples/3_ESPNOW_Gateway/3_ESPNOW_Gateway.ino @@ -8,7 +8,7 @@ #define DEBUG #define CREDENTIALS -#include +#include #include "fdrs_config.h" #ifdef ESP8266 @@ -73,7 +73,6 @@ void setup() { #endif //DBG(sizeof(DataReading)); - client.publish(TOPIC_STATUS, "FDRS initialized"); } diff --git a/Examples/3_ESPNOW_Gateway/fdrs_config.h b/Examples/3_ESPNOW_Gateway/fdrs_config.h index 965ff4b..ff4093e 100644 --- a/Examples/3_ESPNOW_Gateway/fdrs_config.h +++ b/Examples/3_ESPNOW_Gateway/fdrs_config.h @@ -39,3 +39,13 @@ #define SS 18 #define RST 14 #define DIO0 26 + +// Buffer Delays - in milliseconds +#define ESPNOW1_DELAY 0 +#define ESPNOW2_DELAY 0 +#define ESPNOWG_DELAY 0 +#define SERIAL_DELAY 0 +#define MQTT_DELAY 0 +#define LORAG_DELAY 1000 +#define LORA1_DELAY 1000 +#define LORA2_DELAY 1000 diff --git a/Examples/3_ESPNOW_Gateway/fdrs_functions.h b/Examples/3_ESPNOW_Gateway/fdrs_functions.h index 003a919..99a0bb3 100644 --- a/Examples/3_ESPNOW_Gateway/fdrs_functions.h +++ b/Examples/3_ESPNOW_Gateway/fdrs_functions.h @@ -4,6 +4,20 @@ #define DBG(a) #endif +#if defined (ESP32) +#define UART_IF Serial1 +#else +#define UART_IF Serial +#endif + +#ifdef GLOBALS +#define FDRS_BAND GLOBAL_BAND +#define FDRS_SF GLOBAL_SF +#else +#define FDRS_BAND BAND +#define FDRS_SF SF +#endif + typedef struct __attribute__((packed)) DataReading { float d; uint16_t id; diff --git a/Examples/4_UART_Gateway/4_UART_Gateway.ino b/Examples/4_UART_Gateway/4_UART_Gateway.ino index 4cbd783..7b0562c 100644 --- a/Examples/4_UART_Gateway/4_UART_Gateway.ino +++ b/Examples/4_UART_Gateway/4_UART_Gateway.ino @@ -8,7 +8,7 @@ #define DEBUG #define CREDENTIALS -#include +#include #include "fdrs_config.h" #ifdef ESP8266 @@ -66,14 +66,13 @@ void setup() { DBG("Initializing LoRa!"); SPI.begin(SCK, MISO, MOSI, SS); LoRa.setPins(SS, RST, DIO0); - if (!LoRa.begin(BAND)) { + if (!LoRa.begin(GLOBAL_BAND)) { while (1); } DBG(" LoRa initialized."); #endif //DBG(sizeof(DataReading)); - client.publish(TOPIC_STATUS, "FDRS initialized"); } diff --git a/Examples/4_UART_Gateway/fdrs_config.h b/Examples/4_UART_Gateway/fdrs_config.h index 3d8c3aa..b7d9acf 100644 --- a/Examples/4_UART_Gateway/fdrs_config.h +++ b/Examples/4_UART_Gateway/fdrs_config.h @@ -2,41 +2,69 @@ // // GATEWAY 2.000 Configuration -#define UNIT_MAC 0x04 // The address of this gateway -#define ESPNOW1_PEER 0xFD // ESPNOW1 Address -#define ESPNOW2_PEER 0xFE // ESPNOW2 Address -#define LORA1_PEER 0xFD // LoRa1 Address -#define LORA2_PEER 0xFE // LoRa2 Address +#include //Uncomment if you install the globals file +#define DEBUG + +#define UNIT_MAC 0x04 // The address of this gateway //Actions -- Define what happens when a packet arrives at each interface: //Current function options are: sendESPNOW(MAC), sendSerial(), sendMQTT(), bufferESPNOW(interface), bufferSerial(), and bufferLoRa(interface). #define ESPNOWG_ACT sendSerial(); -#define SERIAL_ACT -#define MQTT_ACT +#define SERIAL_ACT //sendMQTT(); +#define MQTT_ACT #define LORAG_ACT sendSerial(); -#define ESPNOW1_ACT -#define ESPNOW2_ACT -#define LORA1_ACT -#define LORA2_ACT #define USE_LORA //#define USE_WIFI //Used only for MQTT gateway -#define CREDENTIALS +//#define WIFI_SSID "Your SSID" +//#define WIFI_PASS "Your Password" +//#define MQTT_ADDR "192.168.0.8" + +// Peer addresses +#define ESPNOW1_PEER 0x04 // ESPNOW1 Address +#define ESPNOW2_PEER 0x05 // ESPNOW2 Address +#define LORA1_PEER 0x04 // LoRa1 Address +#define LORA2_PEER 0x05 // LoRa2 Address -#if defined (ESP32) +// Peer Actions +#define ESPNOW1_ACT +#define ESPNOW2_ACT +#define LORA1_ACT +#define LORA2_ACT + +//Pins for UART data interface (ESP32 only) #define RXD2 14 #define TXD2 15 -#define UART_IF Serial2 -#else -#define UART_IF Serial -#endif -//LoRa Configuration -- Needed only if this device is using LoRa +//LoRa Configuration -- Needed only if using LoRa #define SCK 5 #define MISO 19 #define MOSI 27 #define SS 18 #define RST 14 #define DIO0 26 +//433E6 for Asia +//866E6 for Europe +//915E6 for North America +//#define BAND 915E6 +//#define SF 7 + +// Buffer Delays - in milliseconds +#define ESPNOW1_DELAY 0 +#define ESPNOW2_DELAY 0 +#define ESPNOWG_DELAY 0 +#define SERIAL_DELAY 0 +#define MQTT_DELAY 0 +#define LORAG_DELAY 1000 +#define LORA1_DELAY 1000 +#define LORA2_DELAY 1000 +//#define USE_LED //Not yet fully implemented +#define LED_PIN 32 +#define NUM_LEDS 4 + +// MQTT Topics +#define TOPIC_DATA "FDRS/DATA" +#define TOPIC_STATUS "FDRS/STATUS" +#define TOPIC_COMMAND "FDRS/COMMAND" diff --git a/Examples/4_UART_Gateway/fdrs_functions.h b/Examples/4_UART_Gateway/fdrs_functions.h index 5eb0d4c..3c39e62 100644 --- a/Examples/4_UART_Gateway/fdrs_functions.h +++ b/Examples/4_UART_Gateway/fdrs_functions.h @@ -140,9 +140,10 @@ void getLoRa() { uint8_t packet[packetSize]; uint8_t incLORAMAC[2]; LoRa.readBytes((uint8_t *)&packet, packetSize); - for (int i = 0; i < packetSize; i++) { + /*for (int i = 0; i < packetSize; i++) { Serial.println(packet[i], HEX); } + */ if (memcmp(&packet, &selfAddress[3], 3) == 0) { //Check if addressed to this device DBG("Packet for me"); memcpy(&incLORAMAC, &packet[3], 2); //Split off address portion of packet