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

131 lines
2.8 KiB
Arduino
Raw Normal View History

2021-12-06 04:19:39 +00:00
// FARM DATA RELAY SYSTEM
//
// GATEWAY 2.000
//
// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
2021-12-06 17:36:18 +00:00
//
2022-02-17 06:12:18 +00:00
#include "fdrs_config.h"
#include "DataReading.h"
#ifdef ESP8266
2021-12-06 04:19:39 +00:00
#include <ESP8266WiFi.h>
#include <espnow.h>
#elif defined(ESP32)
#include <esp_now.h>
#include <WiFi.h>
#include <esp_wifi.h>
#endif
#include <ArduinoJson.h>
#include <PubSubClient.h>
2022-02-17 06:12:18 +00:00
#ifdef USE_LORA
#include <LoRa.h>
#endif
2022-02-17 06:12:18 +00:00
#include "fdrs_functions.h"
2021-12-07 17:36:45 +00:00
2021-12-06 04:19:39 +00:00
void setup() {
2021-12-08 03:45:33 +00:00
#if defined(ESP8266)
Serial.begin(115200);
#elif defined(ESP32)
2022-02-17 06:12:18 +00:00
#if defined(RXD2)
2021-12-08 03:45:33 +00:00
Serial.begin(115200, SERIAL_8N1, RXD2, TXD2);
2022-02-17 06:12:18 +00:00
#elif !defined(RXD2)
Serial.begin(115200);
#endif
2021-12-08 03:45:33 +00:00
#endif
2022-02-19 01:47:40 +00:00
2021-12-06 04:19:39 +00:00
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
#ifdef USE_LORA
SPI.begin(SCK, MISO, MOSI, SS);
LoRa.setPins(SS, RST, DIO0);
if (!LoRa.begin(BAND)) {
while (1);
}
2022-02-19 01:47:40 +00:00
#endif
Serial.println(sizeof(DataReading));
2021-12-06 04:19:39 +00:00
}
void loop() {
2022-01-05 00:02:12 +00:00
if (millis() > timeESPNOWG) {
timeESPNOWG += ESPNOWG_DELAY;
if (lenESPNOWG > 0) releaseESPNOW(0);
}
if (millis() > timeESPNOW1) {
timeESPNOW1 += ESPNOW1_DELAY;
if (lenESPNOW1 > 0) releaseESPNOW(1);
}
if (millis() > timeESPNOW2) {
timeESPNOW2 += ESPNOW2_DELAY;
if (lenESPNOW2 > 0) releaseESPNOW(2);
}
if (millis() > timeSERIAL) {
timeSERIAL += SERIAL_DELAY;
if (lenSERIAL > 0) releaseSerial();
}
if (millis() > timeMQTT) {
timeMQTT += MQTT_DELAY;
if (lenMQTT > 0) releaseMQTT();
}
2022-02-17 06:12:18 +00:00
if (millis() > timeLORAG) {
timeLORAG += LORAG_DELAY;
if (lenLORAG > 0) releaseLoRa(0);
}
if (millis() > timeLORA1) {
timeLORA1 += LORA1_DELAY;
if (lenLORA1 > 0) releaseLoRa(1);
}
if (millis() > timeLORA2) {
timeLORA2 += LORA2_DELAY;
if (lenLORA2 > 0) releaseLoRa(2);
2022-01-05 00:02:12 +00:00
}
2021-12-06 04:19:39 +00:00
while (Serial.available()) {
getSerial();
}
2022-02-17 06:12:18 +00:00
getLoRa();
2022-01-05 00:02:12 +00:00
#ifdef USE_WIFI
if (!client.connected()) {
reconnect();
}
client.loop();
#endif
if (newData) {
2021-12-06 04:19:39 +00:00
switch (newData) {
case 1: //ESP-NOW #1
2021-12-06 17:36:18 +00:00
ESPNOW1_ACT
2021-12-06 04:19:39 +00:00
break;
case 2: //ESP-NOW #2
2021-12-06 17:36:18 +00:00
ESPNOW2_ACT
2021-12-06 04:19:39 +00:00
break;
case 3: //ESP-NOW General
2021-12-06 17:36:18 +00:00
ESPNOWG_ACT
2021-12-06 04:19:39 +00:00
break;
case 4: //Serial
2021-12-06 17:36:18 +00:00
SERIAL_ACT
2021-12-06 04:19:39 +00:00
break;
case 5: //MQTT
2021-12-06 17:36:18 +00:00
MQTT_ACT
2021-12-06 04:19:39 +00:00
break;
2022-02-17 06:12:18 +00:00
case 6: //LoRa General
LORAG_ACT
break;
case 7: //LoRa #1
LORA1_ACT
break;
case 8: //LoRa #2
LORA2_ACT
break;
2021-12-06 04:19:39 +00:00
}
newData = 0;
}
}