繁体   English   中英

让 ESP32 WiFi/蓝牙协同工作

[英]Making ESP32 WiFi/Bluetooth work together

我正在创建一个涉及蓝牙和 WiFi 的应用程序。 我首先将手机连接到蓝牙并通过它传递 WiFi ssid 和密码。 然后在收到 ssid 和密码后,我尝试将其连接到 WiFi。 我设法将 ssid 和密码存储到一个字符数组中。

从蓝牙发送数据后,ESP32 与 WiFi 建立连接,蓝牙断开连接。 由于已连接 WiFi,我无法再次连接蓝牙,因为我通过 Bluetooth.read() 从代码中控制某些语句,因此我需要允许 WiFi 和蓝牙之间的连接共存。

该项目包括 SPIFFS(闪存 memory 保存)。 WiFi ssid 和密码首先从蓝牙传递,然后,我将接收到的蓝牙数据连接到一个字符数组中,以便稍后将其连接到互联网。


我首先想到的是添加一个回调,在设备尝试连接时发出警报,但在文档中找不到任何能够这样做的代码。 我有一个回调 function 告诉我设备何时连接和断开。

我搜索并找到了有关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这里 软件 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来做“运行时配置”的事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM