Updated to current version

This commit is contained in:
Timm Bogner 2022-06-12 23:43:35 -05:00
parent 38a4214039
commit faf4cbe2a6
9 changed files with 233 additions and 193 deletions

View File

@ -1,10 +1,10 @@
// FARM DATA RELAY SYSTEM
//
// Sensor Configuration
// (In the future this file will be known as 'sensor_config.h')
// (This file will soon be known as 'sensor_config.h')
//
//#include <fdrs_globals.h> //Uncomment if you install the globals file
//#include <fdrs_globals.h> //Uncomment when you install the globals file
#define READING_ID 1 //Unique ID for this sensor
#define GTWY_MAC 0x04 //Address of the nearest gateway

View File

@ -1,29 +0,0 @@
typedef struct __attribute__((packed)) DataReading {
float d;
uint16_t id;
uint8_t t;
} DataReading;
//Type definitions in progress:
#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 OXYGEN_T 8 // Oxygen
#define CO2_T 9 // Carbon Dioxide
#define WINDSPD_T 10 // Wind Speed
#define WINDHDG_T 11 // Wind Direction
#define RAINFALL_T 12 // Rainfall
#define MOTION_T 13 // Motion
#define VOLTAGE_T 14 // Voltage
#define VOLTAGE2_T 15 // Voltage #2
#define CURRENT_T 16 // Current
#define CURRENT2_T 17 // Current #2
#define IT_T 18 // Iterations

View File

