Skip to content

smartConfig support #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions libraries/WiFi/examples/WiFiSmartConfig/WiFiSmartConfig.ino
Original file line number Diff line number Diff line change
@@ -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:

}
62 changes: 62 additions & 0 deletions libraries/WiFi/src/WiFiSTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -498,3 +499,64 @@ IPv6Address WiFiSTAClass::localIPv6()
}
return IPv6Address(addr.addr);
}


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not forget to add #include <esp_smartconfig.h> up on top

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i do not see _smartConfigDone being set to true anywhere? Maybe missing from the callback?

}

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();
}
}
10 changes: 10 additions & 0 deletions libraries/WiFi/src/WiFiSTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ class WiFiSTAClass
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);

};


Expand Down