Tutorial: Provisioning your ESP32 for WiFi. 3 Different Ways.

Tutorial: Provisioning your ESP32 for WiFi. 3 Different Ways.

Provisioning is a big deal with IOT devices.   How do you get a device with no keyboard or display to connect with your local WiFi?  In telecommunication, provisioning involves the process of preparing and equipping a network to allow it to provide new services to its users. It requires the existence of networking equipment and depends on network planning and design.  This article is a tutorial for provisioning the ESP32.

Provisioning programs can generate a lot of cash for their developers in the telecommunication industry.    A friend of our CTO, John Shovic, made over $40,000,000 by selling their software for provisioining network equipment to Nortel in the 1990’s.

In this article, we talk about how to provision WiFi device connections (getting an IP address to your device and the SSID/Password combination) to an ESP32 based system with no keyboard.   First, let’s talk about the devices we are using.

What is the BC24?

BC24The BC24 is a flexible way to display all sorts of data while being able to communicate with other devices and the Internet.   It is a board that you can plug in your sensors and create new things!

The BC24 consists of three major parts:

1) Dual CPU ESP32 Central Processor with WiFi and Bluetooth

2) The 24 RGBW Programmable LED Pixel Circle

3) No Solder Grove Connectors for expansion!

The RGBW (Red Green Blue White) Pixels are completely programmable (millions of colors) and includes a bright white LED to give you pure warm white.   These programmable pixels are 100% compatible with NeoPixels.

The Grove connectors allow you to easily and safely connect hundreds of sensors (and more Pixel strips if you want!) with no soldering.   And you CAN’T plug them in backwards.   Fewer boards in the Box of Death.

You can get the BC24 here.

What is the ESP32?ESP32

The ESP32 is an awesome processor.  It has 2 CPU cores, lots of memory and has really interesting stuff built in like capacitive touch sensors, a Digital to Analog Converter (think Music out from your BC24!), lots of GPIO (General Purpose Input Output) for your projects and a robust I2C bus for connecting to your sensors.

Programmable with the Arduino IDE.

 

Three Methods of Provisioning WiFi IP Addresses to your ESP32

This software library is designed to provide a flexible way of provisioning WiFi connections in a system and router flexible way.

It consists of three different methods:
– SmartConfig
– WPS Button on AP
– Local AP on ESP32 (192.168.4.1)

This software library is designed to provide a flexible way of provisioning WiFiconnections in a system and router flexible way.

Note: This was developed for the BC24 ESP32 Based 24 RGBW Pixel LED Board

#1 SmartConfig

This method uses the TI method of provisioning, called SmartConfig.

Here is a good reference to SmartConfig:
https://community.particle.io/t/smart-config-the-missing-manual-now-available/442

In order to do SmartConfig, you need a smartphone or tablet (Android or iOS) that is connected to your WiFi network (which you want ESP32 to connect to) and install 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.  When this software is being run, the ESP32 (with the SmartConfig software loaded) will capture these packets, decode the ssid and password and use them to connect to Wifi network.

After connecting to WiFi, the ESP32 will use mDNS to multicast a message to the application to notify that it has successfully connected to WiFi.

The source code of special application implementing the SmartConfig  is supplied by Espressif. You can download  it at:
https://github.com/EspressifApp/EsptouchForAndroid
https://github.com/EspressifApp/EsptouchForIOS


This application is also available on App Store for both Android and iOS devices.

– 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 WPS Button on AP

Wi-Fi Protected Setup (WPS; originally, Wi-Fi Simple Config) is a network security standard to create a secure wireless home network.

Introduced in 2006, the goal of the protocol is to allow home users who know little of wireless security and may be intimidated by the available security options to set up Wi-Fi Protected Access, as well as making it easy to add new devices to an existing network without entering long passphrases.

This library will wait 60 seconds (in the example) for the WPS packets to be recieved by the ESP32.

A major security flaw was revealed in December 2011 that affects wireless routers with the WPS PIN feature, which most recent models have enabled by default.

This software does not use the PIN feature.

#3 Local AP (192.168.4.1)

For the third provisioning method, the ESP32 is set up as an access point (192.168.4.1) – look at your list of WiFi APs on your computer when it is running. A small web server is started that will allow you to select the AP that you want the ESP32 to connect to and then you can enter the password for the access point.

It runs for 60 seconds by default.

Notes

The two general defines in the example are:
– BC24:
Define BC24 if you are using a BC24 ESP32 based device. THere are visual clues for what the ESP32 is doing while provisioning. See the archive at:
https://github.com/switchdoclabs/SDL_ESP32_BC24NEO

