gpt4 book ai didi

arduino - 使 ESP32 WiFi/蓝牙协同工作

转载 作者:行者123 更新时间:2023-12-05 03:45:42 29 4
gpt4 key购买 nike

我正在创建一个涉及蓝牙和 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 和蓝牙之间建立连接。

最佳答案

menuconfighere .软件WiFi/BL共存代码为here .相关问题是here .因为 WiFi 和蓝牙使用相同的天线,所以我不建议完全共存 WiFi/BL。相反,您可以使用 WiFi.setMode(WiFi_AP_STA) 同时实例化 SoftAP 模式和 STA 模式。尝试下面的代码来实例化 softAPSTA 模式:

#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 来做“运行时配置”的事情。

关于arduino - 使 ESP32 WiFi/蓝牙协同工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65636417/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com