2021-10-09 01:55:56 +00:00
|
|
|
// FARM DATA RELAY SYSTEM
|
2021-02-05 16:41:45 +00:00
|
|
|
//
|
|
|
|
// GATEWAY MODULE
|
|
|
|
//
|
|
|
|
// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
|
|
|
|
// Setup instructions located in the "fdrs_config.h" file.
|
|
|
|
|
2021-03-27 19:51:32 +00:00
|
|
|
#if defined(ESP8266)
|
2021-02-05 16:41:45 +00:00
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
#include <espnow.h>
|
2021-03-27 19:51:32 +00:00
|
|
|
#elif defined(ESP32)
|
|
|
|
#include <esp_now.h>
|
|
|
|
#include <WiFi.h>
|
|
|
|
#include <esp_wifi.h>
|
|
|
|
#endif
|
2021-02-05 16:41:45 +00:00
|
|
|
#include "fdrs_config.h"
|
|
|
|
#include <ArduinoJson.h>
|
2021-05-29 19:43:41 +00:00
|
|
|
#include "DataReading.h"
|
2021-02-05 16:41:45 +00:00
|
|
|
|
2021-05-29 19:43:41 +00:00
|
|
|
uint8_t prevAddress[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, PREV_MAC};
|
2021-03-27 19:51:32 +00:00
|
|
|
uint8_t selfAddress[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, UNIT_MAC};
|
2021-05-29 19:43:41 +00:00
|
|
|
DataReading incData[31];
|
|
|
|
DataReading theData[31];
|
2021-02-05 16:41:45 +00:00
|
|
|
bool newData = false;
|
2021-05-29 19:43:41 +00:00
|
|
|
int pkt_readings;
|
2021-02-05 16:41:45 +00:00
|
|
|
void encodeJSON() {
|
2021-03-27 19:51:32 +00:00
|
|
|
StaticJsonDocument<2048> doc;
|
2021-05-29 19:43:41 +00:00
|
|
|
for (int i = 0; i < pkt_readings; i++) {
|
2021-03-27 19:51:32 +00:00
|
|
|
doc[i]["id"] = theData[i].id;
|
|
|
|
doc[i]["type"] = theData[i].t;
|
|
|
|
doc[i]["data"] = theData[i].d;
|
2021-05-29 19:43:41 +00:00
|
|
|
theData[i].id = 0;
|
|
|
|
theData[i].t = 0;
|
|
|
|
theData[i].d = 0;
|
2021-02-05 16:41:45 +00:00
|
|
|
}
|
2021-05-29 19:43:41 +00:00
|
|
|
Serial.write('~');
|
2021-02-05 16:41:45 +00:00
|
|
|
serializeJson(doc, Serial);
|
|
|
|
}
|
|
|
|
|
2021-03-27 19:51:32 +00:00
|
|
|
#if defined(ESP8266)
|
2021-02-05 16:41:45 +00:00
|
|
|
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
|
2021-05-29 19:43:41 +00:00
|
|
|
Serial.print("Last Packet Send Status: ");
|
|
|
|
if (sendStatus == 0) {
|
|
|
|
Serial.println("Delivery success");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Serial.println("Delivery fail");
|
|
|
|
}
|
2021-02-05 16:41:45 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 19:51:32 +00:00
|
|
|
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) {
|
2021-05-29 19:43:41 +00:00
|
|
|
Serial.print("Last Packet Send Status:");
|
|
|
|
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
|
2021-03-27 19:51:32 +00:00
|
|
|
}
|
|
|
|
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
|
|
|
|
#endif
|
2021-05-29 19:43:41 +00:00
|
|
|
memcpy(&theData, incomingData, len);
|
|
|
|
pkt_readings = len / sizeof(DataReading);
|
2021-02-05 16:41:45 +00:00
|
|
|
newData = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
// Init Serial Monitor
|
|
|
|
Serial.begin(115200);
|
2021-03-27 19:51:32 +00:00
|
|
|
// Init WiFi
|
2021-02-05 16:41:45 +00:00
|
|
|
WiFi.mode(WIFI_STA);
|
|
|
|
WiFi.disconnect();
|
2021-05-29 19:43:41 +00:00
|
|
|
Serial.println();
|
|
|
|
Serial.println("FDRS Gateway Module");
|
2021-03-27 19:51:32 +00:00
|
|
|
// Init ESP-NOW for either ESP8266 or ESP32 and set MAC address
|
|
|
|
#if defined(ESP8266)
|
2021-02-05 16:41:45 +00:00
|
|
|
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 peer
|
2021-05-29 19:43:41 +00:00
|
|
|
esp_now_add_peer(prevAddress, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
|
2021-03-27 19:51:32 +00:00
|
|
|
#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;
|
2021-05-29 19:43:41 +00:00
|
|
|
// register peer
|
|
|
|
memcpy(peerInfo.peer_addr, prevAddress, 6);
|
|
|
|
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
|
|
|
Serial.println("Failed to add peer");
|
|
|
|
return;
|
|
|
|
}
|
2021-03-27 19:51:32 +00:00
|
|
|
#endif
|
2021-02-05 16:41:45 +00:00
|
|
|
}
|
|
|
|
|
2021-05-29 19:43:41 +00:00
|
|
|
|
|
|
|
void getSerial() {
|
|
|
|
String incomingString = Serial.readString();
|
|
|
|
StaticJsonDocument<3072> doc;
|
2021-10-09 01:55:56 +00:00
|
|
|
Serial.println(incomingString);
|
2021-05-29 19:43:41 +00:00
|
|
|
DeserializationError error = deserializeJson(doc, incomingString);
|
|
|
|
if (error) { // Test if parsing succeeds.
|
2021-10-09 01:55:56 +00:00
|
|
|
Serial.println("yikes");
|
|
|
|
|
|
|
|
|
2021-05-29 19:43:41 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
DataReading newCMD;
|
2021-10-09 01:55:56 +00:00
|
|
|
|
|
|
|
newCMD.id = doc["id"];
|
|
|
|
newCMD.t = doc["type"];
|
|
|
|
newCMD.d = doc["data"];
|
|
|
|
Serial.println(newCMD.id );
|
2021-05-29 19:43:41 +00:00
|
|
|
esp_now_send(prevAddress, (uint8_t *) &newCMD, sizeof(newCMD));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 16:41:45 +00:00
|
|
|
void loop() {
|
2021-05-29 19:43:41 +00:00
|
|
|
while (Serial.available()) {
|
|
|
|
if (Serial.read() == '~') {
|
|
|
|
getSerial();
|
|
|
|
}
|
|
|
|
}
|
2021-02-05 16:41:45 +00:00
|
|
|
if (newData) {
|
|
|
|
newData = false;
|
|
|
|
encodeJSON();
|
|
|
|
}
|
|
|
|
}
|