mirror of
https://github.com/timmbogner/Farm-Data-Relay-System
synced 2024-11-10 07:10:42 +00:00
Merge pull request #44 from Gulpman/dev
Dev - cleaned up library structure and got rid of redundant files
This commit is contained in:
commit
449b565f0e
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,2 +1 @@
|
||||
Examples/FDRS_Install/fdrs_defaults.h
|
||||
Examples/FDRS_Install/fdrs_credentials.h
|
||||
|
||||
|
@ -1,158 +0,0 @@
|
||||
// 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"
|
||||
#include <FDRS_datatypes.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 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);
|
||||
#ifdef ESP32
|
||||
SPI.begin(SCK, MISO, MOSI, SS);
|
||||
#endif
|
||||
LoRa.setPins(SS, RST, DIO0);
|
||||
if (!LoRa.begin(FDRS_BAND)) {
|
||||
DBG("Unable to initialize LoRa!");
|
||||
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);
|
||||
}
|
@ -1,158 +0,0 @@
|
||||
// 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"
|
||||
#include <FDRS_datatypes.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 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);
|
||||
#ifdef ESP32
|
||||
SPI.begin(SCK, MISO, MOSI, SS);
|
||||
#endif
|
||||
LoRa.setPins(SS, RST, DIO0);
|
||||
if (!LoRa.begin(FDRS_BAND)) {
|
||||
DBG("Unable to initialize LoRa!");
|
||||
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);
|
||||
}
|
@ -1,572 +0,0 @@
|
||||
// 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)
|
||||
|
||||
#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_MQTT_PORT GLOBAL_MQTT_PORT
|
||||
#define FDRS_MQTT_USER GLOBAL_MQTT_USER
|
||||
#define FDRS_MQTT_PASS GLOBAL_MQTT_PASS
|
||||
#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_MQTT_PORT MQTT_PORT
|
||||
#define FDRS_MQTT_USER MQTT_USER
|
||||
#define FDRS_MQTT_PASS MQTT_PASS
|
||||
#define FDRS_BAND BAND
|
||||
#define FDRS_SF SF
|
||||
#endif
|
||||
|
||||
#if defined (MQTT_AUTH) || defined (GLOBAL_MQTT_AUTH)
|
||||
#define FDRS_MQTT_AUTH
|
||||
#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 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;
|
||||
uint8_t newData = 0;
|
||||
|
||||
DataReading ESPNOW1buffer[256];
|
||||
uint8_t lenESPNOW1 = 0;
|
||||
uint32_t timeESPNOW1 = 0;
|
||||
DataReading ESPNOW2buffer[256];
|
||||
uint8_t lenESPNOW2 = 0;
|
||||
uint32_t timeESPNOW2 = 0;
|
||||
DataReading ESPNOWGbuffer[256];
|
||||
uint8_t lenESPNOWG = 0;
|
||||
uint32_t timeESPNOWG = 0;
|
||||
DataReading SERIALbuffer[256];
|
||||
uint8_t lenSERIAL = 0;
|
||||
uint32_t timeSERIAL = 0;
|
||||
DataReading MQTTbuffer[256];
|
||||
uint8_t lenMQTT = 0;
|
||||
uint32_t timeMQTT = 0;
|
||||
DataReading LORAGbuffer[256];
|
||||
uint8_t lenLORAG = 0;
|
||||
uint32_t timeLORAG = 0;
|
||||
DataReading LORA1buffer[256];
|
||||
uint8_t lenLORA1 = 0;
|
||||
uint32_t timeLORA1 = 0;
|
||||
DataReading LORA2buffer[256];
|
||||
uint8_t lenLORA2 = 0;
|
||||
uint32_t timeLORA2 = 0;
|
||||
|
||||
WiFiClient espClient;
|
||||
#ifdef USE_LED
|
||||
CRGB leds[NUM_LEDS];
|
||||
#endif
|
||||
#ifdef USE_WIFI
|
||||
PubSubClient client(espClient);
|
||||
const char* ssid = FDRS_WIFI_SSID;
|
||||
const char* password = FDRS_WIFI_PASS;
|
||||
const char* mqtt_server = FDRS_MQTT_ADDR;
|
||||
const int mqtt_port = FDRS_MQTT_PORT;
|
||||
#endif
|
||||
#ifdef FDRS_MQTT_AUTH
|
||||
const char* mqtt_user = FDRS_MQTT_USER;
|
||||
const char* mqtt_pass = FDRS_MQTT_PASS;
|
||||
#else
|
||||
const char* mqtt_user = NULL;
|
||||
const char* mqtt_pass = NULL;
|
||||
#endif
|
||||
|
||||
// Set ESP-NOW send and receive callbacks for either ESP8266 or ESP32
|
||||
#if defined(ESP8266)
|
||||
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
|
||||
}
|
||||
void OnDataRecv(uint8_t* mac, uint8_t *incomingData, uint8_t len) {
|
||||
#elif defined(ESP32)
|
||||
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
|
||||
}
|
||||
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;
|
||||
}
|
||||
void getSerial() {
|
||||
String incomingString = UART_IF.readStringUntil('\n');
|
||||
DynamicJsonDocument doc(24576);
|
||||
DeserializationError error = deserializeJson(doc, incomingString);
|
||||
if (error) { // Test if parsing succeeds.
|
||||
// DBG("json parse err");
|
||||
// DBG(incomingString);
|
||||
return;
|
||||
} else {
|
||||
int s = doc.size();
|
||||
//UART_IF.println(s);
|
||||
for (int i = 0; i < s; i++) {
|
||||
theData[i].id = doc[i]["id"];
|
||||
theData[i].t = doc[i]["type"];
|
||||
theData[i].d = doc[i]["data"];
|
||||
}
|
||||
ln = s;
|
||||
newData = 4;
|
||||
DBG("Incoming Serial.");
|
||||
|
||||
}
|
||||
}
|
||||
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];
|
||||
}
|
||||
StaticJsonDocument<2048> doc;
|
||||
DeserializationError error = deserializeJson(doc, incomingString);
|
||||
if (error) { // Test if parsing succeeds.
|
||||
DBG("json parse err");
|
||||
DBG(incomingString);
|
||||
return;
|
||||
} else {
|
||||
int s = doc.size();
|
||||
//UART_IF.println(s);
|
||||
for (int i = 0; i < s; i++) {
|
||||
theData[i].id = doc[i]["id"];
|
||||
theData[i].t = doc[i]["type"];
|
||||
theData[i].d = doc[i]["data"];
|
||||
}
|
||||
ln = s;
|
||||
newData = 5;
|
||||
DBG("Incoming MQTT.");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void getLoRa() {
|
||||
#ifdef USE_LORA
|
||||
int packetSize = LoRa.parsePacket();
|
||||
if (packetSize) {
|
||||
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 = 7; //Check if it is from a registered sender
|
||||
else if (memcmp(&incLORAMAC, &LoRa2, 2) == 0) newData = 8;
|
||||
else newData = 6;
|
||||
ln = (packetSize - 5) / sizeof(DataReading);
|
||||
newData = 6;
|
||||
DBG("Incoming LoRa.");
|
||||
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void sendESPNOW(uint8_t address) {
|
||||
DBG("Sending ESP-NOW.");
|
||||
uint8_t NEWPEER[] = {MAC_PREFIX, address};
|
||||
#if defined(ESP32)
|
||||
esp_now_peer_info_t peerInfo;
|
||||
peerInfo.ifidx = WIFI_IF_STA;
|
||||
peerInfo.channel = 0;
|
||||
peerInfo.encrypt = false;
|
||||
memcpy(peerInfo.peer_addr, NEWPEER, 6);
|
||||
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||
DBG("Failed to add peer");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
DataReading thePacket[ln];
|
||||
int j = 0;
|
||||
for (int i = 0; i < ln; i++) {
|
||||
if ( j > espnow_size) {
|
||||
j = 0;
|
||||
esp_now_send(NEWPEER, (uint8_t *) &thePacket, sizeof(thePacket));
|
||||
}
|
||||
thePacket[j] = theData[i];
|
||||
j++;
|
||||
}
|
||||
esp_now_send(NEWPEER, (uint8_t *) &thePacket, j * sizeof(DataReading));
|
||||
esp_now_del_peer(NEWPEER);
|
||||
}
|
||||
|
||||
void sendSerial() {
|
||||
DBG("Sending Serial.");
|
||||
DynamicJsonDocument doc(24576);
|
||||
for (int i = 0; i < ln; i++) {
|
||||
doc[i]["id"] = theData[i].id;
|
||||
doc[i]["type"] = theData[i].t;
|
||||
doc[i]["data"] = theData[i].d;
|
||||
}
|
||||
serializeJson(doc, UART_IF);
|
||||
UART_IF.println();
|
||||
|
||||
#ifndef ESP8266
|
||||
serializeJson(doc, Serial);
|
||||
Serial.println();
|
||||
#endif
|
||||
|
||||
}
|
||||
void sendMQTT() {
|
||||
#ifdef USE_WIFI
|
||||
DBG("Sending MQTT.");
|
||||
DynamicJsonDocument doc(24576);
|
||||
for (int i = 0; i < ln; i++) {
|
||||
doc[i]["id"] = theData[i].id;
|
||||
doc[i]["type"] = theData[i].t;
|
||||
doc[i]["data"] = theData[i].d;
|
||||
}
|
||||
String outgoingString;
|
||||
serializeJson(doc, outgoingString);
|
||||
client.publish(TOPIC_DATA, (char*) outgoingString.c_str());
|
||||
#endif
|
||||
}
|
||||
|
||||
void bufferESPNOW(uint8_t interface) {
|
||||
DBG("Buffering ESP-NOW.");
|
||||
|
||||
switch (interface) {
|
||||
case 0:
|
||||
for (int i = 0; i < ln; i++) {
|
||||
ESPNOWGbuffer[lenESPNOWG + i] = theData[i];
|
||||
}
|
||||
lenESPNOWG += ln;
|
||||
break;
|
||||
case 1:
|
||||
for (int i = 0; i < ln; i++) {
|
||||
ESPNOW1buffer[lenESPNOW1 + i] = theData[i];
|
||||
}
|
||||
lenESPNOW1 += ln;
|
||||
break;
|
||||
case 2:
|
||||
for (int i = 0; i < ln; i++) {
|
||||
ESPNOW2buffer[lenESPNOW2 + i] = theData[i];
|
||||
}
|
||||
lenESPNOW2 += ln;
|
||||
break;
|
||||
}
|
||||
}
|
||||
void bufferSerial() {
|
||||
DBG("Buffering Serial.");
|
||||
for (int i = 0; i < ln; i++) {
|
||||
SERIALbuffer[lenSERIAL + i] = theData[i];
|
||||
}
|
||||
lenSERIAL += ln;
|
||||
//UART_IF.println("SENDSERIAL:" + String(lenSERIAL) + " ");
|
||||
}
|
||||
void bufferMQTT() {
|
||||
DBG("Buffering MQTT.");
|
||||
for (int i = 0; i < ln; i++) {
|
||||
MQTTbuffer[lenMQTT + i] = theData[i];
|
||||
}
|
||||
lenMQTT += ln;
|
||||
}
|
||||
//void bufferLoRa() {
|
||||
// for (int i = 0; i < ln; i++) {
|
||||
// LORAbuffer[lenLORA + i] = theData[i];
|
||||
// }
|
||||
// lenLORA += ln;
|
||||
//}
|
||||
void bufferLoRa(uint8_t interface) {
|
||||
DBG("Buffering LoRa.");
|
||||
switch (interface) {
|
||||
case 0:
|
||||
for (int i = 0; i < ln; i++) {
|
||||
LORAGbuffer[lenLORAG + i] = theData[i];
|
||||
}
|
||||
lenLORAG += ln;
|
||||
break;
|
||||
case 1:
|
||||
for (int i = 0; i < ln; i++) {
|
||||
LORA1buffer[lenLORA1 + i] = theData[i];
|
||||
}
|
||||
lenLORA1 += ln;
|
||||
break;
|
||||
case 2:
|
||||
for (int i = 0; i < ln; i++) {
|
||||
LORA2buffer[lenLORA2 + i] = theData[i];
|
||||
}
|
||||
lenLORA2 += ln;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void releaseESPNOW(uint8_t interface) {
|
||||
DBG("Releasing ESP-NOW.");
|
||||
switch (interface) {
|
||||
case 0:
|
||||
{
|
||||
DataReading thePacket[espnow_size];
|
||||
int j = 0;
|
||||
for (int i = 0; i < lenESPNOWG; i++) {
|
||||
if ( j > espnow_size) {
|
||||
j = 0;
|
||||
esp_now_send(broadcast_mac, (uint8_t *) &thePacket, sizeof(thePacket));
|
||||
}
|
||||
thePacket[j] = ESPNOWGbuffer[i];
|
||||
j++;
|
||||
}
|
||||
esp_now_send(broadcast_mac, (uint8_t *) &thePacket, j * sizeof(DataReading));
|
||||
lenESPNOWG = 0;
|
||||
break;
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
DataReading thePacket[espnow_size];
|
||||
int j = 0;
|
||||
for (int i = 0; i < lenESPNOW1; i++) {
|
||||
if ( j > espnow_size) {
|
||||
j = 0;
|
||||
esp_now_send(ESPNOW1, (uint8_t *) &thePacket, sizeof(thePacket));
|
||||
}
|
||||
thePacket[j] = ESPNOW1buffer[i];
|
||||
j++;
|
||||
}
|
||||
esp_now_send(ESPNOW1, (uint8_t *) &thePacket, j * sizeof(DataReading));
|
||||
lenESPNOW1 = 0;
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
DataReading thePacket[espnow_size];
|
||||
int j = 0;
|
||||
for (int i = 0; i < lenESPNOW2; i++) {
|
||||
if ( j > espnow_size) {
|
||||
j = 0;
|
||||
esp_now_send(ESPNOW2, (uint8_t *) &thePacket, sizeof(thePacket));
|
||||
}
|
||||
thePacket[j] = ESPNOW2buffer[i];
|
||||
j++;
|
||||
}
|
||||
esp_now_send(ESPNOW2, (uint8_t *) &thePacket, j * sizeof(DataReading));
|
||||
lenESPNOW2 = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
#ifdef USE_LORA
|
||||
void transmitLoRa(uint8_t* mac, DataReading * packet, uint8_t len) {
|
||||
DBG("Transmitting LoRa.");
|
||||
|
||||
uint8_t pkt[5 + (len * sizeof(DataReading))];
|
||||
memcpy(&pkt, mac, 3);
|
||||
memcpy(&pkt[3], &selfAddress[4], 2);
|
||||
memcpy(&pkt[5], packet, len * sizeof(DataReading));
|
||||
LoRa.beginPacket();
|
||||
LoRa.write((uint8_t*)&pkt, sizeof(pkt));
|
||||
LoRa.endPacket();
|
||||
}
|
||||
#endif
|
||||
|
||||
void releaseLoRa(uint8_t interface) {
|
||||
#ifdef USE_LORA
|
||||
DBG("Releasing LoRa.");
|
||||
|
||||
switch (interface) {
|
||||
case 0:
|
||||
{
|
||||
DataReading thePacket[lora_size];
|
||||
int j = 0;
|
||||
for (int i = 0; i < lenLORAG; i++) {
|
||||
if ( j > lora_size) {
|
||||
j = 0;
|
||||
transmitLoRa(broadcast_mac, thePacket, j);
|
||||
}
|
||||
thePacket[j] = LORAGbuffer[i];
|
||||
j++;
|
||||
}
|
||||
transmitLoRa(broadcast_mac, thePacket, j);
|
||||
lenLORAG = 0;
|
||||
|
||||
break;
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
DataReading thePacket[lora_size];
|
||||
int j = 0;
|
||||
for (int i = 0; i < lenLORA1; i++) {
|
||||
if ( j > lora_size) {
|
||||
j = 0;
|
||||
transmitLoRa(LoRa1, thePacket, j);
|
||||
}
|
||||
thePacket[j] = LORA1buffer[i];
|
||||
j++;
|
||||
}
|
||||
transmitLoRa(LoRa1, thePacket, j);
|
||||
lenLORA1 = 0;
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
DataReading thePacket[lora_size];
|
||||
int j = 0;
|
||||
for (int i = 0; i < lenLORA2; i++) {
|
||||
if ( j > lora_size) {
|
||||
j = 0;
|
||||
transmitLoRa(LoRa2, thePacket, j);
|
||||
}
|
||||
thePacket[j] = LORA2buffer[i];
|
||||
j++;
|
||||
}
|
||||
transmitLoRa(LoRa2, thePacket, j);
|
||||
lenLORA2 = 0;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
void releaseSerial() {
|
||||
DBG("Releasing Serial.");
|
||||
DynamicJsonDocument doc(24576);
|
||||
for (int i = 0; i < lenSERIAL; i++) {
|
||||
doc[i]["id"] = SERIALbuffer[i].id;
|
||||
doc[i]["type"] = SERIALbuffer[i].t;
|
||||
doc[i]["data"] = SERIALbuffer[i].d;
|
||||
}
|
||||
serializeJson(doc, UART_IF);
|
||||
UART_IF.println();
|
||||
lenSERIAL = 0;
|
||||
}
|
||||
void releaseMQTT() {
|
||||
#ifdef USE_WIFI
|
||||
DBG("Releasing MQTT.");
|
||||
DynamicJsonDocument doc(24576);
|
||||
for (int i = 0; i < lenMQTT; i++) {
|
||||
doc[i]["id"] = MQTTbuffer[i].id;
|
||||
doc[i]["type"] = MQTTbuffer[i].t;
|
||||
doc[i]["data"] = MQTTbuffer[i].d;
|
||||
}
|
||||
String outgoingString;
|
||||
serializeJson(doc, outgoingString);
|
||||
client.publish(TOPIC_DATA, (char*) outgoingString.c_str());
|
||||
lenMQTT = 0;
|
||||
#endif
|
||||
}
|
||||
void reconnect() {
|
||||
#ifdef USE_WIFI
|
||||
// Loop until reconnected
|
||||
while (!client.connected()) {
|
||||
// Attempt to connect
|
||||
if (client.connect("FDRS_GATEWAY", mqtt_user, mqtt_pass)) {
|
||||
// Subscribe
|
||||
client.subscribe(TOPIC_COMMAND);
|
||||
} else {
|
||||
DBG("Connecting MQTT.");
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
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
|
||||
#if defined(ESP8266)
|
||||
wifi_set_macaddr(STATION_IF, selfAddress);
|
||||
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 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) {
|
||||
DBG("Error initializing ESP-NOW");
|
||||
return;
|
||||
}
|
||||
esp_now_register_send_cb(OnDataSent);
|
||||
esp_now_register_recv_cb(OnDataRecv);
|
||||
|
||||
peerInfo.channel = 0;
|
||||
peerInfo.encrypt = false;
|
||||
// Register first peer
|
||||
|
||||
memcpy(peerInfo.peer_addr, broadcast_mac, 6);
|
||||
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||
DBG("Failed to add peer bcast");
|
||||
return;
|
||||
}
|
||||
#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.");
|
||||
}
|
@ -1,572 +0,0 @@
|
||||
// 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)
|
||||
|
||||
#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_MQTT_PORT GLOBAL_MQTT_PORT
|
||||
#define FDRS_MQTT_USER GLOBAL_MQTT_USER
|
||||
#define FDRS_MQTT_PASS GLOBAL_MQTT_PASS
|
||||
#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_MQTT_PORT MQTT_PORT
|
||||
#define FDRS_MQTT_USER MQTT_USER
|
||||
#define FDRS_MQTT_PASS MQTT_PASS
|
||||
#define FDRS_BAND BAND
|
||||
#define FDRS_SF SF
|
||||
#endif
|
||||
|
||||
#if defined (MQTT_AUTH) || defined (GLOBAL_MQTT_AUTH)
|
||||
#define FDRS_MQTT_AUTH
|
||||
#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 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;
|
||||
uint8_t newData = 0;
|
||||
|
||||
DataReading ESPNOW1buffer[256];
|
||||
uint8_t lenESPNOW1 = 0;
|
||||
uint32_t timeESPNOW1 = 0;
|
||||
DataReading ESPNOW2buffer[256];
|
||||
uint8_t lenESPNOW2 = 0;
|
||||
uint32_t timeESPNOW2 = 0;
|
||||
DataReading ESPNOWGbuffer[256];
|
||||
uint8_t lenESPNOWG = 0;
|
||||
uint32_t timeESPNOWG = 0;
|
||||
DataReading SERIALbuffer[256];
|
||||
uint8_t lenSERIAL = 0;
|
||||
uint32_t timeSERIAL = 0;
|
||||
DataReading MQTTbuffer[256];
|
||||
uint8_t lenMQTT = 0;
|
||||
uint32_t timeMQTT = 0;
|
||||
DataReading LORAGbuffer[256];
|
||||
uint8_t lenLORAG = 0;
|
||||
uint32_t timeLORAG = 0;
|
||||
DataReading LORA1buffer[256];
|
||||
uint8_t lenLORA1 = 0;
|
||||
uint32_t timeLORA1 = 0;
|
||||
DataReading LORA2buffer[256];
|
||||
uint8_t lenLORA2 = 0;
|
||||
uint32_t timeLORA2 = 0;
|
||||
|
||||
WiFiClient espClient;
|
||||
#ifdef USE_LED
|
||||
CRGB leds[NUM_LEDS];
|
||||
#endif
|
||||
#ifdef USE_WIFI
|
||||
PubSubClient client(espClient);
|
||||
const char* ssid = FDRS_WIFI_SSID;
|
||||
const char* password = FDRS_WIFI_PASS;
|
||||
const char* mqtt_server = FDRS_MQTT_ADDR;
|
||||
const int mqtt_port = FDRS_MQTT_PORT;
|
||||
#endif
|
||||
#ifdef FDRS_MQTT_AUTH
|
||||
const char* mqtt_user = FDRS_MQTT_USER;
|
||||
const char* mqtt_pass = FDRS_MQTT_PASS;
|
||||
#else
|
||||
const char* mqtt_user = NULL;
|
||||
const char* mqtt_pass = NULL;
|
||||
#endif
|
||||
|
||||
// Set ESP-NOW send and receive callbacks for either ESP8266 or ESP32
|
||||
#if defined(ESP8266)
|
||||
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
|
||||
}
|
||||
void OnDataRecv(uint8_t* mac, uint8_t *incomingData, uint8_t len) {
|
||||
#elif defined(ESP32)
|
||||
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
|
||||
}
|
||||
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;
|
||||
}
|
||||
void getSerial() {
|
||||
String incomingString = UART_IF.readStringUntil('\n');
|
||||
DynamicJsonDocument doc(24576);
|
||||
DeserializationError error = deserializeJson(doc, incomingString);
|
||||
if (error) { // Test if parsing succeeds.
|
||||
// DBG("json parse err");
|
||||
// DBG(incomingString);
|
||||
return;
|
||||
} else {
|
||||
int s = doc.size();
|
||||
//UART_IF.println(s);
|
||||
for (int i = 0; i < s; i++) {
|
||||
theData[i].id = doc[i]["id"];
|
||||
theData[i].t = doc[i]["type"];
|
||||
theData[i].d = doc[i]["data"];
|
||||
}
|
||||
ln = s;
|
||||
newData = 4;
|
||||
DBG("Incoming Serial.");
|
||||
|
||||
}
|
||||
}
|
||||
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];
|
||||
}
|
||||
StaticJsonDocument<2048> doc;
|
||||
DeserializationError error = deserializeJson(doc, incomingString);
|
||||
if (error) { // Test if parsing succeeds.
|
||||
DBG("json parse err");
|
||||
DBG(incomingString);
|
||||
return;
|
||||
} else {
|
||||
int s = doc.size();
|
||||
//UART_IF.println(s);
|
||||
for (int i = 0; i < s; i++) {
|
||||
theData[i].id = doc[i]["id"];
|
||||
theData[i].t = doc[i]["type"];
|
||||
theData[i].d = doc[i]["data"];
|
||||
}
|
||||
ln = s;
|
||||
newData = 5;
|
||||
DBG("Incoming MQTT.");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void getLoRa() {
|
||||
#ifdef USE_LORA
|
||||
int packetSize = LoRa.parsePacket();
|
||||
if (packetSize) {
|
||||
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 = 7; //Check if it is from a registered sender
|
||||
else if (memcmp(&incLORAMAC, &LoRa2, 2) == 0) newData = 8;
|
||||
else newData = 6;
|
||||
ln = (packetSize - 5) / sizeof(DataReading);
|
||||
newData = 6;
|
||||
DBG("Incoming LoRa.");
|
||||
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void sendESPNOW(uint8_t address) {
|
||||
DBG("Sending ESP-NOW.");
|
||||
uint8_t NEWPEER[] = {MAC_PREFIX, address};
|
||||
#if defined(ESP32)
|
||||
esp_now_peer_info_t peerInfo;
|
||||
peerInfo.ifidx = WIFI_IF_STA;
|
||||
peerInfo.channel = 0;
|
||||
peerInfo.encrypt = false;
|
||||
memcpy(peerInfo.peer_addr, NEWPEER, 6);
|
||||
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||
DBG("Failed to add peer");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
DataReading thePacket[ln];
|
||||
int j = 0;
|
||||
for (int i = 0; i < ln; i++) {
|
||||
if ( j > espnow_size) {
|
||||
j = 0;
|
||||
esp_now_send(NEWPEER, (uint8_t *) &thePacket, sizeof(thePacket));
|
||||
}
|
||||
thePacket[j] = theData[i];
|
||||
j++;
|
||||
}
|
||||
esp_now_send(NEWPEER, (uint8_t *) &thePacket, j * sizeof(DataReading));
|
||||
esp_now_del_peer(NEWPEER);
|
||||
}
|
||||
|
||||
void sendSerial() {
|
||||
DBG("Sending Serial.");
|
||||
DynamicJsonDocument doc(24576);
|
||||
for (int i = 0; i < ln; i++) {
|
||||
doc[i]["id"] = theData[i].id;
|
||||
doc[i]["type"] = theData[i].t;
|
||||
doc[i]["data"] = theData[i].d;
|
||||
}
|
||||
serializeJson(doc, UART_IF);
|
||||
UART_IF.println();
|
||||
|
||||
#ifndef ESP8266
|
||||
serializeJson(doc, Serial);
|
||||
Serial.println();
|
||||
#endif
|
||||
|
||||
}
|
||||
void sendMQTT() {
|
||||
#ifdef USE_WIFI
|
||||
DBG("Sending MQTT.");
|
||||
DynamicJsonDocument doc(24576);
|
||||
for (int i = 0; i < ln; i++) {
|
||||
doc[i]["id"] = theData[i].id;
|
||||
doc[i]["type"] = theData[i].t;
|
||||
doc[i]["data"] = theData[i].d;
|
||||
}
|
||||
String outgoingString;
|
||||
serializeJson(doc, outgoingString);
|
||||
client.publish(TOPIC_DATA, (char*) outgoingString.c_str());
|
||||
#endif
|
||||
}
|
||||
|
||||
void bufferESPNOW(uint8_t interface) {
|
||||
DBG("Buffering ESP-NOW.");
|
||||
|
||||
switch (interface) {
|
||||
case 0:
|
||||
for (int i = 0; i < ln; i++) {
|
||||
ESPNOWGbuffer[lenESPNOWG + i] = theData[i];
|
||||
}
|
||||
lenESPNOWG += ln;
|
||||
break;
|
||||
case 1:
|
||||
for (int i = 0; i < ln; i++) {
|
||||
ESPNOW1buffer[lenESPNOW1 + i] = theData[i];
|
||||
}
|
||||
lenESPNOW1 += ln;
|
||||
break;
|
||||
case 2:
|
||||
for (int i = 0; i < ln; i++) {
|
||||
ESPNOW2buffer[lenESPNOW2 + i] = theData[i];
|
||||
}
|
||||
lenESPNOW2 += ln;
|
||||
break;
|
||||
}
|
||||
}
|
||||
void bufferSerial() {
|
||||
DBG("Buffering Serial.");
|
||||
for (int i = 0; i < ln; i++) {
|
||||
SERIALbuffer[lenSERIAL + i] = theData[i];
|
||||
}
|
||||
lenSERIAL += ln;
|
||||
//UART_IF.println("SENDSERIAL:" + String(lenSERIAL) + " ");
|
||||
}
|
||||
void bufferMQTT() {
|
||||
DBG("Buffering MQTT.");
|
||||
for (int i = 0; i < ln; i++) {
|
||||
MQTTbuffer[lenMQTT + i] = theData[i];
|
||||
}
|
||||
lenMQTT += ln;
|
||||
}
|
||||
//void bufferLoRa() {
|
||||
// for (int i = 0; i < ln; i++) {
|
||||
// LORAbuffer[lenLORA + i] = theData[i];
|
||||
// }
|
||||
// lenLORA += ln;
|
||||
//}
|
||||
void bufferLoRa(uint8_t interface) {
|
||||
DBG("Buffering LoRa.");
|
||||
switch (interface) {
|
||||
case 0:
|
||||
for (int i = 0; i < ln; i++) {
|
||||
LORAGbuffer[lenLORAG + i] = theData[i];
|
||||
}
|
||||
lenLORAG += ln;
|
||||
break;
|
||||
case 1:
|
||||
for (int i = 0; i < ln; i++) {
|
||||
LORA1buffer[lenLORA1 + i] = theData[i];
|
||||
}
|
||||
lenLORA1 += ln;
|
||||
break;
|
||||
case 2:
|
||||
for (int i = 0; i < ln; i++) {
|
||||
LORA2buffer[lenLORA2 + i] = theData[i];
|
||||
}
|
||||
lenLORA2 += ln;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void releaseESPNOW(uint8_t interface) {
|
||||
DBG("Releasing ESP-NOW.");
|
||||
switch (interface) {
|
||||
case 0:
|
||||
{
|
||||
DataReading thePacket[espnow_size];
|
||||
int j = 0;
|
||||
for (int i = 0; i < lenESPNOWG; i++) {
|
||||
if ( j > espnow_size) {
|
||||
j = 0;
|
||||
esp_now_send(broadcast_mac, (uint8_t *) &thePacket, sizeof(thePacket));
|
||||
}
|
||||
thePacket[j] = ESPNOWGbuffer[i];
|
||||
j++;
|
||||
}
|
||||
esp_now_send(broadcast_mac, (uint8_t *) &thePacket, j * sizeof(DataReading));
|
||||
lenESPNOWG = 0;
|
||||
break;
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
DataReading thePacket[espnow_size];
|
||||
int j = 0;
|
||||
for (int i = 0; i < lenESPNOW1; i++) {
|
||||
if ( j > espnow_size) {
|
||||
j = 0;
|
||||
esp_now_send(ESPNOW1, (uint8_t *) &thePacket, sizeof(thePacket));
|
||||
}
|
||||
thePacket[j] = ESPNOW1buffer[i];
|
||||
j++;
|
||||
}
|
||||
esp_now_send(ESPNOW1, (uint8_t *) &thePacket, j * sizeof(DataReading));
|
||||
lenESPNOW1 = 0;
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
DataReading thePacket[espnow_size];
|
||||
int j = 0;
|
||||
for (int i = 0; i < lenESPNOW2; i++) {
|
||||
if ( j > espnow_size) {
|
||||
j = 0;
|
||||
esp_now_send(ESPNOW2, (uint8_t *) &thePacket, sizeof(thePacket));
|
||||
}
|
||||
thePacket[j] = ESPNOW2buffer[i];
|
||||
j++;
|
||||
}
|
||||
esp_now_send(ESPNOW2, (uint8_t *) &thePacket, j * sizeof(DataReading));
|
||||
lenESPNOW2 = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
#ifdef USE_LORA
|
||||
void transmitLoRa(uint8_t* mac, DataReading * packet, uint8_t len) {
|
||||
DBG("Transmitting LoRa.");
|
||||
|
||||
uint8_t pkt[5 + (len * sizeof(DataReading))];
|
||||
memcpy(&pkt, mac, 3);
|
||||
memcpy(&pkt[3], &selfAddress[4], 2);
|
||||
memcpy(&pkt[5], packet, len * sizeof(DataReading));
|
||||
LoRa.beginPacket();
|
||||
LoRa.write((uint8_t*)&pkt, sizeof(pkt));
|
||||
LoRa.endPacket();
|
||||
}
|
||||
#endif
|
||||
|
||||
void releaseLoRa(uint8_t interface) {
|
||||
#ifdef USE_LORA
|
||||
DBG("Releasing LoRa.");
|
||||
|
||||
switch (interface) {
|
||||
case 0:
|
||||
{
|
||||
DataReading thePacket[lora_size];
|
||||
int j = 0;
|
||||
for (int i = 0; i < lenLORAG; i++) {
|
||||
if ( j > lora_size) {
|
||||
j = 0;
|
||||
transmitLoRa(broadcast_mac, thePacket, j);
|
||||
}
|
||||
thePacket[j] = LORAGbuffer[i];
|
||||
j++;
|
||||
}
|
||||
transmitLoRa(broadcast_mac, thePacket, j);
|
||||
lenLORAG = 0;
|
||||
|
||||
break;
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
DataReading thePacket[lora_size];
|
||||
int j = 0;
|
||||
for (int i = 0; i < lenLORA1; i++) {
|
||||
if ( j > lora_size) {
|
||||
j = 0;
|
||||
transmitLoRa(LoRa1, thePacket, j);
|
||||
}
|
||||
thePacket[j] = LORA1buffer[i];
|
||||
j++;
|
||||
}
|
||||
transmitLoRa(LoRa1, thePacket, j);
|
||||
lenLORA1 = 0;
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
DataReading thePacket[lora_size];
|
||||
int j = 0;
|
||||
for (int i = 0; i < lenLORA2; i++) {
|
||||
if ( j > lora_size) {
|
||||
j = 0;
|
||||
transmitLoRa(LoRa2, thePacket, j);
|
||||
}
|
||||
thePacket[j] = LORA2buffer[i];
|
||||
j++;
|
||||
}
|
||||
transmitLoRa(LoRa2, thePacket, j);
|
||||
lenLORA2 = 0;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
void releaseSerial() {
|
||||
DBG("Releasing Serial.");
|
||||
DynamicJsonDocument doc(24576);
|
||||
for (int i = 0; i < lenSERIAL; i++) {
|
||||
doc[i]["id"] = SERIALbuffer[i].id;
|
||||
doc[i]["type"] = SERIALbuffer[i].t;
|
||||
doc[i]["data"] = SERIALbuffer[i].d;
|
||||
}
|
||||
serializeJson(doc, UART_IF);
|
||||
UART_IF.println();
|
||||
lenSERIAL = 0;
|
||||
}
|
||||
void releaseMQTT() {
|
||||
#ifdef USE_WIFI
|
||||
DBG("Releasing MQTT.");
|
||||
DynamicJsonDocument doc(24576);
|
||||
for (int i = 0; i < lenMQTT; i++) {
|
||||
doc[i]["id"] = MQTTbuffer[i].id;
|
||||
doc[i]["type"] = MQTTbuffer[i].t;
|
||||
doc[i]["data"] = MQTTbuffer[i].d;
|
||||
}
|
||||
String outgoingString;
|
||||
serializeJson(doc, outgoingString);
|
||||
client.publish(TOPIC_DATA, (char*) outgoingString.c_str());
|
||||
lenMQTT = 0;
|
||||
#endif
|
||||
}
|
||||
void reconnect() {
|
||||
#ifdef USE_WIFI
|
||||
// Loop until reconnected
|
||||
while (!client.connected()) {
|
||||
// Attempt to connect
|
||||
if (client.connect("FDRS_GATEWAY", mqtt_user, mqtt_pass)) {
|
||||
// Subscribe
|
||||
client.subscribe(TOPIC_COMMAND);
|
||||
} else {
|
||||
DBG("Connecting MQTT.");
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
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
|
||||
#if defined(ESP8266)
|
||||
wifi_set_macaddr(STATION_IF, selfAddress);
|
||||
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 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) {
|
||||
DBG("Error initializing ESP-NOW");
|
||||
return;
|
||||
}
|
||||
esp_now_register_send_cb(OnDataSent);
|
||||
esp_now_register_recv_cb(OnDataRecv);
|
||||
|
||||
peerInfo.channel = 0;
|
||||
peerInfo.encrypt = false;
|
||||
// Register first peer
|
||||
|
||||
memcpy(peerInfo.peer_addr, broadcast_mac, 6);
|
||||
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||
DBG("Failed to add peer bcast");
|
||||
return;
|
||||
}
|
||||
#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.");
|
||||
}
|
@ -1,157 +0,0 @@
|
||||
// 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"
|
||||
#include <FDRS_datatypes.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 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);
|
||||
#ifdef ESP32
|
||||
SPI.begin(SCK, MISO, MOSI, SS);
|
||||
#endif
|
||||
LoRa.setPins(SS, RST, DIO0);
|
||||
if (!LoRa.begin(FDRS_BAND)) {
|
||||
DBG("Unable to initialize LoRa!");
|
||||
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);
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
|
||||
typedef struct DataReading {
|
||||
float d;
|
||||
uint16_t id;
|
||||
uint8_t t;
|
||||
|
||||
} DataReading;
|
||||
|
||||
typedef struct DataReading {
|
||||
float d;
|
||||
uint16_t id;
|
||||
uint8_t t;
|
||||
|
||||
} DataReading;
|
@ -1,161 +1,161 @@
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// FastLED Controller
|
||||
//
|
||||
// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
|
||||
// Rest in Peace Dan Garcia, creator of FastLED.
|
||||
//
|
||||
#include <FastLED.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
|
||||
|
||||
|
||||
#define READING_ID 118 //Unique ID for controller
|
||||
#define GTWY_MAC 0x00 //Gateway MAC
|
||||
|
||||
#define DATA_PIN 4
|
||||
#define BAT_ADC 33
|
||||
#define POWER_CTRL 5
|
||||
#define NUM_LEDS 24
|
||||
|
||||
|
||||
uint8_t broadcastAddress[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, GTWY_MAC};
|
||||
CRGB leds[NUM_LEDS];
|
||||
|
||||
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;
|
||||
int wait_time = 0;
|
||||
|
||||
|
||||
#if defined(ESP8266)
|
||||
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) {
|
||||
#elif defined(ESP32)
|
||||
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");
|
||||
}
|
||||
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
|
||||
#endif
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float readBattery()
|
||||
{
|
||||
int vref = 1100;
|
||||
uint16_t volt = analogRead(BAT_ADC);
|
||||
Serial.println(volt);
|
||||
float battery_voltage = ((float)volt / 4095.0) * 2.0 * 3.3 * (vref);
|
||||
return battery_voltage;
|
||||
}
|
||||
|
||||
void setup() {
|
||||
|
||||
Serial.begin(115200);
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.disconnect();
|
||||
#if defined(ESP8266)
|
||||
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);
|
||||
#elif defined(ESP32)
|
||||
if (esp_now_init() != ESP_OK) {
|
||||
Serial.println("Error initializing ESP-NOW");
|
||||
return;
|
||||
}
|
||||
esp_now_register_send_cb(OnDataSent);
|
||||
esp_now_register_recv_cb(OnDataRecv);
|
||||
esp_now_peer_info_t peerInfo;
|
||||
peerInfo.channel = 0;
|
||||
peerInfo.encrypt = false;
|
||||
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
|
||||
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||
Serial.println("Failed to add peer");
|
||||
return;
|
||||
}
|
||||
pinMode(POWER_CTRL, OUTPUT);
|
||||
digitalWrite(POWER_CTRL, 1);
|
||||
#endif
|
||||
|
||||
Serial.println();
|
||||
Serial.println("FARM DATA RELAY SYSTEM :: Pretty Lantern FastLED Module");
|
||||
Serial.println("Thank You DAN GARCIA!");
|
||||
Serial.println("MAC: " + WiFi.macAddress());
|
||||
Serial.println("COLOR ID: " + String(COLOR_ID));
|
||||
Serial.println("BRIGHT ID: " + String(BRIGHT_ID));
|
||||
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
|
||||
FastLED.show();
|
||||
fill_solid(leds, NUM_LEDS, CRGB::Red); FastLED.show();
|
||||
delay(250);
|
||||
fill_solid(leds, NUM_LEDS, CRGB::Blue); FastLED.show();
|
||||
delay(250);
|
||||
fill_solid(leds, NUM_LEDS, CRGB::Green); FastLED.show();
|
||||
delay(250);
|
||||
fill_solid(leds, NUM_LEDS, CRGB::Black); FastLED.show();
|
||||
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (newData) {
|
||||
newData = false;
|
||||
fill_solid(leds, NUM_LEDS, CHSV(the_color, 255, the_bright)); FastLED.show();
|
||||
}
|
||||
// if (millis() > wait_time) {
|
||||
// wait_time = wait_time + 30 * 1000;
|
||||
// DataReading theVoltage;
|
||||
// theVoltage.d = readBattery();
|
||||
// theVoltage.id = BAT_ID;
|
||||
// theVoltage.t = 50;
|
||||
// esp_now_send(broadcastAddress, (uint8_t *) &theVoltage, sizeof(theVoltage));
|
||||
//
|
||||
//
|
||||
// }
|
||||
}
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// FastLED Controller
|
||||
//
|
||||
// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
|
||||
// Rest in Peace Dan Garcia, creator of FastLED.
|
||||
//
|
||||
#include <FastLED.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
|
||||
|
||||
|
||||
#define READING_ID 118 //Unique ID for controller
|
||||
#define GTWY_MAC 0x00 //Gateway MAC
|
||||
|
||||
#define DATA_PIN 4
|
||||
#define BAT_ADC 33
|
||||
#define POWER_CTRL 5
|
||||
#define NUM_LEDS 24
|
||||
|
||||
|
||||
uint8_t broadcastAddress[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, GTWY_MAC};
|
||||
CRGB leds[NUM_LEDS];
|
||||
|
||||
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;
|
||||
int wait_time = 0;
|
||||
|
||||
|
||||
#if defined(ESP8266)
|
||||
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) {
|
||||
#elif defined(ESP32)
|
||||
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");
|
||||
}
|
||||
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
|
||||
#endif
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float readBattery()
|
||||
{
|
||||
int vref = 1100;
|
||||
uint16_t volt = analogRead(BAT_ADC);
|
||||
Serial.println(volt);
|
||||
float battery_voltage = ((float)volt / 4095.0) * 2.0 * 3.3 * (vref);
|
||||
return battery_voltage;
|
||||
}
|
||||
|
||||
void setup() {
|
||||
|
||||
Serial.begin(115200);
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.disconnect();
|
||||
#if defined(ESP8266)
|
||||
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);
|
||||
#elif defined(ESP32)
|
||||
if (esp_now_init() != ESP_OK) {
|
||||
Serial.println("Error initializing ESP-NOW");
|
||||
return;
|
||||
}
|
||||
esp_now_register_send_cb(OnDataSent);
|
||||
esp_now_register_recv_cb(OnDataRecv);
|
||||
esp_now_peer_info_t peerInfo;
|
||||
peerInfo.channel = 0;
|
||||
peerInfo.encrypt = false;
|
||||
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
|
||||
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||
Serial.println("Failed to add peer");
|
||||
return;
|
||||
}
|
||||
pinMode(POWER_CTRL, OUTPUT);
|
||||
digitalWrite(POWER_CTRL, 1);
|
||||
#endif
|
||||
|
||||
Serial.println();
|
||||
Serial.println("FARM DATA RELAY SYSTEM :: Pretty Lantern FastLED Module");
|
||||
Serial.println("Thank You DAN GARCIA!");
|
||||
Serial.println("MAC: " + WiFi.macAddress());
|
||||
Serial.println("COLOR ID: " + String(COLOR_ID));
|
||||
Serial.println("BRIGHT ID: " + String(BRIGHT_ID));
|
||||
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
|
||||
FastLED.show();
|
||||
fill_solid(leds, NUM_LEDS, CRGB::Red); FastLED.show();
|
||||
delay(250);
|
||||
fill_solid(leds, NUM_LEDS, CRGB::Blue); FastLED.show();
|
||||
delay(250);
|
||||
fill_solid(leds, NUM_LEDS, CRGB::Green); FastLED.show();
|
||||
delay(250);
|
||||
fill_solid(leds, NUM_LEDS, CRGB::Black); FastLED.show();
|
||||
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (newData) {
|
||||
newData = false;
|
||||
fill_solid(leds, NUM_LEDS, CHSV(the_color, 255, the_bright)); FastLED.show();
|
||||
}
|
||||
// if (millis() > wait_time) {
|
||||
// wait_time = wait_time + 30 * 1000;
|
||||
// DataReading theVoltage;
|
||||
// theVoltage.d = readBattery();
|
||||
// theVoltage.id = BAT_ID;
|
||||
// theVoltage.t = 50;
|
||||
// esp_now_send(broadcastAddress, (uint8_t *) &theVoltage, sizeof(theVoltage));
|
||||
//
|
||||
//
|
||||
// }
|
||||
}
|
@ -1,167 +1,167 @@
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// Irrigation Controller
|
||||
//
|
||||
// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
|
||||
//
|
||||
//
|
||||
#define CONTROL_1 133 //Address for controller 1
|
||||
#define CONTROL_2 134 //Address for controller 2
|
||||
#define CONTROL_3 135 //Address for controller 3
|
||||
#define CONTROL_4 136 //Address for controller 4
|
||||
#define GTWY_MAC 0x00 //Gateway MAC
|
||||
#define COIL_1 5 //Coil Pin 1
|
||||
#define COIL_2 4 //Coil Pin 2
|
||||
#define COIL_3 4 //Coil Pin 3
|
||||
#define COIL_4 4 //Coil Pin 4
|
||||
|
||||
#include <FastLED.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
|
||||
|
||||
uint8_t broadcastAddress[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, GTWY_MAC};
|
||||
|
||||
typedef struct DataReading {
|
||||
float d;
|
||||
uint16_t id;
|
||||
uint8_t t;
|
||||
|
||||
} DataReading;
|
||||
|
||||
const uint16_t espnow_size = 250 / sizeof(DataReading);
|
||||
DataReading theCommands[espnow_size];
|
||||
|
||||
int status_1 = 0;
|
||||
int status_2 = 0;
|
||||
int status_3 = 0;
|
||||
int status_4 = 0;
|
||||
|
||||
bool newData = false;
|
||||
int pkt_readings;
|
||||
int wait_time = 0;
|
||||
|
||||
#if defined(ESP8266)
|
||||
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) {
|
||||
#elif defined(ESP32)
|
||||
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");
|
||||
}
|
||||
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
|
||||
#endif
|
||||
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
|
||||
switch (theCommands[i].id) {
|
||||
case CONTROL_1:
|
||||
status_1 = (int)theCommands[i].d;
|
||||
Serial.println("D:" + String(theCommands[i].d));
|
||||
newData = true;
|
||||
break;
|
||||
case CONTROL_2:
|
||||
status_2 = (int)theCommands[i].d;
|
||||
Serial.println("D:" + String(theCommands[i].d));
|
||||
newData = true;
|
||||
break;
|
||||
case CONTROL_3:
|
||||
status_3 = (int)theCommands[i].d;
|
||||
Serial.println("D:" + String(theCommands[i].d));
|
||||
newData = true;
|
||||
break;
|
||||
case CONTROL_4:
|
||||
status_4 = (int)theCommands[i].d;
|
||||
Serial.println("D:" + String(theCommands[i].d));
|
||||
newData = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.disconnect();
|
||||
#if defined(ESP8266)
|
||||
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);
|
||||
#elif defined(ESP32)
|
||||
if (esp_now_init() != ESP_OK) {
|
||||
Serial.println("Error initializing ESP-NOW");
|
||||
return;
|
||||
}
|
||||
esp_now_register_send_cb(OnDataSent);
|
||||
esp_now_register_recv_cb(OnDataRecv);
|
||||
esp_now_peer_info_t peerInfo;
|
||||
peerInfo.channel = 0;
|
||||
peerInfo.encrypt = false;
|
||||
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
|
||||
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||
Serial.println("Failed to add peer");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
pinMode(5, OUTPUT);
|
||||
digitalWrite(COIL_1, LOW);
|
||||
pinMode(COIL_2, OUTPUT);
|
||||
digitalWrite(COIL_2, LOW);
|
||||
pinMode(COIL_3, OUTPUT);
|
||||
digitalWrite(COIL_3, LOW);
|
||||
pinMode(COIL_4, OUTPUT);
|
||||
digitalWrite(COIL_4, LOW);
|
||||
|
||||
Serial.println();
|
||||
Serial.println("FARM DATA RELAY SYSTEM :: Irrigation Module");
|
||||
|
||||
}
|
||||
void updateCoils() {
|
||||
if (status_1) {
|
||||
digitalWrite(COIL_1, HIGH);
|
||||
} else {
|
||||
digitalWrite(COIL_1, LOW);
|
||||
}
|
||||
if (status_2) {
|
||||
digitalWrite(COIL_2, HIGH);
|
||||
} else {
|
||||
digitalWrite(COIL_2, LOW);
|
||||
}
|
||||
if (status_3) {
|
||||
digitalWrite(COIL_3, HIGH);
|
||||
} else {
|
||||
digitalWrite(COIL_3, LOW);
|
||||
}
|
||||
if (status_4) {
|
||||
digitalWrite(COIL_4, HIGH);
|
||||
} else {
|
||||
digitalWrite(COIL_4, LOW);
|
||||
}
|
||||
}
|
||||
void loop()
|
||||
{
|
||||
if (newData) {
|
||||
newData = false;
|
||||
updateCoils();
|
||||
}
|
||||
}
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// Irrigation Controller
|
||||
//
|
||||
// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
|
||||
//
|
||||
//
|
||||
#define CONTROL_1 133 //Address for controller 1
|
||||
#define CONTROL_2 134 //Address for controller 2
|
||||
#define CONTROL_3 135 //Address for controller 3
|
||||
#define CONTROL_4 136 //Address for controller 4
|
||||
#define GTWY_MAC 0x00 //Gateway MAC
|
||||
#define COIL_1 5 //Coil Pin 1
|
||||
#define COIL_2 4 //Coil Pin 2
|
||||
#define COIL_3 4 //Coil Pin 3
|
||||
#define COIL_4 4 //Coil Pin 4
|
||||
|
||||
#include <FastLED.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
|
||||
|
||||
uint8_t broadcastAddress[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, GTWY_MAC};
|
||||
|
||||
typedef struct DataReading {
|
||||
float d;
|
||||
uint16_t id;
|
||||
uint8_t t;
|
||||
|
||||
} DataReading;
|
||||
|
||||
const uint16_t espnow_size = 250 / sizeof(DataReading);
|
||||
DataReading theCommands[espnow_size];
|
||||
|
||||
int status_1 = 0;
|
||||
int status_2 = 0;
|
||||
int status_3 = 0;
|
||||
int status_4 = 0;
|
||||
|
||||
bool newData = false;
|
||||
int pkt_readings;
|
||||
int wait_time = 0;
|
||||
|
||||
#if defined(ESP8266)
|
||||
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) {
|
||||
#elif defined(ESP32)
|
||||
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");
|
||||
}
|
||||
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
|
||||
#endif
|
||||
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
|
||||
switch (theCommands[i].id) {
|
||||
case CONTROL_1:
|
||||
status_1 = (int)theCommands[i].d;
|
||||
Serial.println("D:" + String(theCommands[i].d));
|
||||
newData = true;
|
||||
break;
|
||||
case CONTROL_2:
|
||||
status_2 = (int)theCommands[i].d;
|
||||
Serial.println("D:" + String(theCommands[i].d));
|
||||
newData = true;
|
||||
break;
|
||||
case CONTROL_3:
|
||||
status_3 = (int)theCommands[i].d;
|
||||
Serial.println("D:" + String(theCommands[i].d));
|
||||
newData = true;
|
||||
break;
|
||||
case CONTROL_4:
|
||||
status_4 = (int)theCommands[i].d;
|
||||
Serial.println("D:" + String(theCommands[i].d));
|
||||
newData = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.disconnect();
|
||||
#if defined(ESP8266)
|
||||
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);
|
||||
#elif defined(ESP32)
|
||||
if (esp_now_init() != ESP_OK) {
|
||||
Serial.println("Error initializing ESP-NOW");
|
||||
return;
|
||||
}
|
||||
esp_now_register_send_cb(OnDataSent);
|
||||
esp_now_register_recv_cb(OnDataRecv);
|
||||
esp_now_peer_info_t peerInfo;
|
||||
peerInfo.channel = 0;
|
||||
peerInfo.encrypt = false;
|
||||
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
|
||||
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||
Serial.println("Failed to add peer");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
pinMode(5, OUTPUT);
|
||||
digitalWrite(COIL_1, LOW);
|
||||
pinMode(COIL_2, OUTPUT);
|
||||
digitalWrite(COIL_2, LOW);
|
||||
pinMode(COIL_3, OUTPUT);
|
||||
digitalWrite(COIL_3, LOW);
|
||||
pinMode(COIL_4, OUTPUT);
|
||||
digitalWrite(COIL_4, LOW);
|
||||
|
||||
Serial.println();
|
||||
Serial.println("FARM DATA RELAY SYSTEM :: Irrigation Module");
|
||||
|
||||
}
|
||||
void updateCoils() {
|
||||
if (status_1) {
|
||||
digitalWrite(COIL_1, HIGH);
|
||||
} else {
|
||||
digitalWrite(COIL_1, LOW);
|
||||
}
|
||||
if (status_2) {
|
||||
digitalWrite(COIL_2, HIGH);
|
||||
} else {
|
||||
digitalWrite(COIL_2, LOW);
|
||||
}
|
||||
if (status_3) {
|
||||
digitalWrite(COIL_3, HIGH);
|
||||
} else {
|
||||
digitalWrite(COIL_3, LOW);
|
||||
}
|
||||
if (status_4) {
|
||||
digitalWrite(COIL_4, HIGH);
|
||||
} else {
|
||||
digitalWrite(COIL_4, LOW);
|
||||
}
|
||||
}
|
||||
void loop()
|
||||
{
|
||||
if (newData) {
|
||||
newData = false;
|
||||
updateCoils();
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@
|
||||
// Sends ESP-NOW packets at approximately 60Hz.
|
||||
//
|
||||
|
||||
#include "sensor_setup.h"
|
||||
#include <fdrs_sensor.h>
|
||||
|
||||
void setup() {
|
@ -4,7 +4,7 @@
|
||||
// (This file will soon be known as 'sensor_config.h')
|
||||
//
|
||||
|
||||
//#include <fdrs_globals.h> //Uncomment when you install the globals file
|
||||
#include <fdrs_globals.h> // Comment if you want to set specific values for this sensor in sensor_setup.h
|
||||
|
||||
#define READING_ID 3 //Unique ID for this sensor
|
||||
#define GTWY_MAC 0x04 //Address of the nearest gateway
|
||||
@ -25,5 +25,5 @@
|
||||
//433E6 for Asia
|
||||
//866E6 for Europe
|
||||
//915E6 for North America
|
||||
#define BAND 915E6
|
||||
#define SF 7
|
||||
//#define BAND 915E6
|
||||
//#define SF 7
|
@ -1,33 +1,33 @@
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// Basic Sensor Example
|
||||
//
|
||||
// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
|
||||
// An example of how to send data using "fdrs_sensor.h".
|
||||
//
|
||||
|
||||
|
||||
#include "fdrs_sensor.h"
|
||||
|
||||
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;
|
||||
}
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// Basic Sensor Example
|
||||
//
|
||||
// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
|
||||
// An example of how to send data using "fdrs_sensor.h".
|
||||
//
|
||||
|
||||
#include "sensor_setup.h"
|
||||
#include <fdrs_sensor.h>
|
||||
|
||||
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 12.069;
|
||||
}
|
||||
|
||||
float readHum() {
|
||||
return (0,100);
|
||||
}
|
@ -4,7 +4,7 @@
|
||||
// (This file will soon be known as 'sensor_config.h')
|
||||
//
|
||||
|
||||
//#include <fdrs_globals.h> //Uncomment when you install the globals file
|
||||
#include <fdrs_globals.h> // Comment if you want to set specific values for this sensor in sensor_setup.h
|
||||
|
||||
#define READING_ID 1 //Unique ID for this sensor
|
||||
#define GTWY_MAC 0x04 //Address of the nearest gateway
|
||||
@ -25,5 +25,5 @@
|
||||
//433E6 for Asia
|
||||
//866E6 for Europe
|
||||
//915E6 for North America
|
||||
#define BAND 915E6
|
||||
#define SF 7
|
||||
//#define BAND 915E6
|
||||
//#define SF 7
|
@ -1,32 +1,33 @@
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// Basic Sensor Example
|
||||
//
|
||||
// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
|
||||
// An example of how to send data using "fdrs_sensor.h".
|
||||
//
|
||||
|
||||
#include "fdrs_sensor.h"
|
||||
|
||||
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;
|
||||
}
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// Basic Sensor Example
|
||||
//
|
||||
// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
|
||||
// An example of how to send data using "fdrs_sensor.h".
|
||||
//
|
||||
|
||||
#include "sensor_setup.h"
|
||||
#include <fdrs_sensor.h>
|
||||
|
||||
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 22.069;
|
||||
}
|
||||
|
||||
float readHum() {
|
||||
return (0,100);
|
||||
}
|
@ -4,7 +4,7 @@
|
||||
// (This file will soon be known as 'sensor_config.h')
|
||||
//
|
||||
|
||||
//#include <fdrs_globals.h> //Uncomment when you install the globals file
|
||||
#include <fdrs_globals.h> // Comment if you want to set specific values for this sensor in sensor_setup.h
|
||||
|
||||
#define READING_ID 2 //Unique ID for this sensor
|
||||
#define GTWY_MAC 0x03 //Address of the nearest gateway
|
||||
@ -25,5 +25,5 @@
|
||||
//433E6 for Asia
|
||||
//866E6 for Europe
|
||||
//915E6 for North America
|
||||
#define BAND 915E6
|
||||
#define SF 7
|
||||
//#define BAND 915E6
|
||||
//#define SF 7
|
@ -1,169 +1,169 @@
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// GATEWAY 2.000
|
||||
//
|
||||
// Developed by Timm Bogner (timmbogner@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
|
||||
//
|
||||
|
||||
#include "fdrs_config.h"
|
||||
#ifdef ESP8266
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <espnow.h>
|
||||
#elif defined(ESP32)
|
||||
#include <esp_now.h>
|
||||
#include <WiFi.h>
|
||||
#include <esp_wifi.h>
|
||||
#endif
|
||||
#include <ArduinoJson.h>
|
||||
#ifdef USE_WIFI
|
||||
#include <PubSubClient.h>
|
||||
#endif
|
||||
#ifdef USE_LORA
|
||||
#include <LoRa.h>
|
||||
#endif
|
||||
#ifdef USE_LED
|
||||
#include <FastLED.h>
|
||||
#endif
|
||||
#include "fdrs_functions.h"
|
||||
|
||||
void setup() {
|
||||
#if defined(ESP8266)
|
||||
Serial.begin(115200);
|
||||
#elif defined(ESP32)
|
||||
Serial.begin(115200);
|
||||
UART_IF.begin(115200, SERIAL_8N1, RXD2, TXD2);
|
||||
#endif
|
||||
DBG("Address:" + String (UNIT_MAC, HEX));
|
||||
#ifdef USE_LED
|
||||
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
|
||||
leds[0] = CRGB::Blue;
|
||||
FastLED.show();
|
||||
#endif
|
||||
#ifdef USE_WIFI
|
||||
delay(10);
|
||||
WiFi.begin(ssid, password);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
DBG("Connecting to WiFi...");
|
||||
DBG(FDRS_WIFI_SSID);
|
||||
|
||||
delay(500);
|
||||
}
|
||||
DBG("WiFi Connected");
|
||||
client.setServer(mqtt_server, mqtt_port);
|
||||
if (!client.connected()) {
|
||||
DBG("Connecting MQTT...");
|
||||
reconnect();
|
||||
}
|
||||
DBG("MQTT Connected");
|
||||
client.setCallback(mqtt_callback);
|
||||
#else
|
||||
begin_espnow();
|
||||
#endif
|
||||
#ifdef USE_LORA
|
||||
DBG("Initializing LoRa!");
|
||||
SPI.begin(SCK, MISO, MOSI, SS);
|
||||
LoRa.setPins(SS, RST, DIO0);
|
||||
if (!LoRa.begin(FDRS_BAND)) {
|
||||
while (1);
|
||||
}
|
||||
LoRa.setSpreadingFactor(FDRS_SF);
|
||||
DBG(" LoRa initialized.");
|
||||
#endif
|
||||
|
||||
//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();
|
||||
}
|
||||
getLoRa();
|
||||
#ifdef USE_WIFI
|
||||
if (!client.connected()) {
|
||||
DBG("Connecting MQTT...");
|
||||
reconnect();
|
||||
}
|
||||
client.loop();
|
||||
#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
|
||||
ESPNOWG_ACT
|
||||
break;
|
||||
case 4: //Serial
|
||||
SERIAL_ACT
|
||||
break;
|
||||
case 5: //MQTT
|
||||
MQTT_ACT
|
||||
break;
|
||||
case 6: //LoRa General
|
||||
LORAG_ACT
|
||||
break;
|
||||
case 7: //LoRa #1
|
||||
LORA1_ACT
|
||||
break;
|
||||
case 8: //LoRa #2
|
||||
LORA2_ACT
|
||||
break;
|
||||
}
|
||||
newData = 0;
|
||||
}
|
||||
}
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// GATEWAY 2.000
|
||||
//
|
||||
// Developed by Timm Bogner (timmbogner@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
|
||||
//
|
||||
|
||||
#include "fdrs_config.h"
|
||||
#ifdef ESP8266
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <espnow.h>
|
||||
#elif defined(ESP32)
|
||||
#include <esp_now.h>
|
||||
#include <WiFi.h>
|
||||
#include <esp_wifi.h>
|
||||
#endif
|
||||
#include <ArduinoJson.h>
|
||||
#ifdef USE_WIFI
|
||||
#include <PubSubClient.h>
|
||||
#endif
|
||||
#ifdef USE_LORA
|
||||
#include <LoRa.h>
|
||||
#endif
|
||||
#ifdef USE_LED
|
||||
#include <FastLED.h>
|
||||
#endif
|
||||
#include <fdrs_functions.h>
|
||||
|
||||
void setup() {
|
||||
#if defined(ESP8266)
|
||||
Serial.begin(115200);
|
||||
#elif defined(ESP32)
|
||||
Serial.begin(115200);
|
||||
UART_IF.begin(115200, SERIAL_8N1, RXD2, TXD2);
|
||||
#endif
|
||||
DBG("Address:" + String (UNIT_MAC, HEX));
|
||||
#ifdef USE_LED
|
||||
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
|
||||
leds[0] = CRGB::Blue;
|
||||
FastLED.show();
|
||||
#endif
|
||||
#ifdef USE_WIFI
|
||||
delay(10);
|
||||
WiFi.begin(ssid, password);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
DBG("Connecting to WiFi...");
|
||||
DBG(FDRS_WIFI_SSID);
|
||||
|
||||
delay(500);
|
||||
}
|
||||
DBG("WiFi Connected");
|
||||
client.setServer(mqtt_server, mqtt_port);
|
||||
if (!client.connected()) {
|
||||
DBG("Connecting MQTT...");
|
||||
reconnect();
|
||||
}
|
||||
DBG("MQTT Connected");
|
||||
client.setCallback(mqtt_callback);
|
||||
#else
|
||||
begin_espnow();
|
||||
#endif
|
||||
#ifdef USE_LORA
|
||||
DBG("Initializing LoRa!");
|
||||
SPI.begin(SCK, MISO, MOSI, SS);
|
||||
LoRa.setPins(SS, RST, DIO0);
|
||||
if (!LoRa.begin(FDRS_BAND)) {
|
||||
while (1);
|
||||
}
|
||||
LoRa.setSpreadingFactor(FDRS_SF);
|
||||
DBG(" LoRa initialized.");
|
||||
#endif
|
||||
|
||||
//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();
|
||||
}
|
||||
getLoRa();
|
||||
#ifdef USE_WIFI
|
||||
if (!client.connected()) {
|
||||
DBG("Connecting MQTT...");
|
||||
reconnect();
|
||||
}
|
||||
client.loop();
|
||||
#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
|
||||
ESPNOWG_ACT
|
||||
break;
|
||||
case 4: //Serial
|
||||
SERIAL_ACT
|
||||
break;
|
||||
case 5: //MQTT
|
||||
MQTT_ACT
|
||||
break;
|
||||
case 6: //LoRa General
|
||||
LORAG_ACT
|
||||
break;
|
||||
case 7: //LoRa #1
|
||||
LORA1_ACT
|
||||
break;
|
||||
case 8: //LoRa #2
|
||||
LORA2_ACT
|
||||
break;
|
||||
}
|
||||
newData = 0;
|
||||
}
|
||||
}
|
@ -1,78 +1,78 @@
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// GATEWAY 2.000 Configuration
|
||||
|
||||
//#include <fdrs_globals.h> //Uncomment if you install the globals file
|
||||
#define DEBUG
|
||||
|
||||
#define UNIT_MAC 0x03 // 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 sendESPNOW(0x04);
|
||||
#define SERIAL_ACT
|
||||
#define MQTT_ACT
|
||||
#define LORAG_ACT
|
||||
|
||||
//#define USE_LORA
|
||||
//#define USE_WIFI //Used only for MQTT gateway
|
||||
|
||||
// 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
|
||||
|
||||
// Peer Actions
|
||||
#define ESPNOW1_ACT
|
||||
#define ESPNOW2_ACT
|
||||
#define LORA1_ACT
|
||||
#define LORA2_ACT
|
||||
|
||||
//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"
|
||||
#define MQTT_PORT 1883 // Default MQTT port is 1883
|
||||
|
||||
//MQTT Credentials -- Needed only if MQTT broker requires authentication
|
||||
//#define MQTT_AUTH //uncomment to enable MQTT authentication
|
||||
#define MQTT_USER "Your MQTT Username"
|
||||
#define MQTT_PASS "Your MQTT Password"
|
||||
|
||||
// MQTT Topics
|
||||
#define TOPIC_DATA "fdrs/data"
|
||||
#define TOPIC_STATUS "fdrs/status"
|
||||
#define TOPIC_COMMAND "fdrs/command"
|
||||
|
||||
//Pins for UART data interface (ESP32 only)
|
||||
#define RXD2 14
|
||||
#define TXD2 15
|
||||
|
||||
//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
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// GATEWAY 2.000 Configuration
|
||||
|
||||
#include <fdrs_globals.h> // Comment if you want to set specific values for this individually
|
||||
#define DEBUG
|
||||
|
||||
#define UNIT_MAC 0x03 // 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 sendESPNOW(0x04);
|
||||
#define SERIAL_ACT
|
||||
#define MQTT_ACT
|
||||
#define LORAG_ACT
|
||||
|
||||
//#define USE_LORA
|
||||
//#define USE_WIFI //Used only for MQTT gateway
|
||||
|
||||
// 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
|
||||
|
||||
// Peer Actions
|
||||
#define ESPNOW1_ACT
|
||||
#define ESPNOW2_ACT
|
||||
#define LORA1_ACT
|
||||
#define LORA2_ACT
|
||||
|
||||
//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"
|
||||
#define MQTT_PORT 1883 // Default MQTT port is 1883
|
||||
|
||||
//MQTT Credentials -- Needed only if MQTT broker requires authentication
|
||||
//#define MQTT_AUTH //uncomment to enable MQTT authentication
|
||||
#define MQTT_USER "Your MQTT Username"
|
||||
#define MQTT_PASS "Your MQTT Password"
|
||||
|
||||
// MQTT Topics
|
||||
#define TOPIC_DATA "fdrs/data"
|
||||
#define TOPIC_STATUS "fdrs/status"
|
||||
#define TOPIC_COMMAND "fdrs/command"
|
||||
|
||||
//Pins for UART data interface (ESP32 only)
|
||||
#define RXD2 14
|
||||
#define TXD2 15
|
||||
|
||||
//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
|
@ -1,169 +1,169 @@
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// GATEWAY 2.000
|
||||
//
|
||||
// Developed by Timm Bogner (timmbogner@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
|
||||
//
|
||||
|
||||
#include "fdrs_config.h"
|
||||
#ifdef ESP8266
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <espnow.h>
|
||||
#elif defined(ESP32)
|
||||
#include <esp_now.h>
|
||||
#include <WiFi.h>
|
||||
#include <esp_wifi.h>
|
||||
#endif
|
||||
#include <ArduinoJson.h>
|
||||
#ifdef USE_WIFI
|
||||
#include <PubSubClient.h>
|
||||
#endif
|
||||
#ifdef USE_LORA
|
||||
#include <LoRa.h>
|
||||
#endif
|
||||
#ifdef USE_LED
|
||||
#include <FastLED.h>
|
||||
#endif
|
||||
#include "fdrs_functions.h"
|
||||
|
||||
void setup() {
|
||||
#if defined(ESP8266)
|
||||
Serial.begin(115200);
|
||||
#elif defined(ESP32)
|
||||
Serial.begin(115200);
|
||||
UART_IF.begin(115200, SERIAL_8N1, RXD2, TXD2);
|
||||
#endif
|
||||
DBG("Address:" + String (UNIT_MAC, HEX));
|
||||
#ifdef USE_LED
|
||||
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
|
||||
leds[0] = CRGB::Blue;
|
||||
FastLED.show();
|
||||
#endif
|
||||
#ifdef USE_WIFI
|
||||
delay(10);
|
||||
WiFi.begin(ssid, password);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
DBG("Connecting to WiFi...");
|
||||
DBG(FDRS_WIFI_SSID);
|
||||
|
||||
delay(500);
|
||||
}
|
||||
DBG("WiFi Connected");
|
||||
client.setServer(mqtt_server, mqtt_port);
|
||||
if (!client.connected()) {
|
||||
DBG("Connecting MQTT...");
|
||||
reconnect();
|
||||
}
|
||||
DBG("MQTT Connected");
|
||||
client.setCallback(mqtt_callback);
|
||||
#else
|
||||
begin_espnow();
|
||||
#endif
|
||||
#ifdef USE_LORA
|
||||
DBG("Initializing LoRa!");
|
||||
SPI.begin(SCK, MISO, MOSI, SS);
|
||||
LoRa.setPins(SS, RST, DIO0);
|
||||
if (!LoRa.begin(FDRS_BAND)) {
|
||||
while (1);
|
||||
}
|
||||
LoRa.setSpreadingFactor(FDRS_SF);
|
||||
DBG(" LoRa initialized.");
|
||||
#endif
|
||||
|
||||
//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();
|
||||
}
|
||||
getLoRa();
|
||||
#ifdef USE_WIFI
|
||||
if (!client.connected()) {
|
||||
DBG("Connecting MQTT...");
|
||||
reconnect();
|
||||
}
|
||||
client.loop();
|
||||
#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
|
||||
ESPNOWG_ACT
|
||||
break;
|
||||
case 4: //Serial
|
||||
SERIAL_ACT
|
||||
break;
|
||||
case 5: //MQTT
|
||||
MQTT_ACT
|
||||
break;
|
||||
case 6: //LoRa General
|
||||
LORAG_ACT
|
||||
break;
|
||||
case 7: //LoRa #1
|
||||
LORA1_ACT
|
||||
break;
|
||||
case 8: //LoRa #2
|
||||
LORA2_ACT
|
||||
break;
|
||||
}
|
||||
newData = 0;
|
||||
}
|
||||
}
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// GATEWAY 2.000
|
||||
//
|
||||
// Developed by Timm Bogner (timmbogner@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
|
||||
//
|
||||
|
||||
#include "fdrs_config.h"
|
||||
#ifdef ESP8266
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <espnow.h>
|
||||
#elif defined(ESP32)
|
||||
#include <esp_now.h>
|
||||
#include <WiFi.h>
|
||||
#include <esp_wifi.h>
|
||||
#endif
|
||||
#include <ArduinoJson.h>
|
||||
#ifdef USE_WIFI
|
||||
#include <PubSubClient.h>
|
||||
#endif
|
||||
#ifdef USE_LORA
|
||||
#include <LoRa.h>
|
||||
#endif
|
||||
#ifdef USE_LED
|
||||
#include <FastLED.h>
|
||||
#endif
|
||||
#include <fdrs_functions.h>
|
||||
|
||||
void setup() {
|
||||
#if defined(ESP8266)
|
||||
Serial.begin(115200);
|
||||
#elif defined(ESP32)
|
||||
Serial.begin(115200);
|
||||
UART_IF.begin(115200, SERIAL_8N1, RXD2, TXD2);
|
||||
#endif
|
||||
DBG("Address:" + String (UNIT_MAC, HEX));
|
||||
#ifdef USE_LED
|
||||
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
|
||||
leds[0] = CRGB::Blue;
|
||||
FastLED.show();
|
||||
#endif
|
||||
#ifdef USE_WIFI
|
||||
delay(10);
|
||||
WiFi.begin(ssid, password);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
DBG("Connecting to WiFi...");
|
||||
DBG(FDRS_WIFI_SSID);
|
||||
|
||||
delay(500);
|
||||
}
|
||||
DBG("WiFi Connected");
|
||||
client.setServer(mqtt_server, mqtt_port);
|
||||
if (!client.connected()) {
|
||||
DBG("Connecting MQTT...");
|
||||
reconnect();
|
||||
}
|
||||
DBG("MQTT Connected");
|
||||
client.setCallback(mqtt_callback);
|
||||
#else
|
||||
begin_espnow();
|
||||
#endif
|
||||
#ifdef USE_LORA
|
||||
DBG("Initializing LoRa!");
|
||||
SPI.begin(SCK, MISO, MOSI, SS);
|
||||
LoRa.setPins(SS, RST, DIO0);
|
||||
if (!LoRa.begin(FDRS_BAND)) {
|
||||
while (1);
|
||||
}
|
||||
LoRa.setSpreadingFactor(FDRS_SF);
|
||||
DBG(" LoRa initialized.");
|
||||
#endif
|
||||
|
||||
//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();
|
||||
}
|
||||
getLoRa();
|
||||
#ifdef USE_WIFI
|
||||
if (!client.connected()) {
|
||||
DBG("Connecting MQTT...");
|
||||
reconnect();
|
||||
}
|
||||
client.loop();
|
||||
#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
|
||||
ESPNOWG_ACT
|
||||
break;
|
||||
case 4: //Serial
|
||||
SERIAL_ACT
|
||||
break;
|
||||
case 5: //MQTT
|
||||
MQTT_ACT
|
||||
break;
|
||||
case 6: //LoRa General
|
||||
LORAG_ACT
|
||||
break;
|
||||
case 7: //LoRa #1
|
||||
LORA1_ACT
|
||||
break;
|
||||
case 8: //LoRa #2
|
||||
LORA2_ACT
|
||||
break;
|
||||
}
|
||||
newData = 0;
|
||||
}
|
||||
}
|
@ -1,78 +1,81 @@
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// GATEWAY 2.000 Configuration
|
||||
|
||||
//#include <fdrs_globals.h> //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 LORAG_ACT sendSerial();
|
||||
|
||||
#define USE_LORA
|
||||
//#define USE_WIFI //Used only for MQTT gateway
|
||||
|
||||
// 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
|
||||
|
||||
// Peer Actions
|
||||
#define ESPNOW1_ACT
|
||||
#define ESPNOW2_ACT
|
||||
#define LORA1_ACT
|
||||
#define LORA2_ACT
|
||||
|
||||
//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"
|
||||
#define MQTT_PORT 1883 // Default MQTT port is 1883
|
||||
|
||||
//MQTT Credentials -- Needed only if MQTT broker requires authentication
|
||||
//#define MQTT_AUTH //uncomment to enable MQTT authentication
|
||||
#define MQTT_USER "Your MQTT Username"
|
||||
#define MQTT_PASS "Your MQTT Password"
|
||||
|
||||
// MQTT Topics
|
||||
#define TOPIC_DATA "fdrs/data"
|
||||
#define TOPIC_STATUS "fdrs/status"
|
||||
#define TOPIC_COMMAND "fdrs/command"
|
||||
|
||||
//Pins for UART data interface (ESP32 only)
|
||||
#define RXD2 14
|
||||
#define TXD2 15
|
||||
|
||||
//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
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// GATEWAY 2.000 Configuration
|
||||
|
||||
#include <fdrs_globals.h> // Comment if you want to set specific values for this individually
|
||||
#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 LORAG_ACT sendSerial();
|
||||
|
||||
#define USE_LORA
|
||||
//#define USE_WIFI //Used only for MQTT gateway
|
||||
|
||||
// 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
|
||||
|
||||
// Peer Actions
|
||||
#define ESPNOW1_ACT
|
||||
#define ESPNOW2_ACT
|
||||
#define LORA1_ACT
|
||||
#define LORA2_ACT
|
||||
|
||||
// TODO: Either way the current system is broken: It should work like the following: IF a specific Global setting is set, it should be used. If a local settings
|
||||
// is set (independent of a global setting) the local setting should be used. Therefore by default all local settings must be commented out.
|
||||
// Needs to be fixed for all sensors and gateways, did this only for the UART_Gateway to bring it back to function.
|
||||
//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"
|
||||
//#define MQTT_PORT 1883 // Default MQTT port is 1883
|
||||
|
||||
//MQTT Credentials -- Needed only if MQTT broker requires authentication
|
||||
//#define MQTT_AUTH //uncomment to enable MQTT authentication
|
||||
//#define MQTT_USER "Your MQTT Username"
|
||||
//#define MQTT_PASS "Your MQTT Password"
|
||||
|
||||
// MQTT Topics
|
||||
#define TOPIC_DATA "fdrs/data"
|
||||
#define TOPIC_STATUS "fdrs/status"
|
||||
#define TOPIC_COMMAND "fdrs/command"
|
||||
|
||||
//Pins for UART data interface (ESP32 only)
|
||||
#define RXD2 14
|
||||
#define TXD2 15
|
||||
|
||||
//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
|
@ -1,169 +1,169 @@
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// GATEWAY 2.000
|
||||
//
|
||||
// Developed by Timm Bogner (timmbogner@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
|
||||
//
|
||||
|
||||
#include "fdrs_config.h"
|
||||
#ifdef ESP8266
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <espnow.h>
|
||||
#elif defined(ESP32)
|
||||
#include <esp_now.h>
|
||||
#include <WiFi.h>
|
||||
#include <esp_wifi.h>
|
||||
#endif
|
||||
#include <ArduinoJson.h>
|
||||
#ifdef USE_WIFI
|
||||
#include <PubSubClient.h>
|
||||
#endif
|
||||
#ifdef USE_LORA
|
||||
#include <LoRa.h>
|
||||
#endif
|
||||
#ifdef USE_LED
|
||||
#include <FastLED.h>
|
||||
#endif
|
||||
#include "fdrs_functions.h"
|
||||
|
||||
void setup() {
|
||||
#if defined(ESP8266)
|
||||
Serial.begin(115200);
|
||||
#elif defined(ESP32)
|
||||
Serial.begin(115200);
|
||||
UART_IF.begin(115200, SERIAL_8N1, RXD2, TXD2);
|
||||
#endif
|
||||
DBG("Address:" + String (UNIT_MAC, HEX));
|
||||
#ifdef USE_LED
|
||||
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
|
||||
leds[0] = CRGB::Blue;
|
||||
FastLED.show();
|
||||
#endif
|
||||
#ifdef USE_WIFI
|
||||
delay(10);
|
||||
WiFi.begin(ssid, password);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
DBG("Connecting to WiFi...");
|
||||
DBG(FDRS_WIFI_SSID);
|
||||
|
||||
delay(500);
|
||||
}
|
||||
DBG("WiFi Connected");
|
||||
client.setServer(mqtt_server, mqtt_port);
|
||||
if (!client.connected()) {
|
||||
DBG("Connecting MQTT...");
|
||||
reconnect();
|
||||
}
|
||||
DBG("MQTT Connected");
|
||||
client.setCallback(mqtt_callback);
|
||||
#else
|
||||
begin_espnow();
|
||||
#endif
|
||||
#ifdef USE_LORA
|
||||
DBG("Initializing LoRa!");
|
||||
SPI.begin(SCK, MISO, MOSI, SS);
|
||||
LoRa.setPins(SS, RST, DIO0);
|
||||
if (!LoRa.begin(FDRS_BAND)) {
|
||||
while (1);
|
||||
}
|
||||
LoRa.setSpreadingFactor(FDRS_SF);
|
||||
DBG(" LoRa initialized.");
|
||||
#endif
|
||||
|
||||
//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();
|
||||
}
|
||||
getLoRa();
|
||||
#ifdef USE_WIFI
|
||||
if (!client.connected()) {
|
||||
DBG("Connecting MQTT...");
|
||||
reconnect();
|
||||
}
|
||||
client.loop();
|
||||
#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
|
||||
ESPNOWG_ACT
|
||||
break;
|
||||
case 4: //Serial
|
||||
SERIAL_ACT
|
||||
break;
|
||||
case 5: //MQTT
|
||||
MQTT_ACT
|
||||
break;
|
||||
case 6: //LoRa General
|
||||
LORAG_ACT
|
||||
break;
|
||||
case 7: //LoRa #1
|
||||
LORA1_ACT
|
||||
break;
|
||||
case 8: //LoRa #2
|
||||
LORA2_ACT
|
||||
break;
|
||||
}
|
||||
newData = 0;
|
||||
}
|
||||
}
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// GATEWAY 2.000
|
||||
//
|
||||
// Developed by Timm Bogner (timmbogner@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
|
||||
//
|
||||
|
||||
#include "fdrs_config.h"
|
||||
#ifdef ESP8266
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <espnow.h>
|
||||
#elif defined(ESP32)
|
||||
#include <esp_now.h>
|
||||
#include <WiFi.h>
|
||||
#include <esp_wifi.h>
|
||||
#endif
|
||||
#include <ArduinoJson.h>
|
||||
#ifdef USE_WIFI
|
||||
#include <PubSubClient.h>
|
||||
#endif
|
||||
#ifdef USE_LORA
|
||||
#include <LoRa.h>
|
||||
#endif
|
||||
#ifdef USE_LED
|
||||
#include <FastLED.h>
|
||||
#endif
|
||||
#include <fdrs_functions.h>
|
||||
|
||||
void setup() {
|
||||
#if defined(ESP8266)
|
||||
Serial.begin(115200);
|
||||
#elif defined(ESP32)
|
||||
Serial.begin(115200);
|
||||
UART_IF.begin(115200, SERIAL_8N1, RXD2, TXD2);
|
||||
#endif
|
||||
DBG("Address:" + String (UNIT_MAC, HEX));
|
||||
#ifdef USE_LED
|
||||
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
|
||||
leds[0] = CRGB::Blue;
|
||||
FastLED.show();
|
||||
#endif
|
||||
#ifdef USE_WIFI
|
||||
delay(10);
|
||||
WiFi.begin(ssid, password);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
DBG("Connecting to WiFi...");
|
||||
DBG(FDRS_WIFI_SSID);
|
||||
|
||||
delay(500);
|
||||
}
|
||||
DBG("WiFi Connected");
|
||||
client.setServer(mqtt_server, mqtt_port);
|
||||
if (!client.connected()) {
|
||||
DBG("Connecting MQTT...");
|
||||
reconnect();
|
||||
}
|
||||
DBG("MQTT Connected");
|
||||
client.setCallback(mqtt_callback);
|
||||
#else
|
||||
begin_espnow();
|
||||
#endif
|
||||
#ifdef USE_LORA
|
||||
DBG("Initializing LoRa!");
|
||||
SPI.begin(SCK, MISO, MOSI, SS);
|
||||
LoRa.setPins(SS, RST, DIO0);
|
||||
if (!LoRa.begin(FDRS_BAND)) {
|
||||
while (1);
|
||||
}
|
||||
LoRa.setSpreadingFactor(FDRS_SF);
|
||||
DBG(" LoRa initialized.");
|
||||
#endif
|
||||
|
||||
//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();
|
||||
}
|
||||
getLoRa();
|
||||
#ifdef USE_WIFI
|
||||
if (!client.connected()) {
|
||||
DBG("Connecting MQTT...");
|
||||
reconnect();
|
||||
}
|
||||
client.loop();
|
||||
#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
|
||||
ESPNOWG_ACT
|
||||
break;
|
||||
case 4: //Serial
|
||||
SERIAL_ACT
|
||||
break;
|
||||
case 5: //MQTT
|
||||
MQTT_ACT
|
||||
break;
|
||||
case 6: //LoRa General
|
||||
LORAG_ACT
|
||||
break;
|
||||
case 7: //LoRa #1
|
||||
LORA1_ACT
|
||||
break;
|
||||
case 8: //LoRa #2
|
||||
LORA2_ACT
|
||||
break;
|
||||
}
|
||||
newData = 0;
|
||||
}
|
||||
}
|
@ -1,80 +1,80 @@
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// GATEWAY 2.000 Configuration
|
||||
|
||||
//#include <fdrs_globals.h> //Uncomment if you install the globals file
|
||||
#define DEBUG //Enable debugging information over serial
|
||||
|
||||
#define UNIT_MAC 0x05 // The address of this gateway
|
||||
|
||||
//Actions -- Define what happens when a packet arrives at each interface:
|
||||
//Current function options are: sendESPNOW(MAC), sendSerial(), sendMQTT(),
|
||||
// bufferLoRa(interface), bufferESPNOW(interface), bufferSerial(), and bufferMQTT().
|
||||
|
||||
#define ESPNOWG_ACT
|
||||
#define SERIAL_ACT sendMQTT();
|
||||
#define MQTT_ACT
|
||||
#define LORAG_ACT
|
||||
|
||||
//#define USE_LORA
|
||||
#define USE_WIFI //Used only for MQTT gateway
|
||||
|
||||
// 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
|
||||
|
||||
// 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
|
||||
|
||||
//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 - 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"
|
||||
#define MQTT_PORT 1883 // Default MQTT port is 1883
|
||||
|
||||
//MQTT Credentials -- Needed only if MQTT broker requires authentication
|
||||
//#define MQTT_AUTH //uncomment to enable MQTT authentication
|
||||
#define MQTT_USER "Your MQTT Username"
|
||||
#define MQTT_PASS "Your MQTT Password"
|
||||
|
||||
// MQTT Topics
|
||||
#define TOPIC_DATA "fdrs/data"
|
||||
#define TOPIC_STATUS "fdrs/status"
|
||||
#define TOPIC_COMMAND "fdrs/command"
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// GATEWAY 2.000 Configuration
|
||||
|
||||
#include <fdrs_globals.h> // Comment if you want to set specific values for this individually
|
||||
#define DEBUG //Enable debugging information over serial
|
||||
|
||||
#define UNIT_MAC 0x05 // The address of this gateway
|
||||
|
||||
//Actions -- Define what happens when a packet arrives at each interface:
|
||||
//Current function options are: sendESPNOW(MAC), sendSerial(), sendMQTT(),
|
||||
// bufferLoRa(interface), bufferESPNOW(interface), bufferSerial(), and bufferMQTT().
|
||||
|
||||
#define ESPNOWG_ACT
|
||||
#define SERIAL_ACT sendMQTT();
|
||||
#define MQTT_ACT
|
||||
#define LORAG_ACT
|
||||
|
||||
//#define USE_LORA
|
||||
#define USE_WIFI //Used only for MQTT gateway
|
||||
|
||||
// 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
|
||||
|
||||
// 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
|
||||
|
||||
//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 - 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"
|
||||
#define MQTT_PORT 1883 // Default MQTT port is 1883
|
||||
|
||||
//MQTT Credentials -- Needed only if MQTT broker requires authentication
|
||||
//#define MQTT_AUTH //uncomment to enable MQTT authentication
|
||||
#define MQTT_USER "Your MQTT Username"
|
||||
#define MQTT_PASS "Your MQTT Password"
|
||||
|
||||
// MQTT Topics
|
||||
#define TOPIC_DATA "fdrs/data"
|
||||
#define TOPIC_STATUS "fdrs/status"
|
||||
#define TOPIC_COMMAND "fdrs/command"
|
@ -1,29 +1,29 @@
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// AHT20 SENSOR MODULE
|
||||
//
|
||||
// Developed by Timm Bogner (timmbogner@gmail.com) in Urbana, Illinois, USA.
|
||||
|
||||
#include "sensor_setup.h"
|
||||
#include <Adafruit_AHTX0.h>
|
||||
#include <fdrs_sensor.h>
|
||||
|
||||
Adafruit_AHTX0 aht;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
beginFDRS();
|
||||
if (! aht.begin()) {
|
||||
Serial.println("Could not find AHT? Check wiring");
|
||||
while (1) delay(10);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
sensors_event_t humidity, temp;
|
||||
aht.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
|
||||
loadFDRS(temp.temperature, TEMP_T);
|
||||
loadFDRS(humidity.relative_humidity, HUMIDITY_T);
|
||||
sendFDRS();
|
||||
sleepFDRS(60); //Sleep time in seconds
|
||||
}
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// AHT20 SENSOR MODULE
|
||||
//
|
||||
// Developed by Timm Bogner (timmbogner@gmail.com) in Urbana, Illinois, USA.
|
||||
|
||||
#include "sensor_setup.h"
|
||||
#include <Adafruit_AHTX0.h>
|
||||
#include <fdrs_sensor.h>
|
||||
|
||||
Adafruit_AHTX0 aht;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
beginFDRS();
|
||||
if (! aht.begin()) {
|
||||
Serial.println("Could not find AHT? Check wiring");
|
||||
while (1) delay(10);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
sensors_event_t humidity, temp;
|
||||
aht.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
|
||||
loadFDRS(temp.temperature, TEMP_T);
|
||||
loadFDRS(humidity.relative_humidity, HUMIDITY_T);
|
||||
sendFDRS();
|
||||
sleepFDRS(60); //Sleep time in seconds
|
||||
}
|
@ -1,28 +1,28 @@
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// BME280 SENSOR MODULE
|
||||
//
|
||||
// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
|
||||
|
||||
#include "sensor_setup.h"
|
||||
#include <Adafruit_BME280.h>
|
||||
#include <fdrs_sensor.h>
|
||||
|
||||
Adafruit_BME280 bme;
|
||||
|
||||
void setup() {
|
||||
//Serial.begin(115200);
|
||||
beginFDRS();
|
||||
while (!bme.begin(0x76)) {
|
||||
//Serial.println("BME not initializing!");
|
||||
delay(10);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
loadFDRS(bme.readTemperature(), TEMP_T);
|
||||
loadFDRS(bme.readHumidity(), HUMIDITY_T);
|
||||
loadFDRS(bme.readPressure() / 100.0F, PRESSURE_T);
|
||||
sendFDRS();
|
||||
sleepFDRS(60); //Sleep time in seconds
|
||||
}
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// BME280 SENSOR MODULE
|
||||
//
|
||||
// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
|
||||
|
||||
#include "sensor_setup.h"
|
||||
#include <Adafruit_BME280.h>
|
||||
#include <fdrs_sensor.h>
|
||||
|
||||
Adafruit_BME280 bme;
|
||||
|
||||
void setup() {
|
||||
//Serial.begin(115200);
|
||||
beginFDRS();
|
||||
while (!bme.begin(0x76)) {
|
||||
//Serial.println("BME not initializing!");
|
||||
delay(10);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
loadFDRS(bme.readTemperature(), TEMP_T);
|
||||
loadFDRS(bme.readHumidity(), HUMIDITY_T);
|
||||
loadFDRS(bme.readPressure() / 100.0F, PRESSURE_T);
|
||||
sendFDRS();
|
||||
sleepFDRS(60); //Sleep time in seconds
|
||||
}
|
@ -1,28 +1,28 @@
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// BMP280 SENSOR MODULE
|
||||
//
|
||||
// Developed by Timm Bogner (timmbogner@gmail.com) in Urbana, Illinois, USA.
|
||||
// Connect sensor SDA and SCL pins to those of the ESP.
|
||||
|
||||
#include "sensor_setup.h"
|
||||
#include <Adafruit_BMP280.h>
|
||||
#include <fdrs_sensor.h>
|
||||
|
||||
Adafruit_BMP280 bmp;
|
||||
|
||||
void setup() {
|
||||
//Serial.begin(115200);
|
||||
beginFDRS();
|
||||
while (!bmp.begin(0x76)) {
|
||||
//Serial.println("BMP not initializing!");
|
||||
delay(10);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
loadFDRS(bmp.readTemperature(), TEMP_T);
|
||||
loadFDRS(bmp.readPressure() / 100.0F, PRESSURE_T);
|
||||
sendFDRS();
|
||||
sleepFDRS(60); //Sleep time in seconds
|
||||
}
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// BMP280 SENSOR MODULE
|
||||
//
|
||||
// Developed by Timm Bogner (timmbogner@gmail.com) in Urbana, Illinois, USA.
|
||||
// Connect sensor SDA and SCL pins to those of the ESP.
|
||||
|
||||
#include "sensor_setup.h"
|
||||
#include <Adafruit_BMP280.h>
|
||||
#include <fdrs_sensor.h>
|
||||
|
||||
Adafruit_BMP280 bmp;
|
||||
|
||||
void setup() {
|
||||
//Serial.begin(115200);
|
||||
beginFDRS();
|
||||
while (!bmp.begin(0x76)) {
|
||||
//Serial.println("BMP not initializing!");
|
||||
delay(10);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
loadFDRS(bmp.readTemperature(), TEMP_T);
|
||||
loadFDRS(bmp.readPressure() / 100.0F, PRESSURE_T);
|
||||
sendFDRS();
|
||||
sleepFDRS(60); //Sleep time in seconds
|
||||
}
|
@ -1,113 +1,113 @@
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// LILYGO HIGROW SENSOR MODULE
|
||||
//
|
||||
|
||||
|
||||
#define I2C_SDA 25
|
||||
#define I2C_SCL 26
|
||||
#define DHT12_PIN 16
|
||||
#define BAT_ADC 33
|
||||
#define SALT_PIN 34
|
||||
#define SOIL_PIN 32
|
||||
#define BOOT_PIN 0
|
||||
#define USER_BUTTON 35
|
||||
#define DS18B20_PIN 21
|
||||
|
||||
#include "sensor_setup.h"
|
||||
#include <fdrs_sensor.h>
|
||||
#include <BH1750.h>
|
||||
#include <Adafruit_BME280.h>
|
||||
|
||||
BH1750 lightMeter(0x23); //0x23
|
||||
Adafruit_BME280 bmp; //0x77
|
||||
RTC_DATA_ATTR int the_count = 0;
|
||||
|
||||
void setup() {
|
||||
|
||||
//Init Sensors
|
||||
Wire.begin(I2C_SDA, I2C_SCL);
|
||||
while (!bmp.begin()) {
|
||||
//Serial.println("bmp");
|
||||
delay(10);
|
||||
}
|
||||
|
||||
if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
|
||||
Serial.println(F("BH1750 Advanced begin"));
|
||||
} else {
|
||||
Serial.println(F("Error initialising BH1750"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void loadData() {
|
||||
float s_battery = readBattery();
|
||||
float bme_temp = bmp.readTemperature();
|
||||
float bme_pressure = (bmp.readPressure() / 100.0F);
|
||||
//float bme_altitude = bmp.readAltitude(1013.25);
|
||||
float bme_humidity = bmp.readHumidity();
|
||||
float s_soil = readSoil();
|
||||
float s_salt = readSalt();
|
||||
while (! lightMeter.measurementReady()) {
|
||||
delay(10);
|
||||
}
|
||||
float lux = lightMeter.readLightLevel();
|
||||
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));
|
||||
|
||||
loadFDRS(bme_temp, TEMP_T);
|
||||
|
||||
loadFDRS(bme_humidity, HUMIDITY_T);
|
||||
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
uint32_t readSalt() //Soil Electrodes: This code came from the LilyGo documentation.
|
||||
{
|
||||
uint8_t samples = 120;
|
||||
uint32_t humi = 0;
|
||||
uint16_t array[120];
|
||||
for (int i = 0; i < samples; i++) {
|
||||
array[i] = analogRead(SALT_PIN);
|
||||
delay(2);
|
||||
}
|
||||
std::sort(array, array + samples);
|
||||
for (int i = 0; i < samples; i++) {
|
||||
if (i == 0 || i == samples - 1)continue;
|
||||
humi += array[i];
|
||||
}
|
||||
humi /= samples - 2;
|
||||
return humi;
|
||||
}
|
||||
|
||||
uint16_t readSoil() //Soil Capacitance: This code came from the LilyGo documentation.
|
||||
{
|
||||
uint16_t soil = analogRead(SOIL_PIN);
|
||||
return map(soil, 0, 4095, 100, 0);
|
||||
}
|
||||
|
||||
float readBattery() //Battery Voltage: This code came from the LilyGo documentation.
|
||||
{
|
||||
int vref = 1100;
|
||||
uint16_t volt = analogRead(BAT_ADC);
|
||||
float battery_voltage = ((float)volt / 4095.0) * 2.0 * 3.3 * (vref);
|
||||
return battery_voltage;
|
||||
}
|
||||
void loop() {
|
||||
}
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// LILYGO HIGROW SENSOR MODULE
|
||||
//
|
||||
|
||||
|
||||
#define I2C_SDA 25
|
||||
#define I2C_SCL 26
|
||||
#define DHT12_PIN 16
|
||||
#define BAT_ADC 33
|
||||
#define SALT_PIN 34
|
||||
#define SOIL_PIN 32
|
||||
#define BOOT_PIN 0
|
||||
#define USER_BUTTON 35
|
||||
#define DS18B20_PIN 21
|
||||
|
||||
#include "sensor_setup.h"
|
||||
#include <fdrs_sensor.h>
|
||||
#include <BH1750.h>
|
||||
#include <Adafruit_BME280.h>
|
||||
|
||||
BH1750 lightMeter(0x23); //0x23
|
||||
Adafruit_BME280 bmp; //0x77
|
||||
RTC_DATA_ATTR int the_count = 0;
|
||||
|
||||
void setup() {
|
||||
|
||||
//Init Sensors
|
||||
Wire.begin(I2C_SDA, I2C_SCL);
|
||||
while (!bmp.begin()) {
|
||||
//Serial.println("bmp");
|
||||
delay(10);
|
||||
}
|
||||
|
||||
if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
|
||||
Serial.println(F("BH1750 Advanced begin"));
|
||||
} else {
|
||||
Serial.println(F("Error initialising BH1750"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void loadData() {
|
||||
float s_battery = readBattery();
|
||||
float bme_temp = bmp.readTemperature();
|
||||
float bme_pressure = (bmp.readPressure() / 100.0F);
|
||||
//float bme_altitude = bmp.readAltitude(1013.25);
|
||||
float bme_humidity = bmp.readHumidity();
|
||||
float s_soil = readSoil();
|
||||
float s_salt = readSalt();
|
||||
while (! lightMeter.measurementReady()) {
|
||||
delay(10);
|
||||
}
|
||||
float lux = lightMeter.readLightLevel();
|
||||
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));
|
||||
|
||||
loadFDRS(bme_temp, TEMP_T);
|
||||
|
||||
loadFDRS(bme_humidity, HUMIDITY_T);
|
||||
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
uint32_t readSalt() //Soil Electrodes: This code came from the LilyGo documentation.
|
||||
{
|
||||
uint8_t samples = 120;
|
||||
uint32_t humi = 0;
|
||||
uint16_t array[120];
|
||||
for (int i = 0; i < samples; i++) {
|
||||
array[i] = analogRead(SALT_PIN);
|
||||
delay(2);
|
||||
}
|
||||
std::sort(array, array + samples);
|
||||
for (int i = 0; i < samples; i++) {
|
||||
if (i == 0 || i == samples - 1)continue;
|
||||
humi += array[i];
|
||||
}
|
||||
humi /= samples - 2;
|
||||
return humi;
|
||||
}
|
||||
|
||||
uint16_t readSoil() //Soil Capacitance: This code came from the LilyGo documentation.
|
||||
{
|
||||
uint16_t soil = analogRead(SOIL_PIN);
|
||||
return map(soil, 0, 4095, 100, 0);
|
||||
}
|
||||
|
||||
float readBattery() //Battery Voltage: This code came from the LilyGo documentation.
|
||||
{
|
||||
int vref = 1100;
|
||||
uint16_t volt = analogRead(BAT_ADC);
|
||||
float battery_voltage = ((float)volt / 4095.0) * 2.0 * 3.3 * (vref);
|
||||
return battery_voltage;
|
||||
}
|
||||
void loop() {
|
||||
}
|
@ -1,45 +1,45 @@
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// Multifunction ESP8266 Sensor Board by Phil Grant
|
||||
//
|
||||
// https://github.com/gadjet/Multifunction-ESP8266-Sensor-board
|
||||
//
|
||||
|
||||
#include "sensor_setup.h"
|
||||
#include <Adafruit_AHT10.h>
|
||||
#include <fdrs_sensor.h>
|
||||
|
||||
Adafruit_AHT10 aht;
|
||||
|
||||
const int reedSwitch = 13;
|
||||
|
||||
void setup() {
|
||||
aht.begin();
|
||||
|
||||
// Init Serial Monitor
|
||||
//Serial.begin(115200);
|
||||
// initialize the reed switch pin as an input:
|
||||
pinMode(reedSwitch, INPUT);
|
||||
// initialize the wakeup pin as an input:
|
||||
pinMode(16, WAKEUP_PULLUP);
|
||||
beginFDRS();
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
sensors_event_t humidity, temp;
|
||||
aht.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
|
||||
// Read the state of the reed switch and send open or closed
|
||||
if (digitalRead(reedSwitch) == HIGH) {
|
||||
loadFDRS(1.0, MOTION_T);
|
||||
}
|
||||
else {
|
||||
loadFDRS(0.0, MOTION_T);
|
||||
}
|
||||
loadFDRS((analogRead(A0) * 4.2 * 10 / 1023), VOLTAGE_T);
|
||||
loadFDRS(humidity.relative_humidity, HUMIDITY_T);
|
||||
loadFDRS(temp.temperature, TEMP_T);
|
||||
// Send message via FDRS
|
||||
sendFDRS();
|
||||
sleepFDRS(15); //15 Min's sleep
|
||||
}
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// Multifunction ESP8266 Sensor Board by Phil Grant
|
||||
//
|
||||
// https://github.com/gadjet/Multifunction-ESP8266-Sensor-board
|
||||
//
|
||||
|
||||
#include "sensor_setup.h"
|
||||
#include <Adafruit_AHT10.h>
|
||||
#include <fdrs_sensor.h>
|
||||
|
||||
Adafruit_AHT10 aht;
|
||||
|
||||
const int reedSwitch = 13;
|
||||
|
||||
void setup() {
|
||||
aht.begin();
|
||||
|
||||
// Init Serial Monitor
|
||||
//Serial.begin(115200);
|
||||
// initialize the reed switch pin as an input:
|
||||
pinMode(reedSwitch, INPUT);
|
||||
// initialize the wakeup pin as an input:
|
||||
pinMode(16, WAKEUP_PULLUP);
|
||||
beginFDRS();
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
sensors_event_t humidity, temp;
|
||||
aht.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
|
||||
// Read the state of the reed switch and send open or closed
|
||||
if (digitalRead(reedSwitch) == HIGH) {
|
||||
loadFDRS(1.0, MOTION_T);
|
||||
}
|
||||
else {
|
||||
loadFDRS(0.0, MOTION_T);
|
||||
}
|
||||
loadFDRS((analogRead(A0) * 4.2 * 10 / 1023), VOLTAGE_T);
|
||||
loadFDRS(humidity.relative_humidity, HUMIDITY_T);
|
||||
loadFDRS(temp.temperature, TEMP_T);
|
||||
// Send message via FDRS
|
||||
sendFDRS();
|
||||
sleepFDRS(15); //15 Min's sleep
|
||||
}
|
@ -53,6 +53,8 @@ std::vector<DataReading_t> lora_peer_2_data;
|
||||
|
||||
#if defined(MQTT_GET) || defined(MQTT_SEND)
|
||||
MQTT_FDRSGateWay MQTT(WIFI_SSID,WIFI_PASS,MQTT_ADDR,MQTT_PORT);
|
||||
// TODO: should be:
|
||||
//MQTT_FDRSGateWay MQTT(GLOBAL_SSID,GLOBAL_PASS,GLOBAL_MQTT_ADDR,GLOBAL_MQTT_PORT);
|
||||
std::vector<DataReading_t> mqtt_data;
|
||||
#endif
|
||||
|
@ -2,7 +2,7 @@
|
||||
//
|
||||
// GATEWAY 2.000 Configuration
|
||||
|
||||
//#include <fdrs_globals.h> //Uncomment if you install the globals file
|
||||
#include <fdrs_globals.h> // uncomment if you want to set specific values for this sensor in sensor_setup.h
|
||||
#define DEBUG
|
||||
|
||||
#define MAC_PREFIX 0xAA, 0xBB, 0xCC, 0xDD, 0xEE // Should only be changed if implementing multiple FDRS systems.
|
||||
@ -33,6 +33,7 @@
|
||||
#define ESPNOW_PEER_1 0x0C // ESPNOW1 Address
|
||||
#define ESPNOW_PEER_2 0x0D // ESPNOW2 Address
|
||||
|
||||
// TODO: Needs to be commented out if FDRS_GLOBALS are assigned
|
||||
//WiFi and MQTT Credentials -- Needed only for MQTT gateway
|
||||
#define WIFI_SSID "Your SSID"
|
||||
#define WIFI_PASS "Your Password"
|
||||
@ -53,6 +54,7 @@
|
||||
//433E6 for Asia
|
||||
//866E6 for Europe
|
||||
//915E6 for North America
|
||||
// TODO: Needs to be commented out if FDRS_GLOBALS are assigned
|
||||
#define BAND 915E6
|
||||
#define SF 7
|
||||
|
@ -10,7 +10,7 @@
|
||||
#define __FDRS_SENSOR__H__
|
||||
|
||||
#include "fdrs_types.h"
|
||||
#include <FDRS_datatypes.h>
|
||||
#include "fdrs_datatypes.h"
|
||||
|
||||
//1 to enable debugging prints. 0 disables the debugging prints
|
||||
#define ENABLE_DEBUG 1
|
@ -1,3 +1,6 @@
|
||||
// ToDo: refactor the global GLOBALS to FDRS_GLOBALS as GLOBALS is ... well, too global ;)
|
||||
#ifndef GLOBALS
|
||||
#define GLOBALS
|
||||
#define GLOBAL_SSID "Your SSID"
|
||||
#define GLOBAL_PASS "Password"
|
||||
#define GLOBAL_MQTT_ADDR "192.168.0.8"
|
||||
@ -9,4 +12,4 @@
|
||||
#define GLOBAL_BAND 915E6 //LoRa Frequency Band
|
||||
#define GLOBAL_SF 7 //LoRa Spreading Factor
|
||||
|
||||
#define GLOBALS
|
||||
#endif GLOBALS
|
||||
|
29
keywords.txt
Normal file
29
keywords.txt
Normal file
@ -0,0 +1,29 @@
|
||||
##########################################################
|
||||
# Syntax Coloring Map For Farm-Data-Relay-System
|
||||
##########################################################
|
||||
|
||||
##########################################################
|
||||
# Datatypes and Class names (KEYWORD1)
|
||||
##########################################################
|
||||
|
||||
DataReading KEYWORD1
|
||||
|
||||
##########################################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
##########################################################
|
||||
|
||||
beginFDRS KEYWORD2
|
||||
loadFDRS KEYWORD2
|
||||
sendFDRS KEYWORD2
|
||||
sleepFDRS KEYWORD2
|
||||
DBG KEYWORD2
|
||||
getLoRa KEYWORD2
|
||||
sendESPNOW KEYWORD2
|
||||
sendSerial KEYWORD2
|
||||
sendMQTT KEYWORD2
|
||||
bufferESPNOW KEYWORD2
|
||||
bufferSerial KEYWORD2
|
||||
bufferMQTT KEYWORD2
|
||||
bufferLoRa KEYWORD2
|
||||
transmitLoRa KEYWORD2
|
||||
getSerial KEYWORD2
|
10
library.properties
Normal file
10
library.properties
Normal file
@ -0,0 +1,10 @@
|
||||
name=Farm-Data-Relay-System
|
||||
version=2.0.1
|
||||
author=Timm Bogner <timmbogner@gmail.com>
|
||||
maintainer=Timm Bogner <timmbogner@gmail.com>
|
||||
sentence=Farm-Data-Relay-System - a infrastructure and cloud-less network
|
||||
paragraph=FDRS is a highly configurable cloud-agnostic network utilizing LoRa and ESPNow. Easily define nodes as sensors or routers and easily configure the way different protocols are forwarded.
|
||||
category=Communication
|
||||
url=https://github.com/timmbogner/Farm-Data-Relay-System
|
||||
architectures=*
|
||||
#includes=fdrs_globals.h,fdrs_sensor.h
|
Loading…
Reference in New Issue
Block a user