Merge branch 'dev' into okay-wait-now-i-really-got-this

This commit is contained in:
Timm Bogner 2022-07-11 18:59:23 -05:00
commit 5822d8e0d9
47 changed files with 1118 additions and 73 deletions

View File

@ -5,7 +5,7 @@
// Developed by Timm Bogner (timmbogner@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
//
#include "fdrs_config.h"
#include "fdrs_gateway_config.h"
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <espnow.h>

View File

@ -5,7 +5,7 @@
// Sends ESP-NOW packets at approximately 60Hz.
//
#include "sensor_setup.h"
#include "fdrs_sensor_config.h"
#include <fdrs_sensor.h>
void setup() {

View File

@ -4,7 +4,7 @@
// (This file will soon be known as 'sensor_config.h')
//
#include <fdrs_globals.h> // Comment if you want to set specific values for this sensor in sensor_setup.h
#include <fdrs_globals.h> // Comment if you want to set specific values for this sensor in fdrs_sensor_config.h
#define READING_ID 3 //Unique ID for this sensor
#define GTWY_MAC 0x04 //Address of the nearest gateway

View File

@ -6,7 +6,7 @@
// An example of how to send data using "fdrs_sensor.h".
//
#include "sensor_setup.h"
#include "fdrs_sensor_config.h"
#include <fdrs_sensor.h>
float data1;

View File

@ -4,7 +4,7 @@
// (This file will soon be known as 'sensor_config.h')
//
#include <fdrs_globals.h> // Comment if you want to set specific values for this sensor in sensor_setup.h
#include <fdrs_globals.h> // Comment if you want to set specific values for this sensor in fdrs_sensor_config.h
#define READING_ID 1 //Unique ID for this sensor
#define GTWY_MAC 0x04 //Address of the nearest gateway

View File

@ -6,7 +6,7 @@
// An example of how to send data using "fdrs_sensor.h".
//
#include "sensor_setup.h"
#include "fdrs_sensor_config.h"
#include <fdrs_sensor.h>
float data1;

View File

@ -4,7 +4,7 @@
// (This file will soon be known as 'sensor_config.h')
//
#include <fdrs_globals.h> // Comment if you want to set specific values for this sensor in sensor_setup.h
#include <fdrs_globals.h> // Comment if you want to set specific values for this sensor in fdrs_sensor_config.h
#define READING_ID 2 //Unique ID for this sensor
#define GTWY_MAC 0x03 //Address of the nearest gateway

View File

@ -5,7 +5,7 @@
// Developed by Timm Bogner (timmbogner@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
//
#include "fdrs_config.h"
#include "fdrs_gateway_config.h"
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <espnow.h>

View File

@ -5,7 +5,7 @@
// Developed by Timm Bogner (timmbogner@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
//
#include "fdrs_config.h"
#include "fdrs_gateway_config.h"
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <espnow.h>

View File

@ -5,7 +5,7 @@
// Developed by Timm Bogner (timmbogner@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
//
#include "fdrs_config.h"
#include "fdrs_gateway_config.h"
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <espnow.h>

View File

@ -4,7 +4,7 @@
//
// Developed by Timm Bogner (timmbogner@gmail.com) in Urbana, Illinois, USA.
#include "sensor_setup.h"
#include "fdrs_sensor_config.h"
#include <Adafruit_AHTX0.h>
#include <fdrs_sensor.h>

View File

@ -4,7 +4,7 @@
//
// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
#include "sensor_setup.h"
#include "fdrs_sensor_config.h"
#include <Adafruit_BME280.h>
#include <fdrs_sensor.h>

View File

@ -5,7 +5,7 @@
// 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 "fdrs_sensor_config.h"
#include <Adafruit_BMP280.h>
#include <fdrs_sensor.h>

View File

@ -3,7 +3,7 @@
// Modified by Timm Bogner for Farm Data Relay System -- Untested because I don't have a DHT sensor onhand.
#include "sensor_setup.h"
#include "fdrs_sensor_config.h"
#include <fdrs_sensor.h>
#include "DHT.h"

View File

@ -5,7 +5,7 @@
#define ONE_WIRE_BUS 13 //Pin that the DS18B20 is connected to
#include "sensor_setup.h"
#include "fdrs_sensor_config.h"
#include <fdrs_sensor.h>
#include <OneWire.h>
#include <DallasTemperature.h>

View File

@ -6,7 +6,7 @@
// Reads in GPS data from serial and sends latitude, longitude and altitude to a gateway.
//
#include "sensor_setup.h"
#include "fdrs_sensor_config.h"
#include <fdrs_sensor.h>
#define SERIAL1_RX 34 // TX pin of GPS sensor

View File

@ -14,7 +14,7 @@
#define USER_BUTTON 35
#define DS18B20_PIN 21
#include "sensor_setup.h"
#include "fdrs_sensor_config.h"
#include <fdrs_sensor.h>
#include <BH1750.h>
#include <Adafruit_BME280.h>

View File

@ -5,7 +5,7 @@
// https://github.com/gadjet/Multifunction-ESP8266-Sensor-board
//
#include "sensor_setup.h"
#include "fdrs_sensor_config.h"
#include <Adafruit_AHT10.h>
#include <fdrs_sensor.h>

View File

@ -6,7 +6,7 @@
#define REED_PIN 2
#include "sensor_setup.h"
#include "fdrs_sensor_config.h"
#include <fdrs_sensor.h>
unsigned int theCount = 0;

View File

@ -0,0 +1,44 @@
#include "fdrs_gateway_config.h"
#include "fdrs_gateway.h"
std::vector<DataReading_t> espnow_peer_unknown_data;
std::vector<DataReading_t> espnow_peer_1_data;
std::vector<DataReading_t> espnow_peer_2_data;
uint8_t selfAddress[6] = {MAC_PREFIX, UNIT_MAC};
uint8_t ESPNOW1[] = {MAC_PREFIX, ESPNOW_PEER_1};
uint8_t ESPNOW2[] = {MAC_PREFIX, ESPNOW_PEER_2};
// ToDo: include global configuration values and use them here
//MQTT_FDRSGateWay MQTT(FDRS_WIFI_SSID,FDRS_WIFI_PASS,FDRS_MQTT_ADDR,FDRS_MQTT_PORT);
MQTT_FDRSGateWay MQTT(WIFI_SSID,WIFI_PASS,MQTT_ADDR,MQTT_PORT);
ESP_FDRSGateWay ESPNow;
void setup() {
MQTT.init();
ESPNow.init(selfAddress);
ESPNow.add_peer(ESPNOW1);
ESPNow.add_peer(ESPNOW2);
}
void loop() {
espnow_peer_unknown_data = ESPNow.get_unkown_peer_data();
espnow_peer_1_data = ESPNow.get_peer_data(ESPNOW1);
espnow_peer_2_data = ESPNow.get_peer_data(ESPNOW2);
MQTT.release(espnow_peer_unknown_data);
MQTT.release(espnow_peer_1_data);
MQTT.release(espnow_peer_2_data);
ESPNow.flush();
ESPNow.flush(ESPNOW1);
ESPNow.flush(ESPNOW2);
MQTT.flush();
delay(1000);
}

View File

@ -0,0 +1,687 @@
#include "fdrs_gateway.h"
// #define ESP8266
// #define ESP32
// #define USE_WIFI
bool ESP_FDRSGateWay::is_init = false;
std::vector<Peer_t> ESP_FDRSGateWay::_peer_list;
std::vector<Peer_t> ESP_FDRSGateWay::_unknow_peer;
std::vector<DataReading_t> MQTT_FDRSGateWay::_buffer;
std::vector<DataReading_t> Serial_FDRSGateWay::_buffer;
// Set ESP-NOW send and receive callbacks for either ESP8266 or ESP32
void ESP_FDRSGateWay::OnDataRecv(uint8_t * mac, const uint8_t *incomingData, int len){
DataReading_t data;
uint32_t i = 0;
uint32_t j = 0;
uint8_t d = len / sizeof(DataReading_t);
for(uint32_t i = 0; i < _peer_list.size();i++){
if(memcmp(_peer_list[i]._get_peer(),mac,6) == 0){
for(j = 0; j < d; j++){
memcpy(&data,&incomingData[j*sizeof(DataReading_t)],sizeof(DataReading_t));
_peer_list[i].buffer.push_back(data);
memset(&data,0,sizeof(DataReading_t));
}
return;
}
}
for(uint32_t i = 0; i < _unknow_peer.size();i++){
if(memcmp(_unknow_peer[i]._get_peer(),mac,6) == 0){
for(j = 0; j < d; j++){
memcpy(&data,&incomingData[j*sizeof(DataReading_t)],sizeof(DataReading_t));
_unknow_peer[i].buffer.push_back(data);
memset(&data,0,sizeof(DataReading_t));
}
return;
}
}
Peer_t peer;
peer._set_peer(mac);
_unknow_peer.push_back(peer);
for(j = 0; j < d; j++){
memcpy(&data,&incomingData[j*sizeof(DataReading_t)],sizeof(DataReading_t));
_unknow_peer.back().buffer.push_back(data);
memset(&data,0,sizeof(DataReading_t));
}
}
#if defined(ESP8266)
void ESP8266OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
}
void ESP8266OnDataRecv(uint8_t* mac, uint8_t *incomingData, uint8_t len) {
ESP_FDRSGateWay::OnDataRecv((uint8_t*)mac,*(const uint8_t *)incomingData,len);
}
#endif
#if defined(ESP32)
void ESP32OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
}
void ESP32OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
ESP_FDRSGateWay::OnDataRecv((uint8_t*)mac,incomingData,len);
}
#endif
FDRSGateWayBase::FDRSGateWayBase(){
}
FDRSGateWayBase::~FDRSGateWayBase(){
}
void FDRSGateWayBase::release(std::vector<DataReading_t> data,uint8_t *peer_mac){
if(peer_mac == NULL){
send(data);
}
forward(peer_mac ,data);
}
ESP_FDRSGateWay::ESP_FDRSGateWay(void)
{
memset(_broadcast_mac,0xFF,6);
memset(_inturnal_mac,0,6);
}
void ESP_FDRSGateWay::init(uint8_t inturnal_mac[5]){
memcpy(_inturnal_mac,inturnal_mac,6);
#if defined(ESP8266)
wifi_set_macaddr(STATION_IF, _inturnal_mac);
#endif
#if defined(ESP32)
esp_wifi_set_mac(WIFI_IF_STA, &_inturnal_mac[0]);
#endif
ESP_FDRSGateWay::setup();
#if defined(ESP32)
esp_now_peer_info_t peerInfo;
memset(&peerInfo, 0, sizeof(peerInfo));
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;
}
#endif
}
void ESP_FDRSGateWay::setup(void){
if(is_init){
return;
}
is_init = true;
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(ESP8266OnDataSent);
esp_now_register_recv_cb(ESP8266OnDataRecv);
#endif
#if defined(ESP32)
if(esp_now_init() != ESP_OK) {
DBG("Error initializing ESP-NOW");
return;
}
esp_now_register_send_cb(ESP32OnDataSent);
esp_now_register_recv_cb(ESP32OnDataRecv);
#endif
DBG("ESP-NOW Initialized.");
}
void ESP_FDRSGateWay::add_peer(uint8_t peer_mac[6]){
uint32_t i = 0;
for(uint32_t i = 0; i < _peer_list.size();i++){
if(memcmp(_peer_list[i]._get_peer(),peer_mac,6) == 0){
return;
}
}
list_peer(peer_mac);
Peer_t peer;
peer._set_peer(peer_mac);
//esp_now_del_peer(NEWPEER);
_peer_list.push_back(peer);
}
void ESP_FDRSGateWay::remove_peer(uint8_t peer_mac[6]){
unlist_peer(peer_mac);
if(_peer_list.size() == 0){
return;
}
Peer_t peer;
peer._set_peer(peer_mac);
_peer_list.erase(std::find(_peer_list.begin(),_peer_list.end(),peer));
}
void ESP_FDRSGateWay::list_peer(uint8_t peer_mac[6]){
#if defined(ESP8266)
esp_now_add_peer(peer_mac, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
#endif
#if defined(ESP32)
esp_now_peer_info_t peerInfo;
memset(&peerInfo, 0, sizeof(peerInfo));
peerInfo.channel = 0;
peerInfo.encrypt = false;
memcpy(peerInfo.peer_addr, peer_mac, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
DBG("Failed to add peer 1");
return;
}
#endif
}
void ESP_FDRSGateWay::unlist_peer(uint8_t peer_mac[6]){
esp_now_del_peer(peer_mac);
}
std::vector<DataReading_t> ESP_FDRSGateWay::get_peer_data(uint8_t *peer_mac){
for(uint32_t i = 0; i < _peer_list.size();i++){
if(memcmp(_peer_list[i]._get_peer(),peer_mac,6) == 0){
return _peer_list[i].buffer;
}
}
return std::vector<DataReading_t>();
}
std::vector<DataReading_t> ESP_FDRSGateWay::get_unkown_peer_data(void){
std::vector<DataReading_t> data;
for(uint32_t i = 0; i < _unknow_peer.size(); i++){
data.insert(data.end(),_unknow_peer[i].buffer.begin(),_unknow_peer[i].buffer.end());
}
return data;
}
void ESP_FDRSGateWay::flush(uint8_t *peer_mac){
if(peer_mac == NULL){
for(uint32_t i = 0; i < _unknow_peer.size(); i++){
_unknow_peer[i].buffer.clear();
return;
}
}
for(uint32_t i = 0; i < _peer_list.size();i++){
if(memcmp(_peer_list[i]._get_peer(),peer_mac,6) == 0){
_peer_list[i].buffer.clear();
return;
}
}
}
void ESP_FDRSGateWay::send(std::vector<DataReading_t> data){
const uint8_t espnow_size = 250 / sizeof(DataReading_t);
uint32_t i = 0;
for(i = 0; i < _unknow_peer.size(); i++){
list_peer(_unknow_peer[i]._get_peer());
}
uint8_t d = data.size() / espnow_size;
DataReading_t buffer1[d * sizeof(DataReading_t)];
memset(buffer1,0,d * sizeof(DataReading_t));
uint32_t j = 0;
for(i = 0; i < data.size(); i++){
buffer1[j++] = data[i];
if(j >= d){
esp_now_send(NULL, (uint8_t *) buffer1, d * sizeof(DataReading_t));
memset(buffer1,0,d * sizeof(DataReading_t));
j = 0;
delay(10);
}
}
if(j != 0){
esp_now_send(NULL, (uint8_t *) buffer1, j * sizeof(DataReading_t));
}
for(i = 0; i < _unknow_peer.size(); i++){
unlist_peer(_unknow_peer[i]._get_peer());
}
_unknow_peer.clear();
}
void ESP_FDRSGateWay::forward(uint8_t *peer_mac ,std::vector<DataReading_t> data){
const uint8_t espnow_size = 250 / sizeof(DataReading_t);
uint8_t d = data.size() / espnow_size;
uint32_t i = 0;
DataReading_t buffer1[d * sizeof(DataReading_t)];
memset(buffer1,0,d * sizeof(DataReading_t));
uint32_t j = 0;
for(i = 0; i < data.size(); i++){
buffer1[j++] = data[i];
if(j >= d){
esp_now_send(NULL, (uint8_t *) buffer1, d * sizeof(DataReading_t));
memset(buffer1,0,d * sizeof(DataReading_t));
j = 0;
delay(10);
}
}
if(j != 0){
esp_now_send(NULL, (uint8_t *) buffer1, j * sizeof(DataReading_t));
}
esp_now_send(peer_mac, (uint8_t *) buffer1, d * sizeof(DataReading_t));
}
MQTT_FDRSGateWay::MQTT_FDRSGateWay(const char *ssid, const char *password, const char *server,int port):
_ssid(ssid),
_password(password),
_server(server),
_port(port)
{
_client = new PubSubClient(espClient);
}
MQTT_FDRSGateWay::~MQTT_FDRSGateWay(void){
delete _client;
}
void MQTT_FDRSGateWay::mqtt_callback(char* topic, byte * message, unsigned int length){
//No point in reading topics that are not data.
if(strcmp(TOPIC_DATA,topic) != 0){
return;
}
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;
}
int s = doc.size();
//UART_IF.println(s);
DataReading_t data;
memset(&data,0,sizeof(DataReading_t));
for (int i = 0; i < s; i++) {
data.id = doc[i]["id"];
data.type = doc[i]["type"];
data.data = doc[i]["data"];
_buffer.push_back(data);
memset(&data,0,sizeof(DataReading_t));
}
DBG("Incoming MQTT.");
}
void MQTT_FDRSGateWay::init(void){
delay(10);
WiFi.begin(_ssid, _password);
while (WiFi.status() != WL_CONNECTED) {
DBG("Connecting to WiFi... ");
DBG(_ssid);
delay(500);
}
DBG("WiFi Connected");
_client->setServer(_server, _port);
if (!_client->connected()) {
DBG("Connecting MQTT...");
reconnect();
}
DBG("MQTT Connected");
_client->setCallback(MQTT_FDRSGateWay::mqtt_callback);
_client->publish(TOPIC_STATUS, "FDRS initialized");
}
void MQTT_FDRSGateWay::reconnect() {
// Loop until reconnected
while (!_client->connected()) {
// Attempt to connect
if (_client->connect("FDRS_GATEWAY")) {
// Subscribe
_client->subscribe(TOPIC_COMMAND);
break;
}
DBG("Connecting MQTT.");
delay(5000);
}
}
void MQTT_FDRSGateWay::send(std::vector<DataReading_t> data) {
DBG("Releasing MQTT.");
DynamicJsonDocument doc(24576);
for (int i = 0; i < data.size(); i++) {
doc[i]["id"] = data[i].id;
doc[i]["type"] = data[i].type;
doc[i]["data"] = data[i].data;
}
String outgoingString;
serializeJson(doc, outgoingString);
_client->publish(TOPIC_DATA, (char*) outgoingString.c_str());
reconnect();
_client->loop();
}
void MQTT_FDRSGateWay::forward(uint8_t *peer_mac ,std::vector<DataReading_t> data){
//does nothing. just here implement the pure virtule from the base class.
send(data);
}
std::vector<DataReading_t> MQTT_FDRSGateWay::get_data(void){
return _buffer;
}
void MQTT_FDRSGateWay::flush(uint8_t *peer_mac){
_buffer.clear();
}
Serial_FDRSGateWay::Serial_FDRSGateWay(HardwareSerial *serial, uint32_t baud):
_serial(serial),
_baud(baud)
{
}
void Serial_FDRSGateWay::init(void){
_serial->begin(_baud);
}
#if defined(ESP32)
void Serial_FDRSGateWay::init(int mode, int rx_pin, int tx_pin){
_serial->begin(_baud,mode,rx_pin,tx_pin);
}
#endif
void Serial_FDRSGateWay::pull(void){
//TDDO: This is blocking. Some method of escaping is required.
// At the momment we are just hoping we get a \n
String incomingString = _serial->readStringUntil('\n');
DynamicJsonDocument doc(24576);
DeserializationError error = deserializeJson(doc, incomingString);
// Test if parsing succeeds.
if (error) {
// DBG("json parse err");
// DBG(incomingString);
return;
}
int s = doc.size();
//UART_IF.println(s);
DataReading_t data;
memset(&data,0,sizeof(DataReading_t));
for (int i = 0; i < s; i++) {
data.id = doc[i]["id"];
data.type = doc[i]["type"];
data.data = doc[i]["data"];
_buffer.push_back(data);
memset(&data,0,sizeof(DataReading_t));
}
DBG("Incoming Serial.");
}
std::vector<DataReading_t> Serial_FDRSGateWay::get_data(void){
return _buffer;
}
void Serial_FDRSGateWay::get(void){
while(_serial->available()){
pull();
}
}
void Serial_FDRSGateWay::send(std::vector<DataReading_t> data){
DBG("Releasing Serial.");
DynamicJsonDocument doc(24576);
for (int i = 0; i < data.size(); i++) {
doc[i]["id"] = data[i].id;
doc[i]["type"] = data[i].type;
doc[i]["data"] = data[i].data;
}
serializeJson(doc, *_serial);
_serial->println();
}
void Serial_FDRSGateWay::forward(uint8_t *peer_mac ,std::vector<DataReading_t> data){
//does nothing. just here implement the pure virtule from the base class.
}
void Serial_FDRSGateWay::flush(uint8_t *peer_mac){
_buffer.clear();
}
LoRa_FDRSGateWay::LoRa_FDRSGateWay(uint8_t miso,uint8_t mosi,uint8_t sck, uint8_t ss,uint8_t rst,uint8_t dio0,double band,uint8_t sf):
_miso(miso),
_mosi(mosi),
_sck(sck),
_ss(ss),
_rst(rst),
_dio0(dio0),
_band(band),
_sf(sf)
{
memset(_mac,0,6);
_peer_list.clear();
}
void LoRa_FDRSGateWay::init(uint8_t mac[6]){
memcpy(_mac,mac,6);
DBG("Initializing LoRa!");
#ifdef ESP32
SPI.begin(_sck, _miso, _mosi);
#endif
LoRa.setPins(_ss, _rst, _dio0);
if (!LoRa.begin(_band)) {
DBG(" LoRa initialize failed");
return;
}
LoRa.setSpreadingFactor(_sf);
DBG(" LoRa initialized.");
}
void LoRa_FDRSGateWay::add_peer(uint8_t peer_mac[6]){
uint32_t i = 0;
for(uint32_t i = 0; i < _peer_list.size();i++){
if(memcmp(_peer_list[i]._get_peer(),peer_mac,6) == 0){
return;
}
}
Peer_t peer;
peer._set_peer(peer_mac);
_peer_list.push_back(peer);
}
void LoRa_FDRSGateWay::remove_peer(uint8_t peer_mac[6]){
if(_peer_list.size() == 0){
return;
}
Peer_t peer;
peer._set_peer(peer_mac);
_peer_list.erase(std::find(_peer_list.begin(),_peer_list.end(),peer));
}
void LoRa_FDRSGateWay::get(void){
int packetSize = LoRa.parsePacket();
if (packetSize== 0) {
return;
}
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);
// }
//Check if addressed to this device
if (memcmp(&packet, &_mac[3], 3) != 0) {
return;
}
uint8_t theData[packetSize - 5];
memcpy(&incLORAMAC, &packet[3], 2); //Split off address portion of packet
memcpy(&theData, &packet[5], packetSize - 5); //Split off data portion of packet
//Check if it is from a registered sender
for(uint32_t i = 0; i < _peer_list.size();i++){
if(memcmp(&_peer_list[i].peer[3],incLORAMAC,2) == 0){
DataReading_t data;
uint32_t i = 0;
uint8_t d = packetSize / sizeof(DataReading_t);
memset(&data,0,sizeof(DataReading_t));
for(uint32_t j = 0; j < d; j++){
memcpy(&data,&theData[j*sizeof(DataReading_t)],sizeof(DataReading_t));
_peer_list[i].buffer.push_back(data);
memset(&data,0,sizeof(DataReading_t));
}
return;
}
}
DBG("Incoming LoRa.");
}
std::vector<DataReading_t> LoRa_FDRSGateWay::get_peer_data(uint8_t *peer_mac){
for(uint32_t i = 0; i < _peer_list.size();i++){
if(memcmp(_peer_list[i]._get_peer(),peer_mac,6) == 0){
return _peer_list[i].buffer;
}
}
return std::vector<DataReading_t>();
}
void LoRa_FDRSGateWay::send(std::vector<DataReading_t> data){
const uint8_t espnow_size = 250 / sizeof(DataReading_t);
uint32_t i = 0;
uint8_t d = data.size() / espnow_size;
DataReading_t buffer1[d];
for(i = 0; i < d; i++){
buffer1[i] = data[i];
}
transmit(buffer1, d * sizeof(DataReading_t));
}
void LoRa_FDRSGateWay::forward(uint8_t *peer_mac ,std::vector<DataReading_t> data){
//TODO: add peer forwading.
}
void LoRa_FDRSGateWay::transmit(DataReading_t *packet, uint8_t len) {
DBG("Transmitting LoRa.");
uint8_t pkt[5 + (len * sizeof(DataReading_t))];
memcpy(&pkt, _mac, 3);
memcpy(&pkt[3], &_mac[4], 2);
memcpy(&pkt[5], packet, len * sizeof(DataReading_t));
LoRa.beginPacket();
LoRa.write((uint8_t*)&pkt, sizeof(pkt));
LoRa.endPacket();
}
void LoRa_FDRSGateWay::flush(uint8_t *peer_mac){
if(peer_mac == NULL){
return;
}
for(uint32_t i = 0; i < _peer_list.size();i++){
if(memcmp(_peer_list[i]._get_peer(),peer_mac,6) == 0){
_peer_list[i].buffer.clear();
return;
}
}
}

