mirror of
https://github.com/timmbogner/Farm-Data-Relay-System
synced 2024-11-10 07:10:42 +00:00
Add files via upload
This commit is contained in:
parent
6e567cf9b2
commit
bab95d98bf
7
FDRS_Gateway2000/DataReading.h
Normal file
7
FDRS_Gateway2000/DataReading.h
Normal file
@ -0,0 +1,7 @@
|
||||
|
||||
typedef struct DataReading {
|
||||
float d;
|
||||
uint16_t id;
|
||||
uint8_t t;
|
||||
|
||||
} DataReading;
|
78
FDRS_Gateway2000/FDRS_Gateway2000.ino
Normal file
78
FDRS_Gateway2000/FDRS_Gateway2000.ino
Normal file
@ -0,0 +1,78 @@
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// GATEWAY 2.000
|
||||
//
|
||||
// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
|
||||
// Setup instructions located in the "fdrs_config.h" file.
|
||||
|
||||
#if defined(ESP8266)
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <espnow.h>
|
||||
#elif defined(ESP32)
|
||||
#include <esp_now.h>
|
||||
#include <WiFi.h>
|
||||
#include <esp_wifi.h>
|
||||
#endif
|
||||
#include "fdrs_config.h"
|
||||
#include <ArduinoJson.h>
|
||||
#include "DataReading.h"
|
||||
#include <PubSubClient.h>
|
||||
#include "fdrs_functions.h"
|
||||
|
||||
const char* ssid = "";
|
||||
const char* password = "";
|
||||
const char* mqtt_server = "192.168.0.8";
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
begin_espnow();
|
||||
#ifdef USE_WIFI
|
||||
delay(10);
|
||||
WiFi.begin(ssid, password);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
}
|
||||
client.setServer(mqtt_server, 1883);
|
||||
client.setCallback(mqtt_callback);
|
||||
#endif
|
||||
}
|
||||
|
||||
void loop() {
|
||||
while (Serial.available()) {
|
||||
getSerial();
|
||||
}
|
||||
if (newData != 0) {
|
||||
|
||||
|
||||
switch (newData) {
|
||||
case 1: //ESP-NOW #1
|
||||
sendESPNOW(2);
|
||||
sendSerial();
|
||||
break;
|
||||
case 2: //ESP-NOW #2
|
||||
sendESPNOW(1);
|
||||
sendSerial();
|
||||
break;
|
||||
case 3: //ESP-NOW General
|
||||
sendSerial();
|
||||
break;
|
||||
case 4: //Serial
|
||||
sendESPNOW(0);
|
||||
sendSerial();
|
||||
sendMQTT();
|
||||
break;
|
||||
case 5: //MQTT
|
||||
sendSerial();
|
||||
break;
|
||||
}
|
||||
newData = 0;
|
||||
}
|
||||
|
||||
|
||||
#ifdef USE_WIFI
|
||||
if (!client.connected()) {
|
||||
reconnect();
|
||||
}
|
||||
client.loop();
|
||||
#endif
|
||||
}
|
21
FDRS_Gateway2000/fdrs_config.h
Normal file
21
FDRS_Gateway2000/fdrs_config.h
Normal file
@ -0,0 +1,21 @@
|
||||
// To configure FDRS:
|
||||
|
||||
// Each device in the system has a unique, one-byte address which
|
||||
// is assigned to the last digit of its MAC address at startup.
|
||||
|
||||
// Each relay receives data from its pre-programmed "PREV_MAC" device and
|
||||
// sends the packet verbatim to the address corresponding to "NEXT_MAC".
|
||||
|
||||
// The gateway receives the data and outputs it as a json string over the serial port.
|
||||
|
||||
//#define USE_WIFI //You cannot use ESP-NOW while WiFi is in use
|
||||
#define USE_LORA
|
||||
|
||||
// THIS UNIT
|
||||
#define UNIT_MAC 0x00
|
||||
|
||||
// NEXT UNIT
|
||||
#define NEXT_MAC 0x02
|
||||
|
||||
// PREVIOUS UNIT
|
||||
#define PREV_MAC 0x01
|
168
FDRS_Gateway2000/fdrs_functions.h
Normal file
168
FDRS_Gateway2000/fdrs_functions.h
Normal file
@ -0,0 +1,168 @@
|
||||
uint8_t broadcast_mac[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
uint8_t prevAddress[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, PREV_MAC};
|
||||
uint8_t selfAddress[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, UNIT_MAC};
|
||||
uint8_t nextAddress[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, NEXT_MAC};
|
||||
uint8_t incMAC[6];
|
||||
|
||||
DataReading theData[31];
|
||||
uint8_t ln;
|
||||
uint8_t newData = 0;
|
||||
|
||||
WiFiClient espClient;
|
||||
PubSubClient client(espClient);
|
||||
|
||||
// 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));
|
||||
if (memcmp(&incMAC, &prevAddress, 6) == 0) newData = 1;
|
||||
else if (memcmp(&incMAC, &nextAddress, 6) == 0) newData = 2;
|
||||
else newData = 3;
|
||||
ln = len;
|
||||
}
|
||||
void begin_espnow() {
|
||||
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
|
||||
esp_now_add_peer(prevAddress, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
|
||||
esp_now_add_peer(nextAddress, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
|
||||
#elif defined(ESP32)
|
||||
esp_wifi_set_mac(WIFI_IF_STA, &selfAddress[0]);
|
||||
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;
|
||||
// Register first peer
|
||||
memcpy(peerInfo.peer_addr, prevAddress, 6);
|
||||
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||
Serial.println("Failed to add peer");
|
||||
return;
|
||||
}
|
||||
memcpy(peerInfo.peer_addr, nextAddress, 6);
|
||||
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||
Serial.println("Failed to add peer");
|
||||
return;
|
||||
}
|
||||
memcpy(peerInfo.peer_addr, broadcast_mac, 6);
|
||||
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||
Serial.println("Failed to add peer");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
void getSerial() {
|
||||
String incomingString = Serial.readStringUntil('\n');
|
||||
StaticJsonDocument<2048> doc;
|
||||
DeserializationError error = deserializeJson(doc, incomingString);
|
||||
if (error) { // Test if parsing succeeds.
|
||||
Serial.println("parse err");
|
||||
return;
|
||||
} else {
|
||||
int s = doc.size();
|
||||
//Serial.println(s);
|
||||
for (int i = 0; i < s; i++) {
|
||||
if (i > 31) break;
|
||||
theData[i].id = doc[i]["id"];
|
||||
theData[i].t = doc[i]["type"];
|
||||
theData[i].d = doc[i]["data"];
|
||||
}
|
||||
ln = s * sizeof(DataReading);
|
||||
newData = 4;
|
||||
}
|
||||
}
|
||||
|
||||
void sendESPNOW(uint8_t interface) {
|
||||
switch (interface) {
|
||||
case 0:
|
||||
esp_now_send(broadcast_mac, (uint8_t *) &theData, ln);
|
||||
break;
|
||||
case 1:
|
||||
esp_now_send(prevAddress, (uint8_t *) &theData, ln);
|
||||
break;
|
||||
case 2:
|
||||
esp_now_send(nextAddress, (uint8_t *) &theData, ln);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
void sendMQTT() {
|
||||
StaticJsonDocument<2048> doc;
|
||||
for (int i = 0; i < ln / sizeof(DataReading); i++) {
|
||||
doc[i]["id"] = theData[i].id;
|
||||
doc[i]["type"] = theData[i].t;
|
||||
doc[i]["data"] = theData[i].d;
|
||||
}
|
||||
String incomingString;
|
||||
serializeJson(doc, incomingString);
|
||||
client.publish("esp/fdrs", (char*) incomingString.c_str());
|
||||
}
|
||||
|
||||
void sendSerial() {
|
||||
StaticJsonDocument<2048> doc;
|
||||
for (int i = 0; i < ln / sizeof(DataReading); i++) {
|
||||
doc[i]["id"] = theData[i].id;
|
||||
doc[i]["type"] = theData[i].t;
|
||||
doc[i]["data"] = theData[i].d;
|
||||
}
|
||||
serializeJson(doc, Serial);
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void mqtt_callback(char* topic, byte * message, unsigned int length) {
|
||||
String incomingString;
|
||||
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.
|
||||
//Serial.println("parse err");
|
||||
return;
|
||||
} else {
|
||||
int s = doc.size();
|
||||
//Serial.println(s);
|
||||
for (int i = 0; i < s; i++) {
|
||||
if (i > 31) break;
|
||||
theData[i].id = doc[i]["id"];
|
||||
theData[i].t = doc[i]["type"];
|
||||
theData[i].d = doc[i]["data"];
|
||||
}
|
||||
ln = s * sizeof(DataReading);
|
||||
newData = 5;
|
||||
}
|
||||
}
|
||||
void reconnect() {
|
||||
// Loop until reconnected
|
||||
while (!client.connected()) {
|
||||
// Attempt to connect
|
||||
if (client.connect("FDRS_GATEWAY")) {
|
||||
// Subscribe
|
||||
client.subscribe("esp/fdrs");
|
||||
} else {
|
||||
// Wait 5 seconds before retrying
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user