$USD
  • EUR€
  • £GBP
  • $USD
TUTORIALS ESP32

ESP32 Soft AP: deauth connected stations

DFRobot Dec 17 2019 1135

The objective of this tutorial is to show how to deauthenticate all the stations connected to a network hosted by the ESP32, operating as soft AP. The tests from this tutorial were performed using an ESP32 board from DFRobot.


Introduction

The objective of this tutorial is to show how to deauthenticate all the stations connected to a network hosted by the ESP32, operating as soft AP. We will be using the Arduino core.

To illustrate this, we will be printing periodically the number of stations connected to the network. Then, if we receive any content on the serial port, we will call the deauth function, so the number of connections should become 0.

The tests from this tutorial were performed using an ESP32 board from DFRobot.


The code

We will start our code by the library includes. We will need the WiFi.h lib, to be able to setup the device to operate in soft AP mode, and the esp_wifi.h, which will make available the function we need to deauthenticate all the stations.

#include <WiFi.h>
#include "esp_wifi.h"

In the Arduino setup function, we will start by opening a serial connection, so we can output the results of our program. We will also need the serial connection to read any incoming bytes, so we know when to perform the deauthentication.

After that, we will setup the network that will be hosted by the ESP32. We do this with a call to the softAP method on the WiFi extern variable, passing as input the name we want to assign to the network.

The full setup function can be seen below.

void setup() {
 
  Serial.begin(115200);
 
  WiFi.softAP("MyESP32AP");
 
}

We will write the rest of the code in the Arduino loop function. We will first check if there are any incoming bytes on the serial port, with a call to the read method on the Serial object.

This method will return the next byte available (if there is incoming data) or -1 if there is no data to read.

In our case, for simplicity, I’m assuming that as long as any content is sent to the serial port, then we want to deauth all stations. So, if the value returned by the read method is different from -1, them we will call the deauth function.

To deauth all the stations, we simply need to call the esp_wifi_deauth_sta function, passing as input the value 0.

Note that if we want to deauthenticate a particular station rather than all of them, we can alternatively pass as input of the function the id of that station. Nonetheless, we won’t be covering that scenario here.

if(Serial.read() != -1){
    esp_wifi_deauth_sta(0);
}

To finalize the loop function, we will print the total number of stations connected to the network. That way, we can confirm if our deauth had effect. The procedure to print the number of connected stations can be checked on this previous tutorial.

In short, we simply need to call the softAPgetStationNum method on the WiFi extern variable.

Serial.print("Stations connected: ");
Serial.println(WiFi.softAPgetStationNum());
The complete loop code can be seen below. We have added a 5 seconds delay between each iteration of the loop.

void loop() {
 
  if(Serial.read() != -1){
    esp_wifi_deauth_sta(0);
  }
 
  Serial.print("Stations connected: ");
  Serial.println(WiFi.softAPgetStationNum());
   
  delay(5000);
   
}

The final code can be seen below.

#include <WiFi.h>
#include "esp_wifi.h"
 
void setup() {
 
  Serial.begin(115200);
 
  WiFi.softAP("MyESP32AP");
 
}
 
void loop() {
 
  if(Serial.read() != -1){
    esp_wifi_deauth_sta(0);
  }
 
  Serial.print("Stations connected: ");
  Serial.println(WiFi.softAPgetStationNum());
   
  delay(5000);
   
}


Testing the code

To test the code, simply compile it and upload it to your ESP32, using the Arduino IDE. When the procedure finishes, open the IDE serial monitor and wait for the network setup to complete.

If you check the Arduino IDE serial monitor, it should be printing 0 stations connected to the network.

Then, connect one or more devices to the network. The number of stations printed to the serial monitor should increase.

Then, write a character and send it to the ESP32 using the Arduino IDE serial monitor. The next time the number of stations is printed, it should be 0, as all the previously connected ones should have been deauthenticated. This is shown in figure 1.


Figure 1 – Output of the program, showing the moment when all the stations are deauthenticated.

Note that, depending on the device that you had connected to the network, it might attempt to reconnect again after a while. Our code only contemplates deauthing all the devices at a given moment, it doesn’t previne them from rejoining the network later.

So, you might see the number of connected stations going to 0 and then increasing again without explicitly connecting the device again to the network. The behavior is normal.


References

[1] https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/network/esp_wifi.html#_CPPv419esp_wifi_deauth_sta8uint16_t

REVIEW