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

186 lines
3.9 KiB
Arduino
Raw Normal View History

2022-06-30 13:30:24 +00:00
// FARM DATA RELAY SYSTEM
//
// GATEWAY 2.000
//
// Developed by Timm Bogner (timmbogner@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
//
#include "fdrs_gateway_config.h"
2022-06-30 13:30:24 +00:00
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <espnow.h>
#elif defined(ESP32)
#include <esp_now.h>
#include <WiFi.h>
#include <esp_wifi.h>
#endif
#include <ArduinoJson.h>
#ifdef USE_WIFI
#include <PubSubClient.h>
2022-07-06 20:42:59 +00:00
#include <WiFiUdp.h>
2022-06-30 13:30:24 +00:00
#endif
#ifdef USE_LORA
#include <LoRa.h>
#endif
#ifdef USE_LED
#include <FastLED.h>
#endif
2022-07-03 16:50:56 +00:00
#ifdef USE_SD_LOG
#include <SPI.h>
#include <SD.h>
#endif
#ifdef USE_FS_LOG
#include <LittleFS.h>
#endif
2022-07-12 19:32:27 +00:00
#if defined (USE_SD_LOG) || defined (USE_FS_LOG)
#include <time.h>
#endif
2022-07-21 04:32:39 +00:00
//#include <fdrs_functions.h> //Use global functions file
#include "fdrs_functions.h" //Use local functions file
2022-06-30 13:30:24 +00:00
void setup() {
#if defined(ESP8266)
Serial.begin(115200);
#elif defined(ESP32)
Serial.begin(115200);
UART_IF.begin(115200, SERIAL_8N1, RXD2, TXD2);
#endif
DBG("Address:" + String (UNIT_MAC, HEX));
#ifdef USE_LED
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
leds[0] = CRGB::Blue;
FastLED.show();
#endif
#ifdef USE_WIFI
delay(10);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
DBG("Connecting to WiFi...");
DBG(FDRS_WIFI_SSID);
delay(500);
}
DBG("WiFi Connected");
client.setServer(mqtt_server, mqtt_port);
if (!client.connected()) {
2022-07-04 19:54:39 +00:00
reconnect(5);
2022-06-30 13:30:24 +00:00
}
client.setCallback(mqtt_callback);
#else
begin_espnow();
#endif
#ifdef USE_LORA
begin_lora();
2022-06-30 13:30:24 +00:00
#endif
2022-07-03 16:50:56 +00:00
#ifdef USE_SD_LOG
2022-07-03 16:03:37 +00:00
begin_SD();
#endif
#ifdef USE_FS_LOG
2022-07-06 22:27:01 +00:00
begin_FS();
#endif
2022-06-30 13:30:24 +00:00
//DBG(sizeof(DataReading));
#ifdef USE_WIFI
client.publish(TOPIC_STATUS, "FDRS initialized");
#endif
}
void loop() {
2022-07-21 04:32:39 +00:00
handleCommands();
2022-06-30 13:30:24 +00:00
#ifdef ESPNOWG_DELAY
if ((millis() - timeESPNOWG) >= ESPNOWG_DELAY) {
timeESPNOWG = millis();
2022-06-30 13:30:24 +00:00
if (lenESPNOWG > 0) releaseESPNOW(0);
}
#endif
#ifdef ESPNOW1_DELAY
if ((millis() - timeESPNOW1) >= ESPNOW1_DELAY) {
timeESPNOW1 = millis();
2022-06-30 13:30:24 +00:00
if (lenESPNOW1 > 0) releaseESPNOW(1);
}
#endif
#ifdef ESPNOW2_DELAY
if ((millis() - timeESPNOW2) >= ESPNOW2_DELAY) {
timeESPNOW2 = millis();
2022-06-30 13:30:24 +00:00
if (lenESPNOW2 > 0) releaseESPNOW(2);
}
#endif
#ifdef SERIAL_DELAY
if ((millis() - timeSERIAL) >= SERIAL_DELAY) {
timeSERIAL = millis();
2022-06-30 13:30:24 +00:00
if (lenSERIAL > 0) releaseSerial();
}
#endif
#ifdef MQTT_DELAY
if ((millis() - timeMQTT) >= MQTT_DELAY) {
timeMQTT = millis();
2022-06-30 13:30:24 +00:00
if (lenMQTT > 0) releaseMQTT();
}
#endif
#ifdef LORAG_DELAY
if ((millis() - timeLORAG) >= LORAG_DELAY) {
timeLORAG = millis();
2022-06-30 13:30:24 +00:00
if (lenLORAG > 0) releaseLoRa(0);
}
#endif
#ifdef LORA1_DELAY
if ((millis() - timeLORA1) >= LORA1_DELAY) {
timeLORA1 = millis();
2022-06-30 13:30:24 +00:00
if (lenLORA1 > 0) releaseLoRa(1);
}
#endif
#ifdef LORA2_DELAY
if ((millis() - timeLORA2) >= LORA2_DELAY) {
timeLORA2 = millis();
2022-06-30 13:30:24 +00:00
if (lenLORA2 > 0) releaseLoRa(2);
}
#endif
#if defined (USE_SD_LOG) || defined (USE_FS_LOG)
if ((millis() - timeLOGBUF) >= LOGBUF_DELAY){
timeLOGBUF = millis();
if (logBufferPos > 0) releaseLogBuffer();
}
#endif
2022-06-30 13:30:24 +00:00
while (UART_IF.available()) {
getSerial();
}
getLoRa();
2022-07-04 19:54:39 +00:00
#ifdef USE_WIFI
2022-06-30 13:30:24 +00:00
if (!client.connected()) {
2022-07-04 19:54:39 +00:00
reconnect(1, true);
2022-06-30 13:30:24 +00:00
}
2022-07-04 19:54:39 +00:00
client.loop(); // for recieving incoming messages and maintaining connection
2022-07-06 20:42:59 +00:00
#endif
2022-06-30 13:30:24 +00:00
if (newData) {
switch (newData) {
2022-07-10 01:22:33 +00:00
case event_espnowg:
ESPNOWG_ACT
break;
2022-07-10 01:22:33 +00:00
case event_espnow1:
2022-06-30 13:30:24 +00:00
ESPNOW1_ACT
break;
2022-07-10 01:22:33 +00:00
case event_espnow2:
2022-06-30 13:30:24 +00:00
ESPNOW2_ACT
break;
2022-07-10 01:22:33 +00:00
case event_serial:
2022-06-30 13:30:24 +00:00
SERIAL_ACT
break;
2022-07-10 01:22:33 +00:00
case event_mqtt:
2022-06-30 13:30:24 +00:00
MQTT_ACT
break;
2022-07-10 01:22:33 +00:00
case event_lorag:
2022-06-30 13:30:24 +00:00
LORAG_ACT
break;
2022-07-10 01:22:33 +00:00
case event_lora1:
2022-06-30 13:30:24 +00:00
LORA1_ACT
break;
2022-07-10 01:22:33 +00:00
case event_lora2:
2022-06-30 13:30:24 +00:00
LORA2_ACT
break;
}
2022-07-19 01:42:42 +00:00
newData = event_clear;
2022-06-30 13:30:24 +00:00
}
}