首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使ESP32 WiFi/蓝牙协同工作

使ESP32 WiFi/蓝牙协同工作
EN

Stack Overflow用户
提问于 2021-01-08 21:06:38
回答 1查看 12.1K关注 0票数 3

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

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

这个项目由SPIFFS (闪存内存保存)组成。首先从蓝牙传递WiFi ssid和密码,然后将接收到的蓝牙数据连接到一个char数组中,以便稍后将其连接到internet。

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

我搜索了一下,发现了一些关于Menuconfig的东西,但是我找不到它的位置。

这就是我读到的:EN.pdf

代码语言:javascript
复制
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

这是代码的一部分:

代码语言:javascript
复制
// ------------------------------ 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和蓝牙。

EN

回答 1

Stack Overflow用户

发布于 2021-01-09 04:27:53

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

代码语言:javascript
复制
#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来进行“运行时配置”。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65636417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档