From 02664bd429e4cc877b1b2178568ab660a3e0e6f7 Mon Sep 17 00:00:00 2001 From: TheHolyRoger Date: Mon, 12 Sep 2022 19:54:42 +0100 Subject: [PATCH] Add functionality to push all switchbots with ESP32 BOOT button --- .../SwitchBot-BLE2MQTT-ESP32.ino | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/Arduino IDE Files/SwitchBot-BLE2MQTT-ESP32.ino b/Arduino IDE Files/SwitchBot-BLE2MQTT-ESP32.ino index 566e3ad..e093d20 100644 --- a/Arduino IDE Files/SwitchBot-BLE2MQTT-ESP32.ino +++ b/Arduino IDE Files/SwitchBot-BLE2MQTT-ESP32.ino @@ -448,6 +448,12 @@ static std::map allBotTypes = { // OPTIONAL - (DEF /*{ "switchbotone", 15 }, { "switchbottwo", 1}*/ }; + + //Add bots to be controlled using ESP32 BOOT button + static std::map botsControlledByESPButton = { + /*{ "switchbotone", true }, + { "switchbottwo", true}*/ + }; /********************************************/ @@ -460,6 +466,7 @@ static std::map allBotTypes = { // OPTIONAL - (DEF #define LED_BUILTIN 2 // If your board doesn't have a defined LED_BUILTIN, replace 2 with the LED pin value #endif static const bool ledHighEqualsON = true; // ESP32 board LED ON=HIGH (Default). If your ESP32 LED is turning OFF on scanning and turning ON while IDLE, then set this value to false +static const bool ledOnESPButtonPress = true; // Turn on LED during initial boot scan static const bool ledOnBootScan = true; // Turn on LED during initial boot scan static const bool ledOnScan = true; // Turn on LED while scanning (non-boot) static const bool ledOnCommand = true; // Turn on LED while MQTT command is processing. If scanning, LED will blink after scan completes. You may not notice it, there is no delay after scan @@ -773,6 +780,7 @@ static std::map deviceTypes; static NimBLEScan* pScan; static bool isRescanning = false; static bool processing = false; +static bool espButtonPressed = false; static bool initialScanComplete = false; static bool lastCommandWasBusy = false; static bool deviceHasBooted = false; @@ -4683,6 +4691,70 @@ void getAllBotSettings() { } } +void pushBotButtons() { + if (espButtonPressed) { + return; + } + espButtonPressed = true; + if (printSerialOutputForDebugging) { + Serial.println("START pushBotButtons"); + } + if (ledOnESPButtonPress) { + digitalWrite(LED_BUILTIN, ledONValue); + } + if (client.isConnected() && initialScanComplete) { + std::map::iterator itT = botsControlledByESPButton.begin(); + std::string aDevice; + while (itT != botsControlledByESPButton.end()) + { + processing = true; + aDevice = itT->first; + std::replace( aDevice.begin(), aDevice.end(), ' ', '_'); + bool shouldCtrl = itT->second; + std::map::iterator itN = allBots.find(aDevice); + if (shouldCtrl && itN != allBots.end()) { + std::string anAddr = itN->second; + std::map::iterator itP = botsInPressMode.find(anAddr); + if (itP != botsInPressMode.end()) { + std::map::iterator itE = botsSimulateONOFFinPRESSmode.find(aDevice); + std::string cmdPayload = "PRESS"; + if (itE != botsSimulateONOFFinPRESSmode.end()) { + std::map::iterator itF = botsSimulatedStates.find(aDevice); + if (itF != botsSimulatedStates.end()) + { + bool boolState = itF->second; + if (boolState) { + cmdPayload = "OFF"; + } + else if (!boolState) { + cmdPayload = "ON"; + } + } + } + struct QueueCommand queueCommand; + queueCommand.payload = cmdPayload; + queueCommand.topic = ESPMQTTTopic + "/control"; + queueCommand.device = aDevice; + queueCommand.disconnectAfter = true; + queueCommand.priority = false; + queueCommand.currentTry = 1; + commandQueue.enqueue(queueCommand); + } + } + processing = false; + itT++; + } + } + delay(500); + if (ledOnESPButtonPress) { + digitalWrite(LED_BUILTIN, ledOFFValue); + } + if (printSerialOutputForDebugging) { + Serial.println("END pushBotButtons"); + } + espButtonPressed = false; +} + unsigned long retainStartTime = 0; bool waitForRetained = true; unsigned long lastWebServerReboot = 0; @@ -4738,6 +4810,9 @@ void loop () { if (isRescanning) { lastRescan = millis(); } + if (!processing && !espButtonPressed && digitalRead(0) == LOW) { + pushBotButtons(); + } if ((!waitForResponse) && (!processing) && (!(pScan->isScanning())) && (!isRescanning)) { if (getSettingsOnBoot && !gotSettings ) { getAllBotSettings();