util.cpp 3.15 KB
Newer Older
1
#include "util.h"
Eric Duminil's avatar
Eric Duminil committed
2
3
4
5
6
#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
7
8
9
10
11
12

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
13
14
#if defined(ESP8266)
const char *current_board = "ESP8266";
Eric Duminil's avatar
Eric Duminil committed
15
16
17
18
19
20
21
#  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
22
23
24
25
26
27
#elif defined(ESP32)
const char *current_board = "ESP32";
#else
const char *current_board = "UNKNOWN";
#endif

28
29
30
31
32
//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);
33
  bool connected_at_least_once = false;
34
35
36
37
38
39

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

  void update() {
40
    connected_at_least_once |= timeClient.update();
41
42
  }

43
44
  void getLocalTime(char *timestamp) {
    timeClient.getFormattedDate(timestamp);
45
  }
46
47
48
49

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

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

Eric Duminil's avatar
Eric Duminil committed
77
char sensorId[10]; // e.g "ESPxxxxxx\0"
78
79
80
81
82
83
84
85
86
87
char macAddress[18]; // e.g "XX:XX:XX:XX:XX:XX\0"
uint8_t mac[6];

char* getMacString() {
  WiFi.macAddress(mac);
  // Get all 6 bytes of ESP MAC
  snprintf(macAddress, sizeof(macAddress), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4],
      mac[5]);
  return macAddress;
}
Eric Duminil's avatar
Eric Duminil committed
88
89
90
91
92
93
94
95

char* getSensorId() {
  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;
}

96
Ampel::Ampel() :
97
    board(current_board), sensorId(getSensorId()), macAddress(getMacString()), max_loop_duration(0) {
98
99
  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
100
101
  sensor_console::defineCommand("reset", []() {
    ESP.restart();
102
  }, F("(Restarts the ESP)"));
103
104
}

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