mirror of
https://github.com/meshtastic/firmware.git
synced 2025-04-24 09:26:52 +00:00
immediately reconnect to mqtt server on wifi reconnect
This commit is contained in:
parent
472e880280
commit
52d7a6b8e4
10
src/main.cpp
10
src/main.cpp
@ -338,7 +338,7 @@ void setup()
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool forceSoftAP = 0;
|
bool forceSoftAP = 0;
|
||||||
|
|
||||||
#ifdef BUTTON_PIN
|
#ifdef BUTTON_PIN
|
||||||
#ifndef NO_ESP32
|
#ifndef NO_ESP32
|
||||||
|
|
||||||
@ -537,6 +537,10 @@ void setup()
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(PORTDUINO) || defined(HAS_WIFI)
|
||||||
|
mqttInit();
|
||||||
|
#endif
|
||||||
|
|
||||||
// Initialize Wifi
|
// Initialize Wifi
|
||||||
initWifi(forceSoftAP);
|
initWifi(forceSoftAP);
|
||||||
|
|
||||||
@ -549,10 +553,6 @@ void setup()
|
|||||||
initApiServer();
|
initApiServer();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(PORTDUINO) || defined(HAS_WIFI)
|
|
||||||
mqttInit();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Start airtime logger thread.
|
// Start airtime logger thread.
|
||||||
airTime = new AirTime();
|
airTime = new AirTime();
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include "concurrency/Periodic.h"
|
#include "concurrency/Periodic.h"
|
||||||
#include "configuration.h"
|
#include "configuration.h"
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
|
#include "mqtt/MQTT.h"
|
||||||
#include "mesh/http/WebServer.h"
|
#include "mesh/http/WebServer.h"
|
||||||
#include "mesh/wifi/WiFiServerAPI.h"
|
#include "mesh/wifi/WiFiServerAPI.h"
|
||||||
#include "target_specific.h"
|
#include "target_specific.h"
|
||||||
@ -109,7 +110,7 @@ void deinitWifi()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void startServices()
|
static void onNetworkConnected()
|
||||||
{
|
{
|
||||||
if (!APStartupComplete) {
|
if (!APStartupComplete) {
|
||||||
// Start web server
|
// Start web server
|
||||||
@ -129,9 +130,11 @@ static void startServices()
|
|||||||
initApiServer();
|
initApiServer();
|
||||||
|
|
||||||
APStartupComplete = true;
|
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
|
// Startup WiFi
|
||||||
@ -139,7 +142,7 @@ bool initWifi(bool forceSoftAP)
|
|||||||
{
|
{
|
||||||
forcedSoftAP = 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 *wifiName = radioConfig.preferences.wifi_ssid;
|
||||||
const char *wifiPsw = radioConfig.preferences.wifi_password;
|
const char *wifiPsw = radioConfig.preferences.wifi_password;
|
||||||
|
|
||||||
@ -249,7 +252,7 @@ static void WiFiEvent(WiFiEvent_t event)
|
|||||||
case SYSTEM_EVENT_STA_GOT_IP:
|
case SYSTEM_EVENT_STA_GOT_IP:
|
||||||
DEBUG_MSG("Obtained IP address: \n");
|
DEBUG_MSG("Obtained IP address: \n");
|
||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
startServices();
|
onNetworkConnected();
|
||||||
break;
|
break;
|
||||||
case SYSTEM_EVENT_STA_LOST_IP:
|
case SYSTEM_EVENT_STA_LOST_IP:
|
||||||
DEBUG_MSG("Lost IP address and IP address is reset to 0\n");
|
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:
|
case SYSTEM_EVENT_AP_START:
|
||||||
DEBUG_MSG("WiFi access point started\n");
|
DEBUG_MSG("WiFi access point started\n");
|
||||||
Serial.println(WiFi.softAPIP());
|
Serial.println(WiFi.softAPIP());
|
||||||
startServices();
|
onNetworkConnected();
|
||||||
break;
|
break;
|
||||||
case SYSTEM_EVENT_AP_STOP:
|
case SYSTEM_EVENT_AP_STOP:
|
||||||
DEBUG_MSG("WiFi access point stopped\n");
|
DEBUG_MSG("WiFi access point stopped\n");
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
#include "MQTT.h"
|
#include "MQTT.h"
|
||||||
#include "NodeDB.h"
|
#include "NodeDB.h"
|
||||||
|
#include "PowerFSM.h"
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "mesh/Channels.h"
|
#include "mesh/Channels.h"
|
||||||
#include "mesh/Router.h"
|
#include "mesh/Router.h"
|
||||||
#include "mesh/generated/mqtt.pb.h"
|
#include "mesh/generated/mqtt.pb.h"
|
||||||
#include "PowerFSM.h"
|
|
||||||
#include "sleep.h"
|
#include "sleep.h"
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
@ -60,42 +60,44 @@ MQTT::MQTT() : concurrency::OSThread("mqtt"), pubSub(mqttClient)
|
|||||||
|
|
||||||
pubSub.setCallback(mqttCallback);
|
pubSub.setCallback(mqttCallback);
|
||||||
|
|
||||||
// preflightSleepObserver.observe(&preflightSleep);
|
// preflightSleepObserver.observe(&preflightSleep);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MQTT::reconnect()
|
void MQTT::reconnect()
|
||||||
{
|
{
|
||||||
const char *serverAddr = "mqtt.meshtastic.org"; // default hostname
|
if (wantsLink()) {
|
||||||
int serverPort = 1883; // default server port
|
const char *serverAddr = "mqtt.meshtastic.org"; // default hostname
|
||||||
|
int serverPort = 1883; // default server port
|
||||||
|
|
||||||
if (*radioConfig.preferences.mqtt_server)
|
if (*radioConfig.preferences.mqtt_server)
|
||||||
serverAddr = radioConfig.preferences.mqtt_server; // Override the default
|
serverAddr = radioConfig.preferences.mqtt_server; // Override the default
|
||||||
|
|
||||||
String server = String(serverAddr);
|
String server = String(serverAddr);
|
||||||
int delimIndex = server.indexOf(':');
|
int delimIndex = server.indexOf(':');
|
||||||
if (delimIndex > 0) {
|
if (delimIndex > 0) {
|
||||||
String port = server.substring(delimIndex+1, server.length());
|
String port = server.substring(delimIndex + 1, server.length());
|
||||||
server[delimIndex] = 0;
|
server[delimIndex] = 0;
|
||||||
serverPort = port.toInt();
|
serverPort = port.toInt();
|
||||||
serverAddr = server.c_str();
|
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()
|
void MQTT::sendSubscriptions()
|
||||||
|
@ -35,6 +35,10 @@ class MQTT : private concurrency::OSThread
|
|||||||
*/
|
*/
|
||||||
void onSend(const MeshPacket &mp, ChannelIndex chIndex);
|
void onSend(const MeshPacket &mp, ChannelIndex chIndex);
|
||||||
|
|
||||||
|
/** Attempt to connect to server if necessary
|
||||||
|
*/
|
||||||
|
void reconnect();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual int32_t runOnce();
|
virtual int32_t runOnce();
|
||||||
|
|
||||||
@ -43,10 +47,6 @@ class MQTT : private concurrency::OSThread
|
|||||||
*/
|
*/
|
||||||
bool wantsLink() const;
|
bool wantsLink() const;
|
||||||
|
|
||||||
/** Attempt to connect to server if necessary
|
|
||||||
*/
|
|
||||||
void reconnect();
|
|
||||||
|
|
||||||
/** Tell the server what subscriptions we want (based on channels.downlink_enabled)
|
/** Tell the server what subscriptions we want (based on channels.downlink_enabled)
|
||||||
*/
|
*/
|
||||||
void sendSubscriptions();
|
void sendSubscriptions();
|
||||||
|
Loading…
Reference in New Issue
Block a user