Commit 991ddaf3 authored by Eric Duminil's avatar Eric Duminil
Browse files

Merge branch 'refactor/allow_millis_overflow' into develop

parents 86309b4a a9636423
......@@ -76,10 +76,9 @@ void setup() {
Serial.println(BOARD);
#ifdef AMPEL_WIFI
// Try to connect to Wi-Fi
WiFiConnect(SENSOR_ID);
Serial.print(F("WiFi STATUS: "));
Serial.print(F("WiFi - Status: "));
Serial.println(WiFi.status());
if (WiFi.status() == WL_CONNECTED) {
......@@ -126,7 +125,7 @@ void loop() {
}
#endif
//NOTE: Loop should never take more than 1000ms. Split in smaller methods and logic if needed.
//TODO: Restart every day or week, in order to not let t0 overflow?
//NOTE: Only use millis() for duration comparison, not timestamps comparison. Otherwise, problems happen when millis roll over.
uint32_t t0 = millis();
keepServicesAlive();
......@@ -151,7 +150,7 @@ void loop() {
uint32_t duration = millis() - t0;
if (duration > max_loop_duration) {
max_loop_duration = duration;
Serial.print("Max loop duration : ");
Serial.print(F("Debug - Max loop duration : "));
Serial.print(max_loop_duration);
Serial.println(" ms.");
}
......
......@@ -116,8 +116,8 @@ namespace sensor {
}
void logToSerial() {
Serial.println(timestamp);
Serial.print(F("co2(ppm): "));
Serial.print(timestamp);
Serial.print(F(" - co2(ppm): "));
Serial.print(co2);
Serial.print(F(" temp(C): "));
Serial.print(temperature, 1);
......
......@@ -115,11 +115,7 @@ namespace csv_writer {
// Open dir folder
Serial.println("Filesystem content:");
showFilesystemContent();
if (FS_LIB.exists(filename)) {
Serial.print(filename);
Serial.println(" has been found.");
}
Serial.println();
}
File openOrCreate() {
......@@ -145,7 +141,7 @@ namespace csv_writer {
if (written_bytes == 0) {
Serial.println(F("Nothing written. Disk full?"));
} else {
Serial.println(F("Wrote file content:"));
Serial.print(F("CSV - Wrote : "));
Serial.print(csv_line);
last_successful_write = ntp::getLocalTime();
}
......@@ -160,7 +156,6 @@ namespace csv_writer {
void logIfTimeHasCome(const String &timeStamp, const int16_t &co2, const float &temperature, const float &humidity) {
unsigned long now = seconds();
//TODO: Write average since last CSV write?
if (now - last_written_at > config::csv_interval) {
last_written_at = now;
log(timeStamp, co2, temperature, humidity);
......
......@@ -146,7 +146,7 @@ namespace led_effects {
static uint16_t wheel_offset = 0;
unsigned long t0 = millis();
pixels.setBrightness(config::max_brightness);
while (millis() < t0 + duration_ms) {
while (millis() - t0 < duration_ms) {
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.ColorHSV(i * 65535 / NUMPIXELS + wheel_offset));
wheel_offset += hue_increment;
......
......@@ -82,18 +82,18 @@ namespace lorawan {
u1_t nwkKey[16];
u1_t artKey[16];
LMIC_getSessionKeys(&netid, &devaddr, nwkKey, artKey);
Serial.print(F("netid: "));
Serial.print(F(" netid: "));
Serial.println(netid, DEC);
Serial.print(F("devaddr: "));
Serial.print(F(" devaddr: "));
Serial.println(devaddr, HEX);
Serial.print(F("AppSKey: "));
Serial.print(F(" AppSKey: "));
for (size_t i = 0; i < sizeof(artKey); ++i) {
if (i != 0)
Serial.print("-");
printHex2(artKey[i]);
}
Serial.println("");
Serial.print("NwkSKey: ");
Serial.println();
Serial.print(" NwkSKey: ");
for (size_t i = 0; i < sizeof(nwkKey); ++i) {
if (i != 0)
Serial.print("-");
......@@ -184,7 +184,7 @@ namespace lorawan {
void preparePayloadIfTimeHasCome(const int16_t &co2, const float &temperature, const float &humidity) {
static unsigned long last_sent_at = 0;
unsigned long now = seconds();
if (connected && (now > last_sent_at + config::lorawan_sending_interval)) {
if (connected && (now - last_sent_at > config::lorawan_sending_interval)) {
last_sent_at = now;
preparePayload(co2, temperature, humidity);
}
......
......@@ -41,7 +41,7 @@ namespace mqtt {
void publish(const String &timestamp, int16_t co2, float temperature, float humidity) {
if (WiFi.status() == WL_CONNECTED && mqttClient.connected()) {
led_effects::onBoardLEDOn();
Serial.print(F("Publishing MQTT message ... "));
Serial.print(F("MQTT - Publishing message ... "));
char payload[75]; // Should be enough for json...
snprintf(payload, sizeof(payload), json_sensor_format, timestamp.c_str(), co2, temperature, humidity);
......@@ -177,7 +177,7 @@ namespace mqtt {
}
void reconnect() {
if (last_failed_at > 0 && seconds() - last_failed_at < config::wait_after_fail) {
if (last_failed_at > 0 && (seconds() - last_failed_at < config::wait_after_fail)) {
// It failed less than wait_after_fail ago. Not even trying.
return;
}
......@@ -185,7 +185,7 @@ namespace mqtt {
// No WIFI
return;
}
Serial.print(F("Attempting MQTT connection..."));
Serial.print(F("MQTT - Attempting connection..."));
led_effects::onBoardLEDOn();
// Wait for connection, at most 15s (default)
......@@ -216,7 +216,6 @@ namespace mqtt {
void publishIfTimeHasCome(const String &timeStamp, const int16_t &co2, const float &temp, const float &hum) {
// Send message via MQTT according to sending interval
unsigned long now = seconds();
//TODO: Send average since last MQTT message?
if (now - last_sent_at > config::sending_interval) {
last_sent_at = now;
publish(timeStamp, co2, temp, hum);
......
......@@ -36,6 +36,7 @@ namespace util {
}
}
//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;
......
......@@ -113,7 +113,7 @@ namespace web_server {
"<tr><td>Free heap space</td><td>%6d bytes</td></tr>\n"
"<tr><td>Max loop duration</td><td>%5d ms</td></tr>\n"
"<tr><td>Board</td><td>%s</td></tr>\n"
"<tr><td>Uptime</td><td>%4d h %02d min %02d s</td></tr>\n"
"<tr><td>Uptime</td><td>%2d d %4d h %02d min %02d s</td></tr>\n"
"</table>\n"
"<div id='log' class='pure-u-1 pure-u-md-1-2'></div>\n"
#ifdef AMPEL_CSV
......@@ -198,6 +198,8 @@ namespace web_server {
}
unsigned long ss = seconds();
uint8_t dd = ss / 86400;
ss -= dd * 86400;
unsigned int hh = ss / 3600;
ss -= hh * 3600;
uint8_t mm = ss / 60;
......@@ -232,7 +234,7 @@ namespace web_server {
config::lorawan_sending_interval,
#endif
config::temperature_offset, 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, hh, mm, ss);
WiFi.localIP().toString().c_str(), get_free_heap_size(), max_loop_duration, BOARD, dd, hh, mm, ss);
http.sendContent(content);
......
......@@ -23,7 +23,7 @@ void WiFiConnect(const String &hostname) {
WiFi.setHostname(hostname.c_str());
#endif
Serial.print("\nConnecting to ");
Serial.print(F("WiFi - Connecting to "));
Serial.println(config::wifi_ssid);
WiFi.begin(config::wifi_ssid, config::wifi_password);
......@@ -35,11 +35,11 @@ void WiFiConnect(const String &hostname) {
if (WiFi.status() == WL_CONNECTED) {
led_effects::showKITTWheel(color::green);
Serial.println();
Serial.print("\nWiFi connected, IP address: ");
Serial.print(F("WiFi - Connected! IP address: "));
Serial.println(WiFi.localIP());
} else {
//TODO: Allow sensor to work as an Access Point, in order to define SSID & password?
led_effects::showKITTWheel(color::red);
Serial.println("\nConnection to WiFi failed");
Serial.println(F("Connection to WiFi failed"));
}
}
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