diff --git a/ampel-firmware/ampel-firmware.h b/ampel-firmware/ampel-firmware.h index 5f50b5138af1e0495e971db2141f8ce034d6289b..340c1adbed373d3479bc4ed705ea43e82380a600 100644 --- a/ampel-firmware/ampel-firmware.h +++ b/ampel-firmware/ampel-firmware.h @@ -11,12 +11,10 @@ #include "csv_writer.h" -#ifdef AMPEL_WIFI -# include "wifi_util.h" -# include "mqtt.h" -# ifdef AMPEL_HTTP -# include "web_server.h" -# endif +#include "wifi_util.h" +#include "mqtt.h" +#ifdef AMPEL_HTTP +# include "web_server.h" #endif #include "lorawan.h" diff --git a/ampel-firmware/ampel-firmware.ino b/ampel-firmware/ampel-firmware.ino index 16653ac80e932cabe50fc52244ca2b9667d604bc..d45326baed5b769cba0bbdd5d820447d349bb192 100644 --- a/ampel-firmware/ampel-firmware.ino +++ b/ampel-firmware/ampel-firmware.ino @@ -72,35 +72,35 @@ void setup() { web_config::initialize(); -#ifdef AMPEL_WIFI // TODO: What to do with those? Replace with webconf::wifi_active? - web_config::setWifiConnectionCallback([]() { - //TODO: Move somewhere else - led_effects::showKITTWheel(color::green); - Serial.println(); - Serial.print(F("WiFi - Connected! IP address: ")); - IPAddress address = WiFi.localIP(); - snprintf(wifi::local_ip, sizeof(wifi::local_ip), "%d.%d.%d.%d", address[0], address[1], address[2], address[3]); - Serial.println(wifi::local_ip); - - ntp::initialize(); - - if (config::mqtt_active()) { - mqtt::initialize(ampel.sensorId); - } + if (config::wifi_active) { + web_config::setWifiConnectionCallback([]() { + //TODO: Move somewhere else + led_effects::showKITTWheel(color::green); + Serial.println(); + Serial.print(F("WiFi - Connected! IP address: ")); + IPAddress address = WiFi.localIP(); + snprintf(wifi::local_ip, sizeof(wifi::local_ip), "%d.%d.%d.%d", address[0], address[1], address[2], address[3]); + Serial.println(wifi::local_ip); + + ntp::initialize(); + + if (config::mqtt_active()) { + mqtt::initialize(ampel.sensorId); + } - Serial.print(F("You can access this sensor via http://")); - Serial.print(ampel.sensorId); - Serial.print(F(".local (might be unstable) or http://")); - Serial.println(WiFi.localIP()); - }); + Serial.print(F("You can access this sensor via http://")); + Serial.print(ampel.sensorId); + Serial.print(F(".local (might be unstable) or http://")); + Serial.println(WiFi.localIP()); + }); - web_config::setWifiFailCallback(failedConnection); + web_config::setWifiFailCallback(failedConnection); // web_config::setWifiConnectionFailedCallback([]() { // led_effects::showKITTWheel(color::red); // Serial.println(F("Connection to WiFi failed")); // }); -#endif + } pinMode(0, INPUT); // Flash button (used for forced calibration) @@ -120,14 +120,14 @@ void setup() { csv_writer::initialize(ampel.sensorId); -#ifdef AMPEL_WIFI - wifi::defineCommands(); + if (config::wifi_active) { + wifi::defineCommands(); # ifdef AMPEL_HTTP - web_server::definePages(); - //TODO: Rename. Not just web_server - // web_server::initialize(); + web_server::definePages(); + //TODO: Rename. Not just web_server + // web_server::initialize(); # endif -#endif + } #if defined(ESP32) if (config::lorawan_active()) { @@ -175,11 +175,9 @@ void loop() { csv_writer::logIfTimeHasCome(sensor::timestamp, sensor::co2, sensor::temperature, sensor::humidity); } -#if defined(AMPEL_WIFI) - if (config::mqtt_active()) { + if (config::wifi_active && config::mqtt_active()) { mqtt::publishIfTimeHasCome(sensor::timestamp, sensor::co2, sensor::temperature, sensor::humidity); } -#endif #if defined(ESP32) if (config::lorawan_active()) { @@ -229,13 +227,13 @@ void checkFlashButton() { } void keepServicesAlive() { -#ifdef AMPEL_WIFI - web_config::update(); - if (wifi::connected()) { - ntp::update(); // NTP client has its own timer. It will connect to NTP server every 60s. - if (config::mqtt_active()) { - mqtt::keepConnection(); // MQTT client has its own timer. It will keep alive every 15s. + if (config::wifi_active) { + web_config::update(); + if (wifi::connected()) { + ntp::update(); // NTP client has its own timer. It will connect to NTP server every 60s. + if (config::mqtt_active()) { + mqtt::keepConnection(); // MQTT client has its own timer. It will keep alive every 15s. + } } } -#endif } diff --git a/ampel-firmware/web_config.cpp b/ampel-firmware/web_config.cpp index 26be17a5da0730e5e446e791f7ba90508bce2b14..3ea0b65595083ccaa63ce2294844ebf692ded6cb 100644 --- a/ampel-firmware/web_config.cpp +++ b/ampel-firmware/web_config.cpp @@ -228,10 +228,19 @@ namespace web_config { Serial.println(F("Done!")); }, F("(resets the complete IotWeb config)")); -#if !defined(AMPEL_WIFI) + sensor_console::defineIntCommand("wifi", [](int32_t onOff) { + config::wifi_active = onOff; + iotWebConf->saveConfig(); + Serial.print(F("WiFi - ")); + Serial.println(onOff ? F("On!") : F("Off!")); + }, F("0/1 (Wifi On?)")); + iotWebConf->loadConfig(); - return; -#endif + + if (!config::wifi_active) { + Serial.println(F("Wifi is off : no access point or connection. Use 'wifi 1' to turn on again!")); + return; + } const int ONBOARD_LED_PIN = 2; # ifdef ESP8266 @@ -251,12 +260,6 @@ namespace web_config { //TODO: Remove AP Password config? //TODO: Save LoRaWAN key if possible? - Serial.print("WIFI ? : "); - Serial.println(ampelWifiParam.value()); - iotWebConf->loadConfig(); - Serial.print("WIFI ? : "); - Serial.println(ampelWifiParam.value()); - iotWebConf->init(); //TODO: Authenticate only if required? @@ -288,6 +291,8 @@ namespace config { bool &auto_calibrate_sensor = web_config::autoCalibrateParam.value(); // [true / false] float &temperature_offset = web_config::temperatureOffsetParam.value(); // [K] Sign isn't relevant. + bool &wifi_active = web_config::ampelWifiParam.value(); + // LEDs uint8_t &max_brightness = web_config::maxBrightnessParam.value(); uint8_t &min_brightness = web_config::minBrightnessParam.value(); diff --git a/ampel-firmware/web_config.h b/ampel-firmware/web_config.h index 3af2cbaf3b7462ac3edd2ac66618a04cb8ca1b93..29cbd920c2569786bcb2c48e82c9d8f51be5bf2a 100644 --- a/ampel-firmware/web_config.h +++ b/ampel-firmware/web_config.h @@ -18,6 +18,8 @@ namespace config { extern bool &auto_calibrate_sensor; // [true / false] extern float &temperature_offset; // [K] Sign isn't relevant. + extern bool &wifi_active; // [true / false] + // LED extern uint8_t &max_brightness; extern uint8_t &min_brightness;