diff --git a/ampel-firmware/mqtt.cpp b/ampel-firmware/mqtt.cpp index 57b5cd026f57586339e3d90eeb91aa204c242300..cf55823621605941285b265213c308945c20a3ce 100644 --- a/ampel-firmware/mqtt.cpp +++ b/ampel-firmware/mqtt.cpp @@ -1,5 +1,6 @@ #include "mqtt.h" +#include "web_config.h" #include "config.h" #include "led_effects.h" #include "sensor_console.h" @@ -18,10 +19,6 @@ namespace config { uint16_t mqtt_sending_interval = MQTT_SENDING_INTERVAL; // [s] //INFO: Listen to every CO2 sensor which is connected to the server: // mosquitto_sub -h MQTT_SERVER -t 'CO2sensors/#' -p 443 --capath /etc/ssl/certs/ -u "MQTT_USER" -P "MQTT_PASSWORD" -v - const char *mqtt_server = MQTT_SERVER; - const uint16_t mqtt_port = MQTT_PORT; - const char *mqtt_user = MQTT_USER; - const char *mqtt_password = MQTT_PASSWORD; const bool allow_mqtt_commands = ALLOW_MQTT_COMMANDS; const unsigned long wait_after_fail = 900; // [s] Wait 15 minutes after an MQTT connection fail, before trying again. } @@ -114,11 +111,18 @@ namespace mqtt { } Serial.print(F("MQTT - Attempting connection to ")); - Serial.print(MQTT_SERVER); + Serial.print(config::mqtt_server); Serial.print(MQTT_ENCRYPTED ? F(" (Encrypted") : F(" (Unencrypted")); Serial.print(F(", port ")); - Serial.print(MQTT_PORT); - Serial.print(F(") ...")); + Serial.print(config::mqtt_port); + Serial.print(F(") ")); + Serial.print(F("User:'")); + Serial.print(config::mqtt_user); + Serial.print(F("' Password:'")); + for (int i = 0; i < strlen(config::mqtt_password); ++i) { + Serial.print("*"); + } + Serial.print(F("' ...")); led_effects::onBoardLEDOn(); // Wait for connection, at most 15s (default) diff --git a/ampel-firmware/ntp.cpp b/ampel-firmware/ntp.cpp index 2cdb4e149f85a7e71e25f7ab3c8249defa4519e2..9bd4c89ce0cdae8cf67653f45b8675d1f308d171 100644 --- a/ampel-firmware/ntp.cpp +++ b/ampel-firmware/ntp.cpp @@ -1,23 +1,21 @@ #include "ntp.h" #include "sensor_console.h" -#include "config.h" #include "web_config.h" #include <WiFiUdp.h> // required for NTP #include "src/lib/NTPClient/NTPClient.h" // NTP -namespace config { - const long utc_offset_in_seconds = UTC_OFFSET_IN_SECONDS; // UTC+1 -} - //NOTE: ESP32 sometimes couldn't access the NTP server, and every loop would take +1000ms // ifdefs could be used to define functions specific to ESP32, e.g. with configTime namespace ntp { WiFiUDP ntpUDP; - NTPClient timeClient(ntpUDP, config::ntp_server, config::utc_offset_in_seconds, 60000UL); + NTPClient timeClient(ntpUDP); bool connected_at_least_once = false; void setLocalTime(int32_t unix_seconds); void initialize() { + timeClient.setPoolServerName(config::ntp_server); + timeClient.setTimeOffset((config::time_zone + config::daylight_saving_time) * 3600); + timeClient.setUpdateInterval(60000UL); Serial.print("NTP - Trying to connect to : "); Serial.println(config::ntp_server); timeClient.begin(); diff --git a/ampel-firmware/web_config.cpp b/ampel-firmware/web_config.cpp index 95dbf69c723acb83309f654ac66832de381b9b34..74cc8d181586be7af11601e5cf5067b7964c3cb5 100644 --- a/ampel-firmware/web_config.cpp +++ b/ampel-firmware/web_config.cpp @@ -126,7 +126,7 @@ namespace web_config { */ ParameterGroup timeParams = ParameterGroup("NTP", "Time server"); - TextTParameter<STRING_LEN> ntpParam = + TextTParameter<STRING_LEN> ntpServerParam = Builder<TextTParameter<STRING_LEN>>("ntp_server").label("NTP Server").defaultValue(NTP_SERVER).build(); IntTParameter<int16_t> timeOffsetParam = Builder<IntTParameter<int16_t>>("timezone").label("Timezone").defaultValue( (UTC_OFFSET_IN_SECONDS) / 3600).min(-23).max(23).step(1).placeholder("[h]").build(); @@ -187,7 +187,7 @@ namespace web_config { ledParams.addItem(&minBrightnessParam); ledParams.addItem(&ledCountParam); - timeParams.addItem(&ntpParam); + timeParams.addItem(&ntpServerParam); timeParams.addItem(&timeOffsetParam); timeParams.addItem(&dstParam); @@ -285,6 +285,15 @@ namespace config { uint8_t &min_brightness = web_config::minBrightnessParam.value(); uint16_t &led_count = web_config::ledCountParam.value(); - char *ntp_server = web_config::ntpParam.value(); + // Time server + char *ntp_server = web_config::ntpServerParam.value(); int16_t &time_zone = web_config::timeOffsetParam.value(); + bool &daylight_saving_time = web_config::dstParam.value(); + +// MQTT + char *mqtt_server = web_config::mqttServerParam.value(); + char *mqtt_user = web_config::mqttUserParam.value(); + char *mqtt_password = web_config::mqttPasswordParam.value(); + uint16_t &mqtt_port = web_config::mqttPortParam.value(); + } diff --git a/ampel-firmware/web_config.h b/ampel-firmware/web_config.h index 376e99de8404a75ec3938936d34f1a03e2fbaaba..e35bf80af350f1257f17e1949e7b4754112f36bb 100644 --- a/ampel-firmware/web_config.h +++ b/ampel-firmware/web_config.h @@ -24,8 +24,16 @@ namespace config { extern uint8_t &min_brightness; extern uint16_t &led_count; + // Time server extern char *ntp_server; - extern int16_t &time_zone; + extern int16_t &time_zone; // [h] + extern bool &daylight_saving_time; // [true / false] + + // MQTT + extern char *mqtt_server; + extern char *mqtt_user; + extern char *mqtt_password; + extern uint16_t &mqtt_port; } namespace web_config {