ESP32 Bluetooth+WiFi+HTMLClient request problem

Hi everyone, i have a problem with a esp32 where i am using 3 libraries, bluetooth, wifi and httpclient.

I am getting a BLE Sensor data, scanning the bluetooth and connecting with a service from this sensor, at the same time i'm connecting to my wi-fi house .

This works pretty good, i don't have problem with this, i adjust the code because i want esp32 always scan bluetooth when it lose the ble sensor signal, and always connect when find it again, same thing with wi-fi, and it works.

The problem comes when i use HTTPClient library, this is the thing, i created a function that do all the process for a POST request, and use it when i'm connected to my wifi, it's not neccesary be connected to the ble sensor, but when it's not connected, the scan start and pauses the rest of the loop you know .

So when i run the program, this happen:

  • The BLE Sensor is on, so at start of the program esp 32 will scan for a little, and will connect to the BLE service.

  • My wifi is available too, so it'll start connecting this, but at start of the loop it'll not be connected because there's no time, first scan BLE, connect to this, and the second iteration, it'll be connected to wi-fi.

  • Because esp32 is connected to wi-fi, in the second iteration, will start doing http POST **
    ** Request, and IT WORKS, i got this:

    20262
    3
    HTTP Response code: 201
    {
    "id": 101
    }

  • So the problem is, when i turn off my ble sensor, simulating that the ble signal lost, first the esp32 program start scanning again bluetooth near signal.

  • After this i turn on my BLE Sensor, and the esp32 find the signal and start connecting again.

  • The program connect, the wifi keeps working and remains connected to my house.

  • but after linking to my ble sensor, the POST HTML Request works 1 or 3 times and after that always fails, getting this:
    341051
    3
    Error code: -1
    ...

This is my problem, after linking to my sensor by second time, all my POST http request is being rejected or is not being sent :/.

The curious thing is, if we suppose that after linking to the sensor for the first time, and linking for a second time(failing POST request), i turn off the sensor and deactivate the ble scan for esp32, the POST request works again, so the problem is when i linking with the ble sensor for a second time, because at the first time, it works.

Sorry by this long text but this is a complex problem for me, i'll be thankfully for any help :s, this is my code:

//--------------------------------------------------------Bluetooth----------------------------------------------------------------------
#include "BLEDevice.h"

// The remote service we wish to connect to.
static BLEUUID serviceUUID("0000FFE5-0000-1000-8000-00805F9A34FB");
// The characteristic of the remote service we are interested in.
static BLEUUID    charUUID("0000FFE4-0000-1000-8000-00805F9A34FB");

static boolean doConnect = false;
static boolean connected = false;
static boolean doScan = true;
static BLERemoteCharacteristic* pRemoteCharacteristic;
static BLEAdvertisedDevice* myDevice;
String value = "";

static void notifyCallback(
  BLERemoteCharacteristic* pBLERemoteCharacteristic,
  uint8_t* pData,
  size_t length,
  bool isNotify) {
    value = "";
    for ( size_t i = 0; i < length; i++ )
    {
      value+= String(pData[i],HEX);
    }
    //Serial.println(value);
}

class MyClientCallback : public BLEClientCallbacks {
  void onConnect(BLEClient* pclient) {
  }

  void onDisconnect(BLEClient* pclient) {
    connected = false;
    Serial.println("onDisconnect");
  }
};

bool connectToServer() {
    Serial.print("Forming a connection to ");
    Serial.println(myDevice->getAddress().toString().c_str());
    
    BLEClient*  pClient  = BLEDevice::createClient();
    Serial.println(" - Created client");

    pClient->setClientCallbacks(new MyClientCallback());

    // Connect to the remove BLE Server.
    bool succes = pClient->connect(myDevice);  // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private)

    if (succes == true){
      Serial.println(" - Connected to server");  
   
      // Obtain a reference to the service we are after in the remote BLE server.
      BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
      if (pRemoteService == nullptr) {
        Serial.print("Failed to find our service UUID: ");
        Serial.println(serviceUUID.toString().c_str());
        pClient->disconnect();
        return false;
      }
      Serial.println(" - Found our service");
    

      // Obtain a reference to the characteristic in the service of the remote BLE server.
      pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
      if (pRemoteCharacteristic == nullptr) {
        Serial.print("Failed to find our characteristic UUID: ");
        Serial.println(charUUID.toString().c_str());
        pClient->disconnect();
        return false;
      }
      Serial.println(" - Found our characteristic");

      // Read the value of the characteristic.
      if(pRemoteCharacteristic->canRead()) {
        std::string value = pRemoteCharacteristic->readValue();
        Serial.print("The characteristic value was: ");
        Serial.println(value.c_str());
      }

      if(pRemoteCharacteristic->canNotify())
        pRemoteCharacteristic->registerForNotify(notifyCallback);

        connected = true;
        return true;
      }
    else{
      Serial.println(" - Connection Lost, Returning to scanning");
      pClient->disconnect();
      return false;
    }
}
/**
 * Scan for BLE servers and find the first one that advertises the service we are looking for.
 */
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
 /**
   * Called for each advertising BLE server.
   */
  void onResult(BLEAdvertisedDevice advertisedDevice) {
    Serial.print("BLE Advertised Device found: ");
    Serial.println(advertisedDevice.toString().c_str());

    // We have found a device, let us now see if it contains the service we are looking for.
    if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID)) {

      BLEDevice::getScan()->stop();
      myDevice = new BLEAdvertisedDevice(advertisedDevice);
      doConnect = true;
      //doScan = true;

    } // Found our server
  } // onResult
}; // MyAdvertisedDeviceCallbacks

