|
|
@@ -499,20 +499,41 @@ void updateStatusLed() {
|
|
|
// =====================================================
|
|
|
|
|
|
class ServerCallbacks : public BLEServerCallbacks {
|
|
|
- void onConnect(BLEServer* s) { bleDeviceConnected = true; Serial.println("BLE connected."); }
|
|
|
- void onDisconnect(BLEServer* s) { bleDeviceConnected = false; Serial.println("BLE disconnected."); delay(100); BLEDevice::startAdvertising(); }
|
|
|
+ void onConnect(BLEServer* s) {
|
|
|
+ bleDeviceConnected = true;
|
|
|
+ Serial.println("BLE connected.");
|
|
|
+ if (currentMode == "init") setMode("traffic");
|
|
|
+ }
|
|
|
+ void onDisconnect(BLEServer* s) {
|
|
|
+ bleDeviceConnected = false;
|
|
|
+ Serial.println("BLE disconnected.");
|
|
|
+ delay(100);
|
|
|
+ BLEDevice::startAdvertising();
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
class ModeCharCallbacks : public BLECharacteristicCallbacks {
|
|
|
void onWrite(BLECharacteristic* c) {
|
|
|
String val = c->getValue();
|
|
|
+ Serial.print("BLE Mode received: ");
|
|
|
+ Serial.println(val);
|
|
|
+ if (val == "restart") {
|
|
|
+ Serial.println("Restarting...");
|
|
|
+ delay(500);
|
|
|
+ ESP.restart();
|
|
|
+ }
|
|
|
setMode(val);
|
|
|
}
|
|
|
void onRead(BLECharacteristic* c) { c->setValue(currentMode.c_str()); }
|
|
|
};
|
|
|
|
|
|
class ConfigCharCallbacks : public BLECharacteristicCallbacks {
|
|
|
- void onWrite(BLECharacteristic* c) { saveConfigFromJson(c->getValue()); }
|
|
|
+ void onWrite(BLECharacteristic* c) {
|
|
|
+ String val = c->getValue();
|
|
|
+ Serial.print("BLE Config received: ");
|
|
|
+ Serial.println(val);
|
|
|
+ saveConfigFromJson(val);
|
|
|
+ }
|
|
|
void onRead(BLECharacteristic* c) { c->setValue(getConfigJson().c_str()); }
|
|
|
};
|
|
|
|
|
|
@@ -583,7 +604,11 @@ bool connectWiFi() {
|
|
|
Serial.printf("\nWiFi %d/5", retry);
|
|
|
int attempts = 0;
|
|
|
while (WiFi.status() != WL_CONNECTED && attempts < 60) {
|
|
|
- delay(5); setOnly(100, 100, 100); updateStatusLed(); Serial.print("."); attempts++;
|
|
|
+ delay(5);
|
|
|
+ updateInit();
|
|
|
+ updateStatusLed();
|
|
|
+ Serial.print(".");
|
|
|
+ attempts++;
|
|
|
}
|
|
|
if (WiFi.status() == WL_CONNECTED) {
|
|
|
Serial.printf("\nWiFi OK. IP: %s\n", WiFi.localIP().toString().c_str());
|
|
|
@@ -641,7 +666,9 @@ void setup() {
|
|
|
pinMode(STATUS_PIN, OUTPUT);
|
|
|
pinMode(BUTTON_PIN, INPUT_PULLUP);
|
|
|
digitalWrite(STATUS_PIN, HIGH);
|
|
|
- allOff();
|
|
|
+
|
|
|
+ currentMode = "init";
|
|
|
+ modeStart = millis();
|
|
|
|
|
|
Serial.println();
|
|
|
Serial.println("=== AI-Light ===");
|
|
|
@@ -650,9 +677,6 @@ void setup() {
|
|
|
Serial.printf("MQTT: %s\n", cfgMqttBroker.length() > 0 ? cfgMqttBroker.c_str() : "(not set)");
|
|
|
Serial.printf("Pins: R=%d G=%d Y=%d\n", redPin, greenPin, yellowPin);
|
|
|
|
|
|
- currentMode = "init";
|
|
|
- modeStart = millis();
|
|
|
-
|
|
|
if (useMQTT && isConfigComplete()) {
|
|
|
wifiConnected = connectWiFi();
|
|
|
if (wifiConnected) {
|
|
|
@@ -665,10 +689,7 @@ void setup() {
|
|
|
}
|
|
|
|
|
|
setupBLE();
|
|
|
- delay(100);
|
|
|
-
|
|
|
- setMode("traffic");
|
|
|
- Serial.println("BLE config mode. Long press BOOT (3s) to switch.");
|
|
|
+ Serial.println("BLE advertising. Waiting for connection...");
|
|
|
}
|
|
|
|
|
|
|