From 81eb809d4f1271f9869d835d39c4812824e6915c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Sat, 26 May 2018 15:24:16 +0200 Subject: [PATCH] Support for Smart Config via JustWifi --- code/espurna/button.ino | 9 ++++--- code/espurna/config/defaults.h | 2 +- code/espurna/config/types.h | 14 +++++----- code/espurna/led.ino | 6 ++--- code/espurna/wifi.ino | 47 +++++++++++++++++++++++++++++----- code/platformio.ini | 2 +- 6 files changed, 60 insertions(+), 20 deletions(-) diff --git a/code/espurna/button.ino b/code/espurna/button.ino index 5e93ba32..ea550e1f 100644 --- a/code/espurna/button.ino +++ b/code/espurna/button.ino @@ -117,9 +117,12 @@ void buttonEvent(unsigned int id, unsigned char event) { } } if (action == BUTTON_MODE_AP) wifiStartAP(); - #if !defined(JUSTWIFI_DISABLE_WPS) - if (action == BUTTON_MODE_WPS) wifiStartWPS(); - #endif + #if defined(JUSTWIFI_ENABLE_WPS) + if (action == BUTTON_MODE_WPS) wifiStartWPS(); + #endif // defined(JUSTWIFI_ENABLE_WPS) + #if defined(JUSTWIFI_ENABLE_SMARTCONFIG) + if (action == BUTTON_MODE_SMART_CONFIG) wifiStartSmartConfig(); + #endif // defined(JUSTWIFI_ENABLE_SMARTCONFIG) if (action == BUTTON_MODE_RESET) { deferredReset(100, CUSTOM_RESET_HARDWARE); } diff --git a/code/espurna/config/defaults.h b/code/espurna/config/defaults.h index 5e4a85be..cae794af 100644 --- a/code/espurna/config/defaults.h +++ b/code/espurna/config/defaults.h @@ -109,7 +109,7 @@ #endif #ifndef BUTTON1_TRIPLECLICK -#define BUTTON1_TRIPLECLICK BUTTON_MODE_WPS +#define BUTTON1_TRIPLECLICK BUTTON_MODE_SMART_CONFIG #endif #ifndef BUTTON2_TRIPLECLICK #define BUTTON2_TRIPLECLICK BUTTON_MODE_NONE diff --git a/code/espurna/config/types.h b/code/espurna/config/types.h index dbd0b23f..45d89766 100644 --- a/code/espurna/config/types.h +++ b/code/espurna/config/types.h @@ -7,13 +7,14 @@ // WIFI // ----------------------------------------------------------------------------- -#define WIFI_STATE_AP 1 -#define WIFI_STATE_STA 2 -#define WIFI_STATE_AP_STA 3 -#define WIFI_STATE_WPS 4 +#define WIFI_STATE_AP 1 +#define WIFI_STATE_STA 2 +#define WIFI_STATE_AP_STA 3 +#define WIFI_STATE_WPS 4 +#define WIFI_STATE_SMARTCONFIG 8 -#define WIFI_AP_ALLWAYS 1 -#define WIFI_AP_FALLBACK 2 +#define WIFI_AP_ALLWAYS 1 +#define WIFI_AP_FALLBACK 2 //------------------------------------------------------------------------------ // BUTTONS @@ -37,6 +38,7 @@ #define BUTTON_MODE_PULSE 6 #define BUTTON_MODE_FACTORY 7 #define BUTTON_MODE_WPS 8 +#define BUTTON_MODE_SMART_CONFIG 9 // Needed for ESP8285 boards under Windows using PlatformIO (?) #ifndef BUTTON_PUSHBUTTON diff --git a/code/espurna/led.ino b/code/espurna/led.ino index 30b6aaa6..06fac792 100644 --- a/code/espurna/led.ino +++ b/code/espurna/led.ino @@ -188,7 +188,7 @@ void ledLoop() { if (_ledMode(i) == LED_MODE_WIFI) { - if (wifi_state & WIFI_STATE_WPS) { + if (wifi_state & WIFI_STATE_WPS || wifi_state & WIFI_STATE_SMARTCONFIG) { _ledBlink(i, 100, 100); } else if (wifi_state & WIFI_STATE_STA) { _ledBlink(i, 4900, 100); @@ -202,7 +202,7 @@ void ledLoop() { if (_ledMode(i) == LED_MODE_FINDME_WIFI) { - if (wifi_state & WIFI_STATE_WPS) { + if (wifi_state & WIFI_STATE_WPS || wifi_state & WIFI_STATE_SMARTCONFIG) { _ledBlink(i, 100, 100); } else if (wifi_state & WIFI_STATE_STA) { if (relayStatus(_leds[i].relay-1)) { @@ -224,7 +224,7 @@ void ledLoop() { if (_ledMode(i) == LED_MODE_RELAY_WIFI) { - if (wifi_state & WIFI_STATE_WPS) { + if (wifi_state & WIFI_STATE_WPS || wifi_state & WIFI_STATE_SMARTCONFIG) { _ledBlink(i, 100, 100); } else if (wifi_state & WIFI_STATE_STA) { if (relayStatus(_leds[i].relay-1)) { diff --git a/code/espurna/wifi.ino b/code/espurna/wifi.ino index c1d9639d..cb34ca38 100644 --- a/code/espurna/wifi.ino +++ b/code/espurna/wifi.ino @@ -11,6 +11,7 @@ Copyright (C) 2016-2018 by Xose PĂ©rez uint32_t _wifi_scan_client_id = 0; bool _wifi_wps_running = false; +bool _wifi_smartconfig_running = false; uint8_t _wifi_ap_mode = WIFI_AP_FALLBACK; // ----------------------------------------------------------------------------- @@ -218,11 +219,16 @@ void _wifiCallback(justwifi_messages_t code, char * parameter) { _wifi_wps_running = true; } - if (MESSAGE_WPS_ERROR == code) { + if (MESSAGE_SMARTCONFIG_START == code) { + _wifi_smartconfig_running = true; + } + + if (MESSAGE_WPS_ERROR == code || MESSAGE_SMARTCONFIG_ERROR == code) { _wifi_wps_running = false; + _wifi_smartconfig_running = false; } - if (MESSAGE_WPS_SUCCESS == code) { + if (MESSAGE_WPS_SUCCESS == code || MESSAGE_SMARTCONFIG_SUCCESS == code) { String ssid = WiFi.SSID(); String pass = WiFi.psk(); @@ -242,6 +248,7 @@ void _wifiCallback(justwifi_messages_t code, char * parameter) { setSetting("pass", count, pass); _wifi_wps_running = false; + _wifi_smartconfig_running = false; } @@ -350,6 +357,20 @@ void _wifiDebugCallback(justwifi_messages_t code, char * parameter) { DEBUG_MSG_P(PSTR("[WIFI] WPS failed\n")); } + // ------------------------------------------------------------------------ + + if (code == MESSAGE_SMARTCONFIG_START) { + DEBUG_MSG_P(PSTR("[WIFI] Smart Config started\n")); + } + + if (code == MESSAGE_SMARTCONFIG_SUCCESS) { + DEBUG_MSG_P(PSTR("[WIFI] Smart Config succeded!\n")); + } + + if (code == MESSAGE_SMARTCONFIG_ERROR) { + DEBUG_MSG_P(PSTR("[WIFI] Smart Config failed\n")); + } + } #endif // DEBUG_SUPPORT @@ -373,12 +394,19 @@ void _wifiInitCommands() { DEBUG_MSG_P(PSTR("+OK\n")); }); - #if !defined(JUSTWIFI_DISABLE_WPS) + #if defined(JUSTWIFI_ENABLE_WPS) settingsRegisterCommand(F("WIFI.WPS"), [](Embedis* e) { wifiStartWPS(); DEBUG_MSG_P(PSTR("+OK\n")); }); - #endif + #endif // defined(JUSTWIFI_ENABLE_WPS) + + #if defined(JUSTWIFI_ENABLE_SMARTCONFIG) + settingsRegisterCommand(F("WIFI.SMARTCONFIG"), [](Embedis* e) { + wifiStartSmartConfig(); + DEBUG_MSG_P(PSTR("+OK\n")); + }); + #endif // defined(JUSTWIFI_ENABLE_SMARTCONFIG) settingsRegisterCommand(F("WIFI.SCAN"), [](Embedis* e) { _wifiScan(); @@ -520,11 +548,17 @@ void wifiStartAP() { wifiStartAP(true); } -#if !defined(JUSTWIFI_DISABLE_WPS) +#if defined(JUSTWIFI_ENABLE_WPS) void wifiStartWPS() { jw.startWPS(); } -#endif +#endif // defined(JUSTWIFI_ENABLE_WPS) + +#if defined(JUSTWIFI_ENABLE_SMARTCONFIG) +void wifiStartSmartConfig() { + jw.startSmartConfig(); +} +#endif // defined(JUSTWIFI_ENABLE_SMARTCONFIG) void wifiReconnectCheck() { bool connected = false; @@ -542,6 +576,7 @@ uint8_t wifiState() { if (jw.connected()) state += WIFI_STATE_STA; if (jw.connectable()) state += WIFI_STATE_AP; if (_wifi_wps_running) state += WIFI_STATE_WPS; + if (_wifi_smartconfig_running) state += WIFI_STATE_SMARTCONFIG; return state; } diff --git a/code/platformio.ini b/code/platformio.ini index 5170c567..b1445be6 100644 --- a/code/platformio.ini +++ b/code/platformio.ini @@ -35,7 +35,7 @@ debug_flags = -DDEBUG_ESP_CORE -DDEBUG_ESP_SSL -DDEBUG_ESP_WIFI -DDEBUG_ESP_HTTP # -DPIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY v2 Lower Memory # -DPIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH v2 Higher Bandwidth # ------------------------------------------------------------------------------ -build_flags = -g -DMQTT_MAX_PACKET_SIZE=400 ${env.ESPURNA_FLAGS} -DJUSTWIFI_DISABLE_WPS -DPIO_FRAMEWORK_ARDUINO_LWIP_HIGHER_BANDWIDTH +build_flags = -g -DMQTT_MAX_PACKET_SIZE=400 -DJUSTWIFI_ENABLE_SMARTCONFIG ${env.ESPURNA_FLAGS} -DPIO_FRAMEWORK_ARDUINO_LWIP_HIGHER_BANDWIDTH build_flags_512k = ${common.build_flags} -Wl,-Tesp8266.flash.512k0.ld build_flags_1m = ${common.build_flags} -Wl,-Tesp8266.flash.1m0.ld