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 \
"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
#ifdef WIFI
#include "src/devboard/wifi/wifi.h"
#ifdef WEBSERVER
#include "src/devboard/webserver/webserver.h"
#include "src/devboard/wifi/wifi.h"
#ifdef MDNSRESPONDER
#include <ESPmDNS.h>
#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"
#endif // MQTT
#endif // WIFI
#ifdef PERIODIC_BMS_RESET_AT
#include "src/devboard/utils/ntp_time.h"
#endif
@ -81,10 +74,10 @@ void setup() {
init_stored_settings();
#ifdef WIFI
if (wifi_enabled) {
xTaskCreatePinnedToCore((TaskFunction_t)&connectivity_loop, "connectivity_loop", 4096, NULL, TASK_CONNECTIVITY_PRIO,
&connectivity_loop_task, esp32hal->WIFICORE());
#endif
}
if (!led_init()) {
return;
@ -132,21 +125,23 @@ void setup() {
// Initialize Task Watchdog for subscribed tasks
esp_task_wdt_config_t wdt_config = {
.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
};
// Start tasks
#ifdef MQTT
if (mqtt_enabled) {
init_mqtt();
xTaskCreatePinnedToCore((TaskFunction_t)&mqtt_loop, "mqtt_loop", 4096, NULL, TASK_MQTT_PRIO, &mqtt_loop_task,
esp32hal->WIFICORE());
#endif
}
xTaskCreatePinnedToCore((TaskFunction_t)&core_loop, "core_loop", 4096, NULL, TASK_CORE_PRIO, &main_loop_task,
esp32hal->CORE_FUNCTION_CORE());
#ifdef PERIODIC_BMS_RESET_AT
bmsResetTimeOffset = getTimeOffsetfromNowUntil(PERIODIC_BMS_RESET_AT);
if (bmsResetTimeOffset == 0) {
@ -179,16 +174,15 @@ void logging_loop(void*) {
}
#endif
#ifdef WIFI
void connectivity_loop(void*) {
esp_task_wdt_add(NULL); // Register this task with WDT
// Init wifi
init_WiFi();
#ifdef WEBSERVER
// Init webserver
if (webserver_enabled) {
init_webserver();
#endif
}
#ifdef MDNSRESPONDER
init_mDNS();
#endif
@ -196,18 +190,18 @@ void connectivity_loop(void*) {
while (true) {
START_TIME_MEASUREMENT(wifi);
wifi_monitor();
#ifdef WEBSERVER
if (webserver_enabled) {
ota_monitor();
#endif
}
END_TIME_MEASUREMENT_MAX(wifi, datalayer.system.status.wifi_task_10s_max_us);
esp_task_wdt_reset(); // Reset watchdog
delay(1);
}
}
#endif
#ifdef MQTT
void mqtt_loop(void*) {
esp_task_wdt_add(NULL); // Register this task with WDT
@ -219,7 +213,6 @@ void mqtt_loop(void*) {
delay(1);
}
}
#endif
static std::list<Transmitter*> transmitters;
@ -245,11 +238,12 @@ void core_loop(void*) {
receive_rs485(); // Process serial2 RS485 interface
END_TIME_MEASUREMENT_MAX(comm, datalayer.system.status.time_comm_us);
#ifdef WEBSERVER
if (webserver_enabled) {
START_TIME_MEASUREMENT(ota);
ElegantOTA.loop();
END_TIME_MEASUREMENT_MAX(ota, datalayer.system.status.time_ota_us);
#endif // WEBSERVER
}
// Process
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 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_password = HTTP_PASSWORD; // Set in USER_SECRETS.h
// Set your Static IP address. Only used incase WIFICONFIG is set in USER_SETTINGS.h
IPAddress local_IP(192, 168, 10, 150);
IPAddress gateway(192, 168, 10, 1);
IPAddress subnet(255, 255, 255, 0);
#endif // WEBSERVER
// MQTT
#ifdef MQTT
const char* mqtt_user = MQTT_USER; // Set in USER_SECRETS.h
const char* mqtt_password = MQTT_PASSWORD; // Set in USER_SECRETS.h
#ifdef MQTT_MANUAL_TOPIC_OBJECT_NAME
@ -50,7 +47,6 @@ const char* mqtt_device_name =
const char* ha_device_id =
"battery-emulator"; // Custom device ID in Home Assistant. Previously, the ID was always "battery-emulator"
#endif // MQTT_MANUAL_TOPIC_OBJECT_NAME
#endif // USE_MQTT
/* Charger settings (Optional, when using generator charging) */
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 "../include.h"
#ifdef MQTT
#include "../devboard/mqtt/mqtt.h"
#endif
#include "../communication/can/comm_can.h"
#include "../datalayer/datalayer.h"
#include "../datalayer/datalayer_extended.h" //For "More battery info" webpage
#include "../devboard/utils/events.h"
#include "../devboard/utils/logging.h"
#include "../include.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);
#endif // LOAD_SAVED_SETTINGS_ON_BOOT
#ifdef WIFI
char tempSSIDstring[63]; // Allocate buffer with sufficient size
size_t lengthSSID = settings.getString("SSID", tempSSIDstring, sizeof(tempSSIDstring));
if (lengthSSID > 0) { // Successfully read the string from memory. Set it to SSID!
@ -40,7 +39,6 @@ void init_stored_settings() {
password = tempPasswordString;
} else { // Reading from settings failed. Do nothing with SSID. Raise event?
}
#endif // WIFI
temp = settings.getUInt("BATTERY_WH_MAX", false);
if (temp != 0) {
@ -128,14 +126,12 @@ void store_settings() {
return;
}
#ifdef WIFI
if (!settings.putString("SSID", String(ssid.c_str()))) {
set_event(EVENT_PERSISTENT_SAVE_INFO, 1);
}
if (!settings.putString("PASSWORD", String(password.c_str()))) {
set_event(EVENT_PERSISTENT_SAVE_INFO, 2);
}
#endif
if (!settings.putUInt("BATTERY_WH_MAX", datalayer.battery.info.total_capacity_Wh)) {
set_event(EVENT_PERSISTENT_SAVE_INFO, 3);

View file

@ -13,6 +13,14 @@
#include "../utils/timer.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_handle_t client;
char mqtt_msg[MQTT_MSG_BUFFER_SIZE];

View file

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

View file

@ -18,6 +18,14 @@
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
AsyncWebServer server(80);

View file

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

View file

@ -2,6 +2,14 @@
#include "../../include.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
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

View file

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