2017-09-21 10:47:45 +02:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
========================
|
2020-10-14 13:27:49 +02:00
|
|
|
ESP32 and a Router having WPS functionality
|
2017-09-21 10:47:45 +02:00
|
|
|
|
|
|
|
This code is under Public Domain License.
|
|
|
|
|
|
|
|
Author:
|
|
|
|
Pranav Cherukupalli <cherukupallip@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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
|
|
|
|
*/
|
2018-06-27 09:01:06 +02:00
|
|
|
#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;
|
2017-09-21 10:47:45 +02:00
|
|
|
|
2018-06-27 09:01:06 +02:00
|
|
|
void wpsInitConfig(){
|
|
|
|
config.crypto_funcs = &g_wifi_default_wps_crypto_funcs;
|
|
|
|
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);
|
|
|
|
}
|
2017-09-21 10:47:45 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WiFiEvent(WiFiEvent_t event, system_event_info_t info){
|
|
|
|
switch(event){
|
|
|
|
case SYSTEM_EVENT_STA_START:
|
2018-06-27 09:01:06 +02:00
|
|
|
Serial.println("Station Mode Started");
|
|
|
|
break;
|
2017-09-21 10:47:45 +02:00
|
|
|
case SYSTEM_EVENT_STA_GOT_IP:
|
2018-06-27 09:01:06 +02:00
|
|
|
Serial.println("Connected to :" + String(WiFi.SSID()));
|
|
|
|
Serial.print("Got IP: ");
|
|
|
|
Serial.println(WiFi.localIP());
|
|
|
|
break;
|
2017-09-21 10:47:45 +02:00
|
|
|
case SYSTEM_EVENT_STA_DISCONNECTED:
|
2018-06-27 09:01:06 +02:00
|
|
|
Serial.println("Disconnected from station, attempting reconnection");
|
|
|
|
WiFi.reconnect();
|
|
|
|
break;
|
2017-09-21 10:47:45 +02:00
|
|
|
case SYSTEM_EVENT_STA_WPS_ER_SUCCESS:
|
2020-10-14 13:27:49 +02:00
|
|
|
Serial.println("WPS Successful, stopping WPS and connecting to: " + String(WiFi.SSID()));
|
2018-06-27 09:01:06 +02:00
|
|
|
esp_wifi_wps_disable();
|
|
|
|
delay(10);
|
|
|
|
WiFi.begin();
|
|
|
|
break;
|
2017-09-21 10:47:45 +02:00
|
|
|
case SYSTEM_EVENT_STA_WPS_ER_FAILED:
|
2018-06-27 09:01:06 +02:00
|
|
|
Serial.println("WPS Failed, retrying");
|
|
|
|
esp_wifi_wps_disable();
|
|
|
|
esp_wifi_wps_enable(&config);
|
|
|
|
esp_wifi_wps_start(0);
|
|
|
|
break;
|
2017-09-21 10:47:45 +02:00
|
|
|
case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT:
|
2020-10-14 13:27:49 +02:00
|
|
|
Serial.println("WPS Timeout, retrying");
|
2018-06-27 09:01:06 +02:00
|
|
|
esp_wifi_wps_disable();
|
|
|
|
esp_wifi_wps_enable(&config);
|
|
|
|
esp_wifi_wps_start(0);
|
|
|
|
break;
|
2017-09-21 10:47:45 +02:00
|
|
|
case SYSTEM_EVENT_STA_WPS_ER_PIN:
|
2018-06-27 09:01:06 +02:00
|
|
|
Serial.println("WPS_PIN = " + wpspin2string(info.sta_er_pin.pin_code));
|
|
|
|
break;
|
2017-09-21 10:47:45 +02:00
|
|
|
default:
|
2018-06-27 09:01:06 +02:00
|
|
|
break;
|
2017-09-21 10:47:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup(){
|
|
|
|
Serial.begin(115200);
|
|
|
|
delay(10);
|
|
|
|
|
|
|
|
Serial.println();
|
|
|
|
|
|
|
|
WiFi.onEvent(WiFiEvent);
|
|
|
|
WiFi.mode(WIFI_MODE_STA);
|
|
|
|
|
|
|
|
Serial.println("Starting WPS");
|
|
|
|
|
2018-06-27 09:01:06 +02:00
|
|
|
wpsInitConfig();
|
2017-09-21 10:47:45 +02:00
|
|
|
esp_wifi_wps_enable(&config);
|
|
|
|
esp_wifi_wps_start(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop(){
|
|
|
|
//nothing to do here
|
2020-10-14 13:27:49 +02:00
|
|
|
}
|