#include "util.h" namespace config { const char *ntp_server = NTP_SERVER; const long utc_offset_in_seconds = UTC_OFFSET_IN_SECONDS; // UTC+1 } // Get last 3 bytes of ESP MAC (worldwide unique) String macToID() { uint8_t mac[6]; WiFi.macAddress(mac); String result; for (int i = 3; i < 6; i++) { if (mac[i] < 16) result += '0'; result += String(mac[i], HEX); } result.toLowerCase(); return result; } //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); void initialize() { timeClient.begin(); } void update() { timeClient.update(); } void getLocalTime(char *timestamp) { timeClient.getFormattedDate(timestamp); } void setLocalTime(int32_t unix_seconds) { char time[23]; timeClient.getFormattedDate(time); Serial.print("Current time : "); Serial.println(time); Serial.print("Setting UNIX time to : "); Serial.println(unix_seconds); timeClient.setEpochTime(unix_seconds - seconds()); timeClient.getFormattedDate(time); Serial.print("Current time : "); Serial.println(time); } } void Ampel::showFreeSpace() { Serial.print(F("Free heap space : ")); Serial.print(get_free_heap_size()); Serial.println(F(" bytes.")); } Ampel::Ampel() : sensorId("ESP" + macToID()), max_loop_duration(0) { sensor_console::defineIntCommand("set_time", ntp::setLocalTime, " 1618829570 (Sets time to the given UNIX time)"); sensor_console::defineCommand("free", Ampel::showFreeSpace, " (Displays available heap space)"); sensor_console::defineCommand("reset", []() { ESP.restart(); }, " (Restarts the sensor)"); } Ampel ampel;