5008 views|5 replies

9702

Posts

24

Resources
The OP
 

ESP32 configures WIFI via Bluetooth [Copy link]

 

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.

Upload the Sketch to the ESP32

Copy the code from here

链接已隐藏,如需查看请登录或者注册
into a new Sketch and upload it to your ESP32 board. If you are using the ESP32-CAM, remember to disconnect pin 0 and press Reset.

Open the serial monitor so you can check the progress of the following steps.

Connecting your phone to the ESP32

I have Android 9, but other phones should be very similar.

Go to Settings and select Connected devices:

Select Pair new device:

Select robot01 from the list of available devices:

Confirm pairing:

You should see the device in "Currently connected":

Using the Serial Terminal

Install the Serial Bluetooth Terminal app from the Play Store – https://play.google.com/store/apps/details?id=de.kai_morich.serial_bluetooth_terminal&hl=en

Open a serial Bluetooth terminal and select "Devices" from the menu:

Select robot01 from the list to start the serial terminal connection:

If this is the first time the ESP32 has connected to this Wi-Fi network or the password has been changed, you will see the following output.

Serial port connected to ESP32:

ESP32 scans Wi-Fi networks:

Select your network SSID number:

Enter the password for this network:

When the ESP32 is connected to a Wi-Fi network, the ESP32 IP address will be displayed and Bluetooth will be disconnected:

If the ESP32 already has the correct connection credentials, only the IP address of the ESP32 will be displayed:

How does this work?

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:

void callback_show_ip(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
{
  if (event == ESP_SPP_SRV_OPEN_EVT) {
    SerialBT.print("ESP32 IP: ");
    SerialBT.println(WiFi.localIP());
    bluetooth_disconnect = true;
  }
}

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.

This post is from Domestic Chip Exchange

Latest reply

Thanks for sharing!   Details Published on 2022-1-5 15:15
Personal signature虾扯蛋,蛋扯虾,虾扯蛋扯虾
 
 

6555

Posts

0

Resources
2
 

Thanks for sharing

Some of the pictures in the article should not be visible

This post is from Domestic Chip Exchange

Comments

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
 
 
 

9702

Posts

24

Resources
3
 
Jacktang posted on 2021-5-22 21:04 Thank you for sharing. I think the pictures in the article cannot be seen.

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.

This post is from Domestic Chip Exchange
Personal signature虾扯蛋,蛋扯虾,虾扯蛋扯虾
 
 
 

9702

Posts

24

Resources
4
 

This is the source code on github

esp32-bluewifi-master.zip (4.53 KB, downloads: 2)

This post is from Domestic Chip Exchange
Personal signature虾扯蛋,蛋扯虾,虾扯蛋扯虾
 
 
 

2

Posts

0

Resources
5
 

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!

This post is from Domestic Chip Exchange
 
 
 

6062

Posts

4

Resources
6
 

Thanks for sharing!

This post is from Domestic Chip Exchange
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list