|
|
@@ -82,6 +82,7 @@ BLEServer* pServer = nullptr;
|
|
|
BLECharacteristic* pModeCharacteristic = nullptr;
|
|
|
BLECharacteristic* pConfigCharacteristic = nullptr;
|
|
|
bool bleDeviceConnected = false;
|
|
|
+bool wifiConnected = false;
|
|
|
|
|
|
Preferences preferences;
|
|
|
|
|
|
@@ -447,7 +448,7 @@ unsigned long lastStatusLedToggle = 0;
|
|
|
bool statusLedState = false;
|
|
|
|
|
|
void updateStatusLed() {
|
|
|
- bool connected = useMQTT ? (WiFi.status() == WL_CONNECTED) : bleDeviceConnected;
|
|
|
+ bool connected = (useMQTT && wifiConnected) ? (WiFi.status() == WL_CONNECTED) : bleDeviceConnected;
|
|
|
if (connected) {
|
|
|
digitalWrite(WIFI_LED_PIN, LOW);
|
|
|
return;
|
|
|
@@ -532,7 +533,7 @@ void mqttCallback(char* topic, byte* payload, unsigned int length) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void connectWiFi() {
|
|
|
+bool connectWiFi() {
|
|
|
Serial.print("Connecting WiFi");
|
|
|
WiFi.mode(WIFI_STA);
|
|
|
WiFi.begin(cfgWifiSsid.c_str(), cfgWifiPass.c_str());
|
|
|
@@ -543,21 +544,21 @@ void connectWiFi() {
|
|
|
while (WiFi.status() != WL_CONNECTED && attempts < 60) {
|
|
|
delay(5); setOnly(100, 100, 100); updateStatusLed(); Serial.print("."); attempts++;
|
|
|
}
|
|
|
- if (WiFi.status() == WL_CONNECTED) break;
|
|
|
+ if (WiFi.status() == WL_CONNECTED) {
|
|
|
+ Serial.printf("\nWiFi OK. IP: %s\n", WiFi.localIP().toString().c_str());
|
|
|
+ digitalWrite(WIFI_LED_PIN, LOW);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
Serial.println("\nFailed, retrying..."); delay(2000);
|
|
|
}
|
|
|
|
|
|
- if (WiFi.status() != WL_CONNECTED) {
|
|
|
- Serial.println("\nWiFi failed. Restarting...");
|
|
|
- delay(1000);
|
|
|
- ESP.restart();
|
|
|
- }
|
|
|
-
|
|
|
- Serial.printf("\nWiFi OK. IP: %s\n", WiFi.localIP().toString().c_str());
|
|
|
- digitalWrite(WIFI_LED_PIN, LOW);
|
|
|
+ Serial.println("\nWiFi failed. Turning off WiFi.");
|
|
|
+ WiFi.disconnect(true);
|
|
|
+ WiFi.mode(WIFI_OFF);
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
-void connectMQTT() {
|
|
|
+bool connectMQTT() {
|
|
|
mqttClient.setServer(cfgMqttBroker.c_str(), cfgMqttPort);
|
|
|
mqttClient.setCallback(mqttCallback);
|
|
|
Serial.print("MQTT connecting");
|
|
|
@@ -567,11 +568,12 @@ void connectMQTT() {
|
|
|
Serial.println("\nMQTT OK.");
|
|
|
mqttClient.subscribe(cfgMqttTopic.c_str());
|
|
|
allOff(); breathingGreen(3); publishStatus();
|
|
|
- return;
|
|
|
+ return true;
|
|
|
}
|
|
|
Serial.print("."); delay(500);
|
|
|
- if (++attempts > 60) { Serial.println("\nMQTT failed!"); attempts = 0; }
|
|
|
+ if (++attempts > 60) { Serial.println("\nMQTT failed!"); return false; }
|
|
|
}
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
void checkMQTTConnection() {
|
|
|
@@ -609,17 +611,21 @@ void setup() {
|
|
|
modeStart = millis();
|
|
|
|
|
|
if (useMQTT && isConfigComplete()) {
|
|
|
- connectWiFi();
|
|
|
- connectMQTT();
|
|
|
- } else if (useMQTT) {
|
|
|
- Serial.println("Config incomplete. Use BLE to configure.");
|
|
|
+ wifiConnected = connectWiFi();
|
|
|
+ if (wifiConnected) {
|
|
|
+ connectMQTT();
|
|
|
+ setMode("traffic");
|
|
|
+ Serial.println("WiFi/MQTT mode. Long press BOOT (3s) to switch.");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Serial.println("WiFi failed. Entering BLE config mode.");
|
|
|
}
|
|
|
|
|
|
setupBLE();
|
|
|
delay(100);
|
|
|
|
|
|
setMode("traffic");
|
|
|
- Serial.println("Long press BOOT (3s) to switch mode.");
|
|
|
+ Serial.println("BLE config mode. Long press BOOT (3s) to switch.");
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -631,9 +637,18 @@ void loop() {
|
|
|
updateStatusLed();
|
|
|
checkBootButton();
|
|
|
|
|
|
- if (useMQTT) {
|
|
|
- checkMQTTConnection();
|
|
|
- mqttClient.loop();
|
|
|
+ if (useMQTT && wifiConnected) {
|
|
|
+ if (WiFi.status() != WL_CONNECTED) {
|
|
|
+ Serial.println("WiFi lost, reconnecting...");
|
|
|
+ wifiConnected = connectWiFi();
|
|
|
+ if (!wifiConnected) {
|
|
|
+ Serial.println("WiFi failed. BLE config mode active.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (wifiConnected) {
|
|
|
+ checkMQTTConnection();
|
|
|
+ mqttClient.loop();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
autoTimeoutCheck();
|