Skip to content

Commit

Permalink
minor fixed to command protocol and wifi connector
Browse files Browse the repository at this point in the history
  • Loading branch information
jpraus committed Dec 2, 2021
1 parent 7612126 commit 49c2f50
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
8 changes: 5 additions & 3 deletions platformio/floower/src/connect/CommandProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down
25 changes: 16 additions & 9 deletions platformio/floower/src/connect/WifiConnect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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");
Expand All @@ -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();
Expand Down Expand Up @@ -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
}
}

Expand Down
3 changes: 3 additions & 0 deletions platformio/floower/src/connect/WifiConnect.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion platformio/floower/src/main.cpp
Original file line number Diff line number Diff line change
@@ -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 <esp_wifi.h>
Expand Down

0 comments on commit 49c2f50

Please sign in to comment.