Add smartConfig support (#136)
* smartConfig support * fixed bug, added example * added _smartConfigDone = ture * changed example name, added explanation
This commit is contained in:
parent
78f2c6f31e
commit
57dbc9ab77
36
libraries/WiFi/examples/WiFiSmartConfig/WiFiSmartConfig.ino
Normal file
36
libraries/WiFi/examples/WiFiSmartConfig/WiFiSmartConfig.ino
Normal file
@ -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:
|
||||
|
||||
}
|
@ -40,6 +40,7 @@ extern "C" {
|
||||
#include <lwip/ip_addr.h>
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/dns.h"
|
||||
#include <esp_smartconfig.h>
|
||||
}
|
||||
|
||||
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<sc_callback_t>(&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<wifi_sta_config_t *>(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();
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user