SerialBT.println failing when callback contains delay() command

xgarbxgarb
Posts: 18
Joined: Fri Jan 25, 2019 3:36 pm

SerialBT.println failing when callback contains delay() command

Postby xgarbxgarb » Fri Oct 11, 2019 6:23 pm

In the code below if I uncomment either the delay() or restart() lines I don't receive the 'Restarting...' message on my phone.

I can understand why just restarting with a delay might kill the serial Bluetooth message before it's sent but why would just a delay command affect it?

Code: Select all

void callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
  if (event == ESP_SPP_DATA_IND_EVT && client_wifi_ssid != "not_set") {
    client_wifi_password = SerialBT.readString();
    SerialBT.println("Restarting...");
    preferences.putString("pref_ssid", client_wifi_ssid);
    preferences.putString("pref_pass", client_wifi_password);
    preferences.end();
    //delay(1000);
    //ESP.restart();
  }
}

xgarbxgarb
Posts: 18
Joined: Fri Jan 25, 2019 3:36 pm

Re: SerialBT.println failing when callback contains delay() command

Postby xgarbxgarb » Wed Nov 13, 2019 10:54 am

Still having a problem with this. I can get it to either show the message over Bluetooth or disconnect but not both. I've tried various combinations of BT commands. New code below. Using Platformio this time.

Code: Select all

#include <Arduino.h>
#include <BluetoothSerial.h>

const char *bluetooth_name = "projectx2";

BluetoothSerial SerialBT;

void callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param);

void setup()
{
    Serial.begin(115200);
    Serial.println("Booting...");
    SerialBT.register_callback(callback);
    SerialBT.begin(bluetooth_name);

}


void callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
{
    if (event == ESP_SPP_SRV_OPEN_EVT)
    {

        Serial.println("Hello World");
        SerialBT.println("Hello World");
        SerialBT.end();
        SerialBT.flush();

        Serial.println("connected still");
        SerialBT.disconnect();
        btStop();
        Serial.println("BT stopped");
    }
}

void loop()
{
}

chegewara
Posts: 963
Joined: Wed Jun 14, 2017 9:00 pm

Re: SerialBT.println failing when callback contains delay() command

Postby chegewara » Thu Nov 14, 2019 12:09 pm

In general its bad idea to have delay or long running code in callbacks.

xgarbxgarb
Posts: 18
Joined: Fri Jan 25, 2019 3:36 pm

Re: SerialBT.println failing when callback contains delay() command

Postby xgarbxgarb » Sun Dec 22, 2019 7:29 pm

Thanks for the tip. I've done this which seems to work...

Code: Select all

#include <Arduino.h>
#include <BluetoothSerial.h>

BluetoothSerial SerialBT;

const char *bluetooth_name = "robot01";
bool bluetooth_disconnect = false;
void callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param);

void setup()
{
  Serial.begin(115200);
  Serial.println("Booting...");
  SerialBT.register_callback(callback);
  SerialBT.begin(bluetooth_name);
}

void callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
{
  if (event == ESP_SPP_SRV_OPEN_EVT)
  {
    Serial.println("Hello World");
    SerialBT.println("Hello World");
    bluetooth_disconnect = true;
  }
}

void loop()
{
  if (bluetooth_disconnect)
  {
    Serial.println("Connected still");
    delay(1000);    
    SerialBT.disconnect();
    btStop();
    Serial.println("BT stopped");
    bluetooth_disconnect = false;
  }
}

Who is online

Users browsing this forum: No registered users and 34 guests