util.cpp 2.67 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
#if defined(ESP8266)
const char *current_board = "ESP8266";
Eric Duminil's avatar
Eric Duminil committed
10
11
12
13
14
15
16
#  if !defined(AMPEL_WIFI)
void preinit() {
  // WiFi would be initialized otherwise (on ESP8266), even if unused.
  // see https://github.com/esp8266/Arduino/issues/2111#issuecomment-224251391
  ESP8266WiFiClass::preinitWiFiOff();
}
#  endif
Eric Duminil's avatar
Eric Duminil committed
17
18
19
20
21
22
#elif defined(ESP32)
const char *current_board = "ESP32";
#else
const char *current_board = "UNKNOWN";
#endif

23
24
25
26
27
//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);
28
  bool connected_at_least_once = false;
29
30
31
32
33
34

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

  void update() {
35
    connected_at_least_once |= timeClient.update();
36
37
  }

38
39
  void getLocalTime(char *timestamp) {
    timeClient.getFormattedDate(timestamp);
40
  }
41
42
43
44

  void setLocalTime(int32_t unix_seconds) {
    char time[23];
    timeClient.getFormattedDate(time);
Eric Duminil's avatar
Eric Duminil committed
45
    Serial.print(F("Current time : "));
46
    Serial.println(time);
47
48
    if (connected_at_least_once) {
      Serial.println(F("NTP update already happened. Not changing anything."));
Eric Duminil's avatar
Eric Duminil committed
49
      return;
50
    }
Eric Duminil's avatar
Eric Duminil committed
51
    Serial.print(F("Setting UNIX time to : "));
52
53
54
    Serial.println(unix_seconds);
    timeClient.setEpochTime(unix_seconds - seconds());
    timeClient.getFormattedDate(time);
Eric Duminil's avatar
Eric Duminil committed
55
    Serial.print(F("Current time : "));
56
57
58
59
    Serial.println(time);
  }
}

Eric Duminil's avatar
Eric Duminil committed
60
61
void Ampel::showFreeSpace() {
  Serial.print(F("Free heap space : "));
Eric Duminil's avatar
Eric Duminil committed
62
  Serial.print(ESP.getFreeHeap());
Eric Duminil's avatar
Eric Duminil committed
63
  Serial.println(F(" bytes."));
Eric Duminil's avatar
Eric Duminil committed
64
65
66
67
68
69
  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
70
71
}

Eric Duminil's avatar
Eric Duminil committed
72
73
74
75
76
77
78
79
80
81
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;
}

82
Ampel::Ampel() :
Eric Duminil's avatar
Eric Duminil committed
83
    board(current_board), sensorId(getSensorId()), max_loop_duration(0) {
84
85
  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
86
87
  sensor_console::defineCommand("reset", []() {
    ESP.restart();
88
  }, F("(Restarts the ESP)"));
89
90
}

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