Skip to content

Commit 17add63

Browse files
Raienryu97me-no-dev
authored andcommitted
Add Example Code for WPS Functionality (#648)
1 parent 4cf0e98 commit 17add63

File tree

2 files changed

+200
-0
lines changed

2 files changed

+200
-0
lines changed

libraries/WiFi/examples/WPS/README.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
Example Serial Logs For Various Cases
2+
======================================
3+
4+
For WPS Push Button method,after the ESP32 boots up and prints that WPS has started, press the button that looks something like [this](https://www.verizon.com/supportresources/images/fqgrouter-frontview-wps-button.png) on your router. In case you dont find anything similar, check your router specs if it does really support WPS Push functionality.
5+
6+
As for WPS Pin Mode, it will output a 8 digit Pin on the Serial Monitor that will change every 2 minutes if it hasn't connected. You need to log in to your router (generally reaching 192.168.0.1) and enter the pin shown in Serial Monitor in the WPS Settings of your router.
7+
8+
#### WPS Push Button Failure
9+
10+
```
11+
ets Jun 8 2016 00:22:57
12+
13+
rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
14+
configsip: 0, SPIWP:0xee
15+
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
16+
mode:DIO, clock div:1
17+
load:0x3fff0010,len:4
18+
load:0x3fff0014,len:732
19+
load:0x40078000,len:0
20+
load:0x40078000,len:11572
21+
entry 0x40078a14
22+
23+
Starting WPS
24+
Station Mode Started
25+
WPS Timedout, retrying
26+
WPS Timedout, retrying
27+
```
28+
29+
#### WPS Push Button Successfull
30+
31+
```
32+
ets Jun 8 2016 00:22:57
33+
34+
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
35+
ets Jun 8 2016 00:22:57
36+
37+
rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
38+
configsip: 0, SPIWP:0xee
39+
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
40+
mode:DIO, clock div:1
41+
load:0x3fff0010,len:4
42+
load:0x3fff0014,len:732
43+
load:0x40078000,len:0
44+
load:0x40078000,len:11572
45+
entry 0x40078a14
46+
47+
Starting WPS
48+
Station Mode Started
49+
WPS Successfull, stopping WPS and connecting to: < Your Router SSID >
50+
Disconnected from station, attempting reconnection
51+
Connected to : < Your Router SSID >
52+
Got IP: 192.168.1.100
53+
```
54+
55+
#### WPS PIN Failure
56+
57+
```
58+
ets Jun 8 2016 00:22:57
59+
60+
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
61+
ets Jun 8 2016 00:22:57
62+
63+
rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
64+
configsip: 0, SPIWP:0xee
65+
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
66+
mode:DIO, clock div:1
67+
load:0x3fff0010,len:4
68+
load:0x3fff0014,len:732
69+
load:0x40078000,len:0
70+
load:0x40078000,len:11572
71+
entry 0x40078a14
72+
73+
Starting WPS
74+
Station Mode Started
75+
WPS_PIN = 94842104
76+
WPS Timedout, retrying
77+
WPS_PIN = 55814171
78+
WPS Timedout, retrying
79+
WPS_PIN = 71321622
80+
```
81+
82+
#### WPS PIN Successfull
83+
84+
```
85+
ets Jun 8 2016 00:22:57
86+
87+
rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
88+
configsip: 0, SPIWP:0xee
89+
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
90+
mode:DIO, clock div:1
91+
load:0x3fff0010,len:4
92+
load:0x3fff0014,len:732
93+
load:0x40078000,len:0
94+
load:0x40078000,len:11572
95+
entry 0x40078a14
96+
97+
Starting WPS
98+
Station Mode Started
99+
WPS_PIN = 36807581
100+
WPS Successfull, stopping WPS and connecting to: <Your Router SSID>
101+
Disconnected from station, attempting reconnection
102+
Connected to :<Your Router SSID>
103+
Got IP: 192.168.1.100
104+
```

libraries/WiFi/examples/WPS/WPS.ino

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
Example Code To Get ESP32 To Connect To A Router Using WPS
3+
===========================================================
4+
This example code provides both Push Button method and Pin
5+
based WPS entry to get your ESP connected to your WiFi router.
6+
7+
Hardware Requirements
8+
========================
9+
ESP32 and a Router having atleast one WPS functionality
10+
11+
This code is under Public Domain License.
12+
13+
Author:
14+
Pranav Cherukupalli <cherukupallip@gmail.com>
15+
*/
16+
17+
#include "WiFi.h"
18+
#include "esp_wps.h"
19+
20+
/*
21+
Change the definition of the WPS mode
22+
from WPS_TYPE_PBC to WPS_TYPE_PIN in
23+
the case that you are using pin type
24+
WPS
25+
*/
26+
#define ESP_WPS_MODE WPS_TYPE_PBC
27+
28+
esp_wps_config_t config = WPS_CONFIG_INIT_DEFAULT(ESP_WPS_MODE);
29+
30+
String wpspin2string(uint8_t a[]){
31+
char wps_pin[9];
32+
for(int i=0;i<8;i++){
33+
wps_pin[i] = a[i];
34+
}
35+
wps_pin[8] = '\0';
36+
return (String)wps_pin;
37+
}
38+
39+
void WiFiEvent(WiFiEvent_t event, system_event_info_t info){
40+
switch(event){
41+
case SYSTEM_EVENT_STA_START:
42+
Serial.println("Station Mode Started");
43+
break;
44+
case SYSTEM_EVENT_STA_GOT_IP:
45+
Serial.println("Connected to :" + String(WiFi.SSID()));
46+
Serial.print("Got IP: ");
47+
Serial.println(WiFi.localIP());
48+
break;
49+
case SYSTEM_EVENT_STA_DISCONNECTED:
50+
Serial.println("Disconnected from station, attempting reconnection");
51+
WiFi.reconnect();
52+
break;
53+
case SYSTEM_EVENT_STA_WPS_ER_SUCCESS:
54+
Serial.println("WPS Successfull, stopping WPS and connecting to: " + String(WiFi.SSID()));
55+
esp_wifi_wps_disable();
56+
delay(10);
57+
WiFi.begin();
58+
break;
59+
case SYSTEM_EVENT_STA_WPS_ER_FAILED:
60+
Serial.println("WPS Failed, retrying");
61+
esp_wifi_wps_disable();
62+
esp_wifi_wps_enable(&config);
63+
esp_wifi_wps_start(0);
64+
break;
65+
case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT:
66+
Serial.println("WPS Timedout, retrying");
67+
esp_wifi_wps_disable();
68+
esp_wifi_wps_enable(&config);
69+
esp_wifi_wps_start(0);
70+
break;
71+
case SYSTEM_EVENT_STA_WPS_ER_PIN:
72+
Serial.println("WPS_PIN = " + wpspin2string(info.sta_er_pin.pin_code));
73+
break;
74+
default:
75+
break;
76+
}
77+
}
78+
79+
void setup(){
80+
Serial.begin(115200);
81+
delay(10);
82+
83+
Serial.println();
84+
85+
WiFi.onEvent(WiFiEvent);
86+
WiFi.mode(WIFI_MODE_STA);
87+
88+
Serial.println("Starting WPS");
89+
90+
esp_wifi_wps_enable(&config);
91+
esp_wifi_wps_start(0);
92+
}
93+
94+
void loop(){
95+
//nothing to do here
96+
}

0 commit comments

Comments
 (0)