问题描述
我正在创建一个涉及蓝牙和 WiFi 的应用。我首先将手机连接到蓝牙并通过它传递WiFi ssid和密码。然后在收到 ssid 和密码后,我尝试将其连接到 WiFi。我设法将 ssid 和密码存储到一个字符数组中。
蓝牙发送数据后,ESP32与WiFi连接后,蓝牙断开。我无法再次连接蓝牙,因为 WiFi 已连接,因为我通过 Bluetooth.read() 控制代码中的某些语句,我需要允许连接在 WiFi 和蓝牙之间共存。
该项目包括 SPIFFS(闪存保存)。 WiFi ssid和密码是先从蓝牙传过来的,然后,我把收到的蓝牙数据连接成一个字符数组,这样我以后就可以把它连接到互联网了。
我首先想到的是添加一个回调,当设备尝试连接时发出警报,但在文档中找不到任何能够这样做的代码。我有一个回调函数,它告诉我设备何时连接和断开连接。
我搜索并找到了有关 Menuconfig
的信息,但找不到它的位置。
这是我阅读的内容:https://www.espressif.com/sites/default/files/documentation/ESP32_FAQs__EN.pdf
5.3.2.
How do ESP32 Bluetooth and Wi-Fi coexist?
In the menuconfig menu,there is a special option called “Software controls WiFi/
Bluetooth coexistence”,which is used to control the ESP32's Bluetooth and Wi-Fi
coexistence using software,thus balancing the coexistence requirement for controlling
the RF module by both the Wi-Fi and Bluetooth modules. Please note that if Option
“Software controls WiFi/Bluetooth coexistence” is enabled,the BLE scan
interval shall not exceed 0x100 slots (about 160 ms).
我找不到 menuconfig
所在的位置。
我正在使用 ```BluetoothSerial.h`` 库
这是一段代码:
// ------------------------------ Inicialização do loop ------------------------------ //
void loop() {
// Preparando String para enviar ao usuario
line_one_length = line_one.length();
line_two_length = line_two.length();
line_three_length = line_three.length();
char receive_line_one[line_one_length],receive_line_two[line_two_length],receive_line_three[line_three_length];
char receive_wifi_ssid[line_one_length],receive_wifi_pass[line_two_length];
String deletar;
file = SPIFFS.open("/wifi.txt");
// ------------------------------ Ve se o usuario digitou a palavra deletar ------------------------------ //
if (SerialBT.available()){
while (SerialBT.available()) {
insert_chars = SerialBT.read();
deletar = String(deletar + insert_chars);
}
deletar.trim();
}
// ------------------------------ Conexão ao WiFi ------------------------------ //
if (line_one != "" and wifi_state == 0 and receive_wifi_ssid != ""){
line_one.tochararray(receive_wifi_ssid,line_one_length);
line_two.tochararray(receive_wifi_pass,line_two_length);
delay(100);
WiFi.begin(receive_wifi_ssid,receive_wifi_pass);
Serial.println("Conectando ao WiFi...");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(1000);
}
Serial.println("\nConectado ao WiFi");
wifi_state = 1;
}
Serial.print("Estado: ");
Serial.println(connection_state);
// ------------------------------ Vê se a conteudo dentro do FILE ------------------------------ //
if (file.size() > 0) {
if (deletar == "deletar"){
delete_file_info();
wifi_ssid = "";
wifi_password = "";
database_info = "";
line_one = "";
line_two = "";
line_three = "";
wifi_state = 0;
Serial.println("Arquivo deletado com sucesso");
return;
}
read_file_info();
all_lines();
}else{
if (wifi_ssid == "" and connection_state == 0 and line_one == ""){
Serial.print("File size: ");
Serial.print(file.size());
Serial.println(",O valor dentro da string é nulo,nada será adicionado ao arquivo");
delay(1000);
}else if (connection_state == 1 and line_one == ""){
// Chamando a função para armazenar os dados do usuario dentro do FILE
bluetooth_while_loop();
}
}
file.close();
delay(3000);
我需要的是能够同时在 WiFi 和蓝牙之间建立连接。
解决方法
menuconfig
是 here。软件 WiFi/BL 共存的代码是 here。相关的问题是here。由于 WiFi 和蓝牙使用相同的天线,我不建议完全进行 WiFi/BL 共存。相反,您可以使用 WiFi.setMode(WiFi_AP_STA)
同时实例化 SoftAP
模式和 STA
模式。
试试下面的代码来实例化 softAP
和 STA
模式:
#include <WiFi.h>
void setup(){
WiFi.setMode(WIFI_AP_STA);
WiFi.begin("my-sta-ssid","my-sta-pass");
WiFi.softAP("my-ap-ssid","my-ap-pass");
}
void loop(){}
您可以创建简单的 WebServer
来执行“运行时配置”。