Skip to content

Commit

Permalink
Merge pull request #20 from cron-eu:make-settings-configurable
Browse files Browse the repository at this point in the history
Make some settings configurable
  • Loading branch information
remuslazar authored Aug 7, 2023
2 parents b4b3d4b + c5b5014 commit c13b67d
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 18 deletions.
15 changes: 12 additions & 3 deletions firmware/include/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion firmware/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ WDTZero Watchdog;
void __unused setup() {
Debug.begin();

Debug.println("Booting..");
DEBUG_PRINTLN("Booting..");

LED.begin();
SiedleService.begin();
Expand Down
12 changes: 6 additions & 6 deletions firmware/src/mqtt-service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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;
}
}
Expand Down
8 changes: 4 additions & 4 deletions firmware/src/rtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -58,17 +58,17 @@ 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();
}
auto epoch = ntp.getEpochTime();
#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
Expand Down
9 changes: 9 additions & 0 deletions firmware/src/serial-debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define FIRMWARE_SERIAL_DEBUG_H

#include <Arduino.h>
#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.
Expand All @@ -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
13 changes: 9 additions & 4 deletions firmware/src/wifi-manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#endif

#include <wifi_client_secrets.h>
#include "settings.h"

void WiFiManagerClass::loop() {
if (millis() - connectionCheckMillis > 250) { // check connection status periodically
Expand All @@ -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
}
Expand All @@ -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)
Expand All @@ -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();
Expand All @@ -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."));
}

Expand Down

0 comments on commit c13b67d

Please sign in to comment.