The following content is automatically translated by software, the source text is: https://robotzero.one/esp32-wi-fi-connection-bluetooth/
Wi-Fi connection manager using Bluetooth serial, preference library, and enumeration state machine.
Sometimes you need to connect to the ESP32 remotely over Wi-Fi, but you don't know the IP address, or the ESP32 reconnects with a new IP address every time.
The easiest way to find the IP address of an unreachable board is to transmit it to your phone over Bluetooth serial. But Bluetooth and Wi-Fi don't coexist well on the ESP32 because they share the same radio system. Once you start using the Wi-Fi connection for data, you'll need to turn off the Bluetooth connection.
In this tutorial and example code, you can see how to use Bluetooth serial to read an IP address and then close the connection so that only Wi-Fi is using the radio.
This demo application uses an ESP32-based camera board but can be adapted for other projects that require access to the ESP32 over Wi-Fi.
This tutorial is divided into three parts - uploading the sketch, pairing your phone with the ESP32, and connecting using the Serial Bluetooth app.
Some important parts of the code are explained below:
This code uses an "enumeration state machine". You can think of an enumeration as a variable with a fixed number of values, such as SCAN_START, SCAN_COMPLETE, SSID_ENTERED, PASS_ENTERED, LOGIN_FAILED
These values are used in the switch statement of the main loop. The function to run or the variable value to be set depends on the current enumeration value:
case SCAN_START:
SerialBT.println("Scanning Wi-Fi networks");
Serial.println("Scanning Wi-Fi networks");
scan_wifi_networks();
SerialBT.println("Please enter the number for your Wi-Fi");
wifi_stage = SCAN_COMPLETE;
break;
case SSID_ENTERED:
SerialBT.println("Please enter your Wi-Fi password");
Serial.println("Please enter your Wi-Fi password");
wifi_stage = WAIT_PASS;
break;
case PASS_ENTERED:
SerialBT.println("Please wait for Wi-Fi connection...");
Serial.println("Please wait for Wi_Fi connection...");
wifi_stage = WAIT_CONNECT;
preferences.putString("pref_ssid", client_wifi_ssid);
preferences.putString("pref_pass", client_wifi_password);
if (init_wifi()) { // Connected to WiFi
connected_string = "ESP32 IP: ";
connected_string = connected_string + WiFi.localIP().toString();
SerialBT.println(connected_string);
Serial.println(connected_string);
bluetooth_disconnect = true;
} else { // try again
wifi_stage = LOGIN_FAILED;
}
break;
case LOGIN_FAILED:
SerialBT.println("Wi-Fi connection failed");
Serial.println("Wi-Fi connection failed");
delay(2000);
wifi_stage = SCAN_START;
break;
Sketch uses two Bluetooth callbacks depending on whether Wi-Fi is connected:
if (!init_wifi()) { // Connect to Wi-Fi fails
SerialBT.register_callback(callback);
} else {
SerialBT.register_callback(callback_show_ip);
}
If the ESP32 cannot connect to Wi-Fi, the first callback is initialized. This listens for Bluetooth data sent to the ESP32. Depending on the data type and the current value of the enumeration, various variables are set and the function is run:
void callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
{
if (event == ESP_SPP_SRV_OPEN_EVT) {
wifi_stage = SCAN_START;
}
if (event == ESP_SPP_DATA_IND_EVT && wifi_stage == SCAN_COMPLETE) { // data from phone is SSID
int client_wifi_ssid_id = SerialBT.readString().toInt();
client_wifi_ssid = ssids_array[client_wifi_ssid_id];
wifi_stage = SSID_ENTERED;
}
if (event == ESP_SPP_DATA_IND_EVT && wifi_stage == WAIT_PASS) { // data from phone is password
client_wifi_password = SerialBT.readString();
client_wifi_password.trim();
wifi_stage = PASS_ENTERED;
}
}
Alternatively, if the ESP32 has successfully connected to Wi-Fi, initialize the second callback and wait for input from the Bluetooth connection:
One final important thing to note in the code is that once the user knows the Wi-Fi IP address, Bluetooth should be turned off. This happens when the IP address has been displayed on the phone or the user connects to the IP in a browser. An example of this is shown in the code above. Another is shown below when the camera is connected via a web socket:
if (socket_server.poll()) {
disconnect_bluetooth();
...
}
Video Demonstration
A demonstration of connecting via Bluetooth and setting up Wi-Fi access details is shown below.
It seems that the server of the image has been blocked, and the original post cannot be viewed on the normal network. I edited it and downloaded it locally.
Details
Published on 2021-5-22 22:20
It seems that the server of the image has been blocked, and the original post cannot be viewed on the normal network. I edited it and downloaded it locally.
Do a small project on the "ESP32 Development Board IOT Bluetooth WIFI Misiqi": use this development board to intercept the input signal of the original device control board, and forward the input signal to the input end of the original control board and to the mobile phone APP program through Wif (so that the input of the original device can be known through the mobile phone); at the same time, the mobile phone APP can directly send the control signal to the signal input end of the original device to realize device control. If you can do it, please contact us!