Demo 11: How to use SmartConfig on Arduino ESP32

1. Introduction
In previous demos, we use WiFi class to connect to a WiFi network "WiFi.begin(ssid, password)". Here we use "hard-coded" ssid and password. It is really inconvenient when we bring our ESP32 to another WiFi network, we have to modify ssid and password according to new network, recompile and flash new code. So there is a technique to overcome this called "SmartConfig" which was invented by TI. You can refer it here:
https://community.particle.io/t/smart-config-the-missing-manual-now-available/442
Now this technique was also applied for ESP32. In order to do SmartConfig, you need a smartphone or tablet (Android or iOS) that connected to WiFi network (which you want ESP32 to connect to) and installed a special application. On this application, you just supply the ssid and password of WiFi network so that the application can use, encode them and then broadcast (via UDP) encoded ssid and password (under packet format) over the air. At this moment, ESP32 with a special software in it will capture these packets, decode back ssid and password and use them to connect to Wifi network. After connecting to WiFi ESP32 will use mDNS to multicast a message to the application to notify that it connected to WiFi.
The source code of special application is supplied by Espressif. You can download at:
https://github.com/EspressifApp/EsptouchForAndroid
https://github.com/EspressifApp/EsptouchForIOS

This application is also available on App Store. You can use it to test SmartConfig feature.
- For Android, this application is available under name "IOT_Espressif" or another application "ESP8266 SmartConfig" (this is for ESP8266 but you can use it for ESP32):
https://play.google.com/store/apps/details?id=com.cmmakerclub.iot.esptouch
https://play.google.com/store/apps/details?id=com.espressif.iot
- For iOS, this application is available under name "Espressif Esptouch":
https://itunes.apple.com/us/app/espressif-esptouch/id1071176700?mt=8 
2. Hardware
You do not need any extra hardware
3. Software
Some functions that are related to SmartConfig feature, also in WiFi class:
 - WiFi.mode(WIFI_AP_STA): set ESP32 to Station mode (To run SmartConfig it must be set to Station mode)
