I'm trying to do a bluetooth serial bridge with the esp32, with some timeouts to shutdown the esp if no connection is made (Retrofitting this into a Heathkit Hero Jr robot, so if a connection isn't made it will hibernate and stop drawing much battery).
The code I've cobbled together works, and connects everytime from a reset condition, but the pc won't reconnect after I disconnect the terminal program from the com port. Resetting the esp32 allows me to connect again.
What am I missing? Do I have to reset the SerialBT every connection?
Thanks.
Code below.
//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
bool isconnected = false;
bool hasconnected = 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.begin(115200);
SerialBT.begin("HeroSerialPortTest1"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}
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");
digitalWrite(LED,LOW);
isconnected = false;
lastchecked = millis();
}
}
void loop() {
SerialBT.register_callback(callback);
//Serial.println(isconnected ? "true" : "false");
if (!isconnected && !hasconnected) {
elapsed = millis() - lastchecked;
if(elapsed > 10000){
Serial.println("Timeout Reached");
lastchecked = millis();
}
}
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(20);
}
I'm not familiar with ESP 32 Bluetooth, but power-on auto-connect should be possible as part of the configuration of Bluetooth rather than the Arduino programme. This is standard practice with Bluetooth "classic". Further, the comments in this programme rather suggest that this is the intention here. It could be that you simply haven't looked deeply enough into this project. I recognise that it is simpler to configure BLE on the fly, but I submit one would only do this when there is a real need, which is not apparent here.
The ESP32 powers on, and then waits for an incoming serial bluetooth connection.
That works. Always.. I can connect and talk to the serial bridge. But If I disconnect the serial bridge on the pc side, the esp32 doesn't accept another incoming connection.
Also, this is SPP bluetooth, not BLE.. 2 different things.
My experience is with a phone, but it sounds like the PC side is like the phone--i.e. it is the device which connects to the ESP 32.
I use Kai Morich's Serial Bluetooth Terminal app on Android and it supports classic and BLE.
With the phone, I have never been able to get the SPP ("clasic bluetooth") using BluetoothSerial.h and the Arduino ide to reconnect after disconnection without resetting the ESP 32.
I do believe the reconnection without restart is possible using the espressif ide and api, but I have never gone that deep.
I do know that disconnect/reconnect is possible using the BLE of the ESP 32and the Arduino ide.
I use BLE UART service between the ESP32 and the phone, and it can disconnect and reconnect. The coding is a bit more complicated but when in operation you can work the connection like it were SPP.
I use BLE UART service between the ESP32 and the phone, and it can disconnect and reconnect. The coding is a bit more complicated but when in operation you can work the connection like it were SPP.
I didn't think a BLE uart would map to a serial port on windows.
I need it to show as a serial port, so standard terminals can be used.
The idea is to be able to use older software. The hero robot works with teraterm, and requires a delay per character and per line, otherwise the old 6808 cpu gets lost. The serial connection is to either upload machine language code, or is how you get to the basic programming language. It's also 7 Bits, Even Parity, 1 stop bit.
The robot runs off a 12v battery like you find in UPSs or alarm systems, and can stay on for hours at a time, so even half power is better than full if it's not needed. 24K of static ram, all ttl. No cmos here..
Tomorrow, I'm going to try getting the ESP-IDF environment up and running to see if I can use that, since the SerialBT seems to be broken in the arduino libraries. I've also tried just ending the connection with serialBT.end(), but that hard locks the sketch.
I have a very simple setup. it is just a main function that streams a serial output with a "fixed" value over time (every 250ms), I just update this value over Bluetooth with a custom C# application. On version 1.0.4 I can connect/reconnect as many times as I want without any issues (so far). version 1.0.5 worked on first time connect only, had to reset if by any reason I disconnected or closed my C# app. version 1.0.6 connects, and reconnect. But after reconnection, "sometimes" the updated value gets lost on the limbo. This does not happen on v1.0.4
At first I thought it could be a timer/interrupt overlay, but now its just a loop with a millis() comparison.
I would like to add this to the subject, the "serial" stream is used to "draw" a wave on a HMI (Nextion) 7'' display. I noticed that the exact same code, with the exact same fixed value (without value update), draws a completely diferent wave, it's like the "frequency" of the plot has changed.
I am attaching a picture with both wave so you can see the diference. It is the exact same code with the exact same value. It just changed the version used to compile the code.
The "wave" is the sum of a couple of sine waves. The value I update over blutooth is used to change the amplitude and frequency of the wave. But the picture shown above, it the "untouched" initial wave.
OK, this confirms that C# application can connect and reconnect like the Android phone after the 1.0.6 fix.
You now bring up two separate issues
1.)
But after reconnection, "sometimes" the updated value gets lost on the limbo. This does not happen on v1.0.4
2.)
I would like to add this to the subject, the "serial" stream is used to "draw" a wave on a HMI (Nextion) 7'' display. I noticed that the exact same code, with the exact same fixed value (without value update), draws a completely different wave, it's like the "frequency" of the plot has changed.
I am attaching a picture with both wave so you can see the difference. It is the exact same code with the exact same value. It just changed the version used to compile the code.
In my opinion, anything related to differences between 1.0.4 and 1.0.6 cores are best addressed through the GitHub page for the Arduino Core https://github.com/espressif/arduino-esp32
Getting your issues reduced to simple and reproducible examples will be very important.
There is a development release 2.0.0-alpha1 available through the Board Manager. You may want to try it, as it has some changes since 1.0.6
Not sure if it matters, but the call to "SerialBT.register_callback()" should be placed in "setup()" before "SerialBT.begin()". You should not call the register method for each iteration of "loop()".