More WPS please....Xiao ESP32C3 & ESP32S3 test your WiFi

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

Compiler output, Make sure it’s not using the ArduinoBLE. seems to break the ESP32 WiFi., YMMV

FQBN: esp32:esp32:XIAO_ESP32C3
Using board 'XIAO_ESP32C3' from platform in folder: C:\Users\Dude\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.8
Using core 'esp32' from platform in folder: C:\Users\Dude\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.8
---Edit for brevity---
Using library WiFi at version 2.0.0 in folder: C:\Users\Dude\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.8\libraries\WiFi 
Using library ESP32 BLE Arduino at version 2.0.0 in folder: C:\Users\Dude\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.8\libraries\BLE 
"C:\\Users\\Dude\\AppData\\Local\\Arduino15\\packages\\esp32\\tools\\riscv32-esp-elf-gcc\\esp-2021r2-patch5-8.4.0/bin/riscv32-esp-elf-size" -A "C:\\Users\\Dude\\AppData\\Local\\Temp\\arduino\\sketches\\4DFBDCF2EC65E02C917896863DF003D0/sketch_feb27a.ino.elf"
Sketch uses 689288 bytes (52%) of program storage space. Maximum is 1310720 bytes.
Global variables use 38564 bytes (11%) of dynamic memory, leaving 289116 bytes for local variables. Maximum is 327680 bytes.
"C:\Users\Dude\AppData\Local\Arduino15\packages\esp32\tools\esptool_py\4.5.1/esptool.exe" --chip esp32c3 --port "COM3" --baud 921600  --before default_reset --after hard_reset write_flash  -z --flash_mode dio --flash_freq 80m --flash_size 4MB 0x0 "C:\Users\Dude\AppData\Local\Temp\arduino\sketches\4DFBDCF2EC65E02C917896863DF003D0/sketch_feb27a.ino.bootloader.bin" 0x8000 "C:\Users\Dude\AppData\Local\Temp\arduino\sketches\4DFBDCF2EC65E02C917896863DF003D0/sketch_feb27a.ino.partitions.bin" 0xe000 "C:\Users\Dude\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.8/tools/partitions/boot_app0.bin" 0x10000 "C:\Users\Dude\AppData\Local\Temp\arduino\sketches\4DFBDCF2EC65E02C917896863DF003D0/sketch_feb27a.ino.bin" 
esptool.py v4.5.1
Serial port COM3
Connecting...
Chip is ESP32-C3 (revision v0.3)
Features: WiFi, BLE
Crystal is 40MHz
MAC: a0:76:4e:3f:a3:88
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 921600
Changed.
Configuring flash size...
Flash will be erased from 0x00000000 to 0x00003fff...
Flash will be erased from 0x00008000 to 0x00008fff...
Flash will be erased from 0x0000e000 to 0x0000ffff...
Flash will be erased from 0x00010000 to 0x000bcfff...
Compressed 13152 bytes to 9473...
Writing at 0x00000000... (100 %)
Wrote 13152 bytes (9473 compressed) at 0x00000000 in 0.3 seconds (effective 378.4 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 146...
Writing at 0x00008000... (100 %)
Wrote 3072 bytes (146 compressed) at 0x00008000 in 0.1 seconds (effective 423.0 kbit/s)...
Hash of data verified.

HTH
GL :slight_smile: PJ

ESP32S3, output…

ESP32 Chip model = ESP32-S3 Rev 0
This chip has 2 cores
Chip ID: 7000304
Starting WPS
Station Mode Started
Scanning...
sm BSSid: 00:00:00:00:00:00 scan BSSID d4:04:cd:ff:0e:54
RSSI:-55
Disconnected from station, attempting reconnection
Disconnected from station, attempting reconnection
WPS Successfull, stopping WPS and connecting to: 
Disconnected from station, attempting reconnection
Connected to :GlassSurf-2.4
Got IP: 192.168.1.166
RSSI:-55

Un-comment “//goBlue (); " Line to scan for BLE devices also , Here I have an Nrf-Dongle Adversitsing the Time Service(empty) and The Scanner Picks it up as NrfConnect” Nice ! :grin: :+1:


ESP32 Chip model = ESP32-S3 Rev 0
This chip has 2 cores
Chip ID: 7000304
Starting WPSStation Mode Started

Scanning...
sm BSSid: 00:00:00:00:00:00 scan BSSID d4:04:cd:ff:0e:54
Disconnected from station, attempting reconnection
WPS Successfull, stopping WPS and connecting to: 
Disconnected from station, attempting reconnection
Connected to :GlassSurf-2.4
Got IP: 192.168.1.166
RSSI:-51
Advertised Device: Name: , Address: 6f:c4:87:90:43:63, serviceUUID: 0000fef3-0000-1000-8000-00805f9b34fb, rssi: -74, serviceData: J#K6492�W�3qw��:,�C�6�/ 
Advertised Device: Name: NrfConnect, Address: c8:65:29:7e:60:0c, txPower: 8, rssi: -47 
Advertised Device: Name: , Address: 59:e9:55:8a:e4:9f, serviceUUID: 0000fef3-0000-1000-8000-00805f9b34fb, rssi: -48, serviceData: J#7KCJ2�xz.�:_%�
��}i� 

BLE-Devices found: 3
BLE-Scan done!
Advertised Device: Name: , Address: 59:e9:55:8a:e4:9f, serviceUUID: 0000fef3-0000-1000-8000-00805f9b34fb, rssi: -51, serviceData: J#7KCJ2�xz.�:_%�
��}i� 

Advertised Device: Name: , Address: 6f:c4:87:90:43:63, serviceUUID: 0000fef3-0000-1000-8000-00805f9b34fb, rssi: -83, serviceData: J#K6492�W�3qw��:,�C�6�/ 
Advertised Device: Name: NrfConnect, Address: c8:65:29:7e:60:0c, txPower: 8, rssi: -59 

Great for testing Wifi & BLE on the ESP32S3
NOTE:
ESP32C3 doesn’t have enough Ram to fit the code…

How to: Upload code, press reset , look at Serial monitor, Press WPS button on router, look at monitor.
does it work? Done. As you were :smile: :v:

HTH
GL :slight_smile: PJ