- WiFi.beginSmartConfig(): start SmartConfig
- WiFi.smartConfigDone(): check whether SmartConfig is done or not
- Let 's make a simple demo for this feature. We use an Android smart phone which installed "ESP8266 SmartConfig" and an ESP32 with SmartConfig code.
From smartphone (connected to WiFi) do: 
Open the application -> fill ssid and password and then press Confirm button.
Figure: ESP8266 SmartConfig app with Menu 
Create an Arduino project, save as esp32smartconfig with code: 



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "WiFi.h"
void setup() {
  Serial.begin(115200);
  /* Set ESP32 to WiFi Station mode */
  WiFi.mode(WIFI_AP_STA);
  /* start SmartConfig */
  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 done.");

  /* 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() {
}
4. Result
Figure: From Terminal SmartConfig done, ESP32 join WIFi network with IP address
 Figure: From smart phone, ESP connected to WiFi network

Post a Comment

10 Comments

Holger said…
Hi,
very interesting thing, but for me, this does not work.
I always get after "SmartConfig done.":

Waiting for WiFi
..[D][WiFiGeneric.cpp:258] _eventCallback(): Event: 5 - STA_DISCONNECTED
[W][WiFiGeneric.cpp:263] _eventCallback(): Reason: 201 - AUTH_FAIL

I'm sure, the password is correct.

Do you have any ideas?

Regards, Holger
Unknown said…
Hi,
I have exactly the same "Waiting for WiFi" response as Holger Schmidt. My password is perfectly correct. I am using the ESPTouch app v0.3.5.2 under IOS version 10.3.3.
Hi all,

it is a known bug. please refer here:
https://github.com/espressif/arduino-esp32/issues/674

Regards,
Anonymous said…
whats hapend after reset ?
Anonymous said…
which WiFi.h Library did you include?

I get the notification class WiFiClass has no member named WiFi.mode. I think for the next Attributes it would be the same.
it still works in my case.
Unknown said…
Hi Team,
I have tried connect with iOS 12.4 iPhone se and 6 but its always return fail.

I used those library
https://github.com/lou-lan/SmartConfig
https://github.com/EspressifApp/EsptouchForIOS/
Getting response every time
[isSuc: NO,isCancelled: NO,bssid: (null),inetAddress: (null)]

Could you please help me what I am doing wrong.

I am using same
func executeForResult() -> ESPTouchResult {
// 同步锁
condition.lock()
// 获得配置所需要的参数
esptouchTask = ESPTouchTask(apSsid: SSID, andApBssid: BSSID, andApPwd: PASS)
// 设置代理
esptouchTask.setEsptouchDelegate(esptouchDelegate)
condition.unlock()
let esptouchResult: ESPTouchResult = self.esptouchTask.executeForResult()
print("⭕️(esptouchResult)")
return esptouchResult
}

Thanks
guyc said…
cannot compile the sketch at a esp12.

Arduino: 1.6.10 (Linux), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, 4M (no SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 57600"

In file included from /home/lutgarde/arduino1.6.10/libraries/WiFi/src/WiFi.h:32:0,
from /home/lutgarde/Arduino/test_smartconfig/test_smartconfig.ino:1:
/home/lutgarde/arduino1.6.10/libraries/WiFi/src/WiFiServer.h:37:14: error: invalid abstract return type for member function 'WiFiClient WiFiServer::available(uint8_t*)'
WiFiClient available(uint8_t* status = NULL);
^
In file included from /home/lutgarde/arduino1.6.10/libraries/WiFi/src/WiFi.h:31:0,
from /home/lutgarde/Arduino/test_smartconfig/test_smartconfig.ino:1:
/home/lutgarde/arduino1.6.10/libraries/WiFi/src/WiFiClient.h:27:7: note: because the following virtual functions are pure within 'WiFiClient':
class WiFiClient : public Client {
^
In file included from /home/lutgarde/arduino1.6.10/libraries/WiFi/src/WiFiClient.h:24:0,
from /home/lutgarde/arduino1.6.10/libraries/WiFi/src/WiFi.h:31,
from /home/lutgarde/Arduino/test_smartconfig/test_smartconfig.ino:1:
/home/lutgarde/arduino1.6.10/hardware/esp8266com/esp8266/cores/esp8266/Client.h:37:22: note: virtual bool Client::flush(unsigned int)
virtual bool flush(unsigned int maxWaitMs = 0) = 0;
^
/home/lutgarde/arduino1.6.10/hardware/esp8266com/esp8266/cores/esp8266/Client.h:38:22: note: virtual bool Client::stop(unsigned int)
virtual bool stop(unsigned int maxWaitMs = 0) = 0;
^
/home/lutgarde/Arduino/test_smartconfig/test_smartconfig.ino: In function 'void setup()':
test_smartconfig:5: error: 'class WiFiClass' has no member named 'mode'
WiFi.mode(WIFI_AP_STA);
^
test_smartconfig:5: error: 'WIFI_AP_STA' was not declared in this scope
WiFi.mode(WIFI_AP_STA);
^
test_smartconfig:7: error: 'class WiFiClass' has no member named 'beginSmartConfig'
WiFi.beginSmartConfig();
^
test_smartconfig:11: error: 'class WiFiClass' has no member named 'smartConfigDone'
while (!WiFi.smartConfigDone()) {
^
exit status 1
'class WiFiClass' has no member named 'mode'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Anonymous said…
The latest version of the provisioning app doesn't look like that.

When I tried it, the app only seems my wireless access point for my network. It doesn't see the SSID of my ESP32.
Anonymous said…
That's great. But what happens if reset? Do you have to do the config all over again? How would you save the information (the access point SSID and password) that the smart config figured out so you can connect the next time without having to do smartconig all over again?