diff --git a/platformio/floower/src/connect/CommandProtocol.cpp b/platformio/floower/src/connect/CommandProtocol.cpp index 551e9f6..b73efa1 100644 --- a/platformio/floower/src/connect/CommandProtocol.cpp +++ b/platformio/floower/src/connect/CommandProtocol.cpp @@ -28,8 +28,10 @@ uint16_t CommandProtocol::run(const uint16_t type, const char *payload, const ui if (jsonPayload.containsKey("t")) { time = jsonPayload["t"]; } - floower->setPetalsOpenLevel(level, time); - fireControlCommandCallback(); + if (level >= 0 && level <= 100) { + floower->setPetalsOpenLevel(level, time); + fireControlCommandCallback(); + } } return STATUS_OK; } @@ -69,7 +71,7 @@ uint16_t CommandProtocol::run(const uint16_t type, const char *payload, const ui )); transitionColor = true; } - if (level != -1) { + if (level >= 0 && level <= 100) { floower->setPetalsOpenLevel(level, time); } if (transitionColor) { diff --git a/platformio/floower/src/connect/WifiConnect.cpp b/platformio/floower/src/connect/WifiConnect.cpp index d2bc912..b18b4f1 100644 --- a/platformio/floower/src/connect/WifiConnect.cpp +++ b/platformio/floower/src/connect/WifiConnect.cpp @@ -54,9 +54,9 @@ void WifiConnect::enable() { if (!config->wifiSsid.isEmpty()) { WiFi.mode(WIFI_STA); - WiFi.onEvent([=](WiFiEvent_t event, WiFiEventInfo_t info){ onWifiConnected(event, info); }, SYSTEM_EVENT_STA_CONNECTED); - WiFi.onEvent([=](WiFiEvent_t event, WiFiEventInfo_t info){ onWifiGotIp(event, info); }, SYSTEM_EVENT_STA_GOT_IP); - WiFi.onEvent([=](WiFiEvent_t event, WiFiEventInfo_t info){ onWifiDisconnected(event, info); }, SYSTEM_EVENT_STA_DISCONNECTED); + wifiConnectedEventId = WiFi.onEvent([=](WiFiEvent_t event, WiFiEventInfo_t info){ onWifiConnected(event, info); }, SYSTEM_EVENT_STA_CONNECTED); + wifiGotIpEventId = WiFi.onEvent([=](WiFiEvent_t event, WiFiEventInfo_t info){ onWifiGotIp(event, info); }, SYSTEM_EVENT_STA_GOT_IP); + wifiDisconnectedEventId = WiFi.onEvent([=](WiFiEvent_t event, WiFiEventInfo_t info){ onWifiDisconnected(event, info); }, SYSTEM_EVENT_STA_DISCONNECTED); WiFi.begin(config->wifiSsid.c_str(), config->wifiPassword.c_str()); ESP_LOGI(LOG_TAG, "WiFi on: %s", config->wifiSsid.c_str()); @@ -73,9 +73,9 @@ void WifiConnect::disable() { // TODO: disconnect the socket WiFi.disconnect(true); // turn off wifi - WiFi.removeEvent(SYSTEM_EVENT_STA_CONNECTED); - WiFi.removeEvent(SYSTEM_EVENT_STA_GOT_IP); - WiFi.removeEvent(SYSTEM_EVENT_STA_DISCONNECTED); + WiFi.removeEvent(wifiConnectedEventId); + WiFi.removeEvent(wifiGotIpEventId); + WiFi.removeEvent(wifiDisconnectedEventId); esp_wifi_stop(); ESP_LOGI(LOG_TAG, "Wifi off"); @@ -94,9 +94,14 @@ bool WifiConnect::isConnected() { void WifiConnect::reconnect() { if (enabled) { if (wifiOn) { - wifiFailed = false; - WiFi.begin(config->wifiSsid.c_str(), config->wifiPassword.c_str()); - ESP_LOGI(LOG_TAG, "WiFi reconnecting: %s", config->wifiSsid); + if (!config->wifiSsid.isEmpty()) { + wifiFailed = false; + WiFi.begin(config->wifiSsid.c_str(), config->wifiPassword.c_str()); + ESP_LOGI(LOG_TAG, "WiFi reconnecting: %s", config->wifiSsid); + } + else { + disable(); + } } else { enable(); @@ -360,11 +365,13 @@ void WifiConnect::onWifiDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) { ESP_LOGI(LOG_TAG, "Failed to connect WiFi"); reconnectTime = millis() + CONNECT_RETRY_INTERVAL_MS; wifiFailed = true; + WiFi.disconnect(true); // turn off wifi } else { ESP_LOGI(LOG_TAG, "Wifi lost: %d", info.disconnected.reason); reconnectTime = millis() + RECONNECT_INTERVAL_MS; wifiConnected = false; + WiFi.disconnect(true); // turn off wifi } } diff --git a/platformio/floower/src/connect/WifiConnect.h b/platformio/floower/src/connect/WifiConnect.h index dc96640..9bd6758 100644 --- a/platformio/floower/src/connect/WifiConnect.h +++ b/platformio/floower/src/connect/WifiConnect.h @@ -64,6 +64,9 @@ class WifiConnect { bool wifiOn = false; bool wifiConnected = false; bool wifiFailed = false; + wifi_event_id_t wifiConnectedEventId; + wifi_event_id_t wifiGotIpEventId; + wifi_event_id_t wifiDisconnectedEventId; uint8_t mode; uint8_t state; diff --git a/platformio/floower/src/main.cpp b/platformio/floower/src/main.cpp index ac76219..79387f8 100644 --- a/platformio/floower/src/main.cpp +++ b/platformio/floower/src/main.cpp @@ -1,4 +1,4 @@ -#define FIRMWARE_VERSION 10 +#define FIRMWARE_VERSION 11 #define HARDWARE_REVISION 9 // Floower revision 9+ is using stepper motor instead of servo and has a sensor platform available #include