Hey
I've got two devices that are connected via Bluetooth. the master is an ESP32 (which uses the BluetoothSerial library to connect and send data), and the slave is an Arduino Uno with a HC-05 module (connected with SoftwareSerial).
There is a connection between them and I'm able to send data back and forth, but when trying to send messages very frequently, it's being delayed for almost 3 seconds...
the ESP32 sends a string from reading a Wii Nunchuk (connected with I2C), which also runs great when printing directly to the COM port.
sure sure...
this is the sketch for the Uno receiving.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
mySerial.begin(9600);
}
void loop() { // run over and over
if (mySerial.available()) {
Serial.write(mySerial.read());
mySerial.flush();
}
if (Serial.available()) {
mySerial.write(Serial.read());
Serial.flush();
}
}
and this is the ESP32 sketch sending
#include <Nunchuk.h>
#include <Wire.h>
#include "BluetoothSerial.h"
#define IDLE_STT 0
#define RUN_STT 1
BluetoothSerial SerialBT;
String MACadd = "AA:BB:CC:11:22:33";
uint8_t address[6] = {0x98, 0xD3, 0x21, 0xF7, 0x3F, 0xD0};
//uint8_t address[6] = {0x00, 0x1D, 0xA5, 0x02, 0xC3, 0x22};
String name = "HC-05";
char *pin = "1234"; //<- standard pin would be provided by default
bool connected;
char buff[100] = {'\0'};
int mode = IDLE_STT;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test", true);
Serial.println("The device started in master mode, make sure remote BT device is on!");
connected = SerialBT.connect(address);
if (connected) {
Serial.println("Connected Succesfully!");
} else {
while (!SerialBT.connected(10000)) {
Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
}
}
SerialBT.connect();
Wire.begin();
pinMode(LED_BUILTIN, OUTPUT);
Wire.setClock(400000);
nunchuk_init();
}
void loop() {
if (connected) {
nunchuk_read();
nunchuk_get_vals(buff);
SerialBT.println(buff);
//Serial.println(buff);
delay(2);
}
}
BTW, nunchuk_get_vals(buff) is a function I wrote in the Nunchuk library the is similar to the nunchuk_print(), but just puts the data in a buffer instead of printing to serial.
I'm not sure that the module is defective, because it does connect and send data. tried another one (also brand new), and got the same results.
But, I know the code works as it should, because I've connected another ESP32 in the same way to receive data and that worked perfectly with no delay whatsoever.
So... maybe it is defective? not sure. I guess it is :\
Probably not, but it shouldn't be hard to find out. In the immortal words of a serial guru round here, Bluetooth is just serial without wires. This means you can use the same code, with Uno at least, while dispensing with Bluetooth and using serial with wires. I guess this means you will have to use different pins on the ESP32 and change the code accordingly but this should enable you to find out if Bluetooth is innocent.
Honestly? I'm not going to bother myself with this test as a connection between two ESP32 works for me. And as a bonus, I can register a callback for all kinds of events in the receiver...
So that's what I'm going with for now.
My best guess is there's some conflict with the timeout set for data transaction, the buffer size or even the default baud rate. Because all of this parameters have default values I didn't change and don't appear in the primary function calls (i.e - the begin call for the ESP32 BT is just it's name and if it is or isn't the master device: SerialBT.begin("ESP32", true))
If anyone in the future sees this and have an answer to why this happens, I would like to know. But for now I'm just going to "waist" another ESP32 and move on...