WEB and MQTT settings are actually run-time variables

This commit is contained in:
Jaakko Haakana 2025-07-05 10:21:15 +03:00
parent 1c8802b6a7
commit 5f8eedbacb
10 changed files with 62 additions and 53 deletions

View file

@ -28,22 +28,15 @@
#error \ #error \
"Initial setup not completed, USER_SECRETS.h is missing. Please rename the file USER_SECRETS.TEMPLATE.h to USER_SECRETS.h and fill in the required credentials. This file is ignored by version control to keep sensitive information private." "Initial setup not completed, USER_SECRETS.h is missing. Please rename the file USER_SECRETS.TEMPLATE.h to USER_SECRETS.h and fill in the required credentials. This file is ignored by version control to keep sensitive information private."
#endif #endif
#ifdef WIFI
#include "src/devboard/wifi/wifi.h"
#ifdef WEBSERVER
#include "src/devboard/webserver/webserver.h" #include "src/devboard/webserver/webserver.h"
#include "src/devboard/wifi/wifi.h"
#ifdef MDNSRESPONDER #ifdef MDNSRESPONDER
#include <ESPmDNS.h> #include <ESPmDNS.h>
#endif // MDNSRESONDER #endif // MDNSRESONDER
#else // WEBSERVER
#ifdef MDNSRESPONDER
#error WEBSERVER needs to be enabled for MDNSRESPONDER!
#endif // MDNSRSPONDER
#endif // WEBSERVER
#ifdef MQTT
#include "src/devboard/mqtt/mqtt.h" #include "src/devboard/mqtt/mqtt.h"
#endif // MQTT
#endif // WIFI
#ifdef PERIODIC_BMS_RESET_AT #ifdef PERIODIC_BMS_RESET_AT
#include "src/devboard/utils/ntp_time.h" #include "src/devboard/utils/ntp_time.h"
#endif #endif
@ -81,10 +74,10 @@ void setup() {
init_stored_settings(); init_stored_settings();
#ifdef WIFI if (wifi_enabled) {
xTaskCreatePinnedToCore((TaskFunction_t)&connectivity_loop, "connectivity_loop", 4096, NULL, TASK_CONNECTIVITY_PRIO, xTaskCreatePinnedToCore((TaskFunction_t)&connectivity_loop, "connectivity_loop", 4096, NULL, TASK_CONNECTIVITY_PRIO,
&connectivity_loop_task, esp32hal->WIFICORE()); &connectivity_loop_task, esp32hal->WIFICORE());
#endif }
if (!led_init()) { if (!led_init()) {
return; return;
@ -132,21 +125,23 @@ void setup() {
// Initialize Task Watchdog for subscribed tasks // Initialize Task Watchdog for subscribed tasks
esp_task_wdt_config_t wdt_config = { esp_task_wdt_config_t wdt_config = {
.timeout_ms = INTERVAL_5_S, // If task hangs for longer than this, reboot .timeout_ms = INTERVAL_5_S, // If task hangs for longer than this, reboot
.idle_core_mask = (1 << esp32hal->CORE_FUNCTION_CORE()) | (1 << esp32hal->WIFICORE()), // Watch both cores .idle_core_mask =
(uint32_t)(1 << esp32hal->CORE_FUNCTION_CORE()) | (uint32_t)(1 << esp32hal->WIFICORE()), // Watch both cores
.trigger_panic = true // Enable panic reset on timeout .trigger_panic = true // Enable panic reset on timeout
}; };
// Start tasks // Start tasks
#ifdef MQTT if (mqtt_enabled) {
init_mqtt(); init_mqtt();
xTaskCreatePinnedToCore((TaskFunction_t)&mqtt_loop, "mqtt_loop", 4096, NULL, TASK_MQTT_PRIO, &mqtt_loop_task, xTaskCreatePinnedToCore((TaskFunction_t)&mqtt_loop, "mqtt_loop", 4096, NULL, TASK_MQTT_PRIO, &mqtt_loop_task,
esp32hal->WIFICORE()); esp32hal->WIFICORE());
#endif }
xTaskCreatePinnedToCore((TaskFunction_t)&core_loop, "core_loop", 4096, NULL, TASK_CORE_PRIO, &main_loop_task, xTaskCreatePinnedToCore((TaskFunction_t)&core_loop, "core_loop", 4096, NULL, TASK_CORE_PRIO, &main_loop_task,
esp32hal->CORE_FUNCTION_CORE()); esp32hal->CORE_FUNCTION_CORE());
#ifdef PERIODIC_BMS_RESET_AT #ifdef PERIODIC_BMS_RESET_AT
bmsResetTimeOffset = getTimeOffsetfromNowUntil(PERIODIC_BMS_RESET_AT); bmsResetTimeOffset = getTimeOffsetfromNowUntil(PERIODIC_BMS_RESET_AT);
if (bmsResetTimeOffset == 0) { if (bmsResetTimeOffset == 0) {
@ -179,16 +174,15 @@ void logging_loop(void*) {
} }
#endif #endif
#ifdef WIFI
void connectivity_loop(void*) { void connectivity_loop(void*) {
esp_task_wdt_add(NULL); // Register this task with WDT esp_task_wdt_add(NULL); // Register this task with WDT
// Init wifi // Init wifi
init_WiFi(); init_WiFi();
#ifdef WEBSERVER if (webserver_enabled) {
// Init webserver init_webserver();
init_webserver(); }
#endif
#ifdef MDNSRESPONDER #ifdef MDNSRESPONDER
init_mDNS(); init_mDNS();
#endif #endif
@ -196,18 +190,18 @@ void connectivity_loop(void*) {
while (true) { while (true) {
START_TIME_MEASUREMENT(wifi); START_TIME_MEASUREMENT(wifi);
wifi_monitor(); wifi_monitor();
#ifdef WEBSERVER
ota_monitor(); if (webserver_enabled) {
#endif ota_monitor();
}
END_TIME_MEASUREMENT_MAX(wifi, datalayer.system.status.wifi_task_10s_max_us); END_TIME_MEASUREMENT_MAX(wifi, datalayer.system.status.wifi_task_10s_max_us);
esp_task_wdt_reset(); // Reset watchdog esp_task_wdt_reset(); // Reset watchdog
delay(1); delay(1);
} }
} }
#endif
#ifdef MQTT
void mqtt_loop(void*) { void mqtt_loop(void*) {
esp_task_wdt_add(NULL); // Register this task with WDT esp_task_wdt_add(NULL); // Register this task with WDT
@ -219,7 +213,6 @@ void mqtt_loop(void*) {
delay(1); delay(1);
} }
} }
#endif
static std::list<Transmitter*> transmitters; static std::list<Transmitter*> transmitters;
@ -245,11 +238,12 @@ void core_loop(void*) {
receive_rs485(); // Process serial2 RS485 interface receive_rs485(); // Process serial2 RS485 interface
END_TIME_MEASUREMENT_MAX(comm, datalayer.system.status.time_comm_us); END_TIME_MEASUREMENT_MAX(comm, datalayer.system.status.time_comm_us);
#ifdef WEBSERVER
START_TIME_MEASUREMENT(ota); if (webserver_enabled) {
ElegantOTA.loop(); START_TIME_MEASUREMENT(ota);
END_TIME_MEASUREMENT_MAX(ota, datalayer.system.status.time_ota_us); ElegantOTA.loop();
#endif // WEBSERVER END_TIME_MEASUREMENT_MAX(ota, datalayer.system.status.time_ota_us);
}
// Process // Process
currentMillis = millis(); currentMillis = millis();

View file

@ -27,17 +27,14 @@ const char* ssidAP = "Battery Emulator"; // Maximum of 63 characters, also used
const char* passwordAP = AP_PASSWORD; // Set in USER_SECRETS.h const char* passwordAP = AP_PASSWORD; // Set in USER_SECRETS.h
const uint8_t wifi_channel = 0; // Set to 0 for automatic channel selection const uint8_t wifi_channel = 0; // Set to 0 for automatic channel selection
#ifdef WEBSERVER
const char* http_username = HTTP_USERNAME; // Set in USER_SECRETS.h const char* http_username = HTTP_USERNAME; // Set in USER_SECRETS.h
const char* http_password = HTTP_PASSWORD; // Set in USER_SECRETS.h const char* http_password = HTTP_PASSWORD; // Set in USER_SECRETS.h
// Set your Static IP address. Only used incase WIFICONFIG is set in USER_SETTINGS.h // Set your Static IP address. Only used incase WIFICONFIG is set in USER_SETTINGS.h
IPAddress local_IP(192, 168, 10, 150); IPAddress local_IP(192, 168, 10, 150);
IPAddress gateway(192, 168, 10, 1); IPAddress gateway(192, 168, 10, 1);
IPAddress subnet(255, 255, 255, 0); IPAddress subnet(255, 255, 255, 0);
#endif // WEBSERVER
// MQTT // MQTT
#ifdef MQTT
const char* mqtt_user = MQTT_USER; // Set in USER_SECRETS.h const char* mqtt_user = MQTT_USER; // Set in USER_SECRETS.h
const char* mqtt_password = MQTT_PASSWORD; // Set in USER_SECRETS.h const char* mqtt_password = MQTT_PASSWORD; // Set in USER_SECRETS.h
#ifdef MQTT_MANUAL_TOPIC_OBJECT_NAME #ifdef MQTT_MANUAL_TOPIC_OBJECT_NAME
@ -50,7 +47,6 @@ const char* mqtt_device_name =
const char* ha_device_id = const char* ha_device_id =
"battery-emulator"; // Custom device ID in Home Assistant. Previously, the ID was always "battery-emulator" "battery-emulator"; // Custom device ID in Home Assistant. Previously, the ID was always "battery-emulator"
#endif // MQTT_MANUAL_TOPIC_OBJECT_NAME #endif // MQTT_MANUAL_TOPIC_OBJECT_NAME
#endif // USE_MQTT
/* Charger settings (Optional, when using generator charging) */ /* Charger settings (Optional, when using generator charging) */
volatile float CHARGER_SET_HV = 384; // Reasonably appropriate 4.0v per cell charging of a 96s pack volatile float CHARGER_SET_HV = 384; // Reasonably appropriate 4.0v per cell charging of a 96s pack

View file

@ -1,13 +1,10 @@
#include "NISSAN-LEAF-BATTERY.h" #include "NISSAN-LEAF-BATTERY.h"
#include "../include.h"
#ifdef MQTT
#include "../devboard/mqtt/mqtt.h"
#endif
#include "../communication/can/comm_can.h" #include "../communication/can/comm_can.h"
#include "../datalayer/datalayer.h" #include "../datalayer/datalayer.h"
#include "../datalayer/datalayer_extended.h" //For "More battery info" webpage #include "../datalayer/datalayer_extended.h" //For "More battery info" webpage
#include "../devboard/utils/events.h" #include "../devboard/utils/events.h"
#include "../devboard/utils/logging.h" #include "../devboard/utils/logging.h"
#include "../include.h"
#include "../charger/CanCharger.h" #include "../charger/CanCharger.h"

View file

@ -27,7 +27,6 @@ void init_stored_settings() {
settings.putBool("EQUIPMENT_STOP", datalayer.system.settings.equipment_stop_active); settings.putBool("EQUIPMENT_STOP", datalayer.system.settings.equipment_stop_active);
#endif // LOAD_SAVED_SETTINGS_ON_BOOT #endif // LOAD_SAVED_SETTINGS_ON_BOOT
#ifdef WIFI
char tempSSIDstring[63]; // Allocate buffer with sufficient size char tempSSIDstring[63]; // Allocate buffer with sufficient size
size_t lengthSSID = settings.getString("SSID", tempSSIDstring, sizeof(tempSSIDstring)); size_t lengthSSID = settings.getString("SSID", tempSSIDstring, sizeof(tempSSIDstring));
if (lengthSSID > 0) { // Successfully read the string from memory. Set it to SSID! if (lengthSSID > 0) { // Successfully read the string from memory. Set it to SSID!
@ -40,7 +39,6 @@ void init_stored_settings() {
password = tempPasswordString; password = tempPasswordString;
} else { // Reading from settings failed. Do nothing with SSID. Raise event? } else { // Reading from settings failed. Do nothing with SSID. Raise event?
} }
#endif // WIFI
temp = settings.getUInt("BATTERY_WH_MAX", false); temp = settings.getUInt("BATTERY_WH_MAX", false);
if (temp != 0) { if (temp != 0) {
@ -128,14 +126,12 @@ void store_settings() {
return; return;
} }
#ifdef WIFI
if (!settings.putString("SSID", String(ssid.c_str()))) { if (!settings.putString("SSID", String(ssid.c_str()))) {
set_event(EVENT_PERSISTENT_SAVE_INFO, 1); set_event(EVENT_PERSISTENT_SAVE_INFO, 1);
} }
if (!settings.putString("PASSWORD", String(password.c_str()))) { if (!settings.putString("PASSWORD", String(password.c_str()))) {
set_event(EVENT_PERSISTENT_SAVE_INFO, 2); set_event(EVENT_PERSISTENT_SAVE_INFO, 2);
} }
#endif
if (!settings.putUInt("BATTERY_WH_MAX", datalayer.battery.info.total_capacity_Wh)) { if (!settings.putUInt("BATTERY_WH_MAX", datalayer.battery.info.total_capacity_Wh)) {
set_event(EVENT_PERSISTENT_SAVE_INFO, 3); set_event(EVENT_PERSISTENT_SAVE_INFO, 3);

View file

@ -13,6 +13,14 @@
#include "../utils/timer.h" #include "../utils/timer.h"
#include "mqtt_client.h" #include "mqtt_client.h"
#ifdef MQTT
const bool mqtt_enabled_default = true;
#else
const bool mqtt_enabled_default = false;
#endif
bool mqtt_enabled = mqtt_enabled_default;
esp_mqtt_client_config_t mqtt_cfg; esp_mqtt_client_config_t mqtt_cfg;
esp_mqtt_client_handle_t client; esp_mqtt_client_handle_t client;
char mqtt_msg[MQTT_MSG_BUFFER_SIZE]; char mqtt_msg[MQTT_MSG_BUFFER_SIZE];

View file

@ -55,4 +55,6 @@ void init_mqtt(void);
void mqtt_loop(void); void mqtt_loop(void);
bool mqtt_publish(const char* topic, const char* mqtt_msg, bool retain); bool mqtt_publish(const char* topic, const char* mqtt_msg, bool retain);
extern bool mqtt_enabled;
#endif #endif

View file

@ -18,6 +18,14 @@
void transmit_can_frame(CAN_frame* tx_frame, int interface); void transmit_can_frame(CAN_frame* tx_frame, int interface);
#ifdef WEBSERVER
const bool webserver_enabled_default = true;
#else
const bool webserver_enabled_default = false;
#endif
bool webserver_enabled = webserver_enabled_default; // Global flag to enable or disable the webserver
// Create AsyncWebServer object on port 80 // Create AsyncWebServer object on port 80
AsyncWebServer server(80); AsyncWebServer server(80);

View file

@ -9,6 +9,8 @@
#include "../../lib/ayushsharma82-ElegantOTA/src/ElegantOTA.h" #include "../../lib/ayushsharma82-ElegantOTA/src/ElegantOTA.h"
#include "../../lib/mathieucarbou-AsyncTCPSock/src/AsyncTCP.h" #include "../../lib/mathieucarbou-AsyncTCPSock/src/AsyncTCP.h"
extern bool webserver_enabled;
extern const char* version_number; // The current software version, shown on webserver extern const char* version_number; // The current software version, shown on webserver
#include <string> #include <string>

View file

@ -2,6 +2,14 @@
#include "../../include.h" #include "../../include.h"
#include "../utils/events.h" #include "../utils/events.h"
#ifdef WIFI
const bool wifi_enabled_default = true;
#else
const bool wifi_enabled_default = false;
#endif
bool wifi_enabled = wifi_enabled_default;
// Configuration Parameters // Configuration Parameters
static const uint16_t WIFI_CHECK_INTERVAL = 2000; // 1 seconds normal check interval when last connected static const uint16_t WIFI_CHECK_INTERVAL = 2000; // 1 seconds normal check interval when last connected
static const uint16_t STEP_WIFI_CHECK_INTERVAL = 2000; // 3 seconds wait step increase in checks for normal reconnects static const uint16_t STEP_WIFI_CHECK_INTERVAL = 2000; // 3 seconds wait step increase in checks for normal reconnects

View file

@ -23,13 +23,11 @@ void onWifiConnect(WiFiEvent_t event, WiFiEventInfo_t info);
void onWifiDisconnect(WiFiEvent_t event, WiFiEventInfo_t info); void onWifiDisconnect(WiFiEvent_t event, WiFiEventInfo_t info);
void onWifiGotIP(WiFiEvent_t event, WiFiEventInfo_t info); void onWifiGotIP(WiFiEvent_t event, WiFiEventInfo_t info);
#ifdef WIFIAP
void init_WiFi_AP(); void init_WiFi_AP();
#endif // WIFIAP
#ifdef MDNSRESPONDER
// Initialise mDNS // Initialise mDNS
void init_mDNS(); void init_mDNS();
#endif // MDNSRESPONDER
extern bool wifi_enabled;
#endif #endif