util.cpp 2.44 KB
Newer Older
1
2
3
4
5
6
7
#include "util.h"

namespace config {
  const char *ntp_server = NTP_SERVER;
  const long utc_offset_in_seconds = UTC_OFFSET_IN_SECONDS; // UTC+1
}

Eric Duminil's avatar
Eric Duminil committed
8
9
10
11
12
13
14
15
#if defined(ESP8266)
const char *current_board = "ESP8266";
#elif defined(ESP32)
const char *current_board = "ESP32";
#else
const char *current_board = "UNKNOWN";
#endif

16
17
18
19
20
//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);
21
  bool connected_at_least_once = false;
22
23
24
25
26
27

  void initialize() {
    timeClient.begin();
  }

  void update() {
28
    connected_at_least_once |= timeClient.update();
29
30
  }

31
32
  void getLocalTime(char *timestamp) {
    timeClient.getFormattedDate(timestamp);
33
  }
34
35
36
37

  void setLocalTime(int32_t unix_seconds) {
    char time[23];
    timeClient.getFormattedDate(time);
Eric Duminil's avatar
Eric Duminil committed
38
    Serial.print(F("Current time : "));
39
    Serial.println(time);
40
41
    if (connected_at_least_once) {
      Serial.println(F("NTP update already happened. Not changing anything."));
Eric Duminil's avatar
Eric Duminil committed
42
      return;
43
    }
Eric Duminil's avatar
Eric Duminil committed
44
    Serial.print(F("Setting UNIX time to : "));
45
46
47
    Serial.println(unix_seconds);
    timeClient.setEpochTime(unix_seconds - seconds());
    timeClient.getFormattedDate(time);
Eric Duminil's avatar
Eric Duminil committed
48
    Serial.print(F("Current time : "));
49
50
51
52
    Serial.println(time);
  }
}

Eric Duminil's avatar
Eric Duminil committed
53
54
void Ampel::showFreeSpace() {
  Serial.print(F("Free heap space : "));
Eric Duminil's avatar
Eric Duminil committed
55
  Serial.print(ESP.getFreeHeap());
Eric Duminil's avatar
Eric Duminil committed
56
  Serial.println(F(" bytes."));
Eric Duminil's avatar
Eric Duminil committed
57
58
59
60
61
62
  Serial.print(F("Max free block size : "));
  Serial.print(esp_get_max_free_block_size());
  Serial.println(F(" bytes."));
  Serial.print(F("Heap fragmentation : "));
  Serial.print(esp_get_heap_fragmentation());
  Serial.println(F(" %"));
Eric Duminil's avatar
Eric Duminil committed
63
64
}

Eric Duminil's avatar
Eric Duminil committed
65
66
67
68
69
70
71
72
73
74
char sensorId[10]; // e.g "ESPxxxxxx\0"

char* getSensorId() {
  uint8_t mac[6];
  WiFi.macAddress(mac);
  // Get last 3 bytes of ESP MAC (worldwide unique)
  snprintf(sensorId, sizeof(sensorId), "ESP%02x%02x%02x", mac[3], mac[4], mac[5]);
  return sensorId;
}

75
Ampel::Ampel() :
Eric Duminil's avatar
Eric Duminil committed
76
    board(current_board), sensorId(getSensorId()), max_loop_duration(0) {
77
78
  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)"));
Eric Duminil's avatar
Eric Duminil committed
79
80
  sensor_console::defineCommand("reset", []() {
    ESP.restart();
81
  }, F(" (Restarts the sensor)"));
82
83
}

Eric Duminil's avatar
Eric Duminil committed
84
Ampel ampel;