0

$USD

$USD
TUTORIALS ESP32

ESP32 Soft AP: Enabling IPv6 support

DFRobot Dec 16 2019 2116

The objective of this tutorial is to check how to enable IPv6 support on the ESP32, when operating in Soft AP mode. We will be using the Arduino core to program the ESP32.


Introduction

The objective of this tutorial is to check how to enable IPv6 support on the ESP32, when operating in Soft AP mode. We will be using the Arduino core to program the ESP32.

To test the code, we will connect a computer to the WiFi network hosted by the ESP32 and then ping it using its IPv6 address.

The tests from this tutorial were done using a DFRobot’s ESP32 module integrated in a ESP32 development board.


The code

We will start the code by including the WiFi.h library, which will expose all the functionalities we need.
#include <WiFi.h>

Moving on to the Arduino setup, we will take care of opening a serial connection, so we can later output the results of our program.

Serial.begin(115200);
Then we will take care of registering some WiFi events that we will use to both enable IPv6 and then print the IP address of the AP.

As can be seen here, we should enable IPv6 on the soft AP interface start. This also matches what is done on this example from the Arduino core, which I recommend you to check.


So, we should only enable the IPv6 interface after the SYSTEM_EVENT_AP_START occurs. Thus, we will register a function called WiFiApStarted, which we will check later how to implement.

WiFi.onEvent(WiFiApStarted, SYSTEM_EVENT_AP_START);
We will also register a function called WiFiGotIp to handle the SYSTEM_EVENT_AP_STA_GOT_IP6 event. That way, we will be able to know when the AP gets an IPv6 address and we can print its value.

WiFi.onEvent(WiFiGotIp, SYSTEM_EVENT_AP_STA_GOT_IP6);

To finalize the setup function, we will setup the ESP32 to work as a soft AP. This is done with a call to the softAP method on the WiFi extern variable, passing as input the name of the network.

WiFi.softAP("MyESP32AP");

The full setup function can be seen below.

void setup() {
  
  Serial.begin(115200);
 
  WiFi.onEvent(WiFiApStarted, SYSTEM_EVENT_AP_START);
  WiFi.onEvent(WiFiGotIp, SYSTEM_EVENT_AP_STA_GOT_IP6);
 
  WiFi.softAP("MyESP32AP");
   
}

The implementation of the WiFiApStarted function will be very simple. We will enable the IPv6 support with a call to the softAPenableIpV6 method on the WiFi extern variable. This method takes no parameters.

void WiFiApStarted(WiFiEvent_t event, WiFiEventInfo_t info){
  WiFi.softAPenableIpV6();
}

The implementation of the WiFiGotIp function will also be very short. We will take care of printing the IPv6 address of the soft AP.

This can be done with a call to the softAPIPv6 method on the WiFi extern variable, which also takes no arguments. As output, it will return the address, which we can directly print to the serial console.

void WiFiGotIp(WiFiEvent_t event, WiFiEventInfo_t info){
  Serial.println(WiFi.softAPIPv6());
}

The final and complete code can be seen below.

#include <WiFi.h>
 
void WiFiApStarted(WiFiEvent_t event, WiFiEventInfo_t info){
  WiFi.softAPenableIpV6();
}
 
void WiFiGotIp(WiFiEvent_t event, WiFiEventInfo_t info){
  Serial.println(WiFi.softAPIPv6());
}
  
void setup() {
  
  Serial.begin(115200);
 
  WiFi.onEvent(WiFiApStarted, SYSTEM_EVENT_AP_START);
  WiFi.onEvent(WiFiGotIp, SYSTEM_EVENT_AP_STA_GOT_IP6);
 
  WiFi.softAP("MyESP32AP");
   
}
  
void loop() {}


Testing the code

To test the code, compile it and upload it to your device using the Arduino IDE. When the procedure finishes, open the serial monitor and wait for the soft AP to be setup and then for the IPv6 address of the ESP32 to be printed.

You should obtain a result similar to the one shown on figure 1. Copy the IPv6 address that gets printed.


Figure 1 – Output of the program, showing the soft AP IPv6 address.

Then use a computer with WiFi support and connect to the network hosted by the ESP32. Recall that it was named “MyESP32AP“.

After this, open a command line and send a ping to the ESP32. As already covered here, the ESP32 natively answers to pings. The command should be the following, where you should change #ESP32IPv6# by the IP you have just copied:

ping -6 #ESP32IPv6#

You should get an output similar to figure 2. As can be seen, the ESP32 answers to the ping, as excepted.

Figure 2 – Output of the ping command.