– BC24DEBUG:
Define BC24DEBUG (by default in the example it is defined) to see lots of debugging information which can help resolve problems and issues.
Remember that all of these features may fail and have to be repeated. Nature of the beast.

See how to use all three in the SDL_ESP32_BC24_GETIP.ino file.

Download the Software

If you are new to the ESP32, set up your Arduino IDE as in the Adafruit tutorial here.

Once you have that up and working, download the SDL_ESP32_BC24_GETIP library.  Then install in your Arduino directory, compile and download it to the BC24 board (or your choice of ESP32).

https://github.com/switchdoclabs/SDL_ESP32_BC24_GETIP

The Key Part of the Software

The repository above contains all the needed software to run the demonstration program.   The demo runs each one of the methods in turn and quits when one of them succeed or you reach the end of the program.   The main part of the software is pretty simple:

 if (WiFiPresent == false)
  {
    // do SmartConfig
#define WAITFORSTART 15
#define WAITFORCONNECT 20

    WiFiPresent = SmartConfigGetIP(WAITFORSTART, WAITFORCONNECT);

  } 

  if (WiFiPresent != true)
  {
#define WPSTIMEOUTSECONDS 60
    // now try WPS Button

    WiFiPresent = WPSGetIP(WPSTIMEOUTSECONDS);

  }

  if (WiFiPresent != true)
  {
#define APTIMEOUTSECONDS 60
    WiFiPresent = localAPGetIP(APTIMEOUTSECONDS);
  }




 

With BC24DEBUG defined, we can see what is going on with the SmartConfig connection below.

--------------------
WiFi Provisiong ESP32 Software Demo
--------------------
Version: 1.0Compiled at:13:56:53 Jun 14 2018
[D][WiFiGeneric.cpp:304] _eventCallback(): Event: 2 - STA_START
[D][WiFiGeneric.cpp:304] _eventCallback(): Event: 0 - WIFI_READY
error_code=0
[D][WiFiGeneric.cpp:304] _eventCallback(): Event: 14 - AP_STOP
[D][WiFiGeneric.cpp:304] _eventCallback(): Event: 14 - AP_STOP
Waiting for SmartConfig.
.....[D][WiFiSTA.cpp:624] _smartConfigCallback(): Status: FIND_CHANNEL
...[D][WiFiSTA.cpp:624] _smartConfigCallback(): Status: GETTING_SSID_PSWD
[D][WiFiSTA.cpp:627] _smartConfigCallback(): Type: ESPTOUCH
.....[D][WiFiSTA.cpp:624] _smartConfigCallback(): Status: LINK
[D][WiFiSTA.cpp:630] _smartConfigCallback(): SSID: gracie
[D][WiFiGeneric.cpp:304] _eventCallback(): Event: 4 - STA_CONNECTED

SmartConfig received.
Waiting for SmartConfig WiFi Start
..[D][WiFiGeneric.cpp:304] _eventCallback(): Event: 7 - STA_GOT_IP
[D][WiFiSTA.cpp:624] _smartConfigCallback(): Status: LINK_OVER
[D][WiFiSTA.cpp:638] _smartConfigCallback(): Sender IP: 192.168.1.112

WiFI Connected.

Wifi Present from Smart Config
-------------
WiFi Connected
-------------
SSID=gracie
psk=xxxx


The next run shows what happens if you just let all three methods time out.

--------------------
WiFi Provisioning ESP32 Software Demo
--------------------
Version: 1.0Compiled at:15:53:02 Jun 14 2018
error_code=0
[D][WiFiGeneric.cpp:304] _eventCallback(): Event: 13 - AP_START
[D][WiFiGeneric.cpp:304] _eventCallback(): Event: 13 - AP_START
[D][WiFiGeneric.cpp:304] _eventCallback(): Event: 0 - WIFI_READY
[D][WiFiGeneric.cpp:304] _eventCallback(): Event: 14 - AP_STOP
Waiting for SmartConfig.
.....[D][WiFiSTA.cpp:624] _smartConfigCallback(): Status: FIND_CHANNEL
..........No Wifi Present from Smart Config
Now Try WPS Button
error_code=0
Starting WPS
error_code=0
error_code=0
...........................................................WPS Failure
*WM: 
*WM: AutoConnect
*WM: Connecting as wifi client...
*WM: Using last saved values, should be faster
*WM: Connection result: 
*WM: 255
*WM: SET AP STA
Entered config mode
192.168.4.1
[D][WiFiGeneric.cpp:304] _eventCallback(): Event: 13 - AP_START
*WM: 
*WM: Configuring access point... 
*WM: BigCircle24-3DFE
*WM: AP IP address: 
*WM: 192.168.4.1
*WM: HTTP server started
failed to connect and hit timeout
-------------
WiFi NOT Connected
-------------