首页 > 解决方案 > 让 ESP32 WiFi/蓝牙协同工作

问题描述

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

标签: arduinoesp32arduino-esp8266arduino-c++

解决方案


menuconfig就在这里。软件 WiFi/BL 共存的代码在这里。相关问题在这里。因为 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的“运行时配置”事情。


推荐阅读