ESP32 WiFi setup over Bluetooth Serial (ESP32 code)

In my other post I wrote about setting up the ESP32 WiFi credentials over BLE. While testing and playing around with different phones, I found that even a quite new (but as well very cheap phone) I have does not support BLE. So I sat back in front of my development IDE and wrote something similar, but this time using Bluetooth Serial as communication.

Most of the code is the same as in ESP32 WiFi setup over BLE so I skip the parts about the WiFi connection and highlight only the small changes in the setup() and loop() functions.

And as always, the full source code is available in my repository!

So we need beside the code for the ESP32 as well an application for Android that can connect over Bluetooth Serial to the module. I wrote this small Android application and it is available for download from

  • Google Play for automatic installation
  • APKfiles if you do not have Google Play on your device
  • from here for manual installation
  • or you can build the Android app by yourself from the sources in my Bitbucket repo.

How does it work?

When the ESP32 is powered up it starts a Bluetooth Serial port and listens to command for setup, check or erase the WiFi credentials. The Android app will find the ESP32 BLE server or ESP32 with active Bluetooth Serial:

In above picture you can see on ESP32 with an active Bluetooth Serial port and a second ESP32 with BLE advertising active. By clicking on the device name the configuration screen opens. If there are already some WiFi credentials stored on the ESP32, they will be read from the device and displayed:

The configuration screen has 4 buttons to read the existing configuration, write a new configuration to the ESP32, to erase any configuration on the device or to restart the ESP32.

In case you want to use two AP settings, the switch button “Enable two AP” will show the edit boxes for the second AP:

The SSID and password for the WiFi APs should be entered in the text edit fields. Then a push on the “Send WiFi config” will send the new WiFi credentials to the ESP32.

The small text field at the bottom will show some debug messages during the process.



The source code

This post concentrates on the software that has to run on the ESP32. The complete source code is available in my Bitbucket repo.

I wrote the code with PlatformIO on Visual Studio Code, but as the code is all in one file, you can simply copy the code into a .ino file and use ArduinoIDE to test it.

As said before, I concentrate here non the code parts for the Bluetooth Serial interface.

The code for the Bluetooth Serial is based on the Bluetooth Serial example from the Arduino ESP32 framework.

Only one include and one global class is required

The initialization of the BT Serial is much simpler than for the BLE

It is just a call to SerialBT.begin with the device name. The creation of apName is shown in my other post.

Then the application just waits in the loop() for an incoming message over the Bluetooth Serial port. If a message arrives, readBTSerial is called

First we read in all received data from the receiving buffer until the buffer is empty. I have a timeout programmed here that stops the reading after 5 seconds in case something went wrong.

The data is no longer sent as clear text, instead I added a simple encoding (XOR with a key) to make the data unreadable for sniffers

The key used here is the apName, which is different for each device. Not the most secure way, if you like you can add here a encryption with AES or better. But I wanted to keep the example simple.

In the next part the received data is checked if it is a valid JSON object. If true, the JSON object is searched for SSID’s and passwords

I all is found, the new WiFi credentials are stored in the devices preferences.

Next check is if the received data is a command. 3 commands are defined and can be sent from the Android app:

  • read – request to receive the current stored WiFi credentials
  • erase – to erase the current stored WiFi credentials from the ESP32
  • reset – to restart the ESP32

If the command was read, the WiFi credentials are packed into a JSON object

Then this JSON object is encoded

and send to the requesting Android app

If the command was erase, then all stored credentials in the preferences are deleted

The last part of the erase procedure is actually optional and you might want to remove it. By erasing the complete NVS partition all stored preferences are deleted, that includes as well Bluetooth pairing informations.

In case the command was reset, we simply reboot the device

And at the very end of this routine there is just some cleanup and debug messages.

Different to BLE, the Bluetooth Serial library is not working with callback functions that tell us when data has arrived. So the same way you poll a normal Serial port we have to poll the Bluetooth Serial port in the loop() and act if some data has arrived

That’s all about WiFi configuration over Bluetooth Serial. If you miss some parts, check out the other post about WiFi configuration over BLE, because here I only talked about the differences between the two.



Leave a Reply

Your email address will not be published. Required fields are marked *

Free Link Directory