View File

@ -0,0 +1,187 @@
/* FARM DATA RELAY SYSTEM
*
* "fdrs_sensor.h"
*
* Developed by Timm Bogner (timmbogner@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
* Condensed and refactored to a single file pair by Binder Tronics (info@bindertronics.com).
*/
#ifndef __FDRS_GATEWAY_H__
#define __FDRS_GATEWAY_H__
#include "HardwareSerial.h"
#include "fdrs_types.h"
#include <string.h>
#include <vector>
#include <memory>
#include <algorithm>
#include <esp_now.h>
#include <WiFi.h>
#include <esp_wifi.h>
#include "ArduinoJson.h"
#include "PubSubClient.h"
#include "LoRa.h"
#define USE_LORA
#define FDRS_DEBUG
#ifdef FDRS_DEBUG
#define DBG(a) (Serial.println(a))
#else
#define DBG(a)
#endif
class FDRSGateWayBase{
public:
FDRSGateWayBase();
~FDRSGateWayBase();
void release(std::vector<DataReading_t> data, uint8_t *peer_mac = NULL);
virtual void flush(uint8_t *peer_mac = NULL) = 0;
private:
static uint32_t peer_id;
virtual void send(std::vector<DataReading_t> data) = 0;
virtual void forward(uint8_t *peer_mac ,std::vector<DataReading_t> data) = 0;
};
class ESP_FDRSGateWay: public FDRSGateWayBase{
public:
ESP_FDRSGateWay(void);
static void OnDataRecv(uint8_t * mac, const uint8_t *incomingData, int len);
void init(uint8_t inturnal_mac[5]);
void add_peer(uint8_t peer_mac[6]);
void remove_peer(uint8_t peer_mac[6]);
std::vector<DataReading_t> get_peer_data(uint8_t peer_mac[6]);
std::vector<DataReading_t> get_unkown_peer_data(void);
void flush(uint8_t *peer_mac = NULL) override;
private:
static bool is_init;
uint8_t _broadcast_mac[6];
uint8_t _inturnal_mac[6];
static std::vector<Peer_t> _peer_list;
static std::vector<Peer_t> _unknow_peer;
static void setup(void);
void send(std::vector<DataReading_t> data) override;
void forward(uint8_t *peer_mac ,std::vector<DataReading_t> data) override;
void list_peer(uint8_t peer_mac[6]);
void unlist_peer(uint8_t peer_mac[6]);
};
class MQTT_FDRSGateWay: public FDRSGateWayBase{
public:
MQTT_FDRSGateWay(const char *ssid, const char *password, const char *server,int port = 1883);
~MQTT_FDRSGateWay(void);
void init(void);
std::vector<DataReading_t> get_data(void);
void flush(uint8_t *peer_mac = NULL) override;
private:
#define TOPIC_DATA "fdrs/data"
#define TOPIC_STATUS "fdrs/status"
#define TOPIC_COMMAND "fdrs/command"
static void mqtt_callback(char* topic, byte * message, unsigned int length);
const char *_ssid;
const char *_password;
const char *_server;
int _port;
WiFiClient espClient;
PubSubClient *_client;
static std::vector<DataReading_t> _buffer;
void reconnect();
void send(std::vector<DataReading_t> data) override;
void forward(uint8_t *peer_mac ,std::vector<DataReading_t> data) override;
};
class Serial_FDRSGateWay: public FDRSGateWayBase{
public:
Serial_FDRSGateWay(HardwareSerial *serial, uint32_t baud);
void init(void);
#if defined(ESP32)
void init(int mode, int rx_pin, int tx_pin);
#endif
void get(void);
std::vector<DataReading_t> get_data(void);
void flush(uint8_t *peer_mac = NULL) override;
private:
HardwareSerial *_serial;
uint32_t _baud;
static std::vector<DataReading_t> _buffer;
static void setup(void);
void pull(void);
void send(std::vector<DataReading_t> data) override;
void forward(uint8_t *peer_mac ,std::vector<DataReading_t> data) override;
};
class LoRa_FDRSGateWay: public FDRSGateWayBase{
public:
LoRa_FDRSGateWay(uint8_t miso,uint8_t mosi,uint8_t sck, uint8_t ss,uint8_t rst,uint8_t dio0,double band,uint8_t sf);
void init(uint8_t mac[6]);
void get(void);
void add_peer(uint8_t peer_mac[6]);
void remove_peer(uint8_t peer_mac[6]);
std::vector<DataReading_t> get_peer_data(uint8_t *peer_mac);
void flush(uint8_t *peer_mac = NULL) override;
private:
uint8_t _mac[6];
uint8_t _miso;
uint8_t _mosi;
uint8_t _sck;
uint8_t _ss;
uint8_t _rst;
uint8_t _dio0;
uint32_t _band;
uint8_t _sf;
std::vector<Peer_t> _peer_list;
void transmit(DataReading_t *packet, uint8_t len);
void send(std::vector<DataReading_t> data) override;
void forward(uint8_t *peer_mac ,std::vector<DataReading_t> data) override;
};
#endif

