Browse Source

Websocket callbacks for OTA and WIFI

fastled
Xose Pérez 7 years ago
parent
commit
2b029dc089
3 changed files with 55 additions and 33 deletions
  1. +7
    -2
      code/espurna/ota.ino
  2. +42
    -0
      code/espurna/wifi.ino
  3. +6
    -31
      code/espurna/ws.ino

+ 7
- 2
code/espurna/ota.ino View File

@ -12,15 +12,20 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
// OTA // OTA
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void otaConfigure() {
void _otaConfigure() {
ArduinoOTA.setPort(OTA_PORT); ArduinoOTA.setPort(OTA_PORT);
ArduinoOTA.setHostname(getSetting("hostname").c_str()); ArduinoOTA.setHostname(getSetting("hostname").c_str());
ArduinoOTA.setPassword(getSetting("adminPass", ADMIN_PASS).c_str()); ArduinoOTA.setPassword(getSetting("adminPass", ADMIN_PASS).c_str());
} }
// -----------------------------------------------------------------------------
void otaSetup() { void otaSetup() {
otaConfigure();
_otaConfigure();
#if WEB_SUPPORT
wsOnAfterParseRegister(_otaConfigure);
#endif
ArduinoOTA.onStart([]() { ArduinoOTA.onStart([]() {
DEBUG_MSG_P(PSTR("[OTA] Start\n")); DEBUG_MSG_P(PSTR("[OTA] Start\n"));


+ 42
- 0
code/espurna/wifi.ino View File

@ -143,6 +143,47 @@ void wifiStatus() {
} }
bool wifiClean(unsigned char num) {
bool changed = false;
int i = 0;
// Clean defined settings
while (i < num) {
// Skip on first non-defined setting
if (!hasSetting("ssid", i)) {
delSetting("ssid", i);
break;
}
// Delete empty values
if (!hasSetting("pass", i)) delSetting("pass", i);
if (!hasSetting("ip", i)) delSetting("ip", i);
if (!hasSetting("gw", i)) delSetting("gw", i);
if (!hasSetting("mask", i)) delSetting("mask", i);
if (!hasSetting("dns", i)) delSetting("dns", i);
++i;
}
// Delete all other settings
while (i < WIFI_MAX_NETWORKS) {
changed = hasSetting("ssid", i);
delSetting("ssid", i);
delSetting("pass", i);
delSetting("ip", i);
delSetting("gw", i);
delSetting("mask", i);
delSetting("dns", i);
++i;
}
return changed;
}
// Inject hardcoded networks // Inject hardcoded networks
void wifiInject() { void wifiInject() {
@ -262,6 +303,7 @@ void wifiSetup() {
#if WEB_SUPPORT #if WEB_SUPPORT
wsOnSendRegister(_wifiWebSocketOnSend); wsOnSendRegister(_wifiWebSocketOnSend);
wsOnAfterParseRegister(wifiConfigure);
#endif #endif
} }


+ 6
- 31
code/espurna/ws.ino View File

@ -214,7 +214,6 @@ void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) {
} }
if (value != getSetting(key)) { if (value != getSetting(key)) {
//DEBUG_MSG_P(PSTR("[WEBSOCKET] Storing %s = %s\n", key.c_str(), value.c_str()));
setSetting(key, value); setSetting(key, value);
save = changed = true; save = changed = true;
if (key.startsWith("mqtt")) changedMQTT = true; if (key.startsWith("mqtt")) changedMQTT = true;
@ -223,34 +222,7 @@ void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) {
} }
if (webMode == WEB_MODE_NORMAL) { if (webMode == WEB_MODE_NORMAL) {
// Clean wifi networks
int i = 0;
while (i < network) {
if (!hasSetting("ssid", i)) {
delSetting("ssid", i);
break;
}
if (!hasSetting("pass", i)) delSetting("pass", i);
if (!hasSetting("ip", i)) delSetting("ip", i);
if (!hasSetting("gw", i)) delSetting("gw", i);
if (!hasSetting("mask", i)) delSetting("mask", i);
if (!hasSetting("dns", i)) delSetting("dns", i);
++i;
}
while (i < WIFI_MAX_NETWORKS) {
if (hasSetting("ssid", i)) {
save = changed = true;
}
delSetting("ssid", i);
delSetting("pass", i);
delSetting("ip", i);
delSetting("gw", i);
delSetting("mask", i);
delSetting("dns", i);
++i;
}
if (wifiClean(network)) save = changed = true;
} }
// Save settings // Save settings
@ -261,16 +233,19 @@ void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) {
(_ws_on_after_parse_callbacks[i])(); (_ws_on_after_parse_callbacks[i])();
} }
wifiConfigure();
otaConfigure();
// This should got to callback as well
// but first change management has to be in place
if (changedMQTT) { if (changedMQTT) {
mqttConfigure(); mqttConfigure();
mqttDisconnect(); mqttDisconnect();
} }
// Persist settings
saveSettings(); saveSettings();
} }
if (changed) { if (changed) {
wsSend_P(client_id, PSTR("{\"message\": 8}")); wsSend_P(client_id, PSTR("{\"message\": 8}"));
} else { } else {


Loading…
Cancel
Save