-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathWPS.ino
116 lines (104 loc) · 3 KB
/
WPS.ino
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
Example Code To Get ESP32 To Connect To A Router Using WPS
===========================================================
This example code provides both Push Button method and Pin
based WPS entry to get your ESP connected to your WiFi router.
Hardware Requirements
========================
ESP32 and a Router having WPS functionality
This code is under Public Domain License.
Author:
Pranav Cherukupalli <[email protected]>
*/
#include "WiFi.h"
#include "esp_wps.h"
/*
Change the definition of the WPS mode
from WPS_TYPE_PBC to WPS_TYPE_PIN in
the case that you are using pin type
WPS
*/
#define ESP_WPS_MODE WPS_TYPE_PBC
#define ESP_MANUFACTURER "ESPRESSIF"
#define ESP_MODEL_NUMBER "ESP32"
#define ESP_MODEL_NAME "ESPRESSIF IOT"
#define ESP_DEVICE_NAME "ESP STATION"
static esp_wps_config_t config;
void wpsInitConfig(){
config.wps_type = ESP_WPS_MODE;
strcpy(config.factory_info.manufacturer, ESP_MANUFACTURER);
strcpy(config.factory_info.model_number, ESP_MODEL_NUMBER);
strcpy(config.factory_info.model_name, ESP_MODEL_NAME);
strcpy(config.factory_info.device_name, ESP_DEVICE_NAME);
}
void wpsStart(){
if(esp_wifi_wps_enable(&config)){
Serial.println("WPS Enable Failed");
} else if(esp_wifi_wps_start(0)){
Serial.println("WPS Start Failed");
}
}
void wpsStop(){
if(esp_wifi_wps_disable()){
Serial.println("WPS Disable Failed");
}
}
String wpspin2string(uint8_t a[]){
char wps_pin[9];
for(int i=0;i<8;i++){
wps_pin[i] = a[i];
}
wps_pin[8] = '\0';
return (String)wps_pin;
}
// WARNING: WiFiEvent is called from a separate FreeRTOS task (thread)!
void WiFiEvent(WiFiEvent_t event, arduino_event_info_t info){
switch(event){
case ARDUINO_EVENT_WIFI_STA_START:
Serial.println("Station Mode Started");
break;
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
Serial.println("Connected to :" + String(WiFi.SSID()));
Serial.print("Got IP: ");
Serial.println(WiFi.localIP());
break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
Serial.println("Disconnected from station, attempting reconnection");
WiFi.reconnect();
break;
case ARDUINO_EVENT_WPS_ER_SUCCESS:
Serial.println("WPS Successfull, stopping WPS and connecting to: " + String(WiFi.SSID()));
wpsStop();
delay(10);
WiFi.begin();
break;
case ARDUINO_EVENT_WPS_ER_FAILED:
Serial.println("WPS Failed, retrying");
wpsStop();
wpsStart();
break;
case ARDUINO_EVENT_WPS_ER_TIMEOUT:
Serial.println("WPS Timedout, retrying");
wpsStop();
wpsStart();
break;
case ARDUINO_EVENT_WPS_ER_PIN:
Serial.println("WPS_PIN = " + wpspin2string(info.wps_er_pin.pin_code));
break;
default:
break;
}
}
void setup(){
Serial.begin(115200);
delay(10);
Serial.println();
WiFi.onEvent(WiFiEvent); // Will call WiFiEvent() from another thread.
WiFi.mode(WIFI_MODE_STA);
Serial.println("Starting WPS");
wpsInitConfig();
wpsStart();
}
void loop(){
//nothing to do here
}