View File

@ -57,7 +57,7 @@
//866E6 for Europe
//915E6 for North America
// TODO: Needs to be commented out if FDRS_GLOBALS are assigned
#define LORA_BAND 915E6
#define LORA_BAND 866E6
#define LORA_SF 7
//#define USE_LED //Not yet fully implemented

View File

@ -0,0 +1,39 @@
#ifndef __FDRS_TYPES_H__
#define __FDRS_TYPES_H__
#include <stdint.h>
#include <string.h>
#include <vector>
typedef struct __attribute__((packed)) DataReading_t {
float data;
uint16_t id;
uint8_t type;
DataReading_t(): data(0.0),id(0),type(0){
}
} DataReading_t;
typedef struct Peer_t{
uint8_t peer[6];
std::vector<DataReading_t> buffer;
Peer_t(){
memset(peer,0,6);
buffer.clear();
}
void _set_peer(uint8_t p[6]){
memcpy(peer,p,6);
}
uint8_t *_get_peer(void){
return peer;
}
bool operator==(Peer_t c){
return (memcmp(this->peer,c.peer,6) == 0);
}
}Peer_t;
#endif

View File

@ -5,13 +5,13 @@
// Under construction, feedback is appreciated!
//
#include "fdrs_config.h"
#include "fdrs_gateway_config.h"
#ifdef USE_LED
#include <FastLED.h>
#endif
#include "fdrs_gateway.h"
#include "fdrs_config.h"
#include "fdrs_gateway_config.h"
#ifdef USE_LED
CRGB leds[NUM_LEDS];

