diff --git a/ampel-firmware/ampel-firmware.h b/ampel-firmware/ampel-firmware.h index 41fe46bd593aa82a1a3c133e08f1fabe88e7b490..b6a3d013b85a84851122c493d8d8172a93985d0c 100644 --- a/ampel-firmware/ampel-firmware.h +++ b/ampel-firmware/ampel-firmware.h @@ -33,6 +33,7 @@ #endif #include "util.h" +#include "time_util.h" #include "sensor_console.h" #include "co2_sensor.h" #include "led_effects.h" diff --git a/ampel-firmware/co2_sensor.cpp b/ampel-firmware/co2_sensor.cpp index 62182b19cfd905323dc81c9a5b0b85d0046ac184..fe02da678d0e77b83aa7e6f4cfce3a56dbad8c04 100644 --- a/ampel-firmware/co2_sensor.cpp +++ b/ampel-firmware/co2_sensor.cpp @@ -1,8 +1,8 @@ #include "co2_sensor.h" #include "config.h" +#include "time_util.h" #include "led_effects.h" -#include "util.h" // Just time.h #include "sensor_console.h" #include <Wire.h> diff --git a/ampel-firmware/csv_writer.cpp b/ampel-firmware/csv_writer.cpp index 6492d3baf14b2f921c48fffe96b7e2baef27c1db..ec59f3cf8cea29a59faba803c3dd1c35dc20bade 100644 --- a/ampel-firmware/csv_writer.cpp +++ b/ampel-firmware/csv_writer.cpp @@ -1,7 +1,7 @@ #include "csv_writer.h" #include "config.h" -#include "util.h" // -> time.h +#include "time_util.h" #include "led_effects.h" #include "sensor_console.h" diff --git a/ampel-firmware/lorawan.cpp b/ampel-firmware/lorawan.cpp index 63a1239d5f4fcb623acb4e0aec2e3d7e107380d7..6f849d47b7dcf3129c8ce9a0d137ca9cdc9b88b5 100644 --- a/ampel-firmware/lorawan.cpp +++ b/ampel-firmware/lorawan.cpp @@ -5,6 +5,7 @@ #include "led_effects.h" #include "sensor_console.h" #include "util.h" +#include "time_util.h" /*** Define region and transceiver type, and ignore lmic_project_config.h from lmic library ***/ // Those values are probably okay if you're in Europe. diff --git a/ampel-firmware/mqtt.cpp b/ampel-firmware/mqtt.cpp index 0a09c875e41c3d70fb9387db44046534a446e5bb..d393e710b5484fc6baed93e5a34bd295dab3968a 100644 --- a/ampel-firmware/mqtt.cpp +++ b/ampel-firmware/mqtt.cpp @@ -1,10 +1,10 @@ #include "mqtt.h" #include "config.h" -#include "util.h" // -> time.h #include "led_effects.h" #include "sensor_console.h" #include "wifi_util.h" +#include "time_util.h" #include "src/lib/PubSubClient/src/PubSubClient.h" #if defined(ESP8266) diff --git a/ampel-firmware/mqtt.h b/ampel-firmware/mqtt.h index 3ff9b1679b4f1ac9bd7e3c8dc9d7fc38eeb25d22..0e39b09f7c88300b93de0b98741a63ffb664a36c 100644 --- a/ampel-firmware/mqtt.h +++ b/ampel-firmware/mqtt.h @@ -3,7 +3,7 @@ #include <stdint.h> // For uint32_t & uint16_t -#include <config.h> +#include "config.h" #if !defined(MQTT_ENCRYPTED) # define MQTT_ENCRYPTED true // Old config files might not define it, and encryption was on by default. #endif diff --git a/ampel-firmware/time_util.cpp b/ampel-firmware/time_util.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b37fcaf0a70f7cd2aac17e10287267c5db2a78d7 --- /dev/null +++ b/ampel-firmware/time_util.cpp @@ -0,0 +1,49 @@ +#include "time_util.h" +#include "sensor_console.h" +#include "config.h" +#include <WiFiUdp.h> // required for NTP +#include "src/lib/NTPClient-master/NTPClient.h" // NTP + +namespace config { + const char *ntp_server = NTP_SERVER; + 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); + bool connected_at_least_once = false; + void setLocalTime(int32_t unix_seconds); + + void initialize() { + timeClient.begin(); + sensor_console::defineIntCommand("set_time", ntp::setLocalTime, F("1618829570 (Sets time to the given UNIX time)")); + } + + void update() { + connected_at_least_once |= timeClient.update(); + } + + void getLocalTime(char *timestamp) { + timeClient.getFormattedDate(timestamp); + } + + void setLocalTime(int32_t unix_seconds) { + char time[23]; + timeClient.getFormattedDate(time); + Serial.print(F("Current time : ")); + Serial.println(time); + if (connected_at_least_once) { + Serial.println(F("NTP update already happened. Not changing anything.")); + return; + } + Serial.print(F("Setting UNIX time to : ")); + Serial.println(unix_seconds); + timeClient.setEpochTime(unix_seconds - seconds()); + timeClient.getFormattedDate(time); + Serial.print(F("Current time : ")); + Serial.println(time); + } +} diff --git a/ampel-firmware/time_util.h b/ampel-firmware/time_util.h new file mode 100644 index 0000000000000000000000000000000000000000..fe1647c7da1f4ca302c6dba3c6bba33e5595d873 --- /dev/null +++ b/ampel-firmware/time_util.h @@ -0,0 +1,13 @@ +#ifndef AMPEL_TIME_H_INCLUDED +#define AMPEL_TIME_H_INCLUDED + +namespace ntp { + void initialize(); + void update(); + void getLocalTime(char *timestamp); +} + +//NOTE: Only use seconds() for duration comparison, not timestamps comparison. Otherwise, problems happen when millis roll over. +#define seconds() (millis() / 1000UL) + +#endif diff --git a/ampel-firmware/util.cpp b/ampel-firmware/util.cpp index 0a13836c828f62829b99abae58d63a832034837b..0edf43ceeaba1fac0c640359b53d6a41100cea00 100644 --- a/ampel-firmware/util.cpp +++ b/ampel-firmware/util.cpp @@ -1,14 +1,5 @@ #include "util.h" #include "sensor_console.h" -#include "config.h" -#include "sensor_console.h" -#include <WiFiUdp.h> // required for NTP -#include "src/lib/NTPClient-master/NTPClient.h" // NTP - -namespace config { - const char *ntp_server = NTP_SERVER; - const long utc_offset_in_seconds = UTC_OFFSET_IN_SECONDS; // UTC+1 -} #if defined(ESP8266) # include <ESP8266WiFi.h> // required to get MAC address @@ -27,43 +18,6 @@ const char *current_board = "ESP32"; const char *current_board = "UNKNOWN"; #endif -//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); - bool connected_at_least_once = false; - - void initialize() { - timeClient.begin(); - } - - void update() { - connected_at_least_once |= timeClient.update(); - } - - void getLocalTime(char *timestamp) { - timeClient.getFormattedDate(timestamp); - } - - void setLocalTime(int32_t unix_seconds) { - char time[23]; - timeClient.getFormattedDate(time); - Serial.print(F("Current time : ")); - Serial.println(time); - if (connected_at_least_once) { - Serial.println(F("NTP update already happened. Not changing anything.")); - return; - } - Serial.print(F("Setting UNIX time to : ")); - Serial.println(unix_seconds); - timeClient.setEpochTime(unix_seconds - seconds()); - timeClient.getFormattedDate(time); - Serial.print(F("Current time : ")); - Serial.println(time); - } -} - void Ampel::showFreeSpace() { Serial.print(F("Free heap space : ")); Serial.print(ESP.getFreeHeap()); @@ -97,7 +51,6 @@ char* getSensorId() { Ampel::Ampel() : board(current_board), sensorId(getSensorId()), macAddress(getMacString()), max_loop_duration(0) { - sensor_console::defineIntCommand("set_time", ntp::setLocalTime, F("1618829570 (Sets time to the given UNIX time)")); sensor_console::defineCommand("free", Ampel::showFreeSpace, F("(Displays available heap space)")); sensor_console::defineCommand("reset", []() { ESP.restart(); diff --git a/ampel-firmware/util.h b/ampel-firmware/util.h index 78d456c2e3a8dfb2ad0f129fb0c5ccd9bdbfabb2..495f7eaa0b149658631ec38b1cd5dab318e00af8 100644 --- a/ampel-firmware/util.h +++ b/ampel-firmware/util.h @@ -11,12 +11,6 @@ # define esp_get_heap_fragmentation() "?" // apparently not available for ESP32 #endif -namespace ntp { - void initialize(); - void update(); - void getLocalTime(char *timestamp); -} - namespace util { template<typename Tpa, typename Tpb> inline auto min(const Tpa &a, const Tpb &b) -> decltype(a < b ? a : b) { @@ -42,7 +36,4 @@ public: extern Ampel ampel; -//NOTE: Only use seconds() for duration comparison, not timestamps comparison. Otherwise, problems happen when millis roll over. -#define seconds() (millis() / 1000UL) - #endif diff --git a/ampel-firmware/web_server.cpp b/ampel-firmware/web_server.cpp index b0d520d3a7cf2387f5274da14a5f21b41a08d4af..86ebb19c07aca75d24f28012a6b471b79ce85734 100644 --- a/ampel-firmware/web_server.cpp +++ b/ampel-firmware/web_server.cpp @@ -8,6 +8,7 @@ #include "config.h" #include "util.h" +#include "time_util.h" #include "wifi_util.h" #include "co2_sensor.h" #include "sensor_console.h" diff --git a/ampel-firmware/wifi_util.cpp b/ampel-firmware/wifi_util.cpp index 0ef6df9a68e9966a983bcd208a678c4746e4be0b..7200206b2c2e530f24652112c7aec8bd5161ffc9 100644 --- a/ampel-firmware/wifi_util.cpp +++ b/ampel-firmware/wifi_util.cpp @@ -1,7 +1,8 @@ #include "wifi_util.h" #include "config.h" -#include "util.h" // -> time.h +#include "util.h" +#include "time_util.h" #include "led_effects.h" #include "sensor_console.h"