WPS is the easiest way to connect… and works well on the Xiao ESP32c3,
I’m using an ARRIS Router with a Hard & Soft WPS button press. AOK
/*
THIS lightly tweeked PJG Example Code To Get ESP32c3 info from chip
and to Connect WPS button AP and display RSSI, as well as do a BLE scan for devices.(WIP) concurrently
or as close as possible.
===========================================================
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
*/
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#define ESP_WPS_MODE WPS_TYPE_PBC
#define ESP_MANUFACTURER "SEEED"
#define ESP_MODEL_NUMBER "ESP32C3"
#define ESP_MODEL_NAME "Xiao ESP"
#define ESP_DEVICE_NAME "ESP STATION"
uint32_t chipId = 0;
static esp_wps_config_t config;
int scanTime = 5; //In seconds
BLEScan* pBLEScan;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
}
};
void setup(){
Serial.begin(115200);
delay(1000);
Serial.println();
for(int i=0; i<17; i=i+8) {
chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
}
Serial.printf("ESP32 Chip model = %s Rev %d\n", ESP.getChipModel(), ESP.getChipRevision());
Serial.printf("This chip has %d cores\n", ESP.getChipCores());
Serial.print("Chip ID: "); Serial.println(chipId);
delay(2000);
WiFi.onEvent(WiFiEvent);
WiFi.mode(WIFI_MODE_STA);
Serial.println("Starting WPS"); // GO press WPS button.
wpsInitConfig();
wpsStart();
Serial.println("Scanning...");
//delay(2000);
while ( WiFi.status() != WL_CONNECTED) { //Wait for connection
//Serial.println(WiFi.localIP());
delay(20000);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("RSSI:");
Serial.println(rssi);
}
}
void loop(){
//Serial.println(WiFi.localIP ());
delay (5000);
//goBlue ();
}
void goBlue () {
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setInterval(100);
pBLEScan->setWindow(99); // less or equal setInterval value
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
Serial.print("BLE-Devices found: ");
Serial.println(foundDevices.getCount());
Serial.println("BLE-Scan done!");
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
}
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;
}
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;
}
}
Output on Ser.Mon.
ESP32 Chip model = ESP32-C3 Rev 3
This chip has 1 cores
Chip ID: 4196588
Station Mode Started
Starting WPS
Scanning...
RSSI:0
sm BSSid: 00:00:00:00:00:00 scan BSSID d4:04:cd:ff:0e:54
RSSI:-53
Disconnected from station, attempting reconnection
WPS Successfull, stopping WPS and connecting to: GlassSurf-2.4
E (45554) wifi:sta is connecting, return error
RSSI:-54
RSSI:-54
RSSI:-54
RSSI:-54
Disconnected from station, attempting reconnection
Connected to :GlassSurf-2.4
Got IP: 192.168.1.189
RSSI:-54
Works well holds connection NO Issues.
on to BLE’ Scan. (goBlue)
HTH
GL :-p