Farm-Data-Relay-System/FDRS_Gateway2000/fdrs_functions.h

466 lines
13 KiB
C
Raw Normal View History

2022-03-08 17:00:09 +00:00
#ifdef ESP8266
#define UART_IF Serial
#endif
2022-01-05 00:02:12 +00:00
const uint8_t espnow_size = 250 / sizeof(DataReading);
const uint8_t lora_size = 256 / sizeof(DataReading);
2022-02-17 06:12:18 +00:00
const uint8_t mac_prefix[] = {MAC_PREFIX};
2022-01-05 00:02:12 +00:00
2021-12-06 04:19:39 +00:00
uint8_t broadcast_mac[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
2022-02-07 17:43:36 +00:00
uint8_t selfAddress[] = {MAC_PREFIX, UNIT_MAC};
2022-02-19 01:47:40 +00:00
uint8_t ESPNOW1[] = {MAC_PREFIX, ESPNOW1_PEER};
uint8_t ESPNOW2[] = {MAC_PREFIX, ESPNOW2_PEER};
2021-12-06 04:19:39 +00:00
uint8_t incMAC[6];
2022-02-19 01:47:40 +00:00
uint8_t LoRa1[] = {mac_prefix[3], mac_prefix[4], LORA1_PEER};
uint8_t LoRa2[] = {mac_prefix[3], mac_prefix[4], LORA2_PEER};
2021-12-06 04:19:39 +00:00
2022-01-05 00:02:12 +00:00
DataReading theData[256];
2021-12-06 04:19:39 +00:00
uint8_t ln;
uint8_t newData = 0;
2022-02-07 17:43:36 +00:00
DataReading ESPNOW1buffer[256];
2022-01-05 00:02:12 +00:00
uint8_t lenESPNOW1 = 0;
uint32_t timeESPNOW1 = 0;
2022-02-07 17:43:36 +00:00
DataReading ESPNOW2buffer[256];
2022-01-05 00:02:12 +00:00
uint8_t lenESPNOW2 = 0;
uint32_t timeESPNOW2 = 0;
2022-02-07 17:43:36 +00:00
DataReading ESPNOWGbuffer[256];
2022-01-05 00:02:12 +00:00
uint8_t lenESPNOWG = 0;
uint32_t timeESPNOWG = 0;
2022-02-07 17:43:36 +00:00
DataReading SERIALbuffer[256];
2022-01-05 00:02:12 +00:00
uint8_t lenSERIAL = 0;
uint32_t timeSERIAL = 0;
2022-02-07 17:43:36 +00:00
DataReading MQTTbuffer[256];
2022-01-05 00:02:12 +00:00
uint8_t lenMQTT = 0;
uint32_t timeMQTT = 0;
2022-02-17 06:12:18 +00:00
DataReading LORAGbuffer[256];
uint8_t lenLORAG = 0;
uint32_t timeLORAG = 0;
DataReading LORA1buffer[256];
uint8_t lenLORA1 = 0;
uint32_t timeLORA1 = 0;
DataReading LORA2buffer[256];
uint8_t lenLORA2 = 0;
uint32_t timeLORA2 = 0;
2022-01-05 00:02:12 +00:00
2021-12-06 04:19:39 +00:00
WiFiClient espClient;
2022-03-08 17:00:09 +00:00
#ifdef USE_LED
CRGB leds[NUM_LEDS];
#endif
2022-02-17 06:12:18 +00:00
#ifdef USE_WIFI
2022-03-08 17:00:09 +00:00
PubSubClient client(espClient);
2022-02-17 06:12:18 +00:00
const char* ssid = WIFI_NET;
const char* password = WIFI_PASS;
const char* mqtt_server = MQTT_ADDR;
#endif
2021-12-06 04:19:39 +00:00
// 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));
2022-01-30 03:17:27 +00:00
if (memcmp(&incMAC, &ESPNOW1, 6) == 0) newData = 1;
else if (memcmp(&incMAC, &ESPNOW2, 6) == 0) newData = 2;
2021-12-06 04:19:39 +00:00
else newData = 3;
2022-01-05 00:02:12 +00:00
ln = len / sizeof(DataReading);
2022-03-08 17:00:09 +00:00
//UART_IF.println("RCV:" + String(ln));
2021-12-06 04:19:39 +00:00
}
void getSerial() {
2022-03-08 17:00:09 +00:00
String incomingString = UART_IF.readStringUntil('\n');
//Serial.println("getting serial");
2022-01-05 00:02:12 +00:00
DynamicJsonDocument doc(24576);
2021-12-06 04:19:39 +00:00
DeserializationError error = deserializeJson(doc, incomingString);
if (error) { // Test if parsing succeeds.
2022-03-08 17:00:09 +00:00
Serial.print("parse err: ");
2022-02-17 06:12:18 +00:00
Serial.println(incomingString);
2021-12-06 04:19:39 +00:00
return;
} else {
int s = doc.size();
2022-03-08 17:00:09 +00:00
//UART_IF.println(s);
2021-12-06 04:19:39 +00:00
for (int i = 0; i < s; i++) {
theData[i].id = doc[i]["id"];
theData[i].t = doc[i]["type"];
theData[i].d = doc[i]["data"];
}
2022-01-05 00:02:12 +00:00
ln = s;
2021-12-06 04:19:39 +00:00
newData = 4;
}
}
2022-01-05 00:02:12 +00:00
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.
2022-03-08 17:00:09 +00:00
//UART_IF.println("parse err");
2022-01-05 00:02:12 +00:00
return;
} else {
int s = doc.size();
2022-03-08 17:00:09 +00:00
//UART_IF.println(s);
2022-01-05 00:02:12 +00:00
for (int i = 0; i < s; i++) {
theData[i].id = doc[i]["id"];
theData[i].t = doc[i]["type"];
theData[i].d = doc[i]["data"];
}
ln = s;
newData = 5;
}
}
2022-02-17 06:12:18 +00:00
2022-01-05 00:02:12 +00:00
void getLoRa() {
2022-02-17 06:12:18 +00:00
#ifdef USE_LORA
2022-01-05 00:02:12 +00:00
int packetSize = LoRa.parsePacket();
2022-02-17 06:12:18 +00:00
if (packetSize) {
uint8_t packet[packetSize];
uint8_t incLORAMAC[2];
LoRa.readBytes((uint8_t *)&packet, packetSize);
2022-02-19 01:47:40 +00:00
// for (int i = 0; i < packetSize; i++) {
2022-03-08 17:00:09 +00:00
// UART_IF.println(packet[i], HEX);
2022-02-19 01:47:40 +00:00
// }
if (memcmp(&packet, &selfAddress[3], 3) == 0) { //Check if addressed to this device
memcpy(&incLORAMAC, &packet[3], 2); //Split off address portion of packet
2022-02-17 06:12:18 +00:00
memcpy(&theData, &packet[5], packetSize - 5); //Split off data portion of packet
if (memcmp(&incLORAMAC, &LoRa1, 2) == 0) newData = 7; //Check if it is from a registered sender
else if (memcmp(&incLORAMAC, &LoRa2, 2) == 0) newData = 8;
else newData = 6;
ln = (packetSize - 5) / sizeof(DataReading);
newData = 6;
}
2022-01-05 00:02:12 +00:00
}
#endif
2022-02-17 06:12:18 +00:00
}
2021-12-06 04:19:39 +00:00
2022-02-07 17:43:36 +00:00
void sendESPNOW(uint8_t address) {
uint8_t NEWPEER[] = {MAC_PREFIX, address};
#if defined(ESP32)
esp_now_peer_info_t peerInfo;
peerInfo.ifidx = WIFI_IF_STA;
peerInfo.channel = 0;
peerInfo.encrypt = false;
memcpy(peerInfo.peer_addr, NEWPEER, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
2022-03-08 17:00:09 +00:00
UART_IF.println("Failed to add peer");
2022-02-07 17:43:36 +00:00
return;
}
#endif
DataReading thePacket[ln];
int j = 0;
for (int i = 0; i < ln; i++) {
if ( j > espnow_size) {
j = 0;
esp_now_send(NEWPEER, (uint8_t *) &thePacket, sizeof(thePacket));
}
thePacket[j] = theData[i];
j++;
}
esp_now_send(NEWPEER, (uint8_t *) &thePacket, j * sizeof(DataReading));
esp_now_del_peer(NEWPEER);
}
void sendSerial() {
DynamicJsonDocument doc(24576);
for (int i = 0; i < ln; i++) {
doc[i]["id"] = theData[i].id;
doc[i]["type"] = theData[i].t;
doc[i]["data"] = theData[i].d;
}
2022-03-08 17:00:09 +00:00
serializeJson(doc, UART_IF);
2022-02-07 17:43:36 +00:00
serializeJson(doc, Serial);
2022-03-08 17:00:09 +00:00
UART_IF.println();
2022-02-07 17:43:36 +00:00
Serial.println();
2022-03-08 17:00:09 +00:00
2022-02-07 17:43:36 +00:00
}
void sendMQTT() {
#ifdef USE_WIFI
DynamicJsonDocument doc(24576);
for (int i = 0; i < ln; i++) {
doc[i]["id"] = theData[i].id;
doc[i]["type"] = theData[i].t;
doc[i]["data"] = theData[i].d;
}
String outgoingString;
serializeJson(doc, outgoingString);
client.publish("esp/fdrs", (char*) outgoingString.c_str());
#endif
}
void bufferESPNOW(uint8_t interface) {
2021-12-06 04:19:39 +00:00
switch (interface) {
case 0:
2022-01-05 00:02:12 +00:00
for (int i = 0; i < ln; i++) {
2022-02-07 17:43:36 +00:00
ESPNOWGbuffer[lenESPNOWG + i] = theData[i];
2022-01-05 00:02:12 +00:00
}
lenESPNOWG += ln;
2021-12-06 04:19:39 +00:00
break;
case 1:
2022-01-05 00:02:12 +00:00
for (int i = 0; i < ln; i++) {
2022-02-07 17:43:36 +00:00
ESPNOW1buffer[lenESPNOW1 + i] = theData[i];
2022-01-05 00:02:12 +00:00
}
lenESPNOW1 += ln;
2021-12-06 04:19:39 +00:00
break;
case 2:
2022-01-05 00:02:12 +00:00
for (int i = 0; i < ln; i++) {
2022-02-07 17:43:36 +00:00
ESPNOW2buffer[lenESPNOW2 + i] = theData[i];
2022-01-05 00:02:12 +00:00
}
lenESPNOW2 += ln;
2021-12-06 04:19:39 +00:00
break;
}
2022-01-05 00:02:12 +00:00
}
2022-02-07 17:43:36 +00:00
void bufferSerial() {
2022-01-05 00:02:12 +00:00
for (int i = 0; i < ln; i++) {
2022-02-07 17:43:36 +00:00
SERIALbuffer[lenSERIAL + i] = theData[i];
2022-01-05 00:02:12 +00:00
}
lenSERIAL += ln;
2022-03-08 17:00:09 +00:00
//UART_IF.println("SENDSERIAL:" + String(lenSERIAL) + " ");
2021-12-06 04:19:39 +00:00
}
2022-02-07 17:43:36 +00:00
void bufferMQTT() {
2022-01-05 00:02:12 +00:00
for (int i = 0; i < ln; i++) {
2022-02-07 17:43:36 +00:00
MQTTbuffer[lenMQTT + i] = theData[i];
2021-12-06 04:19:39 +00:00
}
2022-01-05 00:02:12 +00:00
lenMQTT += ln;
}
2022-02-17 06:12:18 +00:00
//void bufferLoRa() {
// for (int i = 0; i < ln; i++) {
// LORAbuffer[lenLORA + i] = theData[i];
// }
// lenLORA += ln;
//}
void bufferLoRa(uint8_t interface) {
switch (interface) {
case 0:
for (int i = 0; i < ln; i++) {
LORAGbuffer[lenLORAG + i] = theData[i];
}
lenLORAG += ln;
break;
case 1:
for (int i = 0; i < ln; i++) {
LORA1buffer[lenLORA1 + i] = theData[i];
}
lenLORA1 += ln;
break;
case 2:
for (int i = 0; i < ln; i++) {
LORA2buffer[lenLORA2 + i] = theData[i];
}
lenLORA2 += ln;
break;
2022-01-05 00:02:12 +00:00
}
2021-12-06 04:19:39 +00:00
}
2022-01-05 00:02:12 +00:00
void releaseESPNOW(uint8_t interface) {
switch (interface) {
case 0:
{
DataReading thePacket[espnow_size];
int j = 0;
for (int i = 0; i < lenESPNOWG; i++) {
2022-02-17 06:12:18 +00:00
if ( j > espnow_size) {
2022-01-05 00:02:12 +00:00
j = 0;
esp_now_send(broadcast_mac, (uint8_t *) &thePacket, sizeof(thePacket));
}
2022-02-07 17:43:36 +00:00
thePacket[j] = ESPNOWGbuffer[i];
2022-01-05 00:02:12 +00:00
j++;
}
esp_now_send(broadcast_mac, (uint8_t *) &thePacket, j * sizeof(DataReading));
lenESPNOWG = 0;
break;
}
case 1:
{
DataReading thePacket[espnow_size];
int j = 0;
for (int i = 0; i < lenESPNOW1; i++) {
2022-02-17 06:12:18 +00:00
if ( j > espnow_size) {
2022-01-05 00:02:12 +00:00
j = 0;
2022-01-30 03:17:27 +00:00
esp_now_send(ESPNOW1, (uint8_t *) &thePacket, sizeof(thePacket));
2022-01-05 00:02:12 +00:00
}
2022-02-07 17:43:36 +00:00
thePacket[j] = ESPNOW1buffer[i];
2022-01-05 00:02:12 +00:00
j++;
}
2022-01-30 03:17:27 +00:00
esp_now_send(ESPNOW1, (uint8_t *) &thePacket, j * sizeof(DataReading));
2022-01-05 00:02:12 +00:00
lenESPNOW1 = 0;
break;
}
case 2:
{
DataReading thePacket[espnow_size];
int j = 0;
for (int i = 0; i < lenESPNOW2; i++) {
2022-02-17 06:12:18 +00:00
if ( j > espnow_size) {
2022-01-05 00:02:12 +00:00
j = 0;
2022-01-30 03:17:27 +00:00
esp_now_send(ESPNOW2, (uint8_t *) &thePacket, sizeof(thePacket));
2022-01-05 00:02:12 +00:00
}
2022-02-07 17:43:36 +00:00
thePacket[j] = ESPNOW2buffer[i];
2022-01-05 00:02:12 +00:00
j++;
}
2022-01-30 03:17:27 +00:00
esp_now_send(ESPNOW2, (uint8_t *) &thePacket, j * sizeof(DataReading));
2022-01-05 00:02:12 +00:00
lenESPNOW2 = 0;
break;
}
}
}
2022-02-17 06:12:18 +00:00
#ifdef USE_LORA
void transmitLoRa(uint8_t* mac, DataReading * packet, uint8_t len) {
uint8_t pkt[5 + (len * sizeof(DataReading))];
2022-02-19 01:47:40 +00:00
memcpy(&pkt, mac, 3);
memcpy(&pkt[3], &selfAddress[4], 2);
2022-02-17 06:12:18 +00:00
memcpy(&pkt[5], packet, len * sizeof(DataReading));
LoRa.beginPacket();
LoRa.write((uint8_t*)&pkt, sizeof(pkt));
LoRa.endPacket();
}
#endif
void releaseLoRa(uint8_t interface) {
#ifdef USE_LORA
switch (interface) {
case 0:
{
DataReading thePacket[lora_size];
int j = 0;
for (int i = 0; i < lenLORAG; i++) {
if ( j > lora_size) {
j = 0;
transmitLoRa(broadcast_mac, thePacket, j);
}
thePacket[j] = LORAGbuffer[i];
j++;
}
transmitLoRa(broadcast_mac, thePacket, j);
lenLORAG = 0;
break;
}
case 1:
{
DataReading thePacket[lora_size];
int j = 0;
for (int i = 0; i < lenLORA1; i++) {
if ( j > lora_size) {
j = 0;
transmitLoRa(LoRa1, thePacket, j);
}
thePacket[j] = LORA1buffer[i];
j++;
}
transmitLoRa(LoRa1, thePacket, j);
lenLORA1 = 0;
break;
}
case 2:
{
DataReading thePacket[lora_size];
int j = 0;
for (int i = 0; i < lenLORA2; i++) {
if ( j > lora_size) {
j = 0;
transmitLoRa(LoRa2, thePacket, j);
}
thePacket[j] = LORA2buffer[i];
j++;
}
transmitLoRa(LoRa2, thePacket, j);
lenLORA2 = 0;
break;
}
}
#endif
}
2022-01-05 00:02:12 +00:00
void releaseSerial() {
2022-02-07 17:43:36 +00:00
DynamicJsonDocument doc(24576);
2022-01-05 00:02:12 +00:00
for (int i = 0; i < lenSERIAL; i++) {
2022-02-07 17:43:36 +00:00
doc[i]["id"] = SERIALbuffer[i].id;
doc[i]["type"] = SERIALbuffer[i].t;
doc[i]["data"] = SERIALbuffer[i].d;
2021-12-06 04:19:39 +00:00
}
2022-03-08 17:00:09 +00:00
serializeJson(doc, UART_IF);
UART_IF.println();
2022-01-05 00:02:12 +00:00
lenSERIAL = 0;
2021-12-06 04:19:39 +00:00
}
2022-01-05 00:02:12 +00:00
void releaseMQTT() {
#ifdef USE_WIFI
DynamicJsonDocument doc(24576);
for (int i = 0; i < lenMQTT; i++) {
2022-02-07 17:43:36 +00:00
doc[i]["id"] = MQTTbuffer[i].id;
doc[i]["type"] = MQTTbuffer[i].t;
doc[i]["data"] = MQTTbuffer[i].d;
2021-12-06 04:19:39 +00:00
}
2022-01-05 00:02:12 +00:00
String outgoingString;
serializeJson(doc, outgoingString);
client.publish("esp/fdrs", (char*) outgoingString.c_str());
lenMQTT = 0;
#endif
}
2021-12-06 04:19:39 +00:00
void reconnect() {
2022-03-08 17:00:09 +00:00
#ifdef USE_WIFI
2021-12-06 04:19:39 +00:00
// 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);
}
}
2022-03-08 17:00:09 +00:00
#endif
2021-12-06 04:19:39 +00:00
}
2022-01-05 00:02:12 +00:00
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
2022-01-30 03:17:27 +00:00
esp_now_add_peer(ESPNOW1, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
esp_now_add_peer(ESPNOW2, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
2022-01-05 00:02:12 +00:00
#elif defined(ESP32)
esp_wifi_set_mac(WIFI_IF_STA, &selfAddress[0]);
if (esp_now_init() != ESP_OK) {
2022-03-08 17:00:09 +00:00
UART_IF.println("Error initializing ESP-NOW");
2022-01-05 00:02:12 +00:00
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
2022-01-30 03:17:27 +00:00
memcpy(peerInfo.peer_addr, ESPNOW1, 6);
2022-01-05 00:02:12 +00:00
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
2022-03-08 17:00:09 +00:00
UART_IF.println("Failed to add peer 1");
2022-01-05 00:02:12 +00:00
return;
}
2022-01-30 03:17:27 +00:00
memcpy(peerInfo.peer_addr, ESPNOW2, 6);
2022-01-05 00:02:12 +00:00
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
2022-03-08 17:00:09 +00:00
UART_IF.println("Failed to add peer 2");
2022-01-05 00:02:12 +00:00
return;
}
memcpy(peerInfo.peer_addr, broadcast_mac, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
2022-03-08 17:00:09 +00:00
UART_IF.println("Failed to add peer bcast");
2022-01-05 00:02:12 +00:00
return;
}
#endif
}