@ -2,10 +2,10 @@
//
// GATEWAY 2.000
//
// 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.
//
#include "fdrs_config.h"
#include "DataReading.h"
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <espnow.h>
@ -25,18 +25,14 @@
#include <FastLED.h>
#endif
#include "fdrs_functions.h"
//#ifdef CREDENTIALS
//#include <credentials.h>
//#endif
void setup() {
#if defined(ESP8266)
UART_IF.begin(115200);
Serial.begin(115200);
#elif defined(ESP32)
Serial.begin(115200);
UART_IF.begin(115200, SERIAL_8N1, RXD2, TXD2);
#endif
DBG("Initializing FDRS Gateway");
DBG("Address:" + String (UNIT_MAC, HEX));
#ifdef USE_LED
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
@ -47,16 +43,18 @@ void setup() {
delay(10);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.println("Connecting to Wifi...");
DBG("Connecting to WiFi...");
DBG(FDRS_WIFI_SSID);
delay(500);
}
DBG("WiFi Connected");
client.setServer(mqtt_server, 1883);
//client.setServer("192.168.137.249", 1883);
if (!client.connected()) {
Serial.println("Connecting mqtt...");
DBG("Connecting MQTT...");
reconnect();
}
DBG("MQTT Connected");
client.setCallback(mqtt_callback);
#else
begin_espnow();
@ -65,48 +63,68 @@ void setup() {
DBG("Initializing LoRa!");
SPI.begin(SCK, MISO, MOSI, SS);
LoRa.setPins(SS, RST, DIO0);
if (!LoRa.begin(BAND)) {
if (!LoRa.begin(FDRS_BAND)) {
while (1);
}
LoRa.setSpreadingFactor(FDRS_SF);
DBG(" LoRa initialized.");
#endif
// UART_IF.println(sizeof(DataReading));
//DBG(sizeof(DataReading));
#ifdef USE_WIFI
client.publish(TOPIC_STATUS, "FDRS initialized");
#endif
}
void loop() {
#ifdef ESPNOWG_DELAY
if (millis() > timeESPNOWG) {
timeESPNOWG += ESPNOWG_DELAY;
if (lenESPNOWG > 0) releaseESPNOW(0);
}
#endif
#ifdef ESPNOW1_DELAY
if (millis() > timeESPNOW1) {
timeESPNOW1 += ESPNOW1_DELAY;
if (lenESPNOW1 > 0) releaseESPNOW(1);
}
#endif
#ifdef ESPNOW2_DELAY
if (millis() > timeESPNOW2) {
timeESPNOW2 += ESPNOW2_DELAY;
if (lenESPNOW2 > 0) releaseESPNOW(2);
}
#endif
#ifdef SERIAL_DELAY
if (millis() > timeSERIAL) {
timeSERIAL += SERIAL_DELAY;
if (lenSERIAL > 0) releaseSerial();
}
#endif
#ifdef MQTT_DELAY
if (millis() > timeMQTT) {
timeMQTT += MQTT_DELAY;
if (lenMQTT > 0) releaseMQTT();
}
#endif
#ifdef LORAG_DELAY
if (millis() > timeLORAG) {
timeLORAG += LORAG_DELAY;
if (lenLORAG > 0) releaseLoRa(0);
}
#endif
#ifdef LORA1_DELAY
if (millis() > timeLORA1) {
timeLORA1 += LORA1_DELAY;
if (lenLORA1 > 0) releaseLoRa(1);
}
#endif
#ifdef LORA2_DELAY
if (millis() > timeLORA2) {
timeLORA2 += LORA2_DELAY;
if (lenLORA2 > 0) releaseLoRa(2);
}
#endif
while (UART_IF.available()) {
getSerial();
@ -114,7 +132,7 @@ void loop() {
getLoRa();
#ifdef USE_WIFI
if (!client.connected()) {
Serial.println("Connecting mqtt...");
DBG("Connecting MQTT...");
reconnect();
}
client.loop();

View File

@ -1,49 +0,0 @@
#define UNIT_MAC 0xFC // THIS UNIT
#define ESPNOW1_PEER 0xFD // ESPNOW1 Address
#define ESPNOW2_PEER 0xFE // ESPNOW2 Address
#define LORA1_PEER 0xFD // LoRa1 Address
#define LORA2_PEER 0xFE // LoRa2 Address
#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 ESPNOW1_ACT
#define ESPNOW2_ACT
#define ESPNOWG_ACT
#define SERIAL_ACT
#define MQTT_ACT
#define LORAG_ACT
#define LORA1_ACT
#define LORA2_ACT
//#define RXD2 21
//#define TXD2 22
#define WIFI_NET "Your SSID"
#define WIFI_PASS "Password"
#define MQTT_ADDR "192.168.0.8"
//#define USE_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 USE_LED
#define LED_PIN 32
#define NUM_LEDS 4
#define MAC_PREFIX 0xAA, 0xBB, 0xCC, 0xDD, 0xEE
#define UART_IF Serial1

View File

@ -2,35 +2,40 @@
//
// GATEWAY 2.000 Configuration
#include "defaults.h"
//#include <fdrs_globals.h> //Uncomment if you install the globals file
#define DEBUG //Enable USB-Serial debugging
#define UNIT_MAC 0x00 // THIS UNIT
#define UNIT_MAC 0x01 // 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).
// Current function options are: sendESPNOW(MAC), sendSerial(), sendMQTT(),
// bufferLoRa(interface), bufferESPNOW(interface), bufferSerial(), and bufferMQTT().
#define ESPNOWG_ACT sendSerial();
#define SERIAL_ACT
#define SERIAL_ACT
#define MQTT_ACT
#define LORAG_ACT sendSerial();
#define USE_LORA
//#define USE_WIFI //Used only for MQTT gateway
//#define USE_LORA
//#define USE_WIFI //Used only for MQTT gateway
//#define CREDENTIALS
// Peer addresses
#define ESPNOW1_PEER 0x0E // ESPNOW1 Address
#define ESPNOW2_PEER 0x0F // ESPNOW2 Address
#define LORA1_PEER 0x0E // LoRa1 Address
#define LORA2_PEER 0x0F // LoRa2 Address
//ESP32 Only -- Define UART pins and interface.
// 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 Serial1
#define DEBUG
//Wifi Configuration -- Needed only if this device is using MQTT
#define WIFI_NET "Your SSID"
#define WIFI_PASS "Password"
#define MQTT_ADDR "192.168.0.8"
////LoRa Configuration -- Needed only if using LoRa
//LoRa Configuration -- Needed only if using LoRa
#define SCK 5
#define MISO 19
#define MOSI 27
@ -41,7 +46,28 @@
//866E6 for Europe
//915E6 for North America
#define BAND 915E6
#define SF 7
#ifdef CREDENTIALS
#include <credentials.h>
#endif
// Buffer Delays - in milliseconds - Uncomment to enable any buffer
//#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
//WiFi and MQTT Credentials -- Needed only for MQTT gateway
#define WIFI_SSID "Your SSID"
#define WIFI_PASS "Your Password"
#define MQTT_ADDR "192.168.0.8"
// MQTT Topics
#define TOPIC_DATA "fdrs/data"
#define TOPIC_STATUS "fdrs/status"
#define TOPIC_COMMAND "fdrs/command"

View File

@ -1,24 +1,71 @@
#define DBG(a)
#ifdef ESP8266
#define UART_IF Serial
#else
// 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.
#ifdef DEBUG
#define DBG(a) (Serial.println(a))
#else
#define DBG(a)
#endif
#if defined (ESP32)
#define UART_IF Serial1
#else
#define UART_IF Serial
#endif
#ifdef GLOBALS
#define FDRS_WIFI_SSID GLOBAL_SSID
#define FDRS_WIFI_PASS GLOBAL_PASS
#define FDRS_MQTT_ADDR GLOBAL_MQTT_ADDR
#define FDRS_BAND GLOBAL_BAND
#define FDRS_SF GLOBAL_SF
#else
#define FDRS_WIFI_SSID WIFI_SSID
#define FDRS_WIFI_PASS WIFI_PASS
#define FDRS_MQTT_ADDR MQTT_ADDR
#define FDRS_BAND BAND
#define FDRS_SF SF
#endif
#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;
uint8_t t;
} DataReading;
const uint8_t espnow_size = 250 / sizeof(DataReading);
const uint8_t lora_size = 256 / sizeof(DataReading);
const uint8_t mac_prefix[] = {MAC_PREFIX};
#ifdef ESP32
esp_now_peer_info_t peerInfo;
#endif
uint8_t broadcast_mac[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
uint8_t selfAddress[] = {MAC_PREFIX, UNIT_MAC};
uint8_t ESPNOW1[] = {MAC_PREFIX, ESPNOW1_PEER};
uint8_t ESPNOW2[] = {MAC_PREFIX, ESPNOW2_PEER};
uint8_t incMAC[6];
#ifdef ESPNOW1_PEER
uint8_t ESPNOW1[] = {MAC_PREFIX, ESPNOW1_PEER};
#else
uint8_t ESPNOW1[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
#endif
#ifdef ESPNOW2_PEER
uint8_t ESPNOW2[] = {MAC_PREFIX, ESPNOW2_PEER};
#else
uint8_t ESPNOW2[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
#endif
#ifdef USE_LORA
uint8_t LoRa1[] = {mac_prefix[3], mac_prefix[4], LORA1_PEER};
uint8_t LoRa2[] = {mac_prefix[3], mac_prefix[4], LORA2_PEER};
//uint8_t LoRaAddress[] = {0x42, 0x00};
#endif
DataReading theData[256];
uint8_t ln;
@ -55,9 +102,9 @@ CRGB leds[NUM_LEDS];
#endif
#ifdef USE_WIFI
PubSubClient client(espClient);
const char* ssid = WIFI_NET;
const char* password = WIFI_PASS;
const char* mqtt_server = MQTT_ADDR;
const char* ssid = FDRS_WIFI_SSID;
const char* password = FDRS_WIFI_PASS;
const char* mqtt_server = FDRS_MQTT_ADDR;
#endif
@ -73,11 +120,11 @@ void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
#endif
memcpy(&theData, incomingData, sizeof(theData));
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;
ln = len / sizeof(DataReading);
DBG("Incoming ESP-NOW.");
}
void getSerial() {
String incomingString = UART_IF.readStringUntil('\n');
@ -103,6 +150,7 @@ void getSerial() {
}
void mqtt_callback(char* topic, byte * message, unsigned int length) {
String incomingString;
DBG(topic);
for (int i = 0; i < length; i++) {
incomingString += (char)message[i];
}
@ -209,7 +257,7 @@ void sendMQTT() {
}
String outgoingString;
serializeJson(doc, outgoingString);
client.publish("esp/fdrs", (char*) outgoingString.c_str());
client.publish(TOPIC_DATA, (char*) outgoingString.c_str());
#endif
}
@ -430,7 +478,7 @@ void releaseMQTT() {
}
String outgoingString;
serializeJson(doc, outgoingString);
client.publish("esp/fdrs", (char*) outgoingString.c_str());
client.publish(TOPIC_DATA, (char*) outgoingString.c_str());
lenMQTT = 0;
#endif
}
@ -441,7 +489,7 @@ void reconnect() {
// Attempt to connect
if (client.connect("FDRS_GATEWAY")) {
// Subscribe
client.subscribe("esp/fdrs");
client.subscribe(TOPIC_COMMAND);
} else {
DBG("Connecting MQTT.");
delay(5000);
@ -451,7 +499,6 @@ void reconnect() {
}
void begin_espnow() {
DBG("Initializing ESP-NOW!");
WiFi.mode(WIFI_STA);
WiFi.disconnect();
// Init ESP-NOW for either ESP8266 or ESP32 and set MAC address
@ -464,8 +511,12 @@ void begin_espnow() {
esp_now_register_send_cb(OnDataSent);
esp_now_register_recv_cb(OnDataRecv);
// Register peers
#ifdef ESPNOW1_PEER
esp_now_add_peer(ESPNOW1, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
#endif
#ifdef ESPNOW2_PEER
esp_now_add_peer(ESPNOW2, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
#endif
#elif defined(ESP32)
esp_wifi_set_mac(WIFI_IF_STA, &selfAddress[0]);
if (esp_now_init() != ESP_OK) {
@ -474,7 +525,7 @@ void begin_espnow() {
}
esp_now_register_send_cb(OnDataSent);
esp_now_register_recv_cb(OnDataRecv);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Register first peer
@ -484,18 +535,20 @@ void begin_espnow() {
DBG("Failed to add peer bcast");
return;
}
memcpy(peerInfo.peer_addr, ESPNOW1, 6);
#ifdef ESPNOW1_PEER
memcpy(peerInfo.peer_addr, ESPNOW1, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
DBG("Failed to add peer 1");
return;
}
#endif
#ifdef ESPNOW2_PEER
memcpy(peerInfo.peer_addr, ESPNOW2, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
DBG("Failed to add peer 2");
return;
}
#endif
#endif
DBG(" ESP-NOW Initialized.");
DBG(WIFI_NET);
}

View File

@ -6,17 +6,28 @@
// An example of how to send data using "fdrs_sensor.h".
//
#include "fdrs_sensor.h"
float data1 = 42.069;
float data2 = 21.0345;
float data1;
float data2;
void setup() {
beginFDRS();
}
void loop() {
data1 = readHum();
loadFDRS(data1, HUMIDITY_T);
data2 = readTemp();
loadFDRS(data2, TEMP_T);
sendFDRS();
sleepFDRS(10); //Sleep time in seconds
}
float readTemp() {
return 42.069;
}
float readHum() {
return 21.0345;
}

View File

@ -2,41 +2,34 @@
//
// "fdrs_sensor.h"
//
// 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.
//
#define READING_ID 21 //Unique ID for this sensor
#define GTWY_MAC 0x00 //Address of the nearest gateway
#define USE_ESPNOW
//#define USE_LORA
#define DEEP_SLEEP
//#define POWER_CTRL 14
#define DEBUG
//#define CREDENTIALS
#define MAC_PREFIX 0xAA, 0xBB, 0xCC, 0xDD, 0xEE
//LoRa Configuration
#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 //May be overwritten if CREDENTIALS is set
#ifdef CREDENTIALS
#include <credentials.h>
#include "sensor_setup.h"
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#include <espnow.h>
#elif defined(ESP32)
#include <esp_now.h>
#include <WiFi.h>
#include <esp_wifi.h>
#endif
#ifdef USE_LORA
#include <LoRa.h>
#endif
typedef struct __attribute__((packed)) DataReading {
float d;
uint16_t id;
uint8_t t;
#ifdef GLOBALS
#define FDRS_BAND GLOBAL_BAND
#define FDRS_SF GLOBAL_SF
#else
#define FDRS_BAND BAND
#define FDRS_SF SF
#endif
} DataReading;
#ifdef DEBUG
#define DBG(a) (Serial.println(a))
#else
#define DBG(a)
#endif
#define STATUS_T 0 // Status
#define TEMP_T 1 // Temperature
@ -60,35 +53,20 @@ typedef struct __attribute__((packed)) DataReading {
#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.
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#include <espnow.h>
#elif defined(ESP32)
#include <esp_now.h>
#include <WiFi.h>
#include <esp_wifi.h>
#endif
typedef struct __attribute__((packed)) DataReading {
float d;
uint16_t id;
uint8_t t;
#ifdef USE_LORA
#include <LoRa.h>
#endif
#define DBG(a)
#ifdef ESP8266
#define UART_IF Serial
#else
#ifdef DEBUG
#define DBG(a) (Serial.println(a))
#endif
#endif
} 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];
uint8_t data_count = 0;
@ -97,7 +75,7 @@ void beginFDRS() {
#ifdef DEBUG
Serial.begin(115200);
#endif
DBG("FDRS Sensor ID " + String(READING_ID)+ " initializing...");
DBG("FDRS Sensor ID " + String(READING_ID) + " initializing...");
DBG(" Gateway: " + String (GTWY_MAC, HEX));
#ifdef POWER_CTRL
DBG("Powering up the sensor array!");
@ -136,20 +114,23 @@ void beginFDRS() {
#endif
#ifdef USE_LORA
DBG("Initializing LoRa!");
DBG(BAND);
DBG(SF);
#ifndef __AVR__
SPI.begin(SCK, MISO, MOSI, SS);
#endif
LoRa.setPins(SS, RST, DIO0);
if (!LoRa.begin(BAND)) {
if (!LoRa.begin(FDRS_BAND)) {
while (1);
}
LoRa.setSpreadingFactor(FDRS_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();
@ -181,9 +162,9 @@ void loadFDRS(float d, uint8_t t) {
data_count++;
}
void sleepFDRS(int sleep_time) {
DBG("Sleepytime!");
DBG("Sleepytime!");
#ifdef DEEP_SLEEP
DBG(" Deep sleeping.");
DBG(" Deep sleeping.");
#ifdef ESP32
esp_sleep_enable_timer_wakeup(sleep_time * 1000000);
esp_deep_sleep_start();
@ -192,6 +173,6 @@ DBG(" Deep sleeping.");
ESP.deepSleep(sleep_time * 1000000);
#endif
#endif
DBG(" Delaying.");
DBG(" Delaying.");
delay(sleep_time * 1000);
}

View File

@ -0,0 +1,29 @@
// FARM DATA RELAY SYSTEM
//
// Sensor Configuration
// (In the future this file will be known as 'sensor_config.h')
//
//#include <fdrs_globals.h> //Uncomment if you install the globals file
#define READING_ID 1 //Unique ID for this sensor
#define GTWY_MAC 0x01 //Address of the nearest gateway
#define USE_ESPNOW
//#define USE_LORA
#define DEEP_SLEEP
//#define POWER_CTRL 14
#define DEBUG
//LoRa Configuration
#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