Beginning Serial Bluetooth question.
Posted: Mon Mar 08, 2021 6:36 pm
I'm making a simple bluetooth serial bridge, SPP. But I can't seem to reconnect at a later time after I disconnect.
My first thought is stopping the serialbt, and then restarting it again, but serialBT.end(), locks up my sketch completely.
Any thoughts?
Thanks.
My first thought is stopping the serialbt, and then restarting it again, but serialBT.end(), locks up my sketch completely.
Any thoughts?
Thanks.
Code: Select all
//HeroJR Bluetooth the Serial Bridge (supports 7,E,1)
//2021 dabone
//based off the example code in arduino.
//
//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial
#include "BluetoothSerial.h"
#define LED 2
char RobotName[] = "HeroSerialPortTest1"; //Name Your Robot's Bluetooth
#define BTtimeout 60000 //How Many Milliseconds to wait before powerdown. 60000 is one minute
bool isconnected = false;
bool hasconnected = false;
bool btstarted = false;
unsigned long lastchecked;
unsigned long elapsed;
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
void setup() {
pinMode(LED,OUTPUT);
lastchecked = millis();
//Serial.begin(9600,SERIAL_7E1); //Serial Settings for the HERO Jr.... Don't you love 80's tech?
Serial.begin(115200); //Serial Settings for debug.
}
void callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param){
if(event == ESP_SPP_SRV_OPEN_EVT){
Serial.println("Client Connected");
digitalWrite(LED,HIGH);
isconnected = true;
hasconnected = true;
}
if(event == ESP_SPP_CLOSE_EVT ){
Serial.println("Client disconnected");
isconnected = false;
Serial.println("Isconnected set to off");
lastchecked = millis();
digitalWrite(LED,LOW);
Serial.println("Led Turned Off");
}
}
int startBT (){
SerialBT.begin(RobotName); //Bluetooth device name
btstarted = true;
}
int restartBT (){
Serial.println ("Stopping BT");
SerialBT.flush();
SerialBT.end();
Serial.println ("Re-starting BT");
delay (1000);
btstarted = false;
hasconnected = true;
}
int serialbridge() {
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(20);
}
void loop() {
if (!btstarted) { //Is the Bluetooth SPP Started yet? If not, so the initial start.
startBT();
Serial.println("Bluetooth active");
}
SerialBT.register_callback(callback); //Check the status of the Bluetooth Connection
if (!isconnected && !hasconnected) { //Do we have a incoming connection active? and also, has there ever been a active incoming connection?
elapsed = millis() - lastchecked; //If neither is true, then lets check for the time out. If we get to the final timeout, put the esp32
if(elapsed > BTtimeout){ //in deep sleep mode to save battery power on the robot.
Serial.println("Timeout Reached");
lastchecked = millis();
//esp_deep_sleep_start();
}
}
if (!isconnected && hasconnected) { //If we have connected, and then disconnected, restart the Bluetooth so we can connect again.
restartBT();
}
if (isconnected) { //If we have a active connection, process serial data on the bridge
serialbridge ();
}
}