diff --git a/ampel-firmware.h b/ampel-firmware.h index f542c74aae87fc567fdc0299b481f6d43976fe72..9fc089a6fd44fea1a4c2203b9bf664f0c513e7b0 100644 --- a/ampel-firmware.h +++ b/ampel-firmware.h @@ -12,18 +12,22 @@ #ifdef AMPEL_CSV # include "csv_writer.h" #endif -#ifdef AMPEL_MQTT -# include "mqtt.h" + +#ifdef AMPEL_WIFI +# include "wifi_util.h" +# ifdef AMPEL_MQTT +# include "mqtt.h" +# endif +# ifdef AMPEL_HTTP +# include "web_server.h" +# endif #endif + #ifdef AMPEL_LORAWAN # include "lorawan.h" #endif -#ifdef AMPEL_HTTP -# include "web_server.h" -#endif #include "util.h" -#include "wifi_util.h" #include "co2_sensor.h" #include "led_effects.h" diff --git a/ampel-firmware.ino b/ampel-firmware.ino index 791443558a84877bc387c93cd6193714960359f8..0e550a3df89eff9867c1799fbac1b8663b48f53b 100644 --- a/ampel-firmware.ino +++ b/ampel-firmware.ino @@ -75,6 +75,7 @@ void setup() { Serial.print(F("Board : ")); Serial.println(BOARD); +#ifdef AMPEL_WIFI // Try to connect to Wi-Fi WiFiConnect(SENSOR_ID); @@ -82,9 +83,9 @@ void setup() { Serial.println(WiFi.status()); if (WiFi.status() == WL_CONNECTED) { -#ifdef AMPEL_HTTP +# ifdef AMPEL_HTTP web_server::initialize(); -#endif +# endif ntp::initialize(); @@ -95,10 +96,11 @@ void setup() { Serial.println(F("Error setting up MDNS responder!")); } -#ifdef AMPEL_MQTT +# ifdef AMPEL_MQTT mqtt::initialize("CO2sensors/" + SENSOR_ID); -#endif +# endif } +#endif #ifdef AMPEL_CSV csv_writer::initialize(); @@ -137,7 +139,7 @@ void loop() { csv_writer::logIfTimeHasCome(sensor::timestamp, sensor::co2, sensor::temperature, sensor::humidity); #endif -#ifdef AMPEL_MQTT +#if defined(AMPEL_WIFI) && defined(AMPEL_MQTT) mqtt::publishIfTimeHasCome(sensor::timestamp, sensor::co2, sensor::temperature, sensor::humidity); #endif @@ -179,18 +181,20 @@ void checkFlashButton() { } void keepServicesAlive() { +#ifdef AMPEL_WIFI if (WiFi.status() == WL_CONNECTED) { -#if defined(ESP8266) +# if defined(ESP8266) //NOTE: Sadly, there seems to be a bug in the current MDNS implementation. // It stops working after 2 minutes. And forcing a restart leads to a memory leak. MDNS.update(); -#endif +# endif ntp::update(); // NTP client has its own timer. It will connect to NTP server every 60s. -#ifdef AMPEL_HTTP +# ifdef AMPEL_HTTP web_server::update(); -#endif -#ifdef AMPEL_MQTT +# endif +# ifdef AMPEL_MQTT mqtt::keepConnection(); // MQTT client has its own timer. It will keep alive every 15s. -#endif +# endif } +#endif } diff --git a/wifi_util.cpp b/wifi_util.cpp index f8c0230c0ffefeb06ee5ff3bd31a0adfa7a99898..2da3d1884f19e9c9a0cdaeed34439d244ab99e06 100644 --- a/wifi_util.cpp +++ b/wifi_util.cpp @@ -2,16 +2,11 @@ namespace config { // WiFi config. See 'config.h' if you want to modify those values. -#ifdef WIFI_SSID const char *wifi_ssid = WIFI_SSID; const char *wifi_password = WIFI_PASSWORD; -#else - const char *wifi_ssid = "NO_WIFI"; - const char *wifi_password = ""; -#endif #ifdef WIFI_TIMEOUT - const uint8_t wifi_timeout = WIFI_TIMEOUT; // [s] Will try to connect during wifi_timeout seconds before failing. + const uint8_t wifi_timeout = WIFI_TIMEOUT; // [s] Will try to connect during wifi_timeout seconds before failing. #else const uint8_t wifi_timeout = 60; // [s] Will try to connect during wifi_timeout seconds before failing. #endif @@ -20,14 +15,8 @@ namespace config { // Initialize Wi-Fi void WiFiConnect(const String &hostname) { //NOTE: WiFi Multi could allow multiple SSID and passwords. - if (strcmp(config::wifi_ssid, "NO_WIFI") == 0) { - Serial.println("Please change WIFI_SSID in config.h if you want to connect."); - WiFi.disconnect(true); - WiFi.mode(WIFI_OFF); - return; - } WiFi.persistent(false); // Don't write user & password to Flash. - WiFi.mode(WIFI_STA); // Set ESP8266 to be a WiFi-client only + WiFi.mode(WIFI_STA); // Set ESP8266 to be a WiFi-client only #if defined(ESP8266) WiFi.hostname(hostname); #elif defined(ESP32)