diff --git a/libraries/WiFi/examples/WiFiSmartConfig/WiFiSmartConfig.ino b/libraries/WiFi/examples/WiFiSmartConfig/WiFiSmartConfig.ino new file mode 100644 index 00000000..4c1f1a8c --- /dev/null +++ b/libraries/WiFi/examples/WiFiSmartConfig/WiFiSmartConfig.ino @@ -0,0 +1,36 @@ +#include "WiFi.h" + +void setup() { + Serial.begin(115200); + + //Init WiFi as Station, start SmartConfig + WiFi.mode(WIFI_AP_STA); + WiFi.beginSmartConfig(); + + //Wait for SmartConfig packet from mobile + Serial.println("Waiting for SmartConfig."); + while (!WiFi.smartConfigDone()) { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.println("SmartConfig received."); + + //Wait for WiFi to connect to AP + Serial.println("Waiting for WiFi"); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println("WiFi Connected."); + + Serial.print("IP Address: "); + Serial.println(WiFi.localIP()); +} + +void loop() { + // put your main code here, to run repeatedly: + +} diff --git a/libraries/WiFi/src/WiFiSTA.cpp b/libraries/WiFi/src/WiFiSTA.cpp index c570d82c..7d84d9a7 100644 --- a/libraries/WiFi/src/WiFiSTA.cpp +++ b/libraries/WiFi/src/WiFiSTA.cpp @@ -40,6 +40,7 @@ extern "C" { #include #include "lwip/err.h" #include "lwip/dns.h" +#include } extern "C" void esp_schedule(); @@ -498,3 +499,64 @@ IPv6Address WiFiSTAClass::localIPv6() } return IPv6Address(addr.addr); } + + +bool WiFiSTAClass::_smartConfigStarted = false; +bool WiFiSTAClass::_smartConfigDone = false; + + +bool WiFiSTAClass::beginSmartConfig() { + if (_smartConfigStarted) { + return false; + } + + if (!WiFi.mode(WIFI_STA)) { + return false; + } + + + esp_err_t err; + err = esp_smartconfig_start(reinterpret_cast(&WiFiSTAClass::_smartConfigCallback), 1); + if (err == ESP_OK) { + _smartConfigStarted = true; + _smartConfigDone = false; + return true; + } + return false; +} + +bool WiFiSTAClass::stopSmartConfig() { + if (!_smartConfigStarted) { + return true; + } + + if (esp_smartconfig_stop() == ESP_OK) { + _smartConfigStarted = false; + return true; + } + + return false; +} + +bool WiFiSTAClass::smartConfigDone() { + if (!_smartConfigStarted) { + return false; + } + + return _smartConfigDone; +} + +void WiFiSTAClass::_smartConfigCallback(uint32_t st, void* result) { + smartconfig_status_t status = (smartconfig_status_t) st; + if (status == SC_STATUS_LINK) { + wifi_sta_config_t *sta_conf = reinterpret_cast(result); + + esp_wifi_set_config(WIFI_IF_AP, (wifi_config_t *)sta_conf); + esp_wifi_disconnect(); + esp_wifi_connect(); + + _smartConfigDone = true; + } else if (status == SC_STATUS_LINK_OVER) { + WiFi.stopSmartConfig(); + } +} diff --git a/libraries/WiFi/src/WiFiSTA.h b/libraries/WiFi/src/WiFiSTA.h index 5e1fcab4..28b0aa6f 100644 --- a/libraries/WiFi/src/WiFiSTA.h +++ b/libraries/WiFi/src/WiFiSTA.h @@ -85,6 +85,16 @@ protected: static wl_status_t _status; static bool _useStaticIp; +public: + bool beginSmartConfig(); + bool stopSmartConfig(); + bool smartConfigDone(); + +protected: + static bool _smartConfigStarted; + static bool _smartConfigDone; + static void _smartConfigCallback(uint32_t status, void* result); + };