mirror of
https://github.com/timmbogner/Farm-Data-Relay-System
synced 2024-11-10 07:10:42 +00:00
original system files moved
This commit is contained in:
parent
e3c2a7142b
commit
f7f7fcb6f4
@ -1,135 +1,135 @@
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// 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)
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <espnow.h>
|
||||
#elif defined(ESP32)
|
||||
#define RXD2 21
|
||||
#define TXD2 22
|
||||
#include <esp_now.h>
|
||||
#include <WiFi.h>
|
||||
#include <esp_wifi.h>
|
||||
#endif
|
||||
#include "fdrs_config.h"
|
||||
#include <ArduinoJson.h>
|
||||
#include "DataReading.h"
|
||||
|
||||
uint8_t broadcast_mac[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
uint8_t prevAddress[] = {MAC_PREFIX, PREV_MAC};
|
||||
uint8_t selfAddress[] = {MAC_PREFIX, UNIT_MAC};
|
||||
DataReading incData[31];
|
||||
bool newData = false;
|
||||
int pkt_readings;
|
||||
uint8_t incMAC[6];
|
||||
|
||||
#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(&incData, incomingData, len);
|
||||
memcpy(&incMAC, mac, 6);
|
||||
pkt_readings = len / sizeof(DataReading);
|
||||
newData = true;
|
||||
}
|
||||
|
||||
void setup() {
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.disconnect();
|
||||
// Init ESP-NOW for either ESP8266 or ESP32 and set MAC address
|
||||
#if defined(ESP8266)
|
||||
Serial.begin(115200);
|
||||
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
|
||||
esp_now_add_peer(prevAddress, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
|
||||
esp_now_add_peer(broadcast_mac, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
|
||||
|
||||
#elif defined(ESP32)
|
||||
Serial.begin(115200, SERIAL_8N1, RXD2, TXD2);
|
||||
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;
|
||||
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, broadcast_mac, 6);
|
||||
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||
Serial.println("Failed to add peer");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
//CMD example {"id":72,"type":201,"data":166}
|
||||
|
||||
void getSerial() {
|
||||
DataReading theCommands[31];
|
||||
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;
|
||||
theCommands[i].id = doc[i]["id"];
|
||||
theCommands[i].t = doc[i]["type"];
|
||||
theCommands[i].d = doc[i]["data"];
|
||||
}
|
||||
esp_now_send(broadcast_mac, (uint8_t *) &theCommands, s * sizeof(DataReading));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void encodeJSON() {
|
||||
StaticJsonDocument<2048> doc;
|
||||
for (int i = 0; i < pkt_readings; i++) {
|
||||
doc[i]["id"] = incData[i].id;
|
||||
doc[i]["type"] = incData[i].t;
|
||||
doc[i]["data"] = incData[i].d;
|
||||
incData[i].id = 0;
|
||||
incData[i].t = 0;
|
||||
incData[i].d = 0;
|
||||
}
|
||||
serializeJson(doc, Serial);
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
while (Serial.available()) {
|
||||
getSerial();
|
||||
}
|
||||
if (newData) {
|
||||
newData = false;
|
||||
encodeJSON();
|
||||
}
|
||||
}
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// 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)
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <espnow.h>
|
||||
#elif defined(ESP32)
|
||||
#define RXD2 21
|
||||
#define TXD2 22
|
||||
#include <esp_now.h>
|
||||
#include <WiFi.h>
|
||||
#include <esp_wifi.h>
|
||||
#endif
|
||||
#include "fdrs_config.h"
|
||||
#include <ArduinoJson.h>
|
||||
#include "DataReading.h"
|
||||
|
||||
uint8_t broadcast_mac[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
uint8_t prevAddress[] = {MAC_PREFIX, PREV_MAC};
|
||||
uint8_t selfAddress[] = {MAC_PREFIX, UNIT_MAC};
|
||||
DataReading incData[31];
|
||||
bool newData = false;
|
||||
int pkt_readings;
|
||||
uint8_t incMAC[6];
|
||||
|
||||
#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(&incData, incomingData, len);
|
||||
memcpy(&incMAC, mac, 6);
|
||||
pkt_readings = len / sizeof(DataReading);
|
||||
newData = true;
|
||||
}
|
||||
|
||||
void setup() {
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.disconnect();
|
||||
// Init ESP-NOW for either ESP8266 or ESP32 and set MAC address
|
||||
#if defined(ESP8266)
|
||||
Serial.begin(115200);
|
||||
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
|
||||
esp_now_add_peer(prevAddress, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
|
||||
esp_now_add_peer(broadcast_mac, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
|
||||
|
||||
#elif defined(ESP32)
|
||||
Serial.begin(115200, SERIAL_8N1, RXD2, TXD2);
|
||||
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;
|
||||
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, broadcast_mac, 6);
|
||||
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||
Serial.println("Failed to add peer");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
//CMD example {"id":72,"type":201,"data":166}
|
||||
|
||||
void getSerial() {
|
||||
DataReading theCommands[31];
|
||||
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;
|
||||
theCommands[i].id = doc[i]["id"];
|
||||
theCommands[i].t = doc[i]["type"];
|
||||
theCommands[i].d = doc[i]["data"];
|
||||
}
|
||||
esp_now_send(broadcast_mac, (uint8_t *) &theCommands, s * sizeof(DataReading));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void encodeJSON() {
|
||||
StaticJsonDocument<2048> doc;
|
||||
for (int i = 0; i < pkt_readings; i++) {
|
||||
doc[i]["id"] = incData[i].id;
|
||||
doc[i]["type"] = incData[i].t;
|
||||
doc[i]["data"] = incData[i].d;
|
||||
incData[i].id = 0;
|
||||
incData[i].t = 0;
|
||||
incData[i].d = 0;
|
||||
}
|
||||
serializeJson(doc, Serial);
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
while (Serial.available()) {
|
||||
getSerial();
|
||||
}
|
||||
if (newData) {
|
||||
newData = false;
|
||||
encodeJSON();
|
||||
}
|
||||
}
|
@ -1,20 +1,20 @@
|
||||
// To configure your FDRS Gateway:
|
||||
|
||||
// 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 device is configured to know what the previous and/or next
|
||||
// device in the line of communication is.
|
||||
|
||||
// The gateway should usually be assigned the address 0x00.
|
||||
// The PREV_MAC is currently not used, as the gateway responds
|
||||
// to all packets in the same manner.
|
||||
|
||||
|
||||
|
||||
// THIS UNIT
|
||||
#define UNIT_MAC 0x00
|
||||
#define PREV_MAC 0x01
|
||||
|
||||
//MAC prefix:
|
||||
#define MAC_PREFIX 0xAA, 0xBB, 0xCC, 0xDD, 0xEE
|
||||
// To configure your FDRS Gateway:
|
||||
|
||||
// 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 device is configured to know what the previous and/or next
|
||||
// device in the line of communication is.
|
||||
|
||||
// The gateway should usually be assigned the address 0x00.
|
||||
// The PREV_MAC is currently not used, as the gateway responds
|
||||
// to all packets in the same manner.
|
||||
|
||||
|
||||
|
||||
// THIS UNIT
|
||||
#define UNIT_MAC 0x00
|
||||
#define PREV_MAC 0x01
|
||||
|
||||
//MAC prefix:
|
||||
#define MAC_PREFIX 0xAA, 0xBB, 0xCC, 0xDD, 0xEE
|
@ -1,141 +1,141 @@
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// RELAY 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)
|
||||
#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"
|
||||
|
||||
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 outMAC[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, DEFT_MAC};
|
||||
uint8_t incMAC[6];
|
||||
|
||||
uint8_t theData[250];
|
||||
uint8_t ln;
|
||||
bool newData = false;
|
||||
bool isLocal = false;
|
||||
void passOn() {
|
||||
switch (incMAC[5]) {
|
||||
case PREV_MAC:
|
||||
outMAC[5] = NEXT_MAC;
|
||||
break;
|
||||
case NEXT_MAC:
|
||||
outMAC[5] = PREV_MAC;
|
||||
break;
|
||||
default:
|
||||
outMAC[5] = DEFT_MAC;
|
||||
break;
|
||||
}
|
||||
if (!isLocal) outMAC[5] = DEFT_MAC;
|
||||
Serial.print("Packet Received from device: ");
|
||||
Serial.println(incMAC[5]);
|
||||
Serial.print("and sending to: ");
|
||||
Serial.println(outMAC[5]);
|
||||
esp_now_send(outMAC, (uint8_t *) &theData, ln);
|
||||
}
|
||||
|
||||
// Set ESP-NOW send and receive callbacks for either ESP8266 or ESP32
|
||||
#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(&theData, incomingData, sizeof(theData));
|
||||
memcpy(&incMAC, mac, sizeof(incMAC));
|
||||
if (memcmp(&incMAC, &selfAddress, 5) != 0) {
|
||||
isLocal = false;
|
||||
} else {
|
||||
isLocal = true;
|
||||
}
|
||||
Serial.print("Data received: ");
|
||||
Serial.println(len);
|
||||
ln = len;
|
||||
newData = true;
|
||||
}
|
||||
|
||||
void setup() {
|
||||
// Init Serial Monitor
|
||||
Serial.begin(115200);
|
||||
// Init WiFi
|
||||
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;
|
||||
}
|
||||
// Register second peer
|
||||
memcpy(peerInfo.peer_addr, nextAddress, 6);
|
||||
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||
Serial.println("Failed to add peer");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
Serial.println();
|
||||
Serial.println("FARM DATA RELAY SYSTEM :: Relay Module");
|
||||
Serial.println("MAC:" + WiFi.macAddress());
|
||||
Serial.print("Previous device: ");
|
||||
Serial.println(PREV_MAC);
|
||||
Serial.print("Next device: ");
|
||||
Serial.println(NEXT_MAC);
|
||||
Serial.print("Default device: ");
|
||||
Serial.println(DEFT_MAC);
|
||||
Serial.println(" ");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (newData) {
|
||||
newData = false;
|
||||
passOn();
|
||||
}
|
||||
}
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// RELAY 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)
|
||||
#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"
|
||||
|
||||
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 outMAC[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, DEFT_MAC};
|
||||
uint8_t incMAC[6];
|
||||
|
||||
uint8_t theData[250];
|
||||
uint8_t ln;
|
||||
bool newData = false;
|
||||
bool isLocal = false;
|
||||
void passOn() {
|
||||
switch (incMAC[5]) {
|
||||
case PREV_MAC:
|
||||
outMAC[5] = NEXT_MAC;
|
||||
break;
|
||||
case NEXT_MAC:
|
||||
outMAC[5] = PREV_MAC;
|
||||
break;
|
||||
default:
|
||||
outMAC[5] = DEFT_MAC;
|
||||
break;
|
||||
}
|
||||
if (!isLocal) outMAC[5] = DEFT_MAC;
|
||||
Serial.print("Packet Received from device: ");
|
||||
Serial.println(incMAC[5]);
|
||||
Serial.print("and sending to: ");
|
||||
Serial.println(outMAC[5]);
|
||||
esp_now_send(outMAC, (uint8_t *) &theData, ln);
|
||||
}
|
||||
|
||||
// Set ESP-NOW send and receive callbacks for either ESP8266 or ESP32
|
||||
#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(&theData, incomingData, sizeof(theData));
|
||||
memcpy(&incMAC, mac, sizeof(incMAC));
|
||||
if (memcmp(&incMAC, &selfAddress, 5) != 0) {
|
||||
isLocal = false;
|
||||
} else {
|
||||
isLocal = true;
|
||||
}
|
||||
Serial.print("Data received: ");
|
||||
Serial.println(len);
|
||||
ln = len;
|
||||
newData = true;
|
||||
}
|
||||
|
||||
void setup() {
|
||||
// Init Serial Monitor
|
||||
Serial.begin(115200);
|
||||
// Init WiFi
|
||||
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;
|
||||
}
|
||||
// Register second peer
|
||||
memcpy(peerInfo.peer_addr, nextAddress, 6);
|
||||
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||
Serial.println("Failed to add peer");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
Serial.println();
|
||||
Serial.println("FARM DATA RELAY SYSTEM :: Relay Module");
|
||||
Serial.println("MAC:" + WiFi.macAddress());
|
||||
Serial.print("Previous device: ");
|
||||
Serial.println(PREV_MAC);
|
||||
Serial.print("Next device: ");
|
||||
Serial.println(NEXT_MAC);
|
||||
Serial.print("Default device: ");
|
||||
Serial.println(DEFT_MAC);
|
||||
Serial.println(" ");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (newData) {
|
||||
newData = false;
|
||||
passOn();
|
||||
}
|
||||
}
|
@ -1,22 +1,22 @@
|
||||
// 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.
|
||||
|
||||
|
||||
// THIS UNIT
|
||||
#define UNIT_MAC 0x01
|
||||
|
||||
// NEXT UNIT
|
||||
#define NEXT_MAC 0x00
|
||||
|
||||
// PREVIOUS UNIT
|
||||
#define PREV_MAC 0x02
|
||||
|
||||
//Packets from unknown MACs are sent here.
|
||||
#define DEFT_MAC NEXT_MAC
|
||||
// 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.
|
||||
|
||||
|
||||
// THIS UNIT
|
||||
#define UNIT_MAC 0x01
|
||||
|
||||
// NEXT UNIT
|
||||
#define NEXT_MAC 0x00
|
||||
|
||||
// PREVIOUS UNIT
|
||||
#define PREV_MAC 0x02
|
||||
|
||||
//Packets from unknown MACs are sent here.
|
||||
#define DEFT_MAC NEXT_MAC
|
@ -1,151 +1,151 @@
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// TERMINAL 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)
|
||||
#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 "DataReading.h"
|
||||
|
||||
|
||||
uint8_t selfAddress[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, UNIT_MAC};
|
||||
uint8_t nextAddress[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, NEXT_MAC};
|
||||
uint8_t broadcast_mac[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
DataReading incData[31];
|
||||
DataReading theData[31];
|
||||
DataReading theCommands[31];
|
||||
bool newCMD = false;
|
||||
int wait_time = 0;
|
||||
int tot_readings = 0;
|
||||
int tot_commands = 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
|
||||
uint8_t incMAC[6];
|
||||
memcpy(&incData, incomingData, len);
|
||||
memcpy(&mac, incMAC, 6);
|
||||
//if (memcmp(&incMAC, &nextAddress, 6) == 0)
|
||||
int pkt_readings = len / sizeof(DataReading);
|
||||
Serial.println(":Packet:");
|
||||
for (byte i = 0; i < pkt_readings; i++) {
|
||||
Serial.println("SENSOR ID: " + String(incData[i].id) + " TYPE " + String(incData[i].t) + " DATA " + String(incData[i].d));
|
||||
|
||||
if (incData[i].t < 200) {
|
||||
|
||||
if (tot_readings >= 250 / sizeof(DataReading)) {
|
||||
Serial.println("ERROR::Too many sensor readings sent within delay period.");
|
||||
break;
|
||||
}
|
||||
theData[tot_readings] = incData[i]; //Save the current incoming reading to the next open packet slot
|
||||
++tot_readings;
|
||||
} else {
|
||||
theCommands[tot_commands] = incData[i]; //Save the current incoming reading to the next open packet slot
|
||||
++tot_commands;
|
||||
newCMD = true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
// Init Serial
|
||||
Serial.begin(115200);
|
||||
// Init WiFi
|
||||
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 peer
|
||||
esp_now_add_peer(nextAddress, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
|
||||
esp_now_add_peer(broadcast_mac, 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 peer
|
||||
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
|
||||
Serial.println();
|
||||
Serial.println("FARM DATA RELAY SYSTEM :: Terminal Module");
|
||||
Serial.println("MAC:" + WiFi.macAddress());
|
||||
Serial.print("Next device: ");
|
||||
Serial.println(NEXT_MAC);
|
||||
// Serial.println(250 / sizeof(DataReading), DEC);
|
||||
// Serial.println(sizeof(DataReading), DEC);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (newCMD) sendCmd();
|
||||
if (millis() > wait_time) {
|
||||
wait_time = wait_time + DELAY;
|
||||
if (tot_readings != 0) passForward();
|
||||
}
|
||||
}
|
||||
|
||||
void passForward() {
|
||||
Serial.println("Passing Forward");
|
||||
esp_now_send(nextAddress, (uint8_t *) &theData, sizeof(DataReading)*tot_readings);
|
||||
tot_readings = 0;
|
||||
for (int i = 0; i < 250 / sizeof(DataReading); i++) {
|
||||
theData[i].d = 0;
|
||||
theData[i].id = 0;
|
||||
theData[i].t = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void sendCmd() {
|
||||
newCMD = false;
|
||||
esp_now_send(broadcast_mac, (uint8_t *) &theCommands, tot_commands* sizeof(DataReading));
|
||||
tot_commands = 0;
|
||||
|
||||
}
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// TERMINAL 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)
|
||||
#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 "DataReading.h"
|
||||
|
||||
|
||||
uint8_t selfAddress[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, UNIT_MAC};
|
||||
uint8_t nextAddress[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, NEXT_MAC};
|
||||
uint8_t broadcast_mac[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
DataReading incData[31];
|
||||
DataReading theData[31];
|
||||
DataReading theCommands[31];
|
||||
bool newCMD = false;
|
||||
int wait_time = 0;
|
||||
int tot_readings = 0;
|
||||
int tot_commands = 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
|
||||
uint8_t incMAC[6];
|
||||
memcpy(&incData, incomingData, len);
|
||||
memcpy(&mac, incMAC, 6);
|
||||
//if (memcmp(&incMAC, &nextAddress, 6) == 0)
|
||||
int pkt_readings = len / sizeof(DataReading);
|
||||
Serial.println(":Packet:");
|
||||
for (byte i = 0; i < pkt_readings; i++) {
|
||||
Serial.println("SENSOR ID: " + String(incData[i].id) + " TYPE " + String(incData[i].t) + " DATA " + String(incData[i].d));
|
||||
|
||||
if (incData[i].t < 200) {
|
||||
|
||||
if (tot_readings >= 250 / sizeof(DataReading)) {
|
||||
Serial.println("ERROR::Too many sensor readings sent within delay period.");
|
||||
break;
|
||||
}
|
||||
theData[tot_readings] = incData[i]; //Save the current incoming reading to the next open packet slot
|
||||
++tot_readings;
|
||||
} else {
|
||||
theCommands[tot_commands] = incData[i]; //Save the current incoming reading to the next open packet slot
|
||||
++tot_commands;
|
||||
newCMD = true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
// Init Serial
|
||||
Serial.begin(115200);
|
||||
// Init WiFi
|
||||
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 peer
|
||||
esp_now_add_peer(nextAddress, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
|
||||
esp_now_add_peer(broadcast_mac, 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 peer
|
||||
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
|
||||
Serial.println();
|
||||
Serial.println("FARM DATA RELAY SYSTEM :: Terminal Module");
|
||||
Serial.println("MAC:" + WiFi.macAddress());
|
||||
Serial.print("Next device: ");
|
||||
Serial.println(NEXT_MAC);
|
||||
// Serial.println(250 / sizeof(DataReading), DEC);
|
||||
// Serial.println(sizeof(DataReading), DEC);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (newCMD) sendCmd();
|
||||
if (millis() > wait_time) {
|
||||
wait_time = wait_time + DELAY;
|
||||
if (tot_readings != 0) passForward();
|
||||
}
|
||||
}
|
||||
|
||||
void passForward() {
|
||||
Serial.println("Passing Forward");
|
||||
esp_now_send(nextAddress, (uint8_t *) &theData, sizeof(DataReading)*tot_readings);
|
||||
tot_readings = 0;
|
||||
for (int i = 0; i < 250 / sizeof(DataReading); i++) {
|
||||
theData[i].d = 0;
|
||||
theData[i].id = 0;
|
||||
theData[i].t = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void sendCmd() {
|
||||
newCMD = false;
|
||||
esp_now_send(broadcast_mac, (uint8_t *) &theCommands, tot_commands* sizeof(DataReading));
|
||||
tot_commands = 0;
|
||||
|
||||
}
|
@ -1,27 +1,27 @@
|
||||
// To configure FDRS:
|
||||
|
||||
// Uncomment the code corresponding to the unit you are configuring,
|
||||
// then uncomment the code corresponding to the unit you would like
|
||||
// to be previous and/or next in the line of communication.
|
||||
|
||||
// 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 device is configured to know what the previous and/or next
|
||||
// device in the line of communication is.
|
||||
|
||||
// The terminal is considered the "first" device, which can be addressed
|
||||
// to a relay or the gateway.
|
||||
|
||||
// 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 DELAY 15000
|
||||
|
||||
// THIS UNIT
|
||||
#define UNIT_MAC 0x01
|
||||
|
||||
// NEXT UNIT
|
||||
#define NEXT_MAC 0x00
|
||||
// To configure FDRS:
|
||||
|
||||
// Uncomment the code corresponding to the unit you are configuring,
|
||||
// then uncomment the code corresponding to the unit you would like
|
||||
// to be previous and/or next in the line of communication.
|
||||
|
||||
// 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 device is configured to know what the previous and/or next
|
||||
// device in the line of communication is.
|
||||
|
||||
// The terminal is considered the "first" device, which can be addressed
|
||||
// to a relay or the gateway.
|
||||
|
||||
// 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 DELAY 15000
|
||||
|
||||
// THIS UNIT
|
||||
#define UNIT_MAC 0x01
|
||||
|
||||
// NEXT UNIT
|
||||
#define NEXT_MAC 0x00
|
@ -1,151 +1,151 @@
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// BLYNK FRONT-END MODULE
|
||||
// Uses JSON data from the serial port to set Blynk variables.
|
||||
//
|
||||
// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
|
||||
// This code was written for the now-outdated version of Blynk, and is mostly here for reference.
|
||||
//
|
||||
|
||||
#define STASSID "" //Your SSID
|
||||
#define STAPSK "" //Your Password
|
||||
#define BLKAUTH "" //Your Blynk auth code
|
||||
|
||||
#define DELAY 30000
|
||||
|
||||
#if defined(ESP8266)
|
||||
#include <BlynkSimpleEsp8266.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#elif defined(ESP32)
|
||||
#include <BlynkSimpleEsp32.h>
|
||||
#include <WiFi.h>
|
||||
#endif
|
||||
#include <ArduinoJson.h>
|
||||
#include "DataReading.h"
|
||||
|
||||
BlynkTimer timer;
|
||||
WidgetTerminal terminal(V69);
|
||||
|
||||
DataReading allData[256];
|
||||
int wait_time = DELAY;
|
||||
char auth[] = BLKAUTH;
|
||||
const char* ssid = STASSID;
|
||||
const char* password = STAPSK;
|
||||
bool is_new[255];
|
||||
int new_readings;
|
||||
bool term_flag;
|
||||
String the_command;
|
||||
|
||||
BLYNK_WRITE(V69)
|
||||
{
|
||||
Serial.println("blynkwrite");
|
||||
the_command = param.asStr();
|
||||
term_flag = true;
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
WiFi.mode(WIFI_STA);
|
||||
Serial.println();
|
||||
Serial.println("FDRS Blynk Module");
|
||||
Serial.println("Connect to WiFi please?");
|
||||
Blynk.begin(auth, ssid, password);
|
||||
// Blynk.config(auth);
|
||||
// Blynk.connectWiFi(ssid, password);
|
||||
Serial.println("Thanks!");
|
||||
//terminal.clear();
|
||||
timer.setInterval(30000L, updateBlynk);
|
||||
Blynk.run();
|
||||
terminal.println("Hello, world!");
|
||||
terminal.flush();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
while (Serial.available()) {
|
||||
Serial.println("waiting for tilda");
|
||||
if (Serial.read() == '~') { //Waits for '~', then parses following data
|
||||
getSerial();
|
||||
Serial.println("getSerial done");
|
||||
break;
|
||||
}
|
||||
}
|
||||
// if (millis() > wait_time) {
|
||||
// wait_time = wait_time + DELAY;
|
||||
// if (Blynk.connected()) {
|
||||
// updateBlynk();
|
||||
// Serial.println("blynk updated");
|
||||
// }
|
||||
// }
|
||||
if (term_flag) {
|
||||
parseBlynkTerm(the_command);
|
||||
term_flag = false;
|
||||
}
|
||||
Blynk.run();
|
||||
timer.run();
|
||||
}
|
||||
|
||||
void getSerial() {
|
||||
String incomingString = Serial.readString();
|
||||
StaticJsonDocument<3072> doc;
|
||||
Serial.println("Received: " + incomingString);
|
||||
DeserializationError error = deserializeJson(doc, incomingString);
|
||||
if (error) { // Test if parsing succeeds.
|
||||
Serial.print(F("deserializeJson() failed: "));
|
||||
Serial.println(error.f_str());
|
||||
return;
|
||||
} else {
|
||||
for (int i = 0; i < doc.size(); i++) {
|
||||
Serial.println(doc.size());
|
||||
DataReading newData;
|
||||
newData.id = doc[i]["id"];
|
||||
newData.t = doc[i]["type"];
|
||||
newData.d = doc[i]["data"];
|
||||
allData[newData.id] = newData;
|
||||
is_new[newData.id] = true;
|
||||
Serial.println("SENSOR ID: " + String(newData.id) + " TYPE " + String(newData.t) + " DATA " + String(newData.d));
|
||||
yield();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void updateBlynk() {
|
||||
Serial.println("Updateblynk");
|
||||
for (int i = 0; i < 127; i++) {
|
||||
if (is_new[i] == true) {
|
||||
Serial.println("abt to write");
|
||||
Blynk.virtualWrite(i, allData[i].d);
|
||||
is_new[i] = false;
|
||||
yield();
|
||||
}
|
||||
}
|
||||
}
|
||||
void parseBlynkTerm(String command) {
|
||||
int ind1 = command.indexOf(" ");
|
||||
String arg1 = command.substring(0, ind1);
|
||||
if (arg1.equalsIgnoreCase(String("SET"))) {
|
||||
int ind2 = command.indexOf(" ", ind1 + 1);
|
||||
String arg2 = command.substring(ind1 + 1, ind2);
|
||||
String arg3 = command.substring(ind2 + 1, command.length());
|
||||
terminal.println("ARG1:" + arg1);
|
||||
terminal.println("ARG2:" + arg2);
|
||||
terminal.println("ARG3:" + arg3);
|
||||
terminal.flush();
|
||||
DataReading newCMD;
|
||||
newCMD.id = arg2.toInt();
|
||||
newCMD.t = 201;
|
||||
newCMD.d = arg3.toFloat();
|
||||
allData[newCMD.id] = newCMD;
|
||||
StaticJsonDocument<2048> doc;
|
||||
doc[0]["id"] = newCMD.id;
|
||||
doc[0]["type"] = newCMD.t;
|
||||
doc[0]["data"] = newCMD.d;
|
||||
Serial.write('~');
|
||||
serializeJson(doc, Serial);
|
||||
Serial.println();
|
||||
} else if (arg1.equalsIgnoreCase(String("GET"))) {
|
||||
String arg2 = command.substring(ind1 + 1, command.length());
|
||||
terminal.println("DataReading " + arg2 + " :: " + String(allData[arg2.toInt()].d));
|
||||
terminal.flush();
|
||||
}
|
||||
}
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// BLYNK FRONT-END MODULE
|
||||
// Uses JSON data from the serial port to set Blynk variables.
|
||||
//
|
||||
// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
|
||||
// This code was written for the now-outdated version of Blynk, and is mostly here for reference.
|
||||
//
|
||||
|
||||
#define STASSID "" //Your SSID
|
||||
#define STAPSK "" //Your Password
|
||||
#define BLKAUTH "" //Your Blynk auth code
|
||||
|
||||
#define DELAY 30000
|
||||
|
||||
#if defined(ESP8266)
|
||||
#include <BlynkSimpleEsp8266.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#elif defined(ESP32)
|
||||
#include <BlynkSimpleEsp32.h>
|
||||
#include <WiFi.h>
|
||||
#endif
|
||||
#include <ArduinoJson.h>
|
||||
#include "DataReading.h"
|
||||
|
||||
BlynkTimer timer;
|
||||
WidgetTerminal terminal(V69);
|
||||
|
||||
DataReading allData[256];
|
||||
int wait_time = DELAY;
|
||||
char auth[] = BLKAUTH;
|
||||
const char* ssid = STASSID;
|
||||
const char* password = STAPSK;
|
||||
bool is_new[255];
|
||||
int new_readings;
|
||||
bool term_flag;
|
||||
String the_command;
|
||||
|
||||
BLYNK_WRITE(V69)
|
||||
{
|
||||
Serial.println("blynkwrite");
|
||||
the_command = param.asStr();
|
||||
term_flag = true;
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
WiFi.mode(WIFI_STA);
|
||||
Serial.println();
|
||||
Serial.println("FDRS Blynk Module");
|
||||
Serial.println("Connect to WiFi please?");
|
||||
Blynk.begin(auth, ssid, password);
|
||||
// Blynk.config(auth);
|
||||
// Blynk.connectWiFi(ssid, password);
|
||||
Serial.println("Thanks!");
|
||||
//terminal.clear();
|
||||
timer.setInterval(30000L, updateBlynk);
|
||||
Blynk.run();
|
||||
terminal.println("Hello, world!");
|
||||
terminal.flush();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
while (Serial.available()) {
|
||||
Serial.println("waiting for tilda");
|
||||
if (Serial.read() == '~') { //Waits for '~', then parses following data
|
||||
getSerial();
|
||||
Serial.println("getSerial done");
|
||||
break;
|
||||
}
|
||||
}
|
||||
// if (millis() > wait_time) {
|
||||
// wait_time = wait_time + DELAY;
|
||||
// if (Blynk.connected()) {
|
||||
// updateBlynk();
|
||||
// Serial.println("blynk updated");
|
||||
// }
|
||||
// }
|
||||
if (term_flag) {
|
||||
parseBlynkTerm(the_command);
|
||||
term_flag = false;
|
||||
}
|
||||
Blynk.run();
|
||||
timer.run();
|
||||
}
|
||||
|
||||
void getSerial() {
|
||||
String incomingString = Serial.readString();
|
||||
StaticJsonDocument<3072> doc;
|
||||
Serial.println("Received: " + incomingString);
|
||||
DeserializationError error = deserializeJson(doc, incomingString);
|
||||
if (error) { // Test if parsing succeeds.
|
||||
Serial.print(F("deserializeJson() failed: "));
|
||||
Serial.println(error.f_str());
|
||||
return;
|
||||
} else {
|
||||
for (int i = 0; i < doc.size(); i++) {
|
||||
Serial.println(doc.size());
|
||||
DataReading newData;
|
||||
newData.id = doc[i]["id"];
|
||||
newData.t = doc[i]["type"];
|
||||
newData.d = doc[i]["data"];
|
||||
allData[newData.id] = newData;
|
||||
is_new[newData.id] = true;
|
||||
Serial.println("SENSOR ID: " + String(newData.id) + " TYPE " + String(newData.t) + " DATA " + String(newData.d));
|
||||
yield();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void updateBlynk() {
|
||||
Serial.println("Updateblynk");
|
||||
for (int i = 0; i < 127; i++) {
|
||||
if (is_new[i] == true) {
|
||||
Serial.println("abt to write");
|
||||
Blynk.virtualWrite(i, allData[i].d);
|
||||
is_new[i] = false;
|
||||
yield();
|
||||
}
|
||||
}
|
||||
}
|
||||
void parseBlynkTerm(String command) {
|
||||
int ind1 = command.indexOf(" ");
|
||||
String arg1 = command.substring(0, ind1);
|
||||
if (arg1.equalsIgnoreCase(String("SET"))) {
|
||||
int ind2 = command.indexOf(" ", ind1 + 1);
|
||||
String arg2 = command.substring(ind1 + 1, ind2);
|
||||
String arg3 = command.substring(ind2 + 1, command.length());
|
||||
terminal.println("ARG1:" + arg1);
|
||||
terminal.println("ARG2:" + arg2);
|
||||
terminal.println("ARG3:" + arg3);
|
||||
terminal.flush();
|
||||
DataReading newCMD;
|
||||
newCMD.id = arg2.toInt();
|
||||
newCMD.t = 201;
|
||||
newCMD.d = arg3.toFloat();
|
||||
allData[newCMD.id] = newCMD;
|
||||
StaticJsonDocument<2048> doc;
|
||||
doc[0]["id"] = newCMD.id;
|
||||
doc[0]["type"] = newCMD.t;
|
||||
doc[0]["data"] = newCMD.d;
|
||||
Serial.write('~');
|
||||
serializeJson(doc, Serial);
|
||||
Serial.println();
|
||||
} else if (arg1.equalsIgnoreCase(String("GET"))) {
|
||||
String arg2 = command.substring(ind1 + 1, command.length());
|
||||
terminal.println("DataReading " + arg2 + " :: " + String(allData[arg2.toInt()].d));
|
||||
terminal.flush();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user