//---------------------------------------------------------------Wi-Fi----------------------------------------------------------------
#include <WiFi.h>
#define WIFI_SSID "Familia_Galan"
#define WIFI_PASSWORD "1098795837"

unsigned long currentMillis = 0;
unsigned long previousMillis = 0;

unsigned long interval = 10000;
//---------------------------------------------------------------------------HTTP Client------------------------------------------------------
#include <HTTPClient.h>

unsigned long previousMillis2 = 0;
unsigned long interval2 = 500;

const char* server = "https://jsonplaceholder.typicode.com/posts/";

String postRequest(const char* serverName){
  HTTPClient http;
  http.begin(serverName);
  http.addHeader("Content-Type", "text/plain");

  int httpResponseCode = http.POST("Posting for esp32");

  String payload = "...";
  if (httpResponseCode > 0){
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    payload = http.getString();
  }else{
    Serial.print("Error code: ");
    Serial.println(httpResponseCode);
  }

  http.end(); 
  return payload;
}
//-----------------------------------------------------------------Void setup----------------------------------------------------------------------

void setup() {
  Serial.begin(115200);
  Serial.println("Starting Arduino BLE Client application...");
  BLEDevice::init("");
  // Retrieve a Scanner and set the callback we want to use to be informed when we
  // have detected a new device.  Specify that we want active scanning and start the
  // scan to run for 5 seconds.
  BLEScan* pBLEScan = BLEDevice::getScan();
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setInterval(1349);
  pBLEScan->setWindow(449);
  pBLEScan->setActiveScan(true); 
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  
} // End of setup.


//-------------------------------------------------------------Void loop----------------------------------------------------------------------------
// This is the Arduino main loop function.
void loop() {
  currentMillis = millis();
  Serial.println(currentMillis);
  Serial.println(WiFi.status());
 
  if (WiFi.status() == WL_CONNECTED){    
    if(currentMillis - previousMillis2 >= interval2){
      String prueba = postRequest(server);
      Serial.println(prueba);
      previousMillis2 = currentMillis;
    }
    
  }else if((WiFi.status() != WL_CONNECTED)&&(currentMillis - previousMillis >=interval)){ //This is reconnecting wifi purpose, each 10 seconds we disconnect and connect.
      Serial.println("Reconnecting to WiFi...");
      WiFi.disconnect();
      WiFi.reconnect();
      previousMillis = currentMillis; 
  }
  
  //------------------------------------Bluetooth---------------------------------------------------- 
  // If the flag "doConnect" is true then we have scanned for and found the desired
  // BLE Server with which we wish to connect.  Now we connect to it.  Once we are 
  // connected we set the connected flag to be true.
  if (doConnect == true) {
    if (connectToServer()) {
      Serial.println("We are now connected to the BLE Server.");
    } else {
      Serial.println("We have failed to connect to the server; there is nothin more we will do.");
    }
    doConnect = false;
  }
  
  // If we are connected to a peer BLE Server, update the characteristic each time we are reached
  // with the current time since boot.
  if (connected) {
    
  }else if(doScan){
    //WiFi.disconnect();
    BLEDevice::getScan()->start(0);  // this is just example to start scan after disconnect, most likely there is better way to do it in arduino
  }
   // Delay a second between loops.
   delay(500);
} // End of loop

In an ESP32, BT and Wi-Fi share a radio so while the ESP32 is sending/receiving a BT packet, it cannot listen to or send a WiFi packet.
Switching between the two is possible but error prone.

i understand but, why it works in the first connection with BLE, and the second i doesnt?, besides i didn't mention but with get request i don't have this problem

timing issues may be ? (switching between radios)

:S, i don't know man, running the program first time, esp32 scan ble and connect, so it supposed it would use that radio, but after that i connect with wifi and start sendind post request, it works.

You may be right, but if it were completely like this, the problem would be replicated at the moment I connect bluetooth and then wifi, and send the post request.

Because the first time, look, I print everything on the serial port and indeed I receive the data from the sensor and make the requests correctly:

HTTP Response code: 201
5561eaffecff08000000aeff7705427 //Sensor data
{
"id": 101
}

The problem comes when I disconnect from the sensor for the first time and reconnect to it for the second time, it is super strange, it is as if the problem with the radios that you tell me happened is to the second connection, not the first.

I think you are right, because the problem occurs right when this is launched, I checked:

if (connectToServer()) {

But it puzzles me a lot that this happens the second time, if it were as you say it should always fail.

Can you think of something that may cause the first time to work and not the second? Is it possible that I am not correctly disconnecting from my sensor the first time? I am still very new to this, and I may not be using it well, the bluetooth library.

Technically it could somewhat work if there are not conflicting transactions and you switch radios slowly enough (maybe add some delays at the end of a Wi-Fi transaction to terminate fully the transaction). So might be fine the first time as things are in sequence.

it is still a bit of black magic and a shot in the dark and you don't control much... I'd stay away from an architecture with both radios being required on ESP32 (may be add a separate BLE module)

1 Like

I understand, well, always i can use
ESP.restart() after a disconnection to bluetooth
:neutral_face:, it's horrible but for the moment is what I can do.

Essentially I think that you are absolutely right, if it fails in the second connection with bluetooth and it works again when I disconnect it is because there is a conflict of channels, on top of that if it is something that I cannot control well ...

Well, thanks for the advice man, it has served me well to clear doubts

yeah sometimes it's frustrating...
good luck with your project!

1 Like