让 ESP32 WiFi/蓝牙协同工作

问题描述 投票:0回答:3

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

arduino esp32 arduino-esp8266 arduino-c++
3个回答
4
投票

menuconfig
这里。软件 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
来执行“运行时配置”操作。


0
投票

嗯..你的问题仍然存在,但你的用例..看起来很像BluFi

BluFi for ESP32 是通过蓝牙通道的 Wi-Fi 网络配置功能。它提供了一个安全协议来将 Wi-Fi 配置和凭证传递给 ESP32。使用此信息,ESP32 可以连接到 AP 或建立 SoftAP。

BluFi 层中的分片、数据加密和校验和验证是此过程的关键要素。


0
投票

Esp32 有自己的解决方案,可通过 Ble 应用程序 Espressif 配置 WiFi

https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/provisioning/wifi_provisioning.html

© www.soinside.com 2019 - 2024. All rights reserved.