Skip to content

Commit 57dbc9a

Browse files
seopyoonme-no-dev
authored andcommitted
Add smartConfig support (espressif#136)
* smartConfig support * fixed bug, added example * added _smartConfigDone = ture * changed example name, added explanation
1 parent 78f2c6f commit 57dbc9a

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "WiFi.h"
2+
3+
void setup() {
4+
Serial.begin(115200);
5+
6+
//Init WiFi as Station, start SmartConfig
7+
WiFi.mode(WIFI_AP_STA);
8+
WiFi.beginSmartConfig();
9+
10+
//Wait for SmartConfig packet from mobile
11+
Serial.println("Waiting for SmartConfig.");
12+
while (!WiFi.smartConfigDone()) {
13+
delay(500);
14+
Serial.print(".");
15+
}
16+
17+
Serial.println("");
18+
Serial.println("SmartConfig received.");
19+
20+
//Wait for WiFi to connect to AP
21+
Serial.println("Waiting for WiFi");
22+
while (WiFi.status() != WL_CONNECTED) {
23+
delay(500);
24+
Serial.print(".");
25+
}
26+
27+
Serial.println("WiFi Connected.");
28+
29+
Serial.print("IP Address: ");
30+
Serial.println(WiFi.localIP());
31+
}
32+
33+
void loop() {
34+
// put your main code here, to run repeatedly:
35+
36+
}

libraries/WiFi/src/WiFiSTA.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ extern "C" {
4040
#include <lwip/ip_addr.h>
4141
#include "lwip/err.h"
4242
#include "lwip/dns.h"
43+
#include <esp_smartconfig.h>
4344
}
4445

4546
extern "C" void esp_schedule();
@@ -498,3 +499,64 @@ IPv6Address WiFiSTAClass::localIPv6()
498499
}
499500
return IPv6Address(addr.addr);
500501
}
502+
503+
504+
bool WiFiSTAClass::_smartConfigStarted = false;
505+
bool WiFiSTAClass::_smartConfigDone = false;
506+
507+
508+
bool WiFiSTAClass::beginSmartConfig() {
509+
if (_smartConfigStarted) {
510+
return false;
511+
}
512+
513+
if (!WiFi.mode(WIFI_STA)) {
514+
return false;
515+
}
516+
517+
518+
esp_err_t err;
519+
err = esp_smartconfig_start(reinterpret_cast<sc_callback_t>(&WiFiSTAClass::_smartConfigCallback), 1);
520+
if (err == ESP_OK) {
521+
_smartConfigStarted = true;
522+
_smartConfigDone = false;
523+
return true;
524+
}
525+
return false;
526+
}
527+
528+
bool WiFiSTAClass::stopSmartConfig() {
529+
if (!_smartConfigStarted) {
530+
return true;
531+
}
532+
533+
if (esp_smartconfig_stop() == ESP_OK) {
534+
_smartConfigStarted = false;
535+
return true;
536+
}
537+
538+
return false;
539+
}
540+
541+
bool WiFiSTAClass::smartConfigDone() {
542+
if (!_smartConfigStarted) {
543+
return false;
544+
}
545+
546+
return _smartConfigDone;
547+
}
548+
549+
void WiFiSTAClass::_smartConfigCallback(uint32_t st, void* result) {
550+
smartconfig_status_t status = (smartconfig_status_t) st;
551+
if (status == SC_STATUS_LINK) {
552+
wifi_sta_config_t *sta_conf = reinterpret_cast<wifi_sta_config_t *>(result);
553+
554+
esp_wifi_set_config(WIFI_IF_AP, (wifi_config_t *)sta_conf);
555+
esp_wifi_disconnect();
556+
esp_wifi_connect();
557+
558+
_smartConfigDone = true;
559+
} else if (status == SC_STATUS_LINK_OVER) {
560+
WiFi.stopSmartConfig();
561+
}
562+
}

libraries/WiFi/src/WiFiSTA.h

+10
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ class WiFiSTAClass
8585
static wl_status_t _status;
8686
static bool _useStaticIp;
8787

88+
public:
89+
bool beginSmartConfig();
90+
bool stopSmartConfig();
91+
bool smartConfigDone();
92+
93+
protected:
94+
static bool _smartConfigStarted;
95+
static bool _smartConfigDone;
96+
static void _smartConfigCallback(uint32_t status, void* result);
97+
8898
};
8999

90100

0 commit comments

Comments
 (0)