From 99d529be51113a71bfd2771ac105f282cbafb884 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Mon, 2 Aug 2021 17:42:44 -0700 Subject: [PATCH] While connected to MQTT server, veto light-sleep (to keep wifi working) --- src/mesh/http/WiFiAPClient.cpp | 19 +++++++++++++++++-- src/mqtt/MQTT.cpp | 6 ++++-- src/mqtt/MQTT.h | 6 ++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/mesh/http/WiFiAPClient.cpp b/src/mesh/http/WiFiAPClient.cpp index 13d4cd44..ed4cd355 100644 --- a/src/mesh/http/WiFiAPClient.cpp +++ b/src/mesh/http/WiFiAPClient.cpp @@ -28,6 +28,21 @@ bool APStartupComplete = 0; static bool needReconnect = true; // If we create our reconnector, run it once at the beginning +// FIXME, veto light sleep if we have a TCP server running +#if 0 +class WifiSleepObserver : public Observer { +protected: + + /// Return 0 if sleep is okay + virtual int onNotify(uint32_t newValue) { + + } +}; + +static WifiSleepObserver wifiSleepObserver; +//preflightSleepObserver.observe(&preflightSleep); +#endif + static int32_t reconnectWiFi() { if (radioConfig.has_preferences && needReconnect) { @@ -218,10 +233,10 @@ static void WiFiEvent(WiFiEvent_t event) DEBUG_MSG("Completed scan for access points\n"); break; case SYSTEM_EVENT_STA_START: - DEBUG_MSG("WiFi client started\n"); + DEBUG_MSG("WiFi station started\n"); break; case SYSTEM_EVENT_STA_STOP: - DEBUG_MSG("WiFi clients stopped\n"); + DEBUG_MSG("WiFi station stopped\n"); break; case SYSTEM_EVENT_STA_CONNECTED: DEBUG_MSG("Connected to access point\n"); diff --git a/src/mqtt/MQTT.cpp b/src/mqtt/MQTT.cpp index 674f13e6..4b4d3a0c 100644 --- a/src/mqtt/MQTT.cpp +++ b/src/mqtt/MQTT.cpp @@ -4,6 +4,7 @@ #include "mesh/Channels.h" #include "mesh/Router.h" #include "mesh/generated/mqtt.pb.h" +#include "sleep.h" #include #include @@ -57,11 +58,12 @@ MQTT::MQTT() : concurrency::OSThread("mqtt"), pubSub(mqttClient) mqtt = this; pubSub.setCallback(mqttCallback); + + preflightSleepObserver.observe(&preflightSleep); } void MQTT::reconnect() { - // pubSub.setServer("devsrv.ezdevice.net", 1883); or 192.168.10.188 const char *serverAddr = "mqtt.meshtastic.org"; // default hostname int serverPort = 1883; // default server port @@ -78,7 +80,7 @@ void MQTT::reconnect() } pubSub.setServer(serverAddr, serverPort); - DEBUG_MSG("Connecting to MQTT server %s, port: %d\n", server.c_str(), 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) { diff --git a/src/mqtt/MQTT.h b/src/mqtt/MQTT.h index 2f98c311..fd02db61 100644 --- a/src/mqtt/MQTT.h +++ b/src/mqtt/MQTT.h @@ -19,6 +19,9 @@ class MQTT : private concurrency::OSThread WiFiClient mqttClient; PubSubClient pubSub; + CallbackObserver preflightSleepObserver = + CallbackObserver(this, &MQTT::preflightSleepCb); + public: MQTT(); @@ -53,6 +56,9 @@ class MQTT : private concurrency::OSThread /// Called when a new publish arrives from the MQTT server void onPublish(char *topic, byte *payload, unsigned int length); + + /// Return 0 if sleep is okay, veto sleep if we are connected to pubsub server + int preflightSleepCb(void *unused = NULL) { return pubSub.connected() ? 1 : 0; } }; void mqttInit();