Commit 9a47750c authored by Eric Duminil's avatar Eric Duminil
Browse files

Ampel class instead of util functions

parent 0e8dc635
......@@ -80,16 +80,15 @@ void setup() {
led_effects::setupRing();
util::initialize();
sensor::initialize();
Serial.print(F("Sensor ID: "));
Serial.println(SENSOR_ID);
Serial.println(ampel.sensorId);
Serial.print(F("Board : "));
Serial.println(BOARD);
#ifdef AMPEL_WIFI
WiFiConnect(SENSOR_ID);
WiFiConnect(ampel.sensorId);
Serial.print(F("WiFi - Status: "));
Serial.println(WiFi.status());
......@@ -101,7 +100,7 @@ void setup() {
ntp::initialize();
if (MDNS.begin(SENSOR_ID.c_str())) { // Start the mDNS responder for SENSOR_ID.local
if (MDNS.begin(ampel.sensorId.c_str())) { // Start the mDNS responder for SENSOR_ID.local
MDNS.addService("http", "tcp", 80);
Serial.println(F("mDNS responder started"));
} else {
......@@ -109,7 +108,7 @@ void setup() {
}
# ifdef AMPEL_MQTT
mqtt::initialize("CO2sensors/" + SENSOR_ID);
mqtt::initialize("CO2sensors/" + ampel.sensorId);
# endif
}
#endif
......
......@@ -78,7 +78,7 @@ namespace csv_writer {
}
#endif
const String filename = "/" + SENSOR_ID + ".csv";
const String filename = "/" + ampel.sensorId + ".csv";
int getAvailableSpace() {
return getTotalSpace() - getUsedSpace();
......
......@@ -51,21 +51,16 @@ namespace ntp {
}
}
namespace util {
void initialize() {
// Define SENSOR ID
// Add callbacks
sensor_console::defineIntCommand("set_time", ntp::setLocalTime, " 1618829570 (Sets time to the given UNIX time)");
sensor_console::defineCommand("free", []() {
Serial.print(F("Free heap space : "));
Serial.print(get_free_heap_size());
Serial.println(F(" bytes."));
}, " (Displays available heap space)");
}
}
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();
Ampel::Ampel() :
sensorId("ESP" + macToID()) {
sensor_console::defineIntCommand("set_time", ntp::setLocalTime, " 1618829570 (Sets time to the given UNIX time)");
sensor_console::defineCommand("free", []() {
Serial.print(F("Free heap space : "));
Serial.print(get_free_heap_size());
Serial.println(F(" bytes."));
}, " (Displays available heap space)");
}
const Ampel ampel;
......@@ -26,8 +26,6 @@ namespace ntp {
}
namespace util {
void initialize();
template<typename Tpa, typename Tpb>
inline auto min(const Tpa &a, const Tpb &b) -> decltype(a < b ? a : b) {
return b < a ? b : a;
......@@ -38,10 +36,15 @@ namespace util {
return b > a ? b : a;
}
}
class Ampel {
public:
const String sensorId;
Ampel();
};
//NOTE: Only use seconds() for duration comparison, not timestamps comparison. Otherwise, problems happen when millis roll over.
#define seconds() (millis() / 1000UL)
extern uint32_t max_loop_duration;
const extern String SENSOR_ID;
extern const Ampel ampel;
#endif
......@@ -184,7 +184,7 @@ namespace web_server {
http.begin();
Serial.print(F("You can access this sensor via http://"));
Serial.print(SENSOR_ID);
Serial.print(ampel.sensorId);
Serial.print(F(".local (might be unstable) or http://"));
Serial.println(WiFi.localIP());
}
......@@ -212,7 +212,7 @@ namespace web_server {
char content[2000]; // Update if needed
// INFO - Header size : 1767 - Body size : 1812 - Script size : 1909
snprintf_P(content, sizeof(content), header_template, sensor::co2, SENSOR_ID.c_str(),
snprintf_P(content, sizeof(content), header_template, sensor::co2, ampel.sensorId.c_str(),
WiFi.localIP().toString().c_str()
#ifdef AMPEL_CSV
, csv_writer::filename.c_str()
......@@ -225,7 +225,7 @@ namespace web_server {
http.send_P(200, PSTR("text/html"), content);
// Body
snprintf_P(content, sizeof(content), body_template, SENSOR_ID.c_str(), sensor::co2, sensor::temperature,
snprintf_P(content, sizeof(content), body_template, ampel.sensorId.c_str(), sensor::co2, sensor::temperature,
sensor::humidity, sensor::timestamp, config::measurement_timestep,
#ifdef AMPEL_CSV
csv_writer::last_successful_write, config::csv_interval, csv_writer::getAvailableSpace() / 1024,
......@@ -237,9 +237,9 @@ namespace web_server {
lorawan::connected ? "Yes" : "No", LMIC_FREQUENCY_PLAN, lorawan::last_transmission,
config::lorawan_sending_interval,
#endif
config::temperature_offset, config::auto_calibrate_sensor ? "Yes" : "No", SENSOR_ID.c_str(), SENSOR_ID.c_str(),
WiFi.localIP().toString().c_str(), WiFi.localIP().toString().c_str(), get_free_heap_size(), max_loop_duration,
BOARD, dd, hh, mm, ss);
config::temperature_offset, config::auto_calibrate_sensor ? "Yes" : "No", ampel.sensorId.c_str(),
ampel.sensorId.c_str(), WiFi.localIP().toString().c_str(), WiFi.localIP().toString().c_str(),
get_free_heap_size(), max_loop_duration, BOARD, dd, hh, mm, ss);
Serial.print(F(" - Body size : "));
http.sendContent(content);
......@@ -248,7 +248,7 @@ namespace web_server {
// Script
snprintf_P(content, sizeof(content), script_template
#ifdef AMPEL_CSV
, csv_writer::filename.c_str(), SENSOR_ID.c_str()
, csv_writer::filename.c_str(), ampel.sensorId.c_str()
#endif
);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment