mDNS controlled by run-time variable

This commit is contained in:
Jaakko Haakana 2025-07-05 10:34:06 +03:00
parent 5f8eedbacb
commit efdbbff916
3 changed files with 28 additions and 24 deletions

View file

@ -28,15 +28,10 @@
#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
#include "src/devboard/mqtt/mqtt.h"
#include "src/devboard/webserver/webserver.h"
#include "src/devboard/wifi/wifi.h"
#ifdef MDNSRESPONDER
#include <ESPmDNS.h>
#endif // MDNSRESONDER
#include "src/devboard/mqtt/mqtt.h"
#ifdef PERIODIC_BMS_RESET_AT
#include "src/devboard/utils/ntp_time.h"
#endif
@ -183,9 +178,9 @@ void connectivity_loop(void*) {
init_webserver();
}
#ifdef MDNSRESPONDER
if (mdns_enabled) {
init_mDNS();
#endif
}
while (true) {
START_TIME_MEASUREMENT(wifi);

View file

@ -1,8 +1,9 @@
#include "wifi.h"
#include <ESPmDNS.h>
#include "../../include.h"
#include "../utils/events.h"
#ifdef WIFI
#if defined(WIFI) || defined(WEBSERVER)
const bool wifi_enabled_default = true;
#else
const bool wifi_enabled_default = false;
@ -10,6 +11,21 @@ const bool wifi_enabled_default = false;
bool wifi_enabled = wifi_enabled_default;
#ifdef WIFIAP
const bool wifiap_enabled_default = true;
#else
const bool wifiap_enabled_default = false;
#endif
bool wifiap_enabled = wifiap_enabled_default;
#ifdef MDNSRESPONDER
const bool mdns_enabled_default = true;
#else
const bool mdns_enabled_default = false;
#endif
bool mdns_enabled = mdns_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
@ -36,12 +52,12 @@ static bool connected_once = false;
void init_WiFi() {
#ifdef WIFIAP
if (!wifiap_enabled) {
WiFi.mode(WIFI_AP_STA); // Simultaneous WiFi AP and Router connection
init_WiFi_AP();
#else
WiFi.mode(WIFI_STA); // Only Router connection
#endif // WIFIAP
} else {
WiFi.mode(WIFI_AP); // Only AP mode
}
// Set WiFi to auto reconnect
WiFi.setAutoReconnect(true);
@ -183,7 +199,6 @@ void onWifiDisconnect(WiFiEvent_t event, WiFiEventInfo_t info) {
//normal reconnect retry start at first 2 seconds
}
#ifdef MDNSRESPONDER
// Initialise mDNS
void init_mDNS() {
// Calulate the host name using the last two chars from the MAC address so each one is likely unique on a network.
@ -201,9 +216,7 @@ void init_mDNS() {
MDNS.addService("battery_emulator", "tcp", 80);
}
}
#endif // MDNSRESPONDER
#ifdef WIFIAP
void init_WiFi_AP() {
#ifdef DEBUG_LOG
logging.println("Creating Access Point: " + String(ssidAP));
@ -217,4 +230,3 @@ void init_WiFi_AP() {
logging.println(IP.toString());
#endif
}
#endif // WIFIAP

View file

@ -5,10 +5,6 @@
#include <string>
#include "../../include.h"
#ifdef MDNSRESPONDER
#include <ESPmDNS.h>
#endif // MDNSRESONDER
extern std::string ssid;
extern std::string password;
extern const uint8_t wifi_channel;
@ -29,5 +25,6 @@ void init_WiFi_AP();
void init_mDNS();
extern bool wifi_enabled;
extern bool mdns_enabled;
#endif