util.cpp 1.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#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();
  }

  String getLocalTime() {
    return timeClient.getFormattedDate();
  }
}

Eric Duminil's avatar
Eric Duminil committed
41
42
43
44
45
46
47
48
void resetAmpel() {
  Serial.print("Resetting");
  FS_LIB.end();
  LedEffects::LEDsOff();
  delay(1000);
  ESP.restart();
}

49
50
51
52
53
uint32_t max_loop_duration = 0;

//FIXME: Remove every instance of Strings, to avoid heap fragmentation problems. (Start:  "Free heap space : 17104 bytes")
// See more https://cpp4arduino.com/2020/02/07/how-to-format-strings-without-the-string-class.html
const String SENSOR_ID = "ESP" + macToID();