View File

@ -0,0 +1,65 @@
// FARM DATA RELAY SYSTEM
//
// GATEWAY 2.000 Configuration
#include <fdrs_globals.h> // uncomment if you want to set specific values for this sensor in fdrs_gateway_config.h
#define FDRS_DEBUG
#define MAC_PREFIX 0xAA, 0xBB, 0xCC, 0xDD, 0xEE // Should only be changed if implementing multiple FDRS systems.
#define UNIT_MAC 0x03 // The address of this gateway
//Where we get the data from
#define LORA_GET
#define MQTT_GET
#define ESP_GET
#define SER_GET
//Where we send the data to
#define LORA_SEND
#define MQTT_SEND
#define ESP_SEND
#define SER_SEND
//#define USE_LORA
//#define USE_WIFI //Used only for MQTT gateway
// Peer addresses
#define LORA_PEER_1 0x0E // LoRa1 Address
#define LORA_PEER_2 0x0F // LoRa2 Address
#define ESPNOW_ALL //send to all know and unknow peers
#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"
#define MQTT_ADDR "192.168.0.8"
#define MQTT_PORT 1883
//Pins for UART data interface (ESP32 only)
#define RXD2 14
#define TXD2 15
//SPI Configuration -- Needed only on Boards with multiple SPI interfaces like the ESP32
#define SPI_SCK 5
#define SPI_MISO 19
#define SPI_MOSI 27
//LoRa Configuration -- Needed only if using LoRa
#define LORA_SS 18
#define LORA_RST 14
#define LORA_DIO0 26
//433E6 for Asia
//866E6 for Europe
//915E6 for North America
// TODO: Needs to be commented out if FDRS_GLOBALS are assigned
#define LORA_BAND 915E6
#define LORA_SF 7
//#define USE_LED //Not yet fully implemented
#define LED_PIN 32
#define NUM_LEDS 4

