Farm-Data-Relay-System/FDRS_Gateway/FDRS_Gateway.ino

136 lines
3.4 KiB
Arduino
Raw Normal View History

// 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.
#if defined(ESP8266)
2021-02-05 16:41:45 +00:00
#include <ESP8266WiFi.h>
#include <espnow.h>
#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};
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() {
StaticJsonDocument<2048> doc;
2021-05-29 19:43:41 +00:00
for (int i = 0; i < pkt_readings; i++) {
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);
}
#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
}
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");
}
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);
// 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");
// 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);
#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;
}
#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;
Serial.println(incomingString);
2021-05-29 19:43:41 +00:00
DeserializationError error = deserializeJson(doc, incomingString);
if (error) { // Test if parsing succeeds.
Serial.println("yikes");
2021-05-29 19:43:41 +00:00
return;
} else {
DataReading newCMD;
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();
}
}