Add WiFi MQTT companion setup portal
This commit is contained in:
1 parent
11aa4bc191
commit
dce99fd5b3
23 files changed
+1664
-117
No files matched your search
@@ -3,6 +3,10 @@
|
||||
#include <Arduino.h> // needed for PlatformIO
|
||||
#include <Mesh.h>
|
||||
|
||||
#if defined(WITH_MQTT_BRIDGE) && defined(ESP32_PLATFORM) && defined(WIFI_SSID)
|
||||
#include <helpers/MQTTDefaults.h>
|
||||
#endif
|
||||
|
||||
static uint32_t nextRadioApplyRetryDelay(uint8_t& failure_count) {
|
||||
uint8_t shift = failure_count < 5 ? failure_count : 5;
|
||||
if (failure_count < 6) failure_count++;
|
||||
@@ -408,6 +412,12 @@ void MyMesh::updateGpsTelemetryPolicy() {
|
||||
}
|
||||
|
||||
void MyMesh::logRxRaw(float snr, float rssi, const uint8_t raw[], int len) {
|
||||
#if defined(WITH_MQTT_BRIDGE) && defined(ESP32_PLATFORM) && defined(WIFI_SSID)
|
||||
if (_mqtt_bridge && _mqtt_bridge->isRunning()) {
|
||||
_mqtt_bridge->storeRawRadioData(raw, len, snr, rssi);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (_serial->isConnected() && len + 3 <= MAX_FRAME_SIZE) {
|
||||
int i = 0;
|
||||
out_frame[i++] = PUSH_CODE_LOG_RX_DATA;
|
||||
@@ -420,6 +430,20 @@ void MyMesh::logRxRaw(float snr, float rssi, const uint8_t raw[], int len) {
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(WITH_MQTT_BRIDGE) && defined(ESP32_PLATFORM) && defined(WIFI_SSID)
|
||||
void MyMesh::logRx(mesh::Packet* packet, int, float) {
|
||||
if (_mqtt_bridge && _mqtt_bridge->isRunning()) {
|
||||
_mqtt_bridge->onPacketReceived(packet);
|
||||
}
|
||||
}
|
||||
|
||||
void MyMesh::logTx(mesh::Packet* packet, int) {
|
||||
if (_mqtt_bridge && _mqtt_bridge->isRunning()) {
|
||||
_mqtt_bridge->sendPacket(packet);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
bool MyMesh::isAutoAddEnabled() const {
|
||||
return (_prefs.manual_add_contacts & 1) == 0;
|
||||
}
|
||||
@@ -1128,6 +1152,13 @@ MyMesh::MyMesh(mesh::Radio &radio, mesh::RNG &rng, mesh::RTCClock &rtc, SimpleMe
|
||||
#endif
|
||||
#endif
|
||||
_prefs.radio_fem_rxgain = 1;
|
||||
|
||||
#if defined(WITH_MQTT_BRIDGE) && defined(ESP32_PLATFORM) && defined(WIFI_SSID)
|
||||
memset(&_mqtt_prefs, 0, sizeof(_mqtt_prefs));
|
||||
_mqtt_bridge = nullptr;
|
||||
_mqtt_configured = false;
|
||||
_mqtt_started = false;
|
||||
#endif
|
||||
}
|
||||
|
||||
void MyMesh::begin(bool has_display) {
|
||||
@@ -1219,6 +1250,32 @@ void MyMesh::begin(bool has_display) {
|
||||
radio_driver.getRxBoostedGainMode() ? "Enabled" : "Disabled");
|
||||
// NOTE: no FEM LNA wiring here — companion has its own NodePrefs without
|
||||
// radio_fem_rxgain, matching upstream (which also doesn't wire companion).
|
||||
|
||||
#if defined(WITH_MQTT_BRIDGE) && defined(ESP32_PLATFORM) && defined(WIFI_SSID)
|
||||
applyMQTTDefaults(&_mqtt_prefs);
|
||||
_mqtt_configured = CompanionMqttSetupPortal::loadStoredConfig(_mqtt_prefs);
|
||||
if (!_mqtt_configured) applyMQTTDefaults(&_mqtt_prefs);
|
||||
|
||||
MQTTNodeInfo node_info;
|
||||
node_info.node_name = _prefs.node_name;
|
||||
node_info.freq = &_prefs.freq;
|
||||
node_info.bw = &_prefs.bw;
|
||||
node_info.sf = &_prefs.sf;
|
||||
node_info.cr = &_prefs.cr;
|
||||
node_info.repeat_flag = &_prefs.client_repeat;
|
||||
node_info.repeat_when_nonzero = true;
|
||||
_mqtt_bridge = new MQTTBridge(node_info, &_mqtt_prefs,
|
||||
getRTCClock(), &self_id, false);
|
||||
if (_mqtt_bridge) {
|
||||
char device_id[65];
|
||||
mesh::Utils::toHex(device_id, self_id.pub_key, PUB_KEY_SIZE);
|
||||
_mqtt_bridge->setDeviceID(device_id);
|
||||
_mqtt_bridge->setFirmwareVersion(FIRMWARE_VERSION);
|
||||
_mqtt_bridge->setBoardModel(board.getManufacturerName());
|
||||
_mqtt_bridge->setBuildDate(FIRMWARE_BUILD_DATE);
|
||||
_mqtt_bridge->setStatsSources(this, _radio, &board, _ms);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
bool MyMesh::applySavedRadioParams() {
|
||||
@@ -1239,6 +1296,57 @@ uint32_t MyMesh::getBLEPin() {
|
||||
return _active_ble_pin;
|
||||
}
|
||||
|
||||
#if defined(WITH_MQTT_BRIDGE) && defined(ESP32_PLATFORM) && defined(WIFI_SSID)
|
||||
static void copyMqttString(char* dest, size_t dest_size, const char* src) {
|
||||
strncpy(dest, src ? src : "", dest_size - 1);
|
||||
dest[dest_size - 1] = 0;
|
||||
}
|
||||
|
||||
void MyMesh::serviceMQTT(const char* wifi_ssid, const char* wifi_password,
|
||||
bool allow_setup_page) {
|
||||
if (!_mqtt_started && !_mqtt_setup.isActive()) {
|
||||
if (strcmp(_mqtt_prefs.wifi_ssid, wifi_ssid ? wifi_ssid : "") != 0) {
|
||||
copyMqttString(_mqtt_prefs.wifi_ssid, sizeof(_mqtt_prefs.wifi_ssid), wifi_ssid);
|
||||
}
|
||||
if (strcmp(_mqtt_prefs.wifi_password, wifi_password ? wifi_password : "") != 0) {
|
||||
copyMqttString(_mqtt_prefs.wifi_password, sizeof(_mqtt_prefs.wifi_password), wifi_password);
|
||||
}
|
||||
}
|
||||
|
||||
if (WiFi.status() != WL_CONNECTED || !allow_setup_page) {
|
||||
_mqtt_setup.stop();
|
||||
return;
|
||||
}
|
||||
|
||||
if (_mqtt_setup.loop()) {
|
||||
if (_mqtt_started && _mqtt_bridge) {
|
||||
_mqtt_bridge->end();
|
||||
}
|
||||
_mqtt_started = false;
|
||||
_mqtt_configured = true;
|
||||
Serial.println("MQTT companion: applying settings saved from the web page");
|
||||
}
|
||||
|
||||
if (!_mqtt_setup.isActive()) {
|
||||
_mqtt_setup.begin(&_mqtt_prefs);
|
||||
}
|
||||
|
||||
if (_mqtt_configured && !_mqtt_started && _mqtt_bridge) {
|
||||
_mqtt_started = true; // begin is one-shot; avoid retrying partial allocations
|
||||
_mqtt_bridge->begin();
|
||||
if (_mqtt_bridge->isRunning()) {
|
||||
Serial.println("MQTT companion: bridge started");
|
||||
} else {
|
||||
Serial.println("MQTT companion: bridge could not start");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MyMesh::stopMQTTSetupPage() {
|
||||
_mqtt_setup.stop();
|
||||
}
|
||||
#endif
|
||||
|
||||
struct FreqRange {
|
||||
uint32_t lower_freq, upper_freq;
|
||||
};
|
||||
|
||||
@@ -26,6 +26,12 @@
|
||||
#include "DataStore.h"
|
||||
#include "NodePrefs.h"
|
||||
|
||||
#if defined(WITH_MQTT_BRIDGE) && defined(ESP32_PLATFORM) && defined(WIFI_SSID)
|
||||
#include <helpers/CompanionMqttSetupPortal.h>
|
||||
#include <helpers/MQTTPrefs.h>
|
||||
#include <helpers/bridges/MQTTBridge.h>
|
||||
#endif
|
||||
|
||||
#include <RTClib.h>
|
||||
#include <helpers/ArduinoHelpers.h>
|
||||
#include <helpers/BaseSerialInterface.h>
|
||||
@@ -95,6 +101,13 @@ public:
|
||||
NodePrefs *getNodePrefs();
|
||||
uint32_t getBLEPin();
|
||||
|
||||
#if defined(WITH_MQTT_BRIDGE) && defined(ESP32_PLATFORM) && defined(WIFI_SSID)
|
||||
void serviceMQTT(const char* wifi_ssid, const char* wifi_password,
|
||||
bool allow_setup_page);
|
||||
void stopMQTTSetupPage();
|
||||
bool isMQTTConfigured() const { return _mqtt_configured; }
|
||||
#endif
|
||||
|
||||
void loop();
|
||||
void handleCmdFrame(size_t len);
|
||||
bool advert();
|
||||
@@ -106,6 +119,9 @@ protected:
|
||||
float getAirtimeBudgetFactor() const override;
|
||||
int getInterferenceThreshold() const override;
|
||||
bool getCADEnabled() const override;
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
uint32_t getRadioWatchdogMillis() const override { return 0; }
|
||||
#endif
|
||||
int calcRxDelay(float score, uint32_t air_time) const override;
|
||||
uint32_t getRetransmitDelay(const mesh::Packet *packet) override;
|
||||
uint32_t getDirectRetransmitDelay(const mesh::Packet *packet) override;
|
||||
@@ -119,6 +135,10 @@ protected:
|
||||
bool sendFloodScoped(const mesh::GroupChannel& channel, mesh::Packet* pkt, uint32_t delay_millis=0) override;
|
||||
|
||||
void logRxRaw(float snr, float rssi, const uint8_t raw[], int len) override;
|
||||
#if defined(WITH_MQTT_BRIDGE) && defined(ESP32_PLATFORM) && defined(WIFI_SSID)
|
||||
void logRx(mesh::Packet* packet, int len, float score) override;
|
||||
void logTx(mesh::Packet* packet, int len) override;
|
||||
#endif
|
||||
bool isAutoAddEnabled() const override;
|
||||
bool shouldAutoAddContactType(uint8_t type) const override;
|
||||
bool shouldOverwriteWhenFull() const override;
|
||||
@@ -210,6 +230,13 @@ private:
|
||||
|
||||
DataStore* _store;
|
||||
NodePrefs _prefs;
|
||||
#if defined(WITH_MQTT_BRIDGE) && defined(ESP32_PLATFORM) && defined(WIFI_SSID)
|
||||
MQTTPrefs _mqtt_prefs;
|
||||
MQTTBridge* _mqtt_bridge;
|
||||
CompanionMqttSetupPortal _mqtt_setup;
|
||||
bool _mqtt_configured;
|
||||
bool _mqtt_started;
|
||||
#endif
|
||||
uint32_t pending_login;
|
||||
uint32_t pending_status;
|
||||
uint32_t pending_telemetry, pending_discovery; // pending _TELEMETRY_REQ
|
||||
|
||||
@@ -42,10 +42,14 @@ static uint32_t _atoi(const char* sp) {
|
||||
#ifdef ESP32
|
||||
#ifdef WIFI_SSID
|
||||
#include <helpers/esp32/SerialWifiInterface.h>
|
||||
#include <helpers/WiFiSetupPortal.h>
|
||||
SerialWifiInterface serial_interface;
|
||||
#ifndef TCP_PORT
|
||||
#define TCP_PORT 5000
|
||||
#endif
|
||||
#ifndef WIFI_PWD
|
||||
#define WIFI_PWD ""
|
||||
#endif
|
||||
#elif defined(BLE_PIN_CODE)
|
||||
#include <helpers/esp32/SerialBLEInterface.h>
|
||||
SerialBLEInterface serial_interface;
|
||||
@@ -112,8 +116,75 @@ void halt() {
|
||||
|
||||
/* WIFI RECONNECT TRACKERS */
|
||||
#if defined(ESP32) && defined(WIFI_SSID)
|
||||
static const unsigned long WIFI_SETUP_FALLBACK_MS = 120000UL;
|
||||
static const char COMPANION_WIFI_SETUP_AP[] = "MeshCore-Setup";
|
||||
bool wifi_needs_reconnect = false;
|
||||
unsigned long last_wifi_reconnect_attempt = 0;
|
||||
unsigned long wifi_disconnected_since = 0;
|
||||
bool wifi_setup_recovery_mode = false;
|
||||
static char configured_wifi_ssid[32];
|
||||
static char configured_wifi_password[64];
|
||||
|
||||
#if defined(WITH_MQTT_BRIDGE) && defined(DISPLAY_CLASS)
|
||||
static DisplayDriver* companion_setup_display = nullptr;
|
||||
static unsigned long companion_setup_display_refresh = 0;
|
||||
|
||||
static void renderCompanionSetupDisplay() {
|
||||
if (!companion_setup_display
|
||||
|| static_cast<int32_t>(millis() - companion_setup_display_refresh) < 0) return;
|
||||
companion_setup_display_refresh = millis() + 1000;
|
||||
|
||||
companion_setup_display->turnOn();
|
||||
companion_setup_display->startFrame();
|
||||
companion_setup_display->setTextSize(1);
|
||||
companion_setup_display->setColor(DisplayDriver::LIGHT);
|
||||
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
companion_setup_display->drawTextCentered(
|
||||
companion_setup_display->width() / 2, 0, "MQTT setup");
|
||||
companion_setup_display->setCursor(0, 14);
|
||||
companion_setup_display->print("Join WiFi:");
|
||||
companion_setup_display->drawTextEllipsized(
|
||||
0, 25, companion_setup_display->width(), configured_wifi_ssid);
|
||||
companion_setup_display->setCursor(0, 39);
|
||||
companion_setup_display->print("Open in browser:");
|
||||
const String ip = WiFi.localIP().toString();
|
||||
companion_setup_display->drawTextCentered(
|
||||
companion_setup_display->width() / 2, 51, ip.c_str());
|
||||
} else if (wifiSetupPortal().isActive()) {
|
||||
companion_setup_display->drawTextCentered(
|
||||
companion_setup_display->width() / 2, 0, "WiFi setup");
|
||||
companion_setup_display->setCursor(0, 14);
|
||||
companion_setup_display->print("Join open WiFi:");
|
||||
companion_setup_display->drawTextEllipsized(
|
||||
0, 25, companion_setup_display->width(), COMPANION_WIFI_SETUP_AP);
|
||||
companion_setup_display->setCursor(0, 39);
|
||||
companion_setup_display->print("Open in browser:");
|
||||
const String ip = wifiSetupPortal().apIP().toString();
|
||||
companion_setup_display->drawTextCentered(
|
||||
companion_setup_display->width() / 2, 51, ip.c_str());
|
||||
} else {
|
||||
companion_setup_display->drawTextCentered(
|
||||
companion_setup_display->width() / 2, 8, "WiFi connecting");
|
||||
companion_setup_display->setCursor(0, 25);
|
||||
companion_setup_display->print("SSID:");
|
||||
companion_setup_display->drawTextEllipsized(
|
||||
0, 38, companion_setup_display->width(), configured_wifi_ssid);
|
||||
companion_setup_display->drawTextCentered(
|
||||
companion_setup_display->width() / 2, 52, "Please wait...");
|
||||
}
|
||||
companion_setup_display->endFrame();
|
||||
}
|
||||
#endif
|
||||
|
||||
static bool saveCompanionWiFi(void*, const char* ssid, const char* password) {
|
||||
if (!WiFiSetupPortal::saveStoredCredentials(ssid, password)) return false;
|
||||
strncpy(configured_wifi_ssid, ssid, sizeof(configured_wifi_ssid) - 1);
|
||||
configured_wifi_ssid[sizeof(configured_wifi_ssid) - 1] = '\0';
|
||||
strncpy(configured_wifi_password, password ? password : "", sizeof(configured_wifi_password) - 1);
|
||||
configured_wifi_password[sizeof(configured_wifi_password) - 1] = '\0';
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* WIFI OTA SEEDER — relay a host folder of .mota over WiFi (motatool `serve --tcp`), on a DEDICATED port
|
||||
@@ -213,6 +284,9 @@ void setup() {
|
||||
DisplayDriver* disp = NULL;
|
||||
if (display.begin()) {
|
||||
disp = &display;
|
||||
#if defined(ESP32) && defined(WIFI_SSID) && defined(WITH_MQTT_BRIDGE)
|
||||
companion_setup_display = disp;
|
||||
#endif
|
||||
disp->startFrame();
|
||||
#ifdef ST7789
|
||||
disp->setTextSize(2);
|
||||
@@ -300,14 +374,35 @@ void setup() {
|
||||
if (event == ARDUINO_EVENT_WIFI_STA_DISCONNECTED) {
|
||||
WIFI_DEBUG_PRINTLN("WiFi disconnected. Flagging for reconnect...");
|
||||
wifi_needs_reconnect = true;
|
||||
if (wifi_disconnected_since == 0) {
|
||||
wifi_disconnected_since = millis() ? millis() : 1;
|
||||
}
|
||||
} else if (event == ARDUINO_EVENT_WIFI_STA_GOT_IP) {
|
||||
WIFI_DEBUG_PRINTLN("connected! IP %s (companion app on :%d)",
|
||||
WiFi.localIP().toString().c_str(), TCP_PORT);
|
||||
wifi_needs_reconnect = false;
|
||||
wifi_disconnected_since = 0;
|
||||
}
|
||||
});
|
||||
|
||||
WiFi.begin(WIFI_SSID, WIFI_PWD);
|
||||
bool have_wifi = WiFiSetupPortal::loadStoredCredentials(
|
||||
configured_wifi_ssid, sizeof(configured_wifi_ssid),
|
||||
configured_wifi_password, sizeof(configured_wifi_password));
|
||||
if (!have_wifi && !WiFiSetupPortal::isPlaceholderSSID(WIFI_SSID)) {
|
||||
strncpy(configured_wifi_ssid, WIFI_SSID, sizeof(configured_wifi_ssid) - 1);
|
||||
configured_wifi_ssid[sizeof(configured_wifi_ssid) - 1] = '\0';
|
||||
strncpy(configured_wifi_password, WIFI_PWD, sizeof(configured_wifi_password) - 1);
|
||||
configured_wifi_password[sizeof(configured_wifi_password) - 1] = '\0';
|
||||
have_wifi = true;
|
||||
}
|
||||
|
||||
if (have_wifi) {
|
||||
WiFi.mode(WIFI_STA);
|
||||
wifi_disconnected_since = millis() ? millis() : 1;
|
||||
WiFi.begin(configured_wifi_ssid, configured_wifi_password);
|
||||
} else if (!wifiSetupPortal().begin(COMPANION_WIFI_SETUP_AP, saveCompanionWiFi, nullptr)) {
|
||||
WIFI_DEBUG_PRINTLN("WiFi setup: could not start setup portal");
|
||||
}
|
||||
// Disable WiFi modem power-save: its periodic modem/light-sleep stalls the SX1262 SPI+DIO servicing,
|
||||
// which makes the LoRa radio go deaf (no TX/RX) while WiFi is associated. Required for LoRa+WiFi to
|
||||
// coexist on this ESP32 — the small extra idle current is well worth a working radio.
|
||||
@@ -381,7 +476,15 @@ void loop() {
|
||||
the_mesh.loop();
|
||||
sensors.loop();
|
||||
#ifdef DISPLAY_CLASS
|
||||
#if defined(ESP32) && defined(WIFI_SSID) && defined(WITH_MQTT_BRIDGE)
|
||||
if (the_mesh.isMQTTConfigured()) {
|
||||
ui_task.loop();
|
||||
} else {
|
||||
renderCompanionSetupDisplay();
|
||||
}
|
||||
#else
|
||||
ui_task.loop();
|
||||
#endif
|
||||
#endif
|
||||
rtc_clock.tick();
|
||||
#ifdef HAS_EXTERNAL_WATCHDOG
|
||||
@@ -403,12 +506,41 @@ void loop() {
|
||||
ota_seeder_loop(); // accept/drop a motatool `serve --tcp` connection on the dedicated seeder port
|
||||
ota_console_loop(); // service the OTA text console (port 5002)
|
||||
#endif
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
wifi_disconnected_since = 0;
|
||||
if (wifi_setup_recovery_mode && !wifiSetupPortal().isActive()) {
|
||||
wifi_setup_recovery_mode = false;
|
||||
}
|
||||
} else if (configured_wifi_ssid[0]) {
|
||||
unsigned long now = millis();
|
||||
if (wifi_disconnected_since == 0) wifi_disconnected_since = now ? now : 1;
|
||||
if (!wifi_setup_recovery_mode && !wifiSetupPortal().isActive()
|
||||
&& now - wifi_disconnected_since >= WIFI_SETUP_FALLBACK_MS) {
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
the_mesh.stopMQTTSetupPage();
|
||||
#endif
|
||||
if (wifiSetupPortal().begin(COMPANION_WIFI_SETUP_AP, saveCompanionWiFi, nullptr)) {
|
||||
wifiSetupPortal().configureRecovery(
|
||||
configured_wifi_ssid, configured_wifi_password, WIFI_SETUP_FALLBACK_MS);
|
||||
wifi_setup_recovery_mode = true;
|
||||
WIFI_DEBUG_PRINTLN("WiFi unavailable for two minutes; setup AP started");
|
||||
} else {
|
||||
// Avoid retrying portal creation on every pass through loop().
|
||||
wifi_disconnected_since = now;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Safely attempt to reconnect every 10 seconds if flagged
|
||||
if (wifi_needs_reconnect && (millis() - last_wifi_reconnect_attempt > 10000)) {
|
||||
if (!wifiSetupPortal().isActive() && wifi_needs_reconnect
|
||||
&& (millis() - last_wifi_reconnect_attempt > 10000)) {
|
||||
WIFI_DEBUG_PRINTLN("Attempting manual WiFi reconnect...");
|
||||
WiFi.disconnect();
|
||||
WiFi.reconnect();
|
||||
last_wifi_reconnect_attempt = millis();
|
||||
}
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
the_mesh.serviceMQTT(configured_wifi_ssid, configured_wifi_password,
|
||||
!wifiSetupPortal().isActive());
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
@@ -2481,9 +2481,18 @@ void MyMesh::begin(FILESYSTEM *fs) {
|
||||
if (_prefs.bridge_enabled) {
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
// Defer construction to avoid static init crashes on ESP32 classic
|
||||
mqtt_bridge = new MQTTBridge(&_prefs, _cli.getObserverPrefs(), _mgr, getRTCClock(), &self_id);
|
||||
MQTTNodeInfo node_info;
|
||||
node_info.node_name = _prefs.node_name;
|
||||
node_info.freq = &_prefs.freq;
|
||||
node_info.bw = &_prefs.bw;
|
||||
node_info.sf = &_prefs.sf;
|
||||
node_info.cr = &_prefs.cr;
|
||||
node_info.repeat_flag = &_prefs.disable_fwd;
|
||||
node_info.repeat_when_nonzero = false;
|
||||
mqtt_bridge = new MQTTBridge(node_info, _cli.getObserverPrefs(),
|
||||
getRTCClock(), &self_id);
|
||||
#endif
|
||||
BridgeBase* active_bridge = activeBridge();
|
||||
AbstractBridge* active_bridge = activeBridge();
|
||||
if (active_bridge) {
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
// Set device public key for MQTT topics
|
||||
@@ -4383,7 +4392,7 @@ void MyMesh::loop() {
|
||||
|
||||
#if defined(WITH_BRIDGE) && !defined(WITH_MQTT_BRIDGE)
|
||||
// MQTT runs its own task; serial and ESP-NOW bridges remain cooperative.
|
||||
BridgeBase* active_bridge = activeBridge();
|
||||
AbstractBridge* active_bridge = activeBridge();
|
||||
if (active_bridge && active_bridge->isRunning()) active_bridge->loop();
|
||||
#endif
|
||||
|
||||
@@ -4458,7 +4467,7 @@ void MyMesh::loop() {
|
||||
// To check if there is pending work
|
||||
bool MyMesh::hasPendingWork() const {
|
||||
#if defined(WITH_BRIDGE)
|
||||
const BridgeBase* active_bridge = activeBridge();
|
||||
const AbstractBridge* active_bridge = activeBridge();
|
||||
if (active_bridge && active_bridge->isRunning()) return true;
|
||||
#endif
|
||||
if (radio_driver.isWatchdogObserving()) return true; // keep MCU awake for one radio duty cycle
|
||||
|
||||
@@ -211,14 +211,14 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
||||
ESPNowBridge bridge;
|
||||
#endif
|
||||
#ifdef WITH_BRIDGE
|
||||
BridgeBase* activeBridge() {
|
||||
AbstractBridge* activeBridge() {
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
return mqtt_bridge;
|
||||
#else
|
||||
return &bridge;
|
||||
#endif
|
||||
}
|
||||
const BridgeBase* activeBridge() const {
|
||||
const AbstractBridge* activeBridge() const {
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
return mqtt_bridge;
|
||||
#else
|
||||
@@ -472,11 +472,20 @@ public:
|
||||
void setBridgeState(bool enable) override {
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
if (!mqtt_bridge) {
|
||||
mqtt_bridge = new MQTTBridge(&_prefs, _cli.getObserverPrefs(), _mgr, getRTCClock(), &self_id);
|
||||
MQTTNodeInfo node_info;
|
||||
node_info.node_name = _prefs.node_name;
|
||||
node_info.freq = &_prefs.freq;
|
||||
node_info.bw = &_prefs.bw;
|
||||
node_info.sf = &_prefs.sf;
|
||||
node_info.cr = &_prefs.cr;
|
||||
node_info.repeat_flag = &_prefs.disable_fwd;
|
||||
node_info.repeat_when_nonzero = false;
|
||||
mqtt_bridge = new MQTTBridge(node_info, _cli.getObserverPrefs(),
|
||||
getRTCClock(), &self_id);
|
||||
if (!mqtt_bridge) return;
|
||||
}
|
||||
#endif
|
||||
BridgeBase* active_bridge = activeBridge();
|
||||
AbstractBridge* active_bridge = activeBridge();
|
||||
if (!active_bridge || enable == active_bridge->isRunning()) return;
|
||||
if (enable)
|
||||
{
|
||||
@@ -506,7 +515,7 @@ public:
|
||||
}
|
||||
|
||||
void restartBridge() override {
|
||||
BridgeBase* active_bridge = activeBridge();
|
||||
AbstractBridge* active_bridge = activeBridge();
|
||||
if (!active_bridge || !active_bridge->isRunning()) return;
|
||||
active_bridge->end();
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
|
||||
@@ -840,7 +840,16 @@ void MyMesh::begin(FILESYSTEM *fs) {
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
if (_prefs.bridge_enabled) {
|
||||
// Defer construction to avoid static init crashes on ESP32 classic
|
||||
bridge = new MQTTBridge(&_prefs, _cli.getObserverPrefs(), _mgr, getRTCClock(), &self_id);
|
||||
MQTTNodeInfo node_info;
|
||||
node_info.node_name = _prefs.node_name;
|
||||
node_info.freq = &_prefs.freq;
|
||||
node_info.bw = &_prefs.bw;
|
||||
node_info.sf = &_prefs.sf;
|
||||
node_info.cr = &_prefs.cr;
|
||||
node_info.repeat_flag = &_prefs.disable_fwd;
|
||||
node_info.repeat_when_nonzero = false;
|
||||
bridge = new MQTTBridge(node_info, _cli.getObserverPrefs(),
|
||||
getRTCClock(), &self_id);
|
||||
if (bridge) {
|
||||
// Set device public key for MQTT topics
|
||||
char device_id[65];
|
||||
|
||||
@@ -277,7 +277,16 @@ public:
|
||||
void setBridgeState(bool enable) override {
|
||||
if (!bridge) {
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
bridge = new MQTTBridge(&_prefs, _cli.getObserverPrefs(), _mgr, getRTCClock(), &self_id);
|
||||
MQTTNodeInfo node_info;
|
||||
node_info.node_name = _prefs.node_name;
|
||||
node_info.freq = &_prefs.freq;
|
||||
node_info.bw = &_prefs.bw;
|
||||
node_info.sf = &_prefs.sf;
|
||||
node_info.cr = &_prefs.cr;
|
||||
node_info.repeat_flag = &_prefs.disable_fwd;
|
||||
node_info.repeat_when_nonzero = false;
|
||||
bridge = new MQTTBridge(node_info, _cli.getObserverPrefs(),
|
||||
getRTCClock(), &self_id);
|
||||
#endif
|
||||
if (!bridge) return;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user