diff --git a/firmware/include/settings.h b/firmware/include/settings.h index 220436e..be02924 100644 --- a/firmware/include/settings.h +++ b/firmware/include/settings.h @@ -13,15 +13,24 @@ // Siedle Log size (shown in the Web UI) #define LOG_SIZE 32 - + #define MQTT_TX_QUEUE_LEN 32 #define SIEDLE_TX_QUEUE_LEN 32 // MQTT connect retry interval in ms -#define MQTT_RECONNECT_INTERVAL_MS 10000 +#define MQTT_RECONNECT_INTERVAL_MS 5000 // MQTT socket timeout (in seconds) -// do not set this too high to avoid issues with the hardware watchdog! +// do not set this too high to avoid issues with the watchdog! #define MQTT_TIMEOUT_SEC 10 +// how often will the RTC synchronize with the WiFi clock +#define RTC_SYNC_INTERVAL_SEC 30 + +// use the WiFi low power mode on SAMD devices +#define USE_WIFI_LOW_POWER + +// Use the DEBUG_PRINT() for debugging using the serial console +// #define USE_DEBUG_SERIAL_CONSOLE + #endif //FIRMWARE_SETTINGS_H diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index e2c008e..b3fbbeb 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -28,7 +28,7 @@ WDTZero Watchdog; void __unused setup() { Debug.begin(); - Debug.println("Booting.."); + DEBUG_PRINTLN("Booting.."); LED.begin(); SiedleService.begin(); diff --git a/firmware/src/mqtt-service.cpp b/firmware/src/mqtt-service.cpp index dacc308..a4f1f61 100644 --- a/firmware/src/mqtt-service.cpp +++ b/firmware/src/mqtt-service.cpp @@ -91,7 +91,7 @@ void MQTTServiceClass::loadSSLConfiguration() { File ca_file = LittleFS.open(F(MQTT_CA_FILE), "r"); if (!cert_file || !private_key_file || !ca_file) { - Debug.println(F("ERROR: load MQTT credentials failed.")); + DEBUG_PRINTLN(F("ERROR: load MQTT credentials failed.")); return; } @@ -125,19 +125,19 @@ void MQTTServiceClass::loop() { auto time = RTCSync.getEpoch(); sslClient.setX509Time(time); auto name = String(F(MQTT_DEVICE_NAME)); - Debug.print(String(F("MQTT: connecting as ")) + name + F(" .. ")); + DEBUG_PRINT(String(F("MQTT: connecting as ")) + name + F(" .. ")); auto connected = mqttClient.connect(name.c_str()); #endif mqttReconnects++; if (connected) { state = mqtt_connected; - Debug.println("mqtt connected"); + DEBUG_PRINTLN("mqtt connected"); } else { - Debug.println("failed!"); + DEBUG_PRINTLN("failed!"); #ifdef ESP8266 char buf[256]; sslClient.getLastSSLError(buf, sizeof(buf)); - Debug.println(String(F("Last SSL error: ")) + buf); + DEBUG_PRINTLN(String(F("Last SSL error: ")) + buf); #endif } } else { @@ -146,7 +146,7 @@ void MQTTServiceClass::loop() { // subscribe to a topic auto success = mqttClient.subscribe("siedle/send"); if (success) { - Debug.println("mqtt subscribed"); + DEBUG_PRINTLN("mqtt subscribed"); state = mqtt_connected_and_subscribed; } } diff --git a/firmware/src/rtc.h b/firmware/src/rtc.h index ec3b02d..92b75e1 100644 --- a/firmware/src/rtc.h +++ b/firmware/src/rtc.h @@ -44,7 +44,7 @@ class RTCSyncClass { #endif // if we're in the initializing phase, re-try every 3 seconds. Else use a longer sync interval - unsigned long interval = !initialized ? 8000 : 5 * 60 * 1000; + unsigned long interval = !initialized ? 8000 : ( RTC_SYNC_INTERVAL_SEC * 1000 ); if (millis() - lastMillis >= interval) { @@ -58,9 +58,9 @@ class RTCSyncClass { auto epoch = WiFi.getTime(); #elif defined(ESP8266) if (!initialized) { - Debug.print(F("NTP force update triggered .. ")); + DEBUG_PRINT(F("NTP force update triggered .. ")); auto success = ntp.forceUpdate(); - Debug.println(success ? F("ok") : F("failed")); + DEBUG_PRINTLN(success ? F("ok") : F("failed")); } else { ntp.update(); } @@ -68,7 +68,7 @@ class RTCSyncClass { #endif if (epoch > 10000) { if (!initialized) { - Debug.println(String(F("Got NTP time: ")) + epoch); + DEBUG_PRINTLN(String(F("Got NTP time: ")) + epoch); } initialized = true; #ifdef ARDUINO_ARCH_SAMD diff --git a/firmware/src/serial-debug.h b/firmware/src/serial-debug.h index 720e8c0..fa023da 100644 --- a/firmware/src/serial-debug.h +++ b/firmware/src/serial-debug.h @@ -6,6 +6,7 @@ #define FIRMWARE_SERIAL_DEBUG_H #include +#include "settings.h" // This class will provide a Print compatible interface to send debug messages to the attached serial device // If there is no serial device attached, it will be a no-op. @@ -27,4 +28,12 @@ class SerialDebugClass : public Print { extern SerialDebugClass Debug; +#ifdef USE_DEBUG_SERIAL_CONSOLE +#define DEBUG_PRINT(...) Debug.print(__VA_ARGS__) +#define DEBUG_PRINTLN(...) Debug.print(__VA_ARGS__) +#else +#define DEBUG_PRINT(...) +#define DEBUG_PRINTLN(...) +#endif + #endif //FIRMWARE_SERIAL_DEBUG_H diff --git a/firmware/src/wifi-manager.cpp b/firmware/src/wifi-manager.cpp index 9ed88b4..301effc 100644 --- a/firmware/src/wifi-manager.cpp +++ b/firmware/src/wifi-manager.cpp @@ -15,6 +15,7 @@ #endif #include +#include "settings.h" void WiFiManagerClass::loop() { if (millis() - connectionCheckMillis > 250) { // check connection status periodically @@ -26,7 +27,7 @@ void WiFiManagerClass::loop() { if (connected) { printWifiStatus(); #ifdef MDNS_HOSTNAME - Debug.println(String(F("mDNS service started for hostname: ")) + F(MDNS_HOSTNAME)); + DEBUG_PRINTLN(String(F("mDNS service started for hostname: ")) + F(MDNS_HOSTNAME)); MDNS.begin(F(MDNS_HOSTNAME)); #endif } @@ -50,16 +51,20 @@ void WiFiManagerClass::begin() { #endif connectionCheckMillis = 0; wifiReconnects = 0; + #ifdef ARDUINO_ARCH_SAMD +#ifdef USE_WIFI_LOW_POWER WiFi.lowPowerMode(); #endif +#endif + connect(); } void WiFiManagerClass::connect() { String ssid = F(SECRET_SSID); String pass = F(SECRET_PASS); - Debug.print(String(F("Connecting to WiFi Network '")) + ssid + "' .. "); + DEBUG_PRINT(String(F("Connecting to WiFi Network '")) + ssid + "' .. "); #ifdef ARDUINO_ARCH_SAMD WiFi.begin(SECRET_SSID, SECRET_PASS); #elif defined(ESP8266) @@ -70,7 +75,7 @@ void WiFiManagerClass::connect() { void WiFiManagerClass::printWifiStatus() { // print the SSID of the network you're attached to: - Debug.println("done!"); + DEBUG_PRINTLN("done!"); #ifdef ARDUINO_ARCH_SAMD auto localIP = WiFi.localIP(); @@ -79,7 +84,7 @@ void WiFiManagerClass::printWifiStatus() { #endif // print ip and rssi of the current link - Debug.println(String(F("Local IP: ")) + localIP + DEBUG_PRINTLN(String(F("Local IP: ")) + localIP + F(", signal strength (RSSI): ") + WiFi.RSSI() + F(" dBm.")); }