4

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.

Nilton Schumacher F
  • 814
  • 3
  • 13
  • 43

2 Answers2

3

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.

Wachid Susilo
  • 496
  • 4
  • 11
  • I need to be able to use Bluetooth and WiFi, but the point is, when I attempt to realize a Bluetooth connection with the ESP, I would need to disconnect the WiFi (When attempting to connect the Bluetooth), and after sending the data from Bluetooth I could disconnect the Bluetooth so the WiFi can take control of the antenna. I don't think that softAP would solve my problem since I don't need a access point. Maybe STA can solve the problem, but I'm not sure... – Nilton Schumacher F Jan 11 '21 at 04:22
  • 1
    I had a similar problem. Using WiFi to find get some clients and then sharing a BT-UUID so that only the selected client had access to the ESP device. I now use 2 ESPs: one with WiFi and one with BT connected via serial interface. Maybe this could be a solution for you too. – Adriano Jan 11 '21 at 13:46
0

Well.. Your question remains, but your use case though.. Seems a lot like BluFi.

The BluFi for ESP32 is a Wi-Fi network configuration function via Bluetooth channel. It provides a secure protocol to pass Wi-Fi configuration and credentials to ESP32. Using this information, ESP32 can then connect to an AP or establish a SoftAP.

Fragmenting, data encryption, and checksum verification in the BluFi layer are the key elements of this process.

ArjanW
  • 443
  • 4
  • 19