123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392 |
- /*
- * Copyright (c) 2021 Thomas Buck <thomas@xythobuz.de>
- *
- * This file is part of Giess-o-mat.
- *
- * Giess-o-mat is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Giess-o-mat is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Giess-o-mat. If not, see <https://www.gnu.org/licenses/>.
- */
-
- #include <Arduino.h>
-
- #ifdef PLATFORM_ESP
-
- #if defined(ARDUINO_ARCH_ESP8266)
-
- #include <ESP8266WiFi.h>
- #include <ESP8266WebServer.h>
- #include <ESP8266mDNS.h>
-
- #elif defined(ARDUINO_ARCH_ESP32)
-
- #include <WiFi.h>
- #include <WebServer.h>
- #include <ESPmDNS.h>
-
- #endif
-
- #include <WebSocketsServer.h>
-
- #include "wifi.h"
- #include "config.h"
- #include "config_pins.h"
- #include "Functionality.h"
- #include "SimpleUpdater.h"
- #include "DebugLog.h"
- #include "BoolField.h"
- #include "WifiStuff.h"
-
- #ifdef TELEGRAM_TOKEN
-
- #include <WiFiClientSecure.h>
- #include <AsyncTelegram2.h>
-
- //#define TELEGRAM_LOG_TIMINGS
-
- #if defined(ARDUINO_ARCH_ESP8266)
- X509List cert(telegram_cert);
- #endif
-
- WiFiClientSecure secured_client;
- AsyncTelegram2 bot(secured_client);
- int64_t trusted_chat_ids[] = TRUSTED_IDS;
-
- enum telegram_state {
- BOT_IDLE,
- BOT_ASKED_FERT,
- BOT_ASKED_PLANTS,
- BOT_ASKED_CONFIRM
- };
-
- enum telegram_state bot_state = BOT_IDLE;
- int64_t bot_lock = 0;
-
- #endif // TELEGRAM_TOKEN
-
- #ifdef MQTT_HOST
-
- #include <PubSubClient.h>
- WiFiClient mqttClient;
- PubSubClient mqtt(mqttClient);
- unsigned long last_mqtt_reconnect_time = 0;
-
- #endif // MQTT_HOST
-
- #if defined(TELEGRAM_TOKEN) || defined(MQTT_HOST)
- static BoolField bot_plants(VALVE_COUNT - 1);
- static BoolField bot_ferts(PUMP_COUNT);
- #endif // TELEGRAM_TOKEN || MQTT_HOST
-
- UPDATE_WEB_SERVER server(80);
- WebSocketsServer socket = WebSocketsServer(81);
- SimpleUpdater updater;
- unsigned long last_server_handle_time = 0;
- unsigned long last_websocket_update_time = 0;
- int wifi_ok = 0;
-
- String message_buffer_a;
- String message_buffer_b;
- String message_buffer_c;
- String message_buffer_d;
-
- #ifdef ENABLE_GPIO_TEST
- static bool runningGpioTest = false;
- static bool gpioTestState = false;
- unsigned long lastGpioTime = 0;
- #endif // ENABLE_GPIO_TEST
-
- #ifdef ENABLE_INFLUXDB_LOGGING
- #include <InfluxDb.h>
- Influxdb influx(INFLUXDB_HOST, INFLUXDB_PORT);
- #endif // ENABLE_INFLUXDB_LOGGING
-
- #ifdef ENABLE_GPIO_TEST
-
- void runGpioTest(bool state) {
- lastGpioTime = millis();
-
- for (int i = 0; i < VALVE_COUNT; i++) {
- get_plants()->getValves()->setPin(i, state);
- delay(GPIO_TEST_DELAY);
- }
-
- for (int i = 0; i < PUMP_COUNT; i++) {
- get_plants()->getPumps()->setPin(i, state);
- if (i < (PUMP_COUNT - 1)) {
- delay(GPIO_TEST_DELAY);
- }
- }
- }
-
- void handleGpioTest() {
- runningGpioTest = !runningGpioTest;
- gpioTestState = runningGpioTest;
-
- String message = F("GPIOs turned ");
- message += runningGpioTest ? "on" : "off";
-
- server.send(200, "text/html", message);
-
- runGpioTest(gpioTestState);
- }
-
- #endif // ENABLE_GPIO_TEST
-
- void wifi_broadcast_state_change(const char *s) {
- #ifdef TELEGRAM_TOKEN
- for (int n = 0; n < (sizeof(trusted_chat_ids) / sizeof(trusted_chat_ids[0])); n++) {
- bot.sendTo(trusted_chat_ids[n], "New state: " + String(s));
- }
- #endif // TELEGRAM_TOKEN
-
- #ifdef MQTT_HOST
- String payload = String("state: ") + s;
- mqtt.publish("giessomat", payload.c_str());
- #endif // MQTT_HOST
- }
-
- #ifdef TELEGRAM_TOKEN
-
- String telegram_help() {
- String s = "Usage:\n";
- s += "Send /auto and follow prompts.\n";
- s += "Send /abort to cancel menus.\n";
- s += "Send /none to skip menus.\n";
- s += "Send /begin to confirm menus.";
- return s;
- }
-
- void telegram_handle_message(TBMessage &msg) {
- if (!sm_is_idle()) {
- debug.println("Telegram: message while machine in use");
-
- if (msg.text == "/abort") {
- sm_bot_abort();
- bot.sendTo(msg.chatId, "Aborted current cycle!");
- } else {
- bot.sendTo(msg.chatId, "Machine is already in use.\nPlease try again later.");
- }
-
- return;
- }
-
- if ((bot_state == BOT_IDLE) && (bot_lock == 0)) {
- bot_lock = msg.chatId;
- debug.println("Telegram: locked to " + bot_lock);
- }
-
- if (bot_lock != msg.chatId) {
- debug.println("Telegram: bot locked. abort for chat " + msg.chatId);
- bot.sendTo(msg.chatId, "Bot is already in use.\nPlease try again later.");
- return;
- }
-
- if (bot_state == BOT_IDLE) {
- if (msg.text == "/auto") {
- String s = "Please enter fertilizer numbers.\n";
- s += "Valid numbers: 1 to " + String(PUMP_COUNT) + "\n";
- s += "Send /none to skip.\n";
- s += "Send /abort to cancel.";
- bot_ferts.clear();
- bot_state = BOT_ASKED_FERT;
- bot.sendTo(msg.chatId, s);
- } else if (msg.text == "/abort") {
- bot_lock = 0;
- bot.sendTo(msg.chatId, "Nothing to abort.");
- } else {
- bot_lock = 0;
- bot.sendTo(msg.chatId, telegram_help());
- }
- } else if (bot_state == BOT_ASKED_FERT) {
- if (msg.text == "/abort") {
- bot_state = BOT_IDLE;
- bot_lock = 0;
- bot.sendTo(msg.chatId, "Aborted.");
- return;
- } else if (msg.text != "/none") {
- String buff;
- for (int i = 0; i < msg.text.length() + 1; i++) {
- if ((i == msg.text.length()) || (msg.text[i] == ' ')) {
- if (buff.length() > 0) {
- int n = buff.toInt() - 1;
- buff = "";
- bot_ferts.set(n);
- }
- } else if ((msg.text[i] >= '0') && (msg.text[i] <= '9')) {
- buff += msg.text[i];
- } else {
- bot_state = BOT_IDLE;
- bot_lock = 0;
- bot.sendTo(msg.chatId, "Invalid input.\nAborted.");
- return;
- }
- }
- }
-
- String s = "Please enter plant numbers.\n";
- s += "Valid numbers: 1 to " + String(VALVE_COUNT - 1) + "\n";
- s += "Send /abort to cancel.";
- bot_plants.clear();
- bot_state = BOT_ASKED_PLANTS;
- bot.sendTo(msg.chatId, s);
- } else if (bot_state == BOT_ASKED_PLANTS) {
- if (msg.text == "/abort") {
- bot_state = BOT_IDLE;
- bot_lock = 0;
- bot.sendTo(msg.chatId, "Aborted.");
- return;
- }
-
- String buff;
- for (int i = 0; i < msg.text.length() + 1; i++) {
- if ((i == msg.text.length()) || (msg.text[i] == ' ')) {
- if (buff.length() > 0) {
- int n = buff.toInt() - 1;
- buff = "";
- bot_plants.set(n);
- }
- } else if ((msg.text[i] >= '0') && (msg.text[i] <= '9')) {
- buff += msg.text[i];
- } else {
- bot_state = BOT_IDLE;
- bot_lock = 0;
- bot.sendTo(msg.chatId, "Invalid input.\nAborted.");
- return;
- }
- }
-
- if (bot_plants.countSet() <= 0) {
- bot_state = BOT_IDLE;
- bot_lock = 0;
- bot.sendTo(msg.chatId, "No plants selected.\nAborted.");
- return;
- }
-
- #ifdef FULLAUTO_MIN_PLANT_COUNT
- if (bot_plants.countSet() < FULLAUTO_MIN_PLANT_COUNT) {
- bot_state = BOT_IDLE;
- bot_lock = 0;
- bot.sendTo(msg.chatId, "Select at least " + String(FULLAUTO_MIN_PLANT_COUNT) + " plants.\nAborted.");
- return;
- }
- #endif
-
- String s = "Input accepted.\nFertilizers:";
- for (int i = 0; i < PUMP_COUNT; i++) {
- if (bot_ferts.isSet(i)) {
- s += " " + String(i + 1);
- }
- }
- s += "\nPlants:";
- for (int i = 0; i < (VALVE_COUNT - 1); i++) {
- if (bot_plants.isSet(i)) {
- s += " " + String(i + 1);
- }
- }
- s += "\nOk? Send /begin to start or /abort to cancel.";
- bot_state = BOT_ASKED_CONFIRM;
- bot.sendTo(msg.chatId, s);
- } else if (bot_state == BOT_ASKED_CONFIRM) {
- if (msg.text == "/abort") {
- bot_state = BOT_IDLE;
- bot_lock = 0;
- bot.sendTo(msg.chatId, "Aborted.");
- } else if (msg.text == "/begin") {
- bot_state = BOT_IDLE;
- bot_lock = 0;
- bot.sendTo(msg.chatId, "Auto watering cycle started.");
- sm_bot_start_auto(bot_ferts, bot_plants);
- } else {
- bot_state = BOT_IDLE;
- bot_lock = 0;
- bot.sendTo(msg.chatId, "Unknown message.\nAborted.");
- }
- } else {
- debug.println("Telegram: invalid state");
- bot_state = BOT_IDLE;
- bot_lock = 0;
- bot.sendTo(msg.chatId, "Internal error.\nPlease try again.");
- }
- }
-
- void telegram_handler(TBMessage &msg) {
- debug.println("Telegram: rx " + String((long int)msg.chatId) + " \"" + msg.text + "\"");
-
- bool found = false;
- for (int n = 0; n < (sizeof(trusted_chat_ids) / sizeof(trusted_chat_ids[0])); n++) {
- if (trusted_chat_ids[n] == msg.chatId) {
- found = true;
- break;
- }
- }
- if (!found) {
- bot.sendTo(msg.chatId, "Sorry, not authorized!");
- return;
- }
-
- telegram_handle_message(msg);
- }
-
- void telegram_poll() {
- #ifdef TELEGRAM_LOG_TIMINGS
- unsigned long start = millis();
- #endif // TELEGRAM_LOG_TIMINGS
-
- TBMessage msg;
- while (bot.getNewMessage(msg)) {
- telegram_handler(msg);
- }
-
- #ifdef TELEGRAM_LOG_TIMINGS
- unsigned long end = millis();
- debug.println("Telegram: took " + String(end - start) + "ms");
- #endif // TELEGRAM_LOG_TIMINGS
- }
-
- void telegram_hello() {
- for (int n = 0; n < (sizeof(trusted_chat_ids) / sizeof(trusted_chat_ids[0])); n++) {
- bot.sendTo(trusted_chat_ids[n], "Giess-o-mat v" FIRMWARE_VERSION " initialized.\nSend /auto to begin.");
- }
- }
- #endif // TELEGRAM_TOKEN
-
- #ifdef MQTT_HOST
-
- static void mqttCallback(char* topic, byte* payload, unsigned int length) {
- String ts(topic), ps;
- for (unsigned int i = 0; i < length; i++) {
- char c = payload[i];
- ps += c;
- }
-
- debug.println("MQTT Rx @ " + ts + ": " + ps);
-
- if (ts != "giessomat") {
- debug.println("MQTT: invalid topic");
- return;
- }
-
- if (ps == "abort") {
- if (sm_is_idle()) {
- debug.println("MQTT: nothing to abort");
- } else {
- sm_bot_abort();
- debug.println("MQTT: user abort");
- }
- } else {
- if (ps.substring(0, 6) == "state:") {
- return;
- }
-
- if (ps.substring(0, 4) != "auto") {
- debug.println("MQTT: invalid payload");
- return;
- }
-
- if (!sm_is_idle()) {
- debug.println("MQTT: machine is in use");
- return;
- }
-
- bot_ferts.clear();
- bot_plants.clear();
-
- String buff;
- bool at_plants = false;
- for (int i = 5; i < ps.length() + 1; i++) {
- if ((i == ps.length()) || (ps[i] == ' ') || (ps[i] == ',')) {
- if (buff != "none") {
- int n = buff.toInt() - 1;
- if (!at_plants) {
- bot_ferts.set(n);
- } else {
- bot_plants.set(n);
- }
- }
- buff = "";
-
- if ((i < ps.length()) && (ps[i] == ' ')) {
- at_plants = true;
- }
- } else {
- buff += ps[i];
- }
- }
-
- String s = "MQTT: fertilizers:";
- for (int i = 0; i < PUMP_COUNT; i++) {
- if (bot_ferts.isSet(i)) {
- s += " " + String(i + 1);
- }
- }
- debug.println(s);
-
- s = "MQTT: plants:";
- for (int i = 0; i < (VALVE_COUNT - 1); i++) {
- if (bot_plants.isSet(i)) {
- s += " " + String(i + 1);
- }
- }
- debug.println(s);
-
- if (bot_plants.countSet() <= 0) {
- debug.println("MQTT: no plants selected");
- return;
- }
-
- #ifdef FULLAUTO_MIN_PLANT_COUNT
- if (bot_plants.countSet() < FULLAUTO_MIN_PLANT_COUNT) {
- debug.println("MQTT: not enough plants selected");
- return;
- }
- #endif
-
- sm_bot_start_auto(bot_ferts, bot_plants);
- }
- }
-
- static void mqttReconnect() {
- // Create a random client ID
- String clientId = F("giessomat-");
- clientId += String(random(0xffff), HEX);
-
- // Attempt to connect
- #if defined(MQTT_USER) && defined(MQTT_PASS)
- if (mqtt.connect(clientId.c_str(), MQTT_USER, MQTT_PASS)) {
- #else
- if (mqtt.connect(clientId.c_str())) {
- #endif
- mqtt.subscribe("giessomat");
- }
- }
-
- #endif // MQTT_HOST
-
- bool wifi_write_database(int duration, const char *type, int id) {
- bool success = false;
-
- #ifdef ENABLE_INFLUXDB_LOGGING
- // we still want to be locally usable / have a snappy ui
- // even when the wifi connection is broken.
- if (WiFi.status() != WL_CONNECTED) {
- debug.println("Won't attempt db write, no WiFi connection.");
- return success;
- }
-
- InfluxData measurement(type);
- measurement.addTag("version", FIRMWARE_VERSION);
- measurement.addTag("device", WiFi.macAddress());
- measurement.addTag("id", String(id));
- measurement.addValue("duration", duration);
- success = influx.write(measurement);
- #endif // ENABLE_INFLUXDB_LOGGING
-
- if (!success) {
- debug.print("Error writing to InfluxDB ");
- debug.print(INFLUXDB_HOST);
- debug.print(":");
- debug.print(INFLUXDB_PORT);
- debug.print("/");
- debug.print(INFLUXDB_DATABASE);
- debug.print("/");
- debug.println(type);
- }
-
- return success;
- }
-
- void wifi_set_message_buffer(String a, String b, String c, String d) {
- message_buffer_a = a;
- message_buffer_b = b;
- message_buffer_c = c;
- message_buffer_d = d;
- }
-
- void wifi_schedule_websocket(void) {
- last_websocket_update_time = 0;
- }
-
- void wifi_send_status_broadcast(void) {
- if (WiFi.status() != WL_CONNECTED) {
- return;
- }
-
- if (socket.connectedClients() <= 0) {
- return;
- }
-
- String a = message_buffer_a ;
- String b = message_buffer_b;
- String c = message_buffer_c;
- String d = message_buffer_d;
-
- a.replace("\"", "'");
- b.replace("\"", "'");
- c.replace("\"", "'");
- d.replace("\"", "'");
-
- String ws = "{\n";
-
- ws += "\"a\": \"" + a + "\",\n";
- ws += "\"b\": \"" + b + "\",\n";
- ws += "\"c\": \"" + c + "\",\n";
- ws += "\"d\": \"" + d + "\",\n";
-
- ws += "\"state\": \"" + String(control_state_name()) + "\",\n";
-
- ws += F("\"valves\": [ ");
- for (int i = 0; i < VALVE_COUNT; i++) {
- ws += "\"";
- ws += get_plants()->getValves()->getPin(i) ? "1" : "0";
- ws += "\"";
-
- if (i < (VALVE_COUNT - 1)) {
- ws += ", ";
- }
- }
- ws += " ],\n";
-
- ws += F("\"pumps\": [ ");
- for (int i = 0; i < PUMP_COUNT; i++) {
- ws += "\"";
- ws += get_plants()->getPumps()->getPin(i) ? "1" : "0";
- ws += "\"";
-
- if (i < (PUMP_COUNT - 1)) {
- ws += ", ";
- }
- }
- ws += " ],\n";
-
- ws += F("\"switches\": [ ");
- for (int i = 0; i < SWITCH_COUNT; i++) {
- bool v = get_plants()->getSwitches()->getPin(i);
-
- #ifdef INVERT_SENSOR_BOTTOM
- if (i == 0) {
- v = !v;
- }
- #endif // INVERT_SENSOR_BOTTOM
-
- #ifdef INVERT_SENSOR_TOP
- if (i == 1) {
- v = !v;
- }
- #endif // INVERT_SENSOR_TOP
-
- ws += "\"";
- ws += v ? "1" : "0";
- ws += "\"";
-
- if (i < (SWITCH_COUNT - 1)) {
- ws += ", ";
- }
- }
- ws += " ],\n";
-
- ws += F("\"aux\": [ ");
- for (int i = 0; i < AUX_COUNT; i++) {
- ws += "\"";
- ws += get_plants()->getAux()->getPin(i) ? "1" : "0";
- ws += "\"";
-
- if (i < (AUX_COUNT - 1)) {
- ws += ", ";
- }
- }
- ws += " ],\n";
-
- ws += F("\"kickstart\": [ ");
- for (int i = 0; i < VALVE_COUNT - 1; i++) {
- ws += "\"";
- ws += get_plants()->getKickstart()->getPin(i) ? "1" : "0";
- ws += "\"";
-
- if (i < (VALVE_COUNT - 2)) {
- ws += ", ";
- }
- }
- ws += " ],\n";
-
- ws += "\"switchstate\": \"";
- Plants::Waterlevel wl = get_plants()->getWaterlevel();
- if (wl == Plants::empty) {
- ws += F("tank empty");
- } else if (wl == Plants::inbetween) {
- ws += F("tank half-filled");
- } else if (wl == Plants::full) {
- ws += F("tank full");
- } else {
- ws += F("invalid sensor state");
- }
- ws += "\"\n";
- ws += "}";
-
- wifi_send_websocket(ws);
- }
-
- void wifi_send_websocket(String s) {
- socket.broadcastTXT(s);
- }
-
- #ifdef ARDUINO_ARCH_ESP32
-
- #include <rom/rtc.h>
-
- // https://github.com/espressif/arduino-esp32/blob/master/libraries/ESP32/examples/ResetReason/ResetReason.ino
- static const char *str_reset_reason(int reason) {
- switch (reason) {
- case 1:
- return "POWERON_RESET";
-
- case 3:
- return "SW_RESET";
-
- case 4:
- return "OWDT_RESET";
-
- case 5:
- return "DEEPSLEEP_RESET";
-
- case 6:
- return "SDIO_RESET";
-
- case 7:
- return "TG0WDT_SYS_RESET";
-
- case 8:
- return "TG1WDT_SYS_RESET";
-
- case 9:
- return "RTCWDT_SYS_RESET";
-
- case 10:
- return "INTRUSION_RESET";
-
- case 11:
- return "TGWDT_CPU_RESET";
-
- case 12:
- return "SW_CPU_RESET";
-
- case 13:
- return "RTCWDT_CPU_RESET";
-
- case 14:
- return "EXT_CPU_RESET";
-
- case 15:
- return "RTCWDT_BROWN_OUT_RESET";
-
- case 16:
- return "RTCWDT_RTC_RESET";
-
- default:
- return "NO_MEAN";
- }
- }
-
- #endif // ARDUINO_ARCH_ESP32
-
- void handleRoot() {
- String message = F("<!DOCTYPE html>\n");
- message += F("<html><head>\n");
- message += F("<meta charset='utf-8'/>\n");
- message += F("<meta name='viewport' content='width=device-width, initial-scale=1'/>\n");
- message += F("<title>Gieß-o-mat</title>\n");
-
- message += F("<style type='text/css'>\n");
- message += F(".head {\n");
- message += F("text-align: center;\n");
- message += F("}\n");
-
- message += F(".container {\n");
- message += F("display: flex;\n");
- message += F("max-width: 1200px;\n");
- message += F("margin: auto;\n");
- message += F("}\n");
-
- message += F(".ui {\n");
- message += F("width: max-content;\n");
- message += F("height: max-content;\n");
- message += F("margin-right: 1em;\n");
- message += F("padding: 0 1.0em;\n");
- message += F("border: 1px dashed black;\n");
- message += F("}\n");
-
- message += F(".io {\n");
- message += F("width: max-content;\n");
- message += F("height: max-content;\n");
- message += F("margin-right: 1em;\n");
- message += F("padding: 0.8em 1.0em;\n");
- message += F("border: 1px dashed black;\n");
- message += F("font-family: monospace;\n");
- message += F("}\n");
-
- message += F(".ioelem {\n");
- message += F("width: max-content;\n");
- message += F("border: 1px solid black;\n");
- message += F("border-radius: 50%;\n");
- message += F("padding: 1em;\n");
- message += F("margin: 0.5em;\n");
- message += F("}\n");
-
- message += F(".info {\n");
- message += F("width: max-content;\n");
- message += F("height: max-content;\n");
- message += F("padding: 0 1.0em;\n");
- message += F("border: 1px dashed black;\n");
- message += F("font-family: monospace;\n");
- message += F("}\n");
-
- message += F(".log {\n");
- message += F("max-height: 300px;\n");
- message += F("padding: 0 1.0em;\n");
- message += F("max-width: 1200px;\n");
- message += F("margin: auto;\n");
- message += F("margin-top: 1.5em;\n");
- message += F("border: 1px dashed black;\n");
- message += F("font-family: monospace;\n");
- message += F("overflow-y: scroll;\n");
- message += F("word-break: break-all;\n");
- message += F("}\n");
-
- message += F("#logbuf {\n");
- message += F("white-space: break-spaces;\n");
- message += F("}\n");
-
- message += F(".pad {\n");
- message += F("background: black;\n");
- message += F("border: 3px solid black;\n");
- message += F("border-radius: 20px;\n");
- message += F("width: max-content;\n");
- message += F("padding: 1.5em;\n");
- message += F("margin-left: auto;\n");
- message += F("margin-right: auto;\n");
- message += F("}\n");
-
- message += F(".pad input {\n");
- message += F("background: #fff0cf;\n");
- message += F("border-radius: 6px;\n");
- message += F("font-weight: bold;\n");
- message += F("font-family: monospace;\n");
- message += F("font-size: 1.2em;\n");
- message += F("padding: 0.5em 1em;\n");
- message += F("margin: 0.5em;\n");
- message += F("}\n");
-
- // https://codepen.io/hawkz/pres/RpPaGK
- message += F(".lcd {\n");
- //message += F("background: #9ea18c;\n");
- message += F("background: #9ed18c;\n");
- message += F("border: 5px solid black;\n");
- message += F("border-radius: 10px;\n");
- message += F("width: max-content;\n");
- message += F("padding: 0.65em 1em;\n");
- message += F("box-shadow: inset 0 0 5px 5px rgba(0,0,0,.1);\n");
- message += F("font-weight: bold;\n");
- message += F("font-family: monospace;\n");
- message += F("letter-spacing: 0.1em;\n");
- message += F("font-size: 1.2em;\n");
- message += F("line-height: 160%;\n");
- message += F("color: #21230e;\n");
- message += F("text-shadow: -1px 2px 1px rgba(0,0,0,.1);\n");
- message += F("margin-left: auto;\n");
- message += F("margin-right: auto;\n");
- message += F("}\n");
-
- message += F("#state {\n");
- message += F("text-align: center;\n");
- message += F("}\n");
-
- message += F("@media (prefers-color-scheme: dark) {\n");
- message += F("body {\n");
- message += F("background-color: black;\n");
- message += F("color: white;\n");
- message += F("}\n");
- message += F(".ui {\n");
- message += F("border: 1px dashed white;\n");
- message += F("}\n");
- message += F(".io {\n");
- message += F("border: 1px dashed white;\n");
- message += F("}\n");
- message += F(".ioelem {\n");
- message += F("border: 1px solid white;\n");
- message += F("}\n");
- message += F(".info {\n");
- message += F("border: 1px dashed white;\n");
- message += F("}\n");
- message += F(".log {\n");
- message += F("border: 1px dashed white;\n");
- message += F("}\n");
- message += F(".pad {\n");
- message += F("background: #222222;\n");
- message += F("border: 3px solid white;\n");
- message += F("}\n");
- message += F(".lcd {\n");
- message += F("border: 5px solid white;\n");
- message += F("}\n");
- message += F("}\n");
-
- message += F("</style>\n");
- message += F("</head><body>\n");
- message += F("<h1 class='head'>Gieß-o-mat</h1>\n");
-
- message += F("<div class='container'>\n");
- message += F("<div class='ui'>\n");
- message += F("<pre class='lcd'>\n");
- message += message_buffer_a + '\n';
- message += message_buffer_b + '\n';
- message += message_buffer_c + '\n';
- message += message_buffer_d + '\n';
- message += F("</pre>\n");
-
- message += F("<form class='pad'>\n");
- message += F("<input type='button' value='1'>");
- message += F("<input type='button' value='2'>");
- message += F("<input type='button' value='3'>");
- message += F("<br>\n");
- message += F("<input type='button' value='4'>");
- message += F("<input type='button' value='5'>");
- message += F("<input type='button' value='6'>");
- message += F("<br>\n");
- message += F("<input type='button' value='7'>");
- message += F("<input type='button' value='8'>");
- message += F("<input type='button' value='9'>");
- message += F("<br>\n");
- message += F("<input type='button' value='*'>");
- message += F("<input type='button' value='0'>");
- message += F("<input type='button' value='#'>");
- message += F("</form>\n");
-
- message += F("<p id='state'>\n");
- message += F("State: ");
- message += control_state_name();
- message += F("</p></div>\n");
-
- message += F("<div class='io'>\n");
- message += F("Switches: <span id='switchstate'>");
-
- Plants::Waterlevel wl = get_plants()->getWaterlevel();
- if (wl == Plants::empty) {
- message += F("tank empty");
- } else if (wl == Plants::inbetween) {
- message += F("tank half-filled");
- } else if (wl == Plants::full) {
- message += F("tank full");
- } else {
- message += F("invalid sensor state");
- }
- message += F("</span>");
-
- message += F("<div class='container'>\n");
- for (int i = 0; i < SWITCH_COUNT; i++) {
- message += F("<div class='ioelem switch' style='background-color: ");
- bool v = get_plants()->getSwitches()->getPin(i);
-
- #ifdef INVERT_SENSOR_BOTTOM
- if (i == 0) {
- v = !v;
- }
- #endif // INVERT_SENSOR_BOTTOM
-
- #ifdef INVERT_SENSOR_TOP
- if (i == 1) {
- v = !v;
- }
- #endif // INVERT_SENSOR_TOP
-
- if (v) {
- message += F("red");
- } else {
- message += F("green");
- }
- message += F(";'>S");
- message += String(i + 1);
- message += F("</div>");
- }
- message += F("</div><hr>\n");
-
- message += F("Kickstart:\n");
- message += F("<div class='container'>\n");
- for (int i = 0; i < VALVE_COUNT - 1; i++) {
- message += F("<div class='ioelem kickstart' style='background-color: ");
- if (get_plants()->getKickstart()->getPin(i)) {
- message += F("red");
- } else {
- message += F("green");
- }
- message += F(";'>A");
- message += String(i + 1);
- message += F("</div>");
- }
- message += F("</div><hr>\n");
-
- message += F("Valves:\n");
- message += F("<div class='container'>\n");
- for (int i = 0; i < VALVE_COUNT; i++) {
- message += F("<div class='ioelem valve' style='background-color: ");
- if (get_plants()->getValves()->getPin(i)) {
- message += F("red");
- } else {
- message += F("green");
- }
- message += F(";'>V");
- message += String(i + 1);
- message += F("</div>");
- }
- message += F("</div><hr>\n");
-
- message += F("Pumps:\n");
- message += F("<div class='container'>\n");
- for (int i = 0; i < PUMP_COUNT; i++) {
- message += F("<div class='ioelem pump' style='background-color: ");
- if (get_plants()->getPumps()->getPin(i)) {
- message += F("red");
- } else {
- message += F("green");
- }
- message += F(";'>P");
- message += String(i + 1);
- message += F("</div>");
- }
- message += F("</div><hr>\n");
-
- message += F("Aux:\n");
- message += F("<div class='container'>\n");
- for (int i = 0; i < AUX_COUNT; i++) {
- message += F("<div class='ioelem aux' style='background-color: ");
- if (get_plants()->getAux()->getPin(i)) {
- message += F("red");
- } else {
- message += F("green");
- }
- message += F(";'>A");
- message += String(i + 1);
- message += F("</div>");
- }
- message += F("</div><hr>\n");
-
- message += F("Green means valve is closed / pump is off / switch is not submersed.\n");
- message += F("<br>\n");
- message += F("Red means valve is open / pump is running / switch is submersed.</div>\n");
-
- message += F("<div class='info'><p>\n");
- message += F("Version: ");
- message += FIRMWARE_VERSION;
- message += F("\n<br>\n");
- message += F("Build Date: ");
- message += __DATE__;
- message += F("\n<br>\n");
- message += F("Build Time: ");
- message += __TIME__;
- message += F("\n</p><p>\n");
- message += F("MAC: ");
- message += WiFi.macAddress();
- message += F("\n<br>\n");
- message += F("IPv4: ");
- message += WiFi.localIP().toString();
- message += F("\n</p>\n");
-
- #if defined(ARDUINO_ARCH_ESP8266)
-
- message += F("\n<p>\n");
- message += F("Reset reason: ");
- message += ESP.getResetReason();
- message += F("\n<br>\n");
- message += F("Free heap: ");
- message += String(ESP.getFreeHeap());
- message += F(" (");
- message += String(ESP.getHeapFragmentation());
- message += F("% fragmentation)");
- message += F("\n<br>\n");
- message += F("Free sketch space: ");
- message += String(ESP.getFreeSketchSpace());
- message += F("\n<br>\n");
- message += F("Flash chip real size: ");
- message += String(ESP.getFlashChipRealSize());
-
- if (ESP.getFlashChipSize() != ESP.getFlashChipRealSize()) {
- message += F("\n<br>\n");
- message += F("WARNING: sdk chip size (");
- message += (ESP.getFlashChipSize());
- message += F(") does not match!");
- }
-
- message += F("\n</p>\n");
-
- #elif defined(ARDUINO_ARCH_ESP32)
-
- message += F("\n<p>\n");
- message += F("Reset reason: ");
- message += str_reset_reason(rtc_get_reset_reason(0));
- message += F(" ");
- message += str_reset_reason(rtc_get_reset_reason(1));
- message += F("\n<br>\n");
- message += F("Free heap: ");
- message += String(ESP.getFreeHeap() / 1024.0);
- message += F("k\n<br>\n");
- message += F("Free sketch space: ");
- message += String(ESP.getFreeSketchSpace() / 1024.0);
- message += F("k\n<br>\n");
- message += F("Flash chip size: ");
- message += String(ESP.getFlashChipSize() / 1024.0);
- message += F("k\n</p>\n");
-
- #endif
-
- message += F("<p>\n");
- #ifdef TELEGRAM_TOKEN
- message += F("Telegram: ");
- message += TELEGRAM_UPDATE_INTERVAL;
- message += F("ms\n");
- #else
- message += F("Telegram bot not enabled!\n");
- #endif
- message += F("</p>\n");
-
- message += F("<p>\n");
- #ifdef MQTT_HOST
- message += F("MQTT: ");
- message += MQTT_HOST;
- message += F(":");
- message += String(MQTT_PORT);
- message += F("\n");
- #else
- message += F("MQTT not enabled!\n");
- #endif
- message += F("</p>\n");
-
- message += F("<p>\n");
- #ifdef ENABLE_INFLUXDB_LOGGING
- message += F("InfluxDB: ");
- message += INFLUXDB_DATABASE;
- message += F(" @ ");
- message += INFLUXDB_HOST;
- message += F(":");
- message += String(INFLUXDB_PORT);
- message += F("\n");
- #else
- message += F("InfluxDB logging not enabled!\n");
- #endif
- message += F("</p>\n");
-
- message += F("<p>Try <a href='/update'>/update</a> for OTA firmware updates!</p>\n");
- message += F("<p>Made by <a href='https://xythobuz.de'>xythobuz</a></p>\n");
- message += F("<p><a href='https://git.xythobuz.de/thomas/giess-o-mat'>Project Repository</a></p>\n");
-
- #ifdef ENABLE_GPIO_TEST
- message += F("<p><a href='/gpiotest'>GPIO Test</a></p>\n");
- #endif // ENABLE_GPIO_TEST
-
- message += F("</div></div>\n");
-
- message += F("<div class='log'><pre id='logbuf'>\n");
- message += debug.getBuffer();
- message += F("</pre></div>\n");
- message += F("</body>\n");
-
- message += F("<script type='text/javascript'>\n");
- message += F("var socket = new WebSocket('ws://' + window.location.hostname + ':81');\n");
- message += F("socket.onmessage = function(e) {\n");
- message += F( "if (e.data.startsWith('log:')) {\n");
- message += F( "var log = document.getElementById('logbuf');\n");
- message += F( "var div = document.getElementsByClassName('log')[0];\n");
- message += F( "log.innerHTML += e.data.substring(4);\n");
- message += F( "if (log.innerHTML.length > (1024 * 1024)) {\n");
- message += F( "log.innerHTML = log.innerHTML.substring(1024 * 1024);\n");
- message += F( "}\n");
- message += F( "div.scrollTop = div.scrollHeight;\n");
- message += F( "return;\n");
- message += F( "}\n");
- message += F( "var msg = JSON.parse(e.data);\n");
- message += F( "var str = msg.a + '\\n' + msg.b + '\\n' + msg.c + '\\n' + msg.d;\n");
- //message += F( "console.log(str);\n");
- message += F( "var lcd = document.getElementsByClassName('lcd');\n");
- message += F( "lcd[0].innerHTML = str;\n");
- message += F( "var state = document.getElementById('state');\n");
- message += F( "state.innerHTML = \"State: \" + msg.state;\n");
-
- message += F( "for (let i = 0; i < ");
- message += String(VALVE_COUNT);
- message += F("; i++) {\n");
- message += F( "var valves = document.getElementsByClassName('valve');\n");
- message += F( "if (msg.valves[i] == '0') {\n");
- message += F( "valves[i].style = 'background-color: green;';\n");
- message += F( "} else {\n");
- message += F( "valves[i].style = 'background-color: red;';\n");
- message += F( "}\n");
- message += F( "}\n");
-
- message += F( "for (let i = 0; i < ");
- message += String(PUMP_COUNT);
- message += F("; i++) {\n");
- message += F( "var pumps = document.getElementsByClassName('pump');\n");
- message += F( "if (msg.pumps[i] == '0') {\n");
- message += F( "pumps[i].style = 'background-color: green;';\n");
- message += F( "} else {\n");
- message += F( "pumps[i].style = 'background-color: red;';\n");
- message += F( "}\n");
- message += F( "}\n");
-
- message += F( "for (let i = 0; i < ");
- message += String(SWITCH_COUNT);
- message += F("; i++) {\n");
- message += F( "var switches = document.getElementsByClassName('switch');\n");
- message += F( "if (msg.switches[i] == '0') {\n");
- message += F( "switches[i].style = 'background-color: green;';\n");
- message += F( "} else {\n");
- message += F( "switches[i].style = 'background-color: red;';\n");
- message += F( "}\n");
- message += F( "}\n");
-
- message += F( "for (let i = 0; i < ");
- message += String(AUX_COUNT);
- message += F("; i++) {\n");
- message += F( "var aux = document.getElementsByClassName('aux');\n");
- message += F( "if (msg.aux[i] == '0') {\n");
- message += F( "aux[i].style = 'background-color: green;';\n");
- message += F( "} else {\n");
- message += F( "aux[i].style = 'background-color: red;';\n");
- message += F( "}\n");
- message += F( "}\n");
-
- message += F( "for (let i = 0; i < ");
- message += String(VALVE_COUNT - 1);
- message += F("; i++) {\n");
- message += F( "var kickstart = document.getElementsByClassName('kickstart');\n");
- message += F( "if (msg.kickstart[i] == '0') {\n");
- message += F( "kickstart[i].style = 'background-color: green;';\n");
- message += F( "} else {\n");
- message += F( "kickstart[i].style = 'background-color: red;';\n");
- message += F( "}\n");
- message += F( "}\n");
-
- message += F( "var switchstate = document.getElementById('switchstate');\n");
- message += F( "switchstate.innerHTML = msg.switchstate;\n");
- message += F("};\n");
- message += F("var hist = document.getElementsByClassName('log')[0];\n");
- message += F("hist.scrollTop = hist.scrollHeight;\n");
-
- message += F("var buttons = document.getElementsByTagName('input');\n");
- message += F("for (let i = 0; i < buttons.length; i++) {\n");
- message += F( "buttons[i].addEventListener('click', updateButton);\n");
- message += F("}\n");
- message += F("function updateButton() {\n");
- message += F( "socket.send(this.value);\n");
- message += F("}\n");
- message += F("</script>\n");
- message += F("</html>\n");
-
- server.send(200, "text/html", message);
- }
-
- void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length) {
- if ((type != WStype_TEXT) || (length != 1)) {
- debug.println("Websocket: invalid type=" + String(type) + " len=" + String(length) /* + " data=" + String((char *)payload) */ );
- return;
- }
-
- char c = payload[0];
- if ((c >= '0') && (c <= '9')) {
- control_act_input(c - '0');
- } else if (c == '*') {
- control_act_input(-1);
- } else if (c == '#') {
- control_act_input(-2);
- }
- }
-
- void wifi_setup() {
- // Build hostname string
- String hostname = "giess-o-mat";
-
- int ws = 0, connect_attempts = 0;
-
- #if defined(ARDUINO_ARCH_ESP8266)
-
- // Connect to WiFi AP
- debug.println("WiFi: initializing");
- WiFi.hostname(hostname);
- WiFi.mode(WIFI_STA);
-
- debug.print("WiFi: connecting");
- WiFi.begin(WIFI_SSID, WIFI_PW);
-
- #ifdef TELEGRAM_TOKEN
- secured_client.setTrustAnchors(&cert);
- #endif // TELEGRAM_TOKEN
-
- while (((ws = WiFi.status()) != WL_CONNECTED) && (connect_attempts < MAX_WIFI_CONNECT_ATTEMPTS)) {
- connect_attempts++;
- debug.print(String(" ") + String(ws));
- delay(LED_CONNECT_BLINK_INTERVAL);
- digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
- }
- debug.println();
- debug.println(String("WiFi: status=") + String(WiFi.status()));
-
- #elif defined(ARDUINO_ARCH_ESP32)
-
- // Set hostname workaround
- debug.println("WiFi: set hostname");
- WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
- WiFi.setHostname(hostname.c_str());
-
- // Workaround for WiFi connecting only every 2nd reset
- // https://github.com/espressif/arduino-esp32/issues/2501#issuecomment-513602522
- debug.println("WiFi: connection work-around");
- WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
- if (info.disconnected.reason == 202) {
- esp_sleep_enable_timer_wakeup(10);
- esp_deep_sleep_start();
- delay(100);
- }
- }, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
-
- // Connect to WiFi AP
- debug.println("WiFi: SSID=" WIFI_SSID);
- debug.print("WiFi: connecting");
- WiFi.mode(WIFI_STA);
- WiFi.begin(WIFI_SSID, WIFI_PW);
-
- #ifdef TELEGRAM_TOKEN
- secured_client.setCACert(telegram_cert);
- #endif // TELEGRAM_TOKEN
-
- while (((ws = WiFi.status()) != WL_CONNECTED) && (connect_attempts < MAX_WIFI_CONNECT_ATTEMPTS)) {
- connect_attempts++;
- debug.print(String(" ") + String(ws));
- delay(LED_CONNECT_BLINK_INTERVAL);
- digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
- }
- debug.println();
- debug.println(String("WiFi: status=") + String(WiFi.status()));
-
- // Set hostname workaround
- debug.println("WiFi: set hostname work-around");
- WiFi.setHostname(hostname.c_str());
-
- #endif
-
- if ((connect_attempts >= MAX_WIFI_CONNECT_ATTEMPTS)
- || (WiFi.status() != WL_CONNECTED)) {
- debug.println("WiFi: init failed!");
- wifi_ok = 0;
- return;
- }
- wifi_ok = 1;
-
- debug.print("WiFi: got IPv4: ");
- debug.println(WiFi.localIP().toString());
-
- #ifdef ENABLE_INFLUXDB_LOGGING
- // Setup InfluxDB Client
- debug.println("WiFi: set InfluxDB database");
- influx.setDb(INFLUXDB_DATABASE);
- #endif // ENABLE_INFLUXDB_LOGGING
-
- // Setup HTTP Server
- debug.println("WiFi: initializing HTTP server");
- MDNS.begin(hostname.c_str());
- updater.setup(&server);
- server.on("/", handleRoot);
-
- #ifdef ENABLE_GPIO_TEST
- server.on("/gpiotest", handleGpioTest);
- #endif // ENABLE_GPIO_TEST
-
- #ifdef TELEGRAM_TOKEN
- debug.print("WiFi: getting NTP time");
- configTime(0, 0, "pool.ntp.org");
- time_t now = time(nullptr);
- while (now < 24 * 60 * 60) {
- debug.print(".");
- delay(100);
- now = time(nullptr);
- }
- debug.println(" done!");
- debug.println("WiFi: time is " + String(now));
-
- debug.print("WiFi: initializing Telegram... ");
- bot.setUpdateTime(TELEGRAM_UPDATE_INTERVAL);
- bot.setTelegramToken(TELEGRAM_TOKEN);
- bot.begin() ? debug.println("Ok") : debug.println("Error");
-
- bot.setMyCommands("auto", "Start automatic watering cycle");
- bot.setMyCommands("confirm", "Proceed with any menu inputs");
- bot.setMyCommands("none", "Proceed without menu input");
- bot.setMyCommands("abort", "Cancel any menu inputs");
-
- telegram_hello();
- #endif // TELEGRAM_TOKEN
-
- #ifdef MQTT_HOST
- debug.println("WiFi: initializing MQTT");
- mqtt.setServer(MQTT_HOST, MQTT_PORT);
- mqtt.setCallback(mqttCallback);
- #endif // MQTT_HOST
-
- server.begin();
- MDNS.addService("http", "tcp", 80);
- MDNS.addService("http", "tcp", 81);
-
- socket.begin();
- socket.onEvent(webSocketEvent);
-
- debug.println("WiFi: setup done");
- }
-
- void wifi_run() {
- if (!wifi_ok) {
- // nothing to handle
- return;
- }
-
- if ((millis() - last_server_handle_time) >= SERVER_HANDLE_INTERVAL) {
- last_server_handle_time = millis();
- server.handleClient();
- socket.loop();
-
- #ifdef ARDUINO_ARCH_ESP8266
- MDNS.update();
- #endif // ARDUINO_ARCH_ESP8266
-
- #ifdef TELEGRAM_TOKEN
- telegram_poll();
- #endif // TELEGRAM_TOKEN
- }
-
- if ((millis() - last_websocket_update_time) >= WEBSOCKET_UPDATE_INTERVAL) {
- last_websocket_update_time = millis();
- wifi_send_status_broadcast();
- }
-
- #ifdef ENABLE_GPIO_TEST
- if (runningGpioTest && ((millis() - lastGpioTime) >= GPIO_TEST_INTERVAL)) {
- gpioTestState = !gpioTestState;
- runGpioTest(gpioTestState);
- }
- #endif // ENABLE_GPIO_TEST
-
- #ifdef MQTT_HOST
- if (!mqtt.connected() && ((millis() - last_mqtt_reconnect_time) >= MQTT_RECONNECT_INTERVAL)) {
- last_mqtt_reconnect_time = millis();
- mqttReconnect();
- }
-
- mqtt.loop();
- #endif // MQTT_HOST
- }
-
- #endif // PLATFORM_ESP
|