简体   繁体   中英

Making ESP32 WiFi/Bluetooth work together

I'm creating a App that involves Bluetooth and WiFi. I firstly connect the Phone to the Bluetooth and pass the WiFi ssid and password through it. Then after receiving the ssid and the password I attempt to connect it to the WiFi. I manage to store the ssid and password into a char array.

After sending the data from Bluetooth, and the ESP32 making the connection with the WiFi, the Bluetooth gets disconnected. I can't connect the Bluetooth again because WiFi is connected, since I control certain statements from the code via Bluetooth.read(), I will need to allow the connection to coexist between WiFi and Bluetooth.

The project consist on SPIFFS (Flash memory saving). The WiFi ssid and password are passed from Bluetooth firstly, then, I make the connection from the Bluetooth data received into a char array, so I can connect it to the internet later on.


I thought firstly on adding a callback that alerts when the device is attempting to connect, but couldn't find in the documentation any code that was able to do so. I have a callback function that tells me when the device connects and disconnects.

I searched up and found something about Menuconfig but I couldn't find where it was located.
This is what I was reading from: 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).

I couldn't find where menuconfig was located.
I'm using the ```BluetoothSerial.h`` library
This is a piece of the code:

// ------------------------------ 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);

What I needed was to be able to make a connection between WiFi and Bluetooth simultaneously.

The menuconfig is here . The code for software WiFi/BL coexistence is here . And the related issue is here . Because WiFi and Bluetooth use the same antenna, i wouldn't recommend doing WiFi/BL coexistence altogether. Instead, you may use WiFi.setMode(WiFi_AP_STA) to instantiate SoftAP mode and STA mode simultaneously. Try the code below to instantiate both softAP and STA mode:

#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(){}

You can create simple WebServer to do "runtime configuration" things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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