Sensor update

This commit is contained in:
Timm Bogner 2022-06-17 18:51:09 -05:00
parent e527430cdf
commit 00ba8de2b3
31 changed files with 1069 additions and 724 deletions

View File

@ -1,110 +0,0 @@
// FARM DATA RELAY SYSTEM
//
// Pretty Lantern Controller
//
// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
//
//
#define READING_ID 72 //Unique ID (0 - 255) for controller
#define GTWY_MAC 0x00 //Gateway MAC
#define REDPIN 12
#define GREENPIN 13
#define BLUEPIN 15
#include <FastLED.h>
#include <ESP8266WiFi.h>
#include <espnow.h>
uint8_t broadcastAddress[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, GTWY_MAC};
typedef struct DataReading {
float d;
uint16_t id;
uint8_t t;
} DataReading;
DataReading theCommands[31];
int the_color = 0;
int the_bright = 255;
bool newData = false;
int pkt_readings;
void showAnalogRGB( const CRGB& rgb)
{
analogWrite(REDPIN, rgb.r );
analogWrite(GREENPIN, rgb.g );
analogWrite(BLUEPIN, rgb.b );
}
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
Serial.print("Last Packet Send Status: ");
if (sendStatus == 0) {
Serial.println("Delivery success");
}
else {
Serial.println("Delivery fail");
}
}
void OnDataRecv(uint8_t* mac, uint8_t *incomingData, uint8_t len) {
memcpy(&theCommands, incomingData, len);
pkt_readings = len / sizeof(DataReading);
for (int i; i <= pkt_readings; i++) { //Cycle through array of incoming DataReadings for any addressed to this device
if (theCommands[i].id == READING_ID) {
if (theCommands[i].t == 201) { //Adjust color or brightness, depending on type.
the_color = (int)theCommands[i].d;
Serial.println("D:" + String(theCommands[i].d));
newData = true;
}
if (theCommands[i].t == 202) {
the_bright = (int)theCommands[i].d;
Serial.println("B:" + String(theCommands[i].d));
newData = true;
}
}
}
}
void colorBars()
{
showAnalogRGB( CRGB::Red ); delay(500);
showAnalogRGB( CRGB::Green ); delay(500);
showAnalogRGB( CRGB::Blue ); delay(500);
showAnalogRGB( CRGB::Black ); delay(500);
}
void loop()
{
if (newData) {
newData = false;
showAnalogRGB(CHSV(the_color, 255, the_bright));
}
}
void setup() {
pinMode(REDPIN, OUTPUT);
pinMode(GREENPIN, OUTPUT);
pinMode(BLUEPIN, OUTPUT);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
if (esp_now_init() != 0) {
return;
}
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
esp_now_register_send_cb(OnDataSent);
esp_now_register_recv_cb(OnDataRecv);
// Register peer
esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
Serial.println("FARM DATA RELAY SYSTEM :: Pretty Lantern Module");
Serial.println("MAC:" + WiFi.macAddress());
colorBars();
}

View File

@ -0,0 +1,3 @@
Please copy this directory into your Arduino "libraries" directory.
Edit the files in your libraries folder according to your needs with your WiFi info, LoRa band, etc.
Never edit them here!!!

View File

@ -0,0 +1,8 @@
#define GLOBAL_SSID "Your SSID"
#define GLOBAL_PASS "Password"
#define GLOBAL_MQTT_ADDR "192.168.0.8"
#define GLOBAL_BAND 915E6 //LoRa Frequency Band
#define GLOBAL_SF 7 //LoRa Spreading Factor
#define GLOBALS

View File

@ -2,7 +2,7 @@
//
// AHT20 SENSOR MODULE
//
// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
// Developed by Timm Bogner (timmbogner@gmail.com) in Urbana, Illinois, USA.
#include <Adafruit_AHTX0.h>
#include "fdrs_sensor.h"

View File

