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() { ...@@ -80,16 +80,15 @@ void setup() {
led_effects::setupRing(); led_effects::setupRing();
util::initialize();
sensor::initialize(); sensor::initialize();
Serial.print(F("Sensor ID: ")); Serial.print(F("Sensor ID: "));
Serial.println(SENSOR_ID); Serial.println(ampel.sensorId);
Serial.print(F("Board : ")); Serial.print(F("Board : "));
Serial.println(BOARD); Serial.println(BOARD);
#ifdef AMPEL_WIFI #ifdef AMPEL_WIFI
WiFiConnect(SENSOR_ID); WiFiConnect(ampel.sensorId);
Serial.print(F("WiFi - Status: ")); Serial.print(F("WiFi - Status: "));
Serial.println(WiFi.status()); Serial.println(WiFi.status());
...@@ -101,7 +100,7 @@ void setup() { ...@@ -101,7 +100,7 @@ void setup() {
ntp::initialize(); 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); MDNS.addService("http", "tcp", 80);
Serial.println(F("mDNS responder started")); Serial.println(F("mDNS responder started"));
} else { } else {
...@@ -109,7 +108,7 @@ void setup() { ...@@ -109,7 +108,7 @@ void setup() {
} }
# ifdef AMPEL_MQTT # ifdef AMPEL_MQTT
mqtt::initialize("CO2sensors/" + SENSOR_ID); mqtt::initialize("CO2sensors/" + ampel.sensorId);
# endif # endif
} }
#endif #endif
......
...@@ -78,7 +78,7 @@ namespace csv_writer { ...@@ -78,7 +78,7 @@ namespace csv_writer {
} }
#endif #endif
const String filename = "/" + SENSOR_ID + ".csv"; const String filename = "/" + ampel.sensorId + ".csv";
int getAvailableSpace() { int getAvailableSpace() {
return getTotalSpace() - getUsedSpace(); return getTotalSpace() - getUsedSpace();
......
...@@ -51,21 +51,16 @@ namespace ntp { ...@@ -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; uint32_t max_loop_duration = 0;
//FIXME: Remove every instance of Strings, to avoid heap fragmentation problems. (Start: "Free heap space : 17104 bytes") Ampel::Ampel() :
// See more https://cpp4arduino.com/2020/02/07/how-to-format-strings-without-the-string-class.html sensorId("ESP" + macToID()) {
const String SENSOR_ID = "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 { ...@@ -26,8 +26,6 @@ namespace ntp {
} }
namespace util { namespace util {
void initialize();
template<typename Tpa, typename Tpb> template<typename Tpa, typename Tpb>
inline auto min(const Tpa &a, const Tpb &b) -> decltype(a < b ? a : b) { inline auto min(const Tpa &a, const Tpb &b) -> decltype(a < b ? a : b) {
return b < a ? b : a; return b < a ? b : a;
...@@ -38,10 +36,15 @@ namespace util { ...@@ -38,10 +36,15 @@ namespace util {
return b > a ? b : a; 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. //NOTE: Only use seconds() for duration comparison, not timestamps comparison. Otherwise, problems happen when millis roll over.
#define seconds() (millis() / 1000UL) #define seconds() (millis() / 1000UL)
extern uint32_t max_loop_duration; extern uint32_t max_loop_duration;
const extern String SENSOR_ID; extern const Ampel ampel;
#endif #endif
...@@ -184,7 +184,7 @@ namespace web_server { ...@@ -184,7 +184,7 @@ namespace web_server {
http.begin(); http.begin();
Serial.print(F("You can access this sensor via http://")); 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.print(F(".local (might be unstable) or http://"));
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
} }
...@@ -212,7 +212,7 @@ namespace web_server { ...@@ -212,7 +212,7 @@ namespace web_server {
char content[2000]; // Update if needed char content[2000]; // Update if needed
// INFO - Header size : 1767 - Body size : 1812 - Script size : 1909 // 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() WiFi.localIP().toString().c_str()
#ifdef AMPEL_CSV #ifdef AMPEL_CSV
, csv_writer::filename.c_str() , csv_writer::filename.c_str()
...@@ -225,7 +225,7 @@ namespace web_server { ...@@ -225,7 +225,7 @@ namespace web_server {
http.send_P(200, PSTR("text/html"), content); http.send_P(200, PSTR("text/html"), content);
// Body // 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, sensor::humidity, sensor::timestamp, config::measurement_timestep,
#ifdef AMPEL_CSV #ifdef AMPEL_CSV
csv_writer::last_successful_write, config::csv_interval, csv_writer::getAvailableSpace() / 1024, csv_writer::last_successful_write, config::csv_interval, csv_writer::getAvailableSpace() / 1024,
...@@ -237,9 +237,9 @@ namespace web_server { ...@@ -237,9 +237,9 @@ namespace web_server {
lorawan::connected ? "Yes" : "No", LMIC_FREQUENCY_PLAN, lorawan::last_transmission, lorawan::connected ? "Yes" : "No", LMIC_FREQUENCY_PLAN, lorawan::last_transmission,
config::lorawan_sending_interval, config::lorawan_sending_interval,
#endif #endif
config::temperature_offset, config::auto_calibrate_sensor ? "Yes" : "No", SENSOR_ID.c_str(), SENSOR_ID.c_str(), config::temperature_offset, config::auto_calibrate_sensor ? "Yes" : "No", ampel.sensorId.c_str(),
WiFi.localIP().toString().c_str(), WiFi.localIP().toString().c_str(), get_free_heap_size(), max_loop_duration, ampel.sensorId.c_str(), WiFi.localIP().toString().c_str(), WiFi.localIP().toString().c_str(),
BOARD, dd, hh, mm, ss); get_free_heap_size(), max_loop_duration, BOARD, dd, hh, mm, ss);
Serial.print(F(" - Body size : ")); Serial.print(F(" - Body size : "));
http.sendContent(content); http.sendContent(content);
...@@ -248,7 +248,7 @@ namespace web_server { ...@@ -248,7 +248,7 @@ namespace web_server {
// Script // Script
snprintf_P(content, sizeof(content), script_template snprintf_P(content, sizeof(content), script_template
#ifdef AMPEL_CSV #ifdef AMPEL_CSV
, csv_writer::filename.c_str(), SENSOR_ID.c_str() , csv_writer::filename.c_str(), ampel.sensorId.c_str()
#endif #endif
); );
......
Markdown is supported
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