immediately reconnect to mqtt server on wifi reconnect

1.2-legacy
Kevin Hester 2021-08-17 19:59:56 -07:00
rodzic 472e880280
commit 52d7a6b8e4
4 zmienionych plików z 51 dodań i 46 usunięć

Wyświetl plik

@ -338,7 +338,7 @@ void setup()
#endif
bool forceSoftAP = 0;
#ifdef BUTTON_PIN
#ifndef NO_ESP32
@ -537,6 +537,10 @@ void setup()
}
#endif
#if defined(PORTDUINO) || defined(HAS_WIFI)
mqttInit();
#endif
// Initialize Wifi
initWifi(forceSoftAP);
@ -549,10 +553,6 @@ void setup()
initApiServer();
#endif
#if defined(PORTDUINO) || defined(HAS_WIFI)
mqttInit();
#endif
// Start airtime logger thread.
airTime = new AirTime();

Wyświetl plik

@ -3,6 +3,7 @@
#include "concurrency/Periodic.h"
#include "configuration.h"
#include "main.h"
#include "mqtt/MQTT.h"
#include "mesh/http/WebServer.h"
#include "mesh/wifi/WiFiServerAPI.h"
#include "target_specific.h"
@ -109,7 +110,7 @@ void deinitWifi()
}
}
static void startServices()
static void onNetworkConnected()
{
if (!APStartupComplete) {
// Start web server
@ -129,9 +130,11 @@ static void startServices()
initApiServer();
APStartupComplete = true;
} else {
DEBUG_MSG("... Not starting network services (They're already running)\n");
}
}
// FIXME this is kinda yucky, instead we should just have an observable for 'wifireconnected'
if(mqtt)
mqtt->reconnect();
}
// Startup WiFi
@ -139,7 +142,7 @@ bool initWifi(bool forceSoftAP)
{
forcedSoftAP = forceSoftAP;
if ((radioConfig.has_preferences && radioConfig.preferences.wifi_ssid) || forceSoftAP) {
if ((radioConfig.has_preferences && radioConfig.preferences.wifi_ssid[0]) || forceSoftAP) {
const char *wifiName = radioConfig.preferences.wifi_ssid;
const char *wifiPsw = radioConfig.preferences.wifi_password;
@ -249,7 +252,7 @@ static void WiFiEvent(WiFiEvent_t event)
case SYSTEM_EVENT_STA_GOT_IP:
DEBUG_MSG("Obtained IP address: \n");
Serial.println(WiFi.localIP());
startServices();
onNetworkConnected();
break;
case SYSTEM_EVENT_STA_LOST_IP:
DEBUG_MSG("Lost IP address and IP address is reset to 0\n");
@ -269,7 +272,7 @@ static void WiFiEvent(WiFiEvent_t event)
case SYSTEM_EVENT_AP_START:
DEBUG_MSG("WiFi access point started\n");
Serial.println(WiFi.softAPIP());
startServices();
onNetworkConnected();
break;
case SYSTEM_EVENT_AP_STOP:
DEBUG_MSG("WiFi access point stopped\n");

Wyświetl plik

@ -1,10 +1,10 @@
#include "MQTT.h"
#include "NodeDB.h"
#include "PowerFSM.h"
#include "main.h"
#include "mesh/Channels.h"
#include "mesh/Router.h"
#include "mesh/generated/mqtt.pb.h"
#include "PowerFSM.h"
#include "sleep.h"
#include <WiFi.h>
#include <assert.h>
@ -60,42 +60,44 @@ MQTT::MQTT() : concurrency::OSThread("mqtt"), pubSub(mqttClient)
pubSub.setCallback(mqttCallback);
// preflightSleepObserver.observe(&preflightSleep);
// preflightSleepObserver.observe(&preflightSleep);
}
void MQTT::reconnect()
{
const char *serverAddr = "mqtt.meshtastic.org"; // default hostname
int serverPort = 1883; // default server port
if (wantsLink()) {
const char *serverAddr = "mqtt.meshtastic.org"; // default hostname
int serverPort = 1883; // default server port
if (*radioConfig.preferences.mqtt_server)
serverAddr = radioConfig.preferences.mqtt_server; // Override the default
if (*radioConfig.preferences.mqtt_server)
serverAddr = radioConfig.preferences.mqtt_server; // Override the default
String server = String(serverAddr);
int delimIndex = server.indexOf(':');
if (delimIndex > 0) {
String port = server.substring(delimIndex+1, server.length());
server[delimIndex] = 0;
serverPort = port.toInt();
serverAddr = server.c_str();
String server = String(serverAddr);
int delimIndex = server.indexOf(':');
if (delimIndex > 0) {
String port = server.substring(delimIndex + 1, server.length());
server[delimIndex] = 0;
serverPort = port.toInt();
serverAddr = server.c_str();
}
pubSub.setServer(serverAddr, serverPort);
DEBUG_MSG("Connecting to MQTT server %s, port: %d\n", serverAddr, serverPort);
auto myStatus = (statusTopic + owner.id);
bool connected = pubSub.connect(owner.id, "meshdev", "large4cats", myStatus.c_str(), 1, true, "offline");
if (connected) {
DEBUG_MSG("MQTT connected\n");
enabled = true; // Start running background process again
runASAP = true;
/// FIXME, include more information in the status text
bool ok = pubSub.publish(myStatus.c_str(), "online", true);
DEBUG_MSG("published %d\n", ok);
sendSubscriptions();
} else
DEBUG_MSG("Failed to contact MQTT server...\n");
}
pubSub.setServer(serverAddr, serverPort);
DEBUG_MSG("Connecting to MQTT server %s, port: %d\n", serverAddr, serverPort);
auto myStatus = (statusTopic + owner.id);
bool connected = pubSub.connect(owner.id, "meshdev", "large4cats", myStatus.c_str(), 1, true, "offline");
if (connected) {
DEBUG_MSG("MQTT connected\n");
enabled = true; // Start running background process again
runASAP = true;
/// FIXME, include more information in the status text
bool ok = pubSub.publish(myStatus.c_str(), "online", true);
DEBUG_MSG("published %d\n", ok);
sendSubscriptions();
} else
DEBUG_MSG("Failed to contact MQTT server...\n");
}
void MQTT::sendSubscriptions()

Wyświetl plik

@ -35,6 +35,10 @@ class MQTT : private concurrency::OSThread
*/
void onSend(const MeshPacket &mp, ChannelIndex chIndex);
/** Attempt to connect to server if necessary
*/
void reconnect();
protected:
virtual int32_t runOnce();
@ -43,10 +47,6 @@ class MQTT : private concurrency::OSThread
*/
bool wantsLink() const;
/** Attempt to connect to server if necessary
*/
void reconnect();
/** Tell the server what subscriptions we want (based on channels.downlink_enabled)
*/
void sendSubscriptions();