@ -2,34 +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 8 //Unique ID for sensor module
#define GTWY_MAC 0x00 //Address of the nearest gateway
#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
#define POWER_CTRL 14
#define MAC_PREFIX 0xAA, 0xBB, 0xCC, 0xDD, 0xEE
#define DEEP_SLEEP
#define USE_ESPNOW
#ifdef GLOBALS
#define FDRS_BAND GLOBAL_BAND
#define FDRS_SF GLOBAL_SF
#else
#define FDRS_BAND BAND
#define FDRS_SF SF
#endif
//#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
typedef struct __attribute__((packed)) DataReading {
float d;
uint16_t id;
uint8_t t;
} DataReading;
#ifdef DEBUG
#define DBG(a) (Serial.println(a))
#else
#define DBG(a)
#endif
#define STATUS_T 0 // Status
#define TEMP_T 1 // Temperature
@ -53,37 +53,40 @@ 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
#include <LoRa.h>
typedef struct __attribute__((packed)) DataReading {
float d;
uint16_t id;
uint8_t t;
} 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;
void beginFDRS() {
#ifdef USE_ESPNOW
WiFi.mode(WIFI_STA);
WiFi.disconnect();
#ifdef DEBUG
Serial.begin(115200);
#endif
DBG("FDRS Sensor ID " + String(READING_ID) + " initializing...");
DBG(" Gateway: " + String (GTWY_MAC, HEX));
#ifdef POWER_CTRL
DBG("Powering up the sensor array!");
pinMode(POWER_CTRL, OUTPUT);
digitalWrite(POWER_CTRL, 1);
#endif
// Init ESP-NOW for either ESP8266 or ESP32 and set MAC address
#ifdef USE_ESPNOW
DBG("Initializing ESP-NOW!");
WiFi.mode(WIFI_STA);
WiFi.disconnect();
#if defined(ESP8266)
if (esp_now_init() != 0) {
return;
@ -93,7 +96,7 @@ void beginFDRS() {
esp_now_add_peer(gatewayAddress, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
#elif defined(ESP32)
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
DBG("Error initializing ESP-NOW");
return;
}
esp_now_peer_info_t peerInfo;
@ -103,25 +106,31 @@ void beginFDRS() {
// Register first peer
memcpy(peerInfo.peer_addr, gatewayAddress, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
DBG("Failed to add peer");
return;
}
#endif
DBG(" ESP-NOW Initialized.");
#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();
@ -130,16 +139,20 @@ void transmitLoRa(uint8_t* mac, DataReading * packet, uint8_t len) {
#endif
}
void sendFDRS() {
DBG("Sending FDRS Packet!");
#ifdef USE_ESPNOW
esp_now_send(gatewayAddress, (uint8_t *) &fdrsData, data_count * sizeof(DataReading));
delay(5);
DBG(" ESP-NOW sent.");
#endif
#ifdef USE_LORA
transmitLoRa(gtwyAddress, fdrsData, data_count);
DBG(" LoRa sent.");
#endif
data_count = 0;
}
void loadFDRS(float d, uint8_t t) {
DBG("Data loaded. Type: " + String(t));
if (data_count > espnow_size) sendFDRS();
DataReading dr;
dr.id = READING_ID;
@ -147,10 +160,11 @@ void loadFDRS(float d, uint8_t t) {
dr.d = d;
fdrsData[data_count] = dr;
data_count++;
}
void sleepFDRS(int sleep_time) {
DBG("Sleepytime!");
#ifdef DEEP_SLEEP
DBG(" Deep sleeping.");
#ifdef ESP32
esp_sleep_enable_timer_wakeup(sleep_time * 1000000);
esp_deep_sleep_start();
@ -159,6 +173,6 @@ void sleepFDRS(int sleep_time) {
ESP.deepSleep(sleep_time * 1000000);
#endif
#endif
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

View File

@ -2,34 +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 7 //Unique ID for sensor module
#define GTWY_MAC 0x00 //Address of the nearest gateway
#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
#define POWER_CTRL 14
#define MAC_PREFIX 0xAA, 0xBB, 0xCC, 0xDD, 0xEE
#define DEEP_SLEEP
#define USE_ESPNOW
#ifdef GLOBALS
#define FDRS_BAND GLOBAL_BAND
#define FDRS_SF GLOBAL_SF
#else
#define FDRS_BAND BAND
#define FDRS_SF SF
#endif
//#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
typedef struct __attribute__((packed)) DataReading {
float d;
uint16_t id;
uint8_t t;
} DataReading;
#ifdef DEBUG
#define DBG(a) (Serial.println(a))
#else
#define DBG(a)
#endif
#define STATUS_T 0 // Status
#define TEMP_T 1 // Temperature
@ -53,37 +53,40 @@ 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
#include <LoRa.h>
typedef struct __attribute__((packed)) DataReading {
float d;
uint16_t id;
uint8_t t;
} 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;
void beginFDRS() {
#ifdef USE_ESPNOW
WiFi.mode(WIFI_STA);
WiFi.disconnect();
#ifdef DEBUG
Serial.begin(115200);
#endif
DBG("FDRS Sensor ID " + String(READING_ID) + " initializing...");
DBG(" Gateway: " + String (GTWY_MAC, HEX));
#ifdef POWER_CTRL
DBG("Powering up the sensor array!");
pinMode(POWER_CTRL, OUTPUT);
digitalWrite(POWER_CTRL, 1);
#endif
// Init ESP-NOW for either ESP8266 or ESP32 and set MAC address
#ifdef USE_ESPNOW
DBG("Initializing ESP-NOW!");
WiFi.mode(WIFI_STA);
WiFi.disconnect();
#if defined(ESP8266)
if (esp_now_init() != 0) {
return;
@ -93,7 +96,7 @@ void beginFDRS() {
esp_now_add_peer(gatewayAddress, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
#elif defined(ESP32)
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
DBG("Error initializing ESP-NOW");
return;
}
esp_now_peer_info_t peerInfo;
@ -103,25 +106,31 @@ void beginFDRS() {
// Register first peer
memcpy(peerInfo.peer_addr, gatewayAddress, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
DBG("Failed to add peer");
return;
}
#endif
DBG(" ESP-NOW Initialized.");
#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();
@ -130,16 +139,20 @@ void transmitLoRa(uint8_t* mac, DataReading * packet, uint8_t len) {
#endif
}
void sendFDRS() {
DBG("Sending FDRS Packet!");
#ifdef USE_ESPNOW
esp_now_send(gatewayAddress, (uint8_t *) &fdrsData, data_count * sizeof(DataReading));
delay(5);
DBG(" ESP-NOW sent.");
#endif
#ifdef USE_LORA
transmitLoRa(gtwyAddress, fdrsData, data_count);
DBG(" LoRa sent.");
#endif
data_count = 0;
}
void loadFDRS(float d, uint8_t t) {
DBG("Data loaded. Type: " + String(t));
if (data_count > espnow_size) sendFDRS();
DataReading dr;
dr.id = READING_ID;
@ -147,10 +160,11 @@ void loadFDRS(float d, uint8_t t) {
dr.d = d;
fdrsData[data_count] = dr;
data_count++;
}
void sleepFDRS(int sleep_time) {
DBG("Sleepytime!");
#ifdef DEEP_SLEEP
DBG(" Deep sleeping.");
#ifdef ESP32
esp_sleep_enable_timer_wakeup(sleep_time * 1000000);
esp_deep_sleep_start();
@ -159,6 +173,6 @@ void sleepFDRS(int sleep_time) {
ESP.deepSleep(sleep_time * 1000000);
#endif
#endif
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

View File

@ -2,34 +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 1 //Unique ID for sensor module
#define GTWY_MAC 0x00 //Address of the nearest gateway
#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
#define POWER_CTRL 14
#define MAC_PREFIX 0xAA, 0xBB, 0xCC, 0xDD, 0xEE
//#define DEEP_SLEEP
#define USE_ESPNOW
#ifdef GLOBALS
#define FDRS_BAND GLOBAL_BAND
#define FDRS_SF GLOBAL_SF
#else
#define FDRS_BAND BAND
#define FDRS_SF SF
#endif
//#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
typedef struct __attribute__((packed)) DataReading {
float d;
uint16_t id;
uint8_t t;
} DataReading;
#ifdef DEBUG
#define DBG(a) (Serial.println(a))
#else
#define DBG(a)
#endif
#define STATUS_T 0 // Status
#define TEMP_T 1 // Temperature
@ -53,37 +53,40 @@ 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
#include <LoRa.h>
typedef struct __attribute__((packed)) DataReading {
float d;
uint16_t id;
uint8_t t;
} 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;
void beginFDRS() {
#ifdef USE_ESPNOW
WiFi.mode(WIFI_STA);
WiFi.disconnect();
#ifdef DEBUG
Serial.begin(115200);
#endif
DBG("FDRS Sensor ID " + String(READING_ID) + " initializing...");
DBG(" Gateway: " + String (GTWY_MAC, HEX));
#ifdef POWER_CTRL
DBG("Powering up the sensor array!");
pinMode(POWER_CTRL, OUTPUT);
digitalWrite(POWER_CTRL, 1);
#endif
// Init ESP-NOW for either ESP8266 or ESP32 and set MAC address
#ifdef USE_ESPNOW
DBG("Initializing ESP-NOW!");
WiFi.mode(WIFI_STA);
WiFi.disconnect();
#if defined(ESP8266)
if (esp_now_init() != 0) {
return;
@ -93,7 +96,7 @@ void beginFDRS() {
esp_now_add_peer(gatewayAddress, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
#elif defined(ESP32)
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
DBG("Error initializing ESP-NOW");
return;
}
esp_now_peer_info_t peerInfo;
@ -103,25 +106,31 @@ void beginFDRS() {
// Register first peer
memcpy(peerInfo.peer_addr, gatewayAddress, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
DBG("Failed to add peer");
return;
}
#endif
DBG(" ESP-NOW Initialized.");
#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();
@ -130,16 +139,20 @@ void transmitLoRa(uint8_t* mac, DataReading * packet, uint8_t len) {
#endif
}
void sendFDRS() {
DBG("Sending FDRS Packet!");
#ifdef USE_ESPNOW
esp_now_send(gatewayAddress, (uint8_t *) &fdrsData, data_count * sizeof(DataReading));
delay(5);
DBG(" ESP-NOW sent.");
#endif
#ifdef USE_LORA
transmitLoRa(gtwyAddress, fdrsData, data_count);
DBG(" LoRa sent.");
#endif
data_count = 0;
}
void loadFDRS(float d, uint8_t t) {
DBG("Data loaded. Type: " + String(t));
if (data_count > espnow_size) sendFDRS();
DataReading dr;
dr.id = READING_ID;
@ -147,10 +160,11 @@ void loadFDRS(float d, uint8_t t) {
dr.d = d;
fdrsData[data_count] = dr;
data_count++;
}
void sleepFDRS(int sleep_time) {
DBG("Sleepytime!");
#ifdef DEEP_SLEEP
DBG(" Deep sleeping.");
#ifdef ESP32
esp_sleep_enable_timer_wakeup(sleep_time * 1000000);
esp_deep_sleep_start();
@ -159,6 +173,6 @@ void sleepFDRS(int sleep_time) {
ESP.deepSleep(sleep_time * 1000000);
#endif
#endif
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

View File

@ -1,79 +0,0 @@
// FARM DATA RELAY SYSTEM
//
// DHT SENSOR MODULE
//
// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
// Each reading is assigned a two-byte identifier along with a one-byte sensor type
//
#define READING_ID 1 //Unique integer for each data reading
#define SLEEPYTIME 30 //Time to sleep in seconds
#define GTWY_MAC 0x00 //Gateway MAC
#define DHT_PIN 4
#include <ESP8266WiFi.h>
#include <espnow.h>
#include "DHTesp.h"
#include "DataReading.h"
DHTesp dht;
uint8_t broadcastAddress[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, GTWY_MAC};
int wait_time = 0;
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
Serial.print("Last Packet Send Status: ");
if (sendStatus == 0) {
Serial.println("Delivery success");
}
else {
Serial.println("Delivery fail");
}
}
void setup() {
Serial.begin(115200);
dht.setup(DHT_PIN, DHTesp::DHT22);
Serial.println("DHT Initialized");
WiFi.mode(WIFI_STA);
if (esp_now_init() != 0) {
return;
}
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
esp_now_register_send_cb(OnDataSent);
// Register peer
esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
}
void loop() {
if (millis() > wait_time) {
wait_time = wait_time + SLEEPYTIME * 1000;
loadData();
}
}
void loadData() {
float h = dht.getHumidity();
float t = dht.getTemperature();
DataReading Temp;
Temp.id = READING_ID;
Temp.t = 1;
DataReading Hum;
Temp.id = READING_ID;
Temp.t = 2;
if (!(isnan(h) || isnan(t))) {
Temp.d = -69.00;
Hum.d = -4.20;
} else {
Temp.d = t;
Hum.d = h;
}
DataPacket thePacket;
thePacket.packet[0] = Temp;
thePacket.packet[1] = Hum;
esp_now_send(broadcastAddress, (uint8_t *) &thePacket, sizeof(thePacket));
}

View File

@ -1,7 +0,0 @@
typedef struct DataReading {
float d;
uint16_t id;
uint8_t t;
} DataReading;

View File

@ -0,0 +1,42 @@
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
// Modified by Timm Bogner for Farm Data Relay System -- Untested because I don't have a DHT sensor onhand.
#include "fdrs_sensor.h"
#include "DHT.h"
#define DHTPIN 2 // Digital pin connected to the DHT sensor
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
void setup() {
beginFDRS();
DBG("DHTxx Sketch!");
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
DBG("Failed to read from DHT sensor!");
return;
}
loadFDRS(h, HUMIDITY_T);
loadFDRS(t, TEMP_T);
sendFDRS();
sleepFDRS(10); //Sleep time in seconds
}

View File

@ -0,0 +1,178 @@
// FARM DATA RELAY SYSTEM
//
// "fdrs_sensor.h"
//
// Developed by Timm Bogner (timmbogner@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
//
#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
#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;
uint8_t t;
} 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;
void beginFDRS() {
#ifdef DEBUG
Serial.begin(115200);
#endif
DBG("FDRS Sensor ID " + String(READING_ID) + " initializing...");
DBG(" Gateway: " + String (GTWY_MAC, HEX));
#ifdef POWER_CTRL
DBG("Powering up the sensor array!");
pinMode(POWER_CTRL, OUTPUT);
digitalWrite(POWER_CTRL, 1);
#endif
// Init ESP-NOW for either ESP8266 or ESP32 and set MAC address
#ifdef USE_ESPNOW
DBG("Initializing ESP-NOW!");
WiFi.mode(WIFI_STA);
WiFi.disconnect();
#if defined(ESP8266)
if (esp_now_init() != 0) {
return;
}
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
// Register peers
esp_now_add_peer(gatewayAddress, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
#elif defined(ESP32)
if (esp_now_init() != ESP_OK) {
DBG("Error initializing ESP-NOW");
return;
}
esp_now_peer_info_t peerInfo;
peerInfo.ifidx = WIFI_IF_STA;
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Register first peer
memcpy(peerInfo.peer_addr, gatewayAddress, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
DBG("Failed to add peer");
return;
}
#endif
DBG(" ESP-NOW Initialized.");
#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(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[3], &LoRaAddress, 2);
memcpy(&pkt[5], packet, len * sizeof(DataReading));
LoRa.beginPacket();
LoRa.write((uint8_t*)&pkt, sizeof(pkt));
LoRa.endPacket();
#endif
}
void sendFDRS() {
DBG("Sending FDRS Packet!");
#ifdef USE_ESPNOW
esp_now_send(gatewayAddress, (uint8_t *) &fdrsData, data_count * sizeof(DataReading));
delay(5);
DBG(" ESP-NOW sent.");
#endif
#ifdef USE_LORA
transmitLoRa(gtwyAddress, fdrsData, data_count);
DBG(" LoRa sent.");
#endif
data_count = 0;
}
void loadFDRS(float d, uint8_t t) {
DBG("Data loaded. Type: " + String(t));
if (data_count > espnow_size) sendFDRS();
DataReading dr;
dr.id = READING_ID;
dr.t = t;
dr.d = d;
fdrsData[data_count] = dr;
data_count++;
}
void sleepFDRS(int sleep_time) {
DBG("Sleepytime!");
#ifdef DEEP_SLEEP
DBG(" Deep sleeping.");
#ifdef ESP32
esp_sleep_enable_timer_wakeup(sleep_time * 1000000);
esp_deep_sleep_start();
#endif
#ifdef ESP8266
ESP.deepSleep(sleep_time * 1000000);
#endif
#endif
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 0x04 //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

View File

@ -1,68 +0,0 @@
// FARM DATA RELAY SYSTEM
//
// DS18B20 SENSOR MODULE
//
// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
// Each reading is assigned a two-byte identifier along with a one-byte sensor type
//
#define READING_ID 29 //Unique integer for each data reading
#define GTWY_MAC 0x00 //Gateway MAC
#define SLEEPYTIME 30 //Time to sleep in seconds
#define ONE_WIRE_BUS 13 //Pin that the DS18B20 is connected to
#include <ESP8266WiFi.h>
#include <espnow.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include "DataReading.h"
uint8_t broadcastAddress[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, GTWY_MAC};
int wait_time = 0;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
Serial.print("Last Packet Send Status: ");
if (sendStatus == 0) {
Serial.println("Delivery success");
}
else {
Serial.println("Delivery fail");
}
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
if (esp_now_init() != 0) {
return;
}
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
esp_now_register_send_cb(OnDataSent);
// Register peer
esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
sensors.begin();
}
void loop() {
if (millis() > wait_time) {
wait_time = wait_time + SLEEPYTIME * 1000;
loadData();
}
}
void loadData() {
sensors.requestTemperatures(); // Send the command to get temperatures
float tempC = sensors.getTempCByIndex(0);
DataReading Temp;
Temp.id = READING_ID;
Temp.t = 1;
// if (tempC = DEVICE_DISCONNECTED_C) Temp.d = -69.00;
// else Temp.d = tempC;
Temp.d = tempC;
esp_now_send(broadcastAddress, (uint8_t *) &Temp, sizeof(Temp));
}

View File

@ -1,7 +0,0 @@
typedef struct DataReading {
float d;
uint16_t id;
uint8_t t;
} DataReading;

View File

@ -0,0 +1,26 @@
// FARM DATA RELAY SYSTEM
//
// DS18B20 SENSOR MODULE
//
#define ONE_WIRE_BUS 13 //Pin that the DS18B20 is connected to
#include "fdrs_sensor.h"
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
beginFDRS();
sensors.begin();
}
void loop() {
sensors.requestTemperatures(); // Send the command to get temperatures
float tempC = sensors.getTempCByIndex(0);
loadFDRS(tempC, TEMP_T);
sendFDRS();
sleepFDRS(60);
}

View File

@ -0,0 +1,178 @@
// FARM DATA RELAY SYSTEM
//
// "fdrs_sensor.h"
//
// Developed by Timm Bogner (timmbogner@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
//
#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
#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;
uint8_t t;
} 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;
void beginFDRS() {
#ifdef DEBUG
Serial.begin(115200);
#endif
DBG("FDRS Sensor ID " + String(READING_ID) + " initializing...");
DBG(" Gateway: " + String (GTWY_MAC, HEX));
#ifdef POWER_CTRL
DBG("Powering up the sensor array!");
pinMode(POWER_CTRL, OUTPUT);
digitalWrite(POWER_CTRL, 1);
#endif
// Init ESP-NOW for either ESP8266 or ESP32 and set MAC address
#ifdef USE_ESPNOW
DBG("Initializing ESP-NOW!");
WiFi.mode(WIFI_STA);
WiFi.disconnect();
#if defined(ESP8266)
if (esp_now_init() != 0) {
return;
}
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
// Register peers
esp_now_add_peer(gatewayAddress, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
#elif defined(ESP32)
if (esp_now_init() != ESP_OK) {
DBG("Error initializing ESP-NOW");
return;
}
esp_now_peer_info_t peerInfo;
peerInfo.ifidx = WIFI_IF_STA;
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Register first peer
memcpy(peerInfo.peer_addr, gatewayAddress, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
DBG("Failed to add peer");
return;
}
#endif
DBG(" ESP-NOW Initialized.");
#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(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[3], &LoRaAddress, 2);
memcpy(&pkt[5], packet, len * sizeof(DataReading));
LoRa.beginPacket();
LoRa.write((uint8_t*)&pkt, sizeof(pkt));
LoRa.endPacket();
#endif
}
void sendFDRS() {
DBG("Sending FDRS Packet!");
#ifdef USE_ESPNOW
esp_now_send(gatewayAddress, (uint8_t *) &fdrsData, data_count * sizeof(DataReading));
delay(5);
DBG(" ESP-NOW sent.");
#endif
#ifdef USE_LORA
transmitLoRa(gtwyAddress, fdrsData, data_count);
DBG(" LoRa sent.");
#endif
data_count = 0;
}
void loadFDRS(float d, uint8_t t) {
DBG("Data loaded. Type: " + String(t));
if (data_count > espnow_size) sendFDRS();
DataReading dr;
dr.id = READING_ID;
dr.t = t;
dr.d = d;
fdrsData[data_count] = dr;
data_count++;
}
void sleepFDRS(int sleep_time) {
DBG("Sleepytime!");
#ifdef DEEP_SLEEP
DBG(" Deep sleeping.");
#ifdef ESP32
esp_sleep_enable_timer_wakeup(sleep_time * 1000000);
esp_deep_sleep_start();
#endif
#ifdef ESP8266
ESP.deepSleep(sleep_time * 1000000);
#endif
#endif
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

View File

@ -1,7 +0,0 @@
typedef struct DataReading {
float d;
uint16_t id;
uint8_t t;
} DataReading;

View File

@ -2,13 +2,6 @@
//
// LILYGO HIGROW SENSOR MODULE
//
// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
// Each reading is assigned a two-byte identifier along with a one-byte sensor type
#define READING_ID 18 //Unique ID (0 - 1023) for each data reading
#define SLEEPYTIME 60 //Time to sleep in seconds
#define GTWY_MAC 0x00 //Gateway MAC
#define I2C_SDA 25
@ -18,32 +11,19 @@
#define SALT_PIN 34
#define SOIL_PIN 32
#define BOOT_PIN 0
#define POWER_CTRL 4
#define USER_BUTTON 35
#define DS18B20_PIN 21
#include <WiFi.h>
#include <esp_now.h>
#include "fdrs_sensor.h"
#include <BH1750.h>
#include <Adafruit_BME280.h>
#include "DataReading.h"
BH1750 lightMeter(0x23); //0x23
Adafruit_BME280 bmp; //0x77
RTC_DATA_ATTR int the_count = 0;
uint8_t broadcastAddress[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, GTWY_MAC};
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
// Serial.print("Last Packet Send Status:");
// Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
esp_deep_sleep_start();
}
void setup() {
//Serial.begin(115200);
// Sensor power control pin, set high to enable sensors.
pinMode(POWER_CTRL, OUTPUT);
digitalWrite(POWER_CTRL, 1);
//Init Sensors
Wire.begin(I2C_SDA, I2C_SCL);
while (!bmp.begin()) {
@ -57,27 +37,6 @@ void setup() {
Serial.println(F("Error initialising BH1750"));
}
WiFi.mode(WIFI_STA);
WiFi.disconnect();
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_send_cb(OnDataSent);
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
loadData();
digitalWrite(POWER_CTRL, 0);
esp_sleep_enable_timer_wakeup(SLEEPYTIME * 1000000);
}
void loadData() {
@ -95,67 +54,27 @@ void loadData() {
the_count++;
Serial.println();
Serial.println("Temp:" + String(bme_temp));
Serial.println("Humidity:" + String(bme_humidity));
Serial.println("Light:" + String(lux));
Serial.println("Pressure:" + String(bme_pressure));
Serial.println("Salt:" + String(s_salt));
Serial.println("Soil:" + String(s_soil));
Serial.println("Voltage:" + String(s_battery));
Serial.println("Count:" + String(the_count));
Serial.println("Temp: " + String(bme_temp));
Serial.println("Humidity: " + String(bme_humidity));
Serial.println("Light: " + String(lux));
Serial.println("Pressure: " + String(bme_pressure));
Serial.println("Salt: " + String(s_salt));
Serial.println("Soil: " + String(s_soil));
Serial.println("Voltage: " + String(s_battery));
Serial.println("Count: " + String(the_count));
DataReading Temp;
Temp.d = bme_temp;
Temp.id = READING_ID;
Temp.t = 1;
loadFDRS(bme_temp, TEMP_T);
DataReading Hum;
Hum.d = bme_humidity;
Hum.id = READING_ID;
Hum.t = 2;
loadFDRS(bme_humidity, HUMIDITY_T);
DataReading Lux;
Lux.d = lux;
Lux.id = READING_ID;
Lux.t = 4;
DataReading Pres;
Pres.d = bme_pressure;
Pres.id = READING_ID;
Pres.t = 3;
loadFDRS(lux, LIGHT_T);
loadFDRS(bme_pressure, PRESSURE_T);
loadFDRS(s_salt, SOILR_T);
loadFDRS(s_soil, SOIL_T);
loadFDRS(s_battery, VOLTAGE_T);
loadFDRS(float(the_count), IT_T);
DataReading Salt;
Salt.d = s_salt;
Salt.id = READING_ID;
Salt.t = 6;
DataReading Soil;
Soil.d = s_soil;
Soil.id = READING_ID;
Soil.t = 5;
DataReading Bat;
Bat.d = s_battery;
Bat.id = READING_ID;
Bat.t = 50;
DataReading Count;
Count.d = float(the_count);
Count.id = READING_ID;
Count.t = 51;
DataReading thePacket[8];
thePacket[0] = Temp;
thePacket[1] = Hum;
thePacket[2] = Lux;
thePacket[3] = Pres;
thePacket[4] = Salt;
thePacket[5] = Soil;
thePacket[6] = Bat;
thePacket[7] = Count;
//Serial.println(sizeof(thePacket), DEC);
esp_now_send(broadcastAddress, (uint8_t *) &thePacket, sizeof(thePacket));
}
uint32_t readSalt() //Soil Electrodes: This code came from the LilyGo documentation.

View File

@ -0,0 +1,178 @@
// FARM DATA RELAY SYSTEM
//
// "fdrs_sensor.h"
//
// Developed by Timm Bogner (timmbogner@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
//
#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
#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;
uint8_t t;
} 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;
void beginFDRS() {
#ifdef DEBUG
Serial.begin(115200);
#endif
DBG("FDRS Sensor ID " + String(READING_ID) + " initializing...");
DBG(" Gateway: " + String (GTWY_MAC, HEX));
#ifdef POWER_CTRL
DBG("Powering up the sensor array!");
pinMode(POWER_CTRL, OUTPUT);
digitalWrite(POWER_CTRL, 1);
#endif
// Init ESP-NOW for either ESP8266 or ESP32 and set MAC address
#ifdef USE_ESPNOW
DBG("Initializing ESP-NOW!");
WiFi.mode(WIFI_STA);
WiFi.disconnect();
#if defined(ESP8266)
if (esp_now_init() != 0) {
return;
}
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
// Register peers
esp_now_add_peer(gatewayAddress, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
#elif defined(ESP32)
if (esp_now_init() != ESP_OK) {
DBG("Error initializing ESP-NOW");
return;
}
esp_now_peer_info_t peerInfo;
peerInfo.ifidx = WIFI_IF_STA;
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Register first peer
memcpy(peerInfo.peer_addr, gatewayAddress, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
DBG("Failed to add peer");
return;
}
#endif
DBG(" ESP-NOW Initialized.");
#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(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[3], &LoRaAddress, 2);
memcpy(&pkt[5], packet, len * sizeof(DataReading));
LoRa.beginPacket();
LoRa.write((uint8_t*)&pkt, sizeof(pkt));
LoRa.endPacket();
#endif
}
void sendFDRS() {
DBG("Sending FDRS Packet!");
#ifdef USE_ESPNOW
esp_now_send(gatewayAddress, (uint8_t *) &fdrsData, data_count * sizeof(DataReading));
delay(5);
DBG(" ESP-NOW sent.");
#endif
#ifdef USE_LORA
transmitLoRa(gtwyAddress, fdrsData, data_count);
DBG(" LoRa sent.");
#endif
data_count = 0;
}
void loadFDRS(float d, uint8_t t) {
DBG("Data loaded. Type: " + String(t));
if (data_count > espnow_size) sendFDRS();
DataReading dr;
dr.id = READING_ID;
dr.t = t;
dr.d = d;
fdrsData[data_count] = dr;
data_count++;
}
void sleepFDRS(int sleep_time) {
DBG("Sleepytime!");
#ifdef DEEP_SLEEP
DBG(" Deep sleeping.");
#ifdef ESP32
esp_sleep_enable_timer_wakeup(sleep_time * 1000000);
esp_deep_sleep_start();
#endif
#ifdef ESP8266
ESP.deepSleep(sleep_time * 1000000);
#endif
#endif
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

View File

@ -2,34 +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 43 //Unique ID for sensor module
#define GTWY_MAC 0x00 //Address of the nearest gateway
#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
//#define POWER_CTRL 14
#define MAC_PREFIX 0xAA, 0xBB, 0xCC, 0xDD, 0xEE
#define DEEP_SLEEP
#define USE_ESPNOW
#ifdef GLOBALS
#define FDRS_BAND GLOBAL_BAND
#define FDRS_SF GLOBAL_SF
#else
#define FDRS_BAND BAND
#define FDRS_SF SF
#endif
//#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 866E6
typedef struct __attribute__((packed)) DataReading {
float d;
uint16_t id;
uint8_t t;
} DataReading;
#ifdef DEBUG
#define DBG(a) (Serial.println(a))
#else
#define DBG(a)
#endif
#define STATUS_T 0 // Status
#define TEMP_T 1 // Temperature
@ -53,37 +53,40 @@ 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
#include <LoRa.h>
typedef struct __attribute__((packed)) DataReading {
float d;
uint16_t id;
uint8_t t;
} 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;
void beginFDRS() {
#ifdef USE_ESPNOW
WiFi.mode(WIFI_STA);
WiFi.disconnect();
#ifdef DEBUG
Serial.begin(115200);
#endif
DBG("FDRS Sensor ID " + String(READING_ID) + " initializing...");
DBG(" Gateway: " + String (GTWY_MAC, HEX));
#ifdef POWER_CTRL
DBG("Powering up the sensor array!");
pinMode(POWER_CTRL, OUTPUT);
digitalWrite(POWER_CTRL, 1);
#endif
// Init ESP-NOW for either ESP8266 or ESP32 and set MAC address
#ifdef USE_ESPNOW
DBG("Initializing ESP-NOW!");
WiFi.mode(WIFI_STA);
WiFi.disconnect();
#if defined(ESP8266)
if (esp_now_init() != 0) {
return;
@ -93,7 +96,7 @@ void beginFDRS() {
esp_now_add_peer(gatewayAddress, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
#elif defined(ESP32)
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
DBG("Error initializing ESP-NOW");
return;
}
esp_now_peer_info_t peerInfo;
@ -103,25 +106,31 @@ void beginFDRS() {
// Register first peer
memcpy(peerInfo.peer_addr, gatewayAddress, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
DBG("Failed to add peer");
return;
}
#endif
DBG(" ESP-NOW Initialized.");
#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();
@ -130,16 +139,20 @@ void transmitLoRa(uint8_t* mac, DataReading * packet, uint8_t len) {
#endif
}
void sendFDRS() {
DBG("Sending FDRS Packet!");
#ifdef USE_ESPNOW
esp_now_send(gatewayAddress, (uint8_t *) &fdrsData, data_count * sizeof(DataReading));
delay(5);
DBG(" ESP-NOW sent.");
#endif
#ifdef USE_LORA
transmitLoRa(gtwyAddress, fdrsData, data_count);
DBG(" LoRa sent.");
#endif
data_count = 0;
}
void loadFDRS(float d, uint8_t t) {
DBG("Data loaded. Type: " + String(t));
if (data_count > espnow_size) sendFDRS();
DataReading dr;
dr.id = READING_ID;
@ -147,10 +160,11 @@ void loadFDRS(float d, uint8_t t) {
dr.d = d;
fdrsData[data_count] = dr;
data_count++;
}
void sleepFDRS(int sleep_time) {
DBG("Sleepytime!");
#ifdef DEEP_SLEEP
DBG(" Deep sleeping.");
#ifdef ESP32
esp_sleep_enable_timer_wakeup(sleep_time * 1000000);
esp_deep_sleep_start();
@ -159,6 +173,6 @@ void sleepFDRS(int sleep_time) {
ESP.deepSleep(sleep_time * 1000000);
#endif
#endif
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

View File

@ -1,7 +0,0 @@
typedef struct DataReading {
float d;
uint16_t id;
uint8_t t;
} DataReading;

View File

@ -1,86 +0,0 @@
// FARM DATA RELAY SYSTEM
//
// MOTION SENSOR MODULE
//
// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
// Each reading is assigned a two-byte identifier along with a one-byte sensor type
// This code is unfinished, but may work.
//
#define READING_ID 32 //Unique integer for each data reading
#define GTWY_MAC 0x00 //Terminal MAC
#define MOTION_PIN 13
#include <ESP8266WiFi.h>
#include <espnow.h>
#include "DataReading.h"
unsigned int theCount = 0;
unsigned long nextCheck = 0;
boolean motionDetected = false;
unsigned long isr_time = millis();
uint8_t broadcastAddress[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, GTWY_MAC};
ICACHE_RAM_ATTR void detectsMovement() {
if (millis() > isr_time + 15000) {
nextCheck = millis();
isr_time = millis();
}
}
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
Serial.print("Last Packet Send Status: ");
if (sendStatus == 0) {
Serial.println("Delivery success");
}
else {
Serial.println("Delivery fail");
}
}
// Checks if motion was detected, sets LED HIGH and starts a timer
void setup() {
// Serial port for debugging purposes
Serial.begin(115200);
WiFi.mode(WIFI_STA);
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
esp_now_register_send_cb(OnDataSent);
// Register peer
esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
pinMode(MOTION_PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(MOTION_PIN), detectsMovement, RISING);
}
void loop() {
if ( millis() > nextCheck) {
checkDetector();
}
}
void checkDetector() {
Serial.println("Checking");
nextCheck = millis() + 15000;
motionDetected = digitalRead(MOTION_PIN);
if (motionDetected == HIGH) {
Serial.println("Motion Detected");
DataReading Motion;
Motion.d = theCount;
Motion.id = READING_ID;
Motion.t = 8;
esp_now_send(broadcastAddress, (uint8_t *) &Motion, sizeof(Motion));
} else {
}
}

View File

@ -5,12 +5,11 @@
| AHT20 | [ASAIR](http://www.aosong.com/userfiles/files/media/Data%20Sheet%20AHT20.pdf) | [Adafruit](https://github.com/adafruit/Adafruit_AHTX0) |
| BME280 | [Bosch](https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme280-ds002.pdf) | [Adafruit](https://github.com/adafruit/Adafruit_BME280_Library) |
| BMP280 | [Bosch](https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp280-ds001.pdf) | [Adafruit](https://github.com/adafruit/Adafruit_BMP280_Library) |
| DHT22 | [Aosong](https://www.sparkfun.com/datasheets/Sensors/Temperature/DHT22.pdf) |[beegee-tokyo](https://github.com/beegee-tokyo/DHTesp) |
| DHT22 | [Aosong](https://www.sparkfun.com/datasheets/Sensors/Temperature/DHT22.pdf) |[adafruit](https://github.com/adafruit/DHT-sensor-library) |
| DS18B20 | [Dallas](https://datasheets.maximintegrated.com/en/ds/DS18B20.pdf) | [milesburton](https://github.com/adafruit/Adafruit_AHTX0) |
| LilyGo HiGrow | [TTGO](http://www.lilygo.cn/prod_view.aspx?TypeId=50033&Id=1172) |
| Multifunction ESP8266 Sensor Board | [Phil Grant](https://github.com/gadjet/Multifunction-ESP8266-Sensor-board) |
| Motion Detector |
| Tipping Bucket Rain Gauge |

View File

@ -1,7 +0,0 @@
typedef struct DataReading {
float d;
uint16_t id;
uint8_t t;
} DataReading;

View File

@ -1,67 +0,0 @@
// FARM DATA RELAY SYSTEM
//
// TIPPING BUCKET RAINFALL SENSOR MODULE
//
// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
// Each reading is assigned a two-byte identifier along with a one-byte sensor type
//
#define READING_ID 45 //Unique integer for each data reading
#define GTWY_MAC 0x00 //Gateway MAC
#define REED_PIN 2
#include <ESP8266WiFi.h>
#include <espnow.h>
#include "DataReading.h"
unsigned int theCount = 0;
unsigned long lastTrigger = 0;
boolean clicked = false;
uint8_t broadcastAddress[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, GTWY_MAC};
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
Serial.print("Last Packet Send Status: ");
if (sendStatus == 0) {
Serial.println("Delivery success");
}
else {
Serial.println("Delivery fail");
}
}
// Checks if motion was detected, sets LED HIGH and starts a timer
ICACHE_RAM_ATTR void detectsMovement() {
clicked = true;
lastTrigger = millis();
}
void setup() {
// Serial port for debugging purposes
Serial.begin(115200);
WiFi.mode(WIFI_STA);
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
esp_now_register_send_cb(OnDataSent);
// Register peer
esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
pinMode(REED_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(REED_PIN), detectsMovement, FALLING);
}
void loop() {
if (clicked && millis() - lastTrigger > 100) {
theCount++;
Serial.print("CLICK.");
Serial.println(theCount);
clicked = false;
DataReading Rain;
Rain.d = theCount;
Rain.id = READING_ID;
Rain.t = 7;
esp_now_send(broadcastAddress, (uint8_t *) &Rain, sizeof(Rain));
}
}