My first post, and sorry for probable mistakes in English!
I am working on a university project that receives ECG (electrocardiogram) signals, digitize them by ESP32 module, and sends data over Bluetooth to an android app. (i am currently testing on Huawei mate 10 with android 8.0)
The app is supposed to draw a graph, store data, and so on.
The data send rate should ideally be 100 numbers per second. although 50 is also acceptable.
Each number is a 12 bit reading from ADC on the ESP32 side. But I can map it to one byte (0-255).
The transmission is to be only one-way, always from ESP32 to the phone.
I program the ESP32 by Arduino IDE.
Everything is going nice except one thing:
The graph is not true real-time. It shows data with about 4-5 seconds delay. The Bluetooth client looks to be slow. Although no data is missed, but the data seem to be stored in an invisible buffer and goes in a queue to be displayed.
When I turn off the ESP32 (transmitter) , the graph continues to show data for a few seconds and then stops.
Clock timer is set to 5.
If I slow down the sending rate, say less than 50 sample/sec , it will run in real-time.
At first I thought the drawing bock of the AI2 is slow. So I deleted everything like canvas, calculations, etc. and rewrote it to just read BT and print the number in a textbox. But the result was the same: data keeps coming after I turn off the transmitter!
My assumptions are:
1- the BT reading procedure is not properly selected
2- the BT transmission protocol in ESP32 is not properly selected
3- the two protocols do not match properly
4- All of the above!
Can anybody please guide me on this?
ESP32 code:
#include "BluetoothSerial.h"
#define LED_BUILTIN 2 //pin with LED to turn on when BT connected
BluetoothSerial ESP_BT; // Object for Bluetooth
// global vars
long oldmillis;
boolean BT_cnx = false;
byte a;
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_BUILTIN, HIGH);
BT_cnx = true;
}
if (event == ESP_SPP_CLOSE_EVT ) {
// Serial.println("Client disconnected");
digitalWrite(LED_BUILTIN, LOW);
BT_cnx = false;
ESP.restart();
}
}
void setup() {
// initialize digital pin 2 as an output.
pinMode(LED_BUILTIN, OUTPUT);
ESP_BT.register_callback(callback);
if (!ESP_BT.begin("SN_Ez_ECG")) {
//Serial.println("An error occurred initializing Bluetooth");
} else {
// Serial.println("Bluetooth initialized... Bluetooth Device is Ready to Pair...");
}
}
void loop() {
if (millis() - oldmillis >= 20)
{
oldmillis = millis();
a = map(analogRead(A0), 0, 4096.0, 0, 255);
ESP_BT.write(a);
}
}