View File

@ -8,7 +8,7 @@
//
#include "sensor_setup.h"
#include "fdrs_sensor_config.h"
#include "fdrs_sensor.h"
FDRSLoRa FDRS(GTWY_MAC,READING_ID,SPI_MISO,SPI_MOSI,SPI_SCK,LORA_SS,LORA_RST,LORA_DIO0,FDRS_BAND,FDRS_SF);

View File

@ -8,7 +8,7 @@ uint8_t LoRaAddress[] = {0x42, 0x00};
uint32_t wait_time = 0;
FDRSBase::FDRSBase(uint8_t gtwy_mac,uint8_t reading_id):
FDRSBase::FDRSBase(uint8_t gtwy_mac,uint16_t reading_id):
_gtwy_mac(gtwy_mac),
_espnow_size(250 / sizeof(DataReading_t)),
_reading_id(reading_id),
@ -77,7 +77,7 @@ void FDRSBase::sleep(int seconds){
}
FDRS_EspNow::FDRS_EspNow(uint8_t gtwy_mac,uint8_t reading_id):
FDRS_EspNow::FDRS_EspNow(uint8_t gtwy_mac,uint16_t reading_id):
FDRSBase(gtwy_mac,reading_id)
{
@ -129,7 +129,7 @@ void FDRS_EspNow::transmit(DataReading_t *fdrsData, uint8_t _data_count){
#ifdef USE_LORA
FDRSLoRa::FDRSLoRa(uint8_t gtwy_mac,
uint8_t reading_id,uint8_t miso,uint8_t mosi,uint8_t sck,
uint16_t reading_id,uint8_t miso,uint8_t mosi,uint8_t sck,
uint8_t ss,uint8_t rst,uint8_t dio0,uint32_t band,uint8_t sf):
FDRSBase(gtwy_mac,reading_id),
_miso(miso),

View File

@ -63,10 +63,10 @@ public:
* @brief Construct a new FDRSBase object
*
* @param gtwy_mac The network MAC
* @param reading_id The network identifier for the sensor
* @param reading_id The identifier for the sensor
*/
FDRSBase(uint8_t gtwy_mac,uint8_t reading_id);
FDRSBase(uint8_t gtwy_mac,uint16_t reading_id);
~FDRSBase();
/**
@ -101,7 +101,7 @@ private:
uint8_t _gtwy_mac;
const uint16_t _espnow_size;
uint8_t _reading_id;
uint16_t _reading_id;
uint8_t _data_count;
DataReading_t *fdrsData;
@ -111,7 +111,7 @@ private:
virtual void init(void) = 0;
/**
* @brief Required impalamnetation of how a sensor will send its data out on the network.
* @brief Required implementation of how a sensor will send its data out on the network.
*
* @param fdrsData Pointer to the data that the sensor will be seding.
* @param _data_count The number of data packets the sensor will be sending.
@ -127,10 +127,10 @@ public:
* @brief Construct a new fdrs espnow object
*
* @param gtwy_mac The network MAC.
* @param reading_id The network identifier for the sensor.
* @param reading_id The identifier for the sensor.
*/
FDRS_EspNow(uint8_t gtwy_mac, uint8_t reading_id);
FDRS_EspNow(uint8_t gtwy_mac, uint16_t reading_id);
private:
@ -159,7 +159,7 @@ public:
* @brief Construct a new FDRSLoRa object
*
* @param gtwy_mac The network MAC.
* @param reading_id The network identifier for the sensor.
* @param reading_id The identifier for the sensor.
* @param miso Master in slave out pin.
* @param mosi Master out slave in pin.
* @param sck Master clock pin.
@ -170,7 +170,7 @@ public:
* @param sf LoRa spread factor
*/
FDRSLoRa(uint8_t gtwy_mac, uint8_t reading_id,uint8_t miso,uint8_t mosi,uint8_t sck, uint8_t ss,uint8_t rst,uint8_t dio0,uint32_t band,uint8_t sf);
FDRSLoRa(uint8_t gtwy_mac, uint16_t reading_id,uint8_t miso,uint8_t mosi,uint8_t sck, uint8_t ss,uint8_t rst,uint8_t dio0,uint32_t band,uint8_t sf);
private:
@ -208,4 +208,4 @@ private:
};
#endif
#endif

View File

@ -0,0 +1,41 @@
// FARM DATA RELAY SYSTEM
//
// Sensor Configuration File
// By default global settings will be used for this sensor.
// If you need to change a specific setting, uncomment and set to your needs further below.
//
#define FDRS_DEBUG // Comment, if you do not want to see debug messages
#include <fdrs_globals.h> // Comment if you want to set specific values for this sensor here
#define READING_ID 1 //Unique ID for this sensor
#define GTWY_MAC 0x04 //Address of the nearest gateway
#define DEEP_SLEEP
//#define POWER_CTRL 14
// TODO: New, taken from Gateway - should be configured in the same way as there!
//Pins for UART data interface (ESP32 only)
#define RXD2 14
#define TXD2 15
// SPI Configuration -- Needed only on Boards with multiple SPI interfaces like the ESP32
#define SPI_SCK 5
#define SPI_MISO 19
#define SPI_MOSI 27
// LoRa Pin Configuration -- Uncomment and configure if you are using LoRa
#define LORA_SS 18
#define LORA_RST 14
#define LORA_DIO0 26
// LoRa Transport Configuration -- This should be set in the global configuration file. However, if you need a
// specific band and spreading factor for this sensor, configure below.
//433E6 for Asia
//866E6 for Europe
//915E6 for North America
//#define LORA_BAND 915E6
//#define LORA_SF 7
// TODO: Addd local MQTT configuration

View File

@ -1,29 +0,0 @@
// FARM DATA RELAY SYSTEM
//
// Sensor Configuration
// (This file will soon be known as 'sensor_config.h')
//
//#include <fdrs_globals.h> //Uncomment when you install the globals file
#define READING_ID 1 //Unique ID for this sensor
#define GTWY_MAC 0x04 //Address of the nearest gateway
#define DEEP_SLEEP
//#define POWER_CTRL 14
#define FDRS_DEBUG
//SPI Configuration -- Needed only on Boards with multiple SPI interfaces like the ESP32
#define SPI_SCK 5
#define SPI_MISO 19
#define SPI_MOSI 27
//LoRa Configuration
#define LORA_SS 18
#define LORA_RST 14
#define LORA_DIO0 26
//433E6 for Asia
//866E6 for Europe
//915E6 for North America
#define LORA_BAND 915E6
#define LORA_SF 7

View File

@ -18,8 +18,8 @@
#endif
#ifdef FDRS_GLOBALS
#define FDRS_BAND GLOBAL_BAND
#define FDRS_SF GLOBAL_SF
#define FDRS_BAND GLOBAL_LORA_BAND
#define FDRS_SF GLOBAL_LORA_SF
#else
#define FDRS_BAND LORA_BAND
#define FDRS_SF LORA_SF
@ -92,8 +92,8 @@ void beginFDRS() {
#endif
#ifdef USE_LORA
DBG("Initializing LoRa!");
DBG(LORA_BAND);
DBG(LORA_SF);
DBG(FDRS_BAND);
DBG(FDRS_SF);
#ifdef ESP32
SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI);
#endif

View File

@ -6,12 +6,15 @@
# Datatypes and Class names (KEYWORD1)
##########################################################
DataReading KEYWORD1
FDRSBase KEYWORD1
FDRSLoRa KEYWORD1
FDRS_EspNow KEYWORD1
##########################################################
# Methods and Functions (KEYWORD2)
##########################################################
FDRS_DBG KEYWORD2
begin KEYWORD2
beginFDRS KEYWORD2
bufferESPNOW KEYWORD2
bufferLoRa KEYWORD2
@ -19,26 +22,34 @@ bufferMQTT KEYWORD2
bufferSerial KEYWORD2
getLoRa KEYWORD2
getSerial KEYWORD2
load KEYWORD2
loadFDRS KEYWORD2
sendESPNOW KEYWORD2
sendFDRS KEYWORD2
sendMQTT KEYWORD2
sendSerial KEYWORD2
sleep KEYWORD2
sleepFDRS KEYWORD2
transmitLoRa KEYWORD2
##########################################################
# Literals (LITERAL1)
##########################################################
FDRS_DEBUG LITERAL1
DEEP_SLEEP LITERAL1
FDRS_BAND LITERAL1
FDRS_DEBUG LITERAL1
FDRS_GLOBALS LITERAL1
FDRS_SF LITERAL1
//GTWY_MAC LITERAL1
GTWY_MAC LITERAL1
GTWY_MAC LITERAL1
LORA_DIO0 LITERAL1
LORA_RST LITERAL1
LORA_SS LITERAL1
MAC_PREFIX LITERAL1
POWER_CTRL LITERAL1
READING_ID LITERAL1
SPI_MISO LITERAL1
SPI_MOSI LITERAL1
SPI_SCK LITERAL1
UNIT_MAC LITERAL1
USE_LORA LITERAL1
USE_LORA LITERAL1