So we have this project which is about sending data using Adafruit QT Py ESP32-C3 Bluetooth module to another device and visualizing it, we are stuck at some point and we are facing a problem with the sampling rate, we are trying to send sine wave generated from a function generator (analog data) using the ADC of the ESP32-C3 to another device (Laptop) using Bluetooth, and then visualize it using python. We are trying to send data at a sampling rate of (100 - 500) Hz, but when we are trying to plot the received data, we only have 25 data points in one second which is 25 hertz no matter how much we change the sampling rate. So our problem is that we are seeking to receive 100 - 500 data points in 1 second on the plotting side.
AT First, the Bluetooth module receives the analog data from the function generator using an ADC pin and the following Arduino IDE code read the data uisng (AnalogRead) function, convert it into chunks of data, and sends it through BLE:
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <BLE2902.h>
BLECharacteristic* pCharacteristic;
bool deviceConnected = false;
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID_TX "beb5483e-36e1-4688-b7f5-ea07361b26a8"
const int analogInputPin = 1;
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
}
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
void setup() {
Serial.begin(115200);
Serial.println("Starting BLE work!");
BLEDevice::init("ESP32");
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
BLEService *pService = pServer->createService(SERVICE_UUID);
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_TX,
BLECharacteristic::PROPERTY_NOTIFY
);
BLE2902* p2902 = new BLE2902();
pCharacteristic->addDescriptor(p2902);
pService->start();
pServer->getAdvertising()->start();
Serial.println("Waiting for a client connection to notify...");
}
void loop() {
if (deviceConnected) {
int analogValue = analogRead(analogInputPin); // Read analog value from function generator
Serial.println(analogValue);
pCharacteristic->setValue(String(analogValue).c_str());
pCharacteristic->notify();
delay(2); // Adjust delay for the desired sampling rate
}
}
Then using python code we try to plot the data as soon as it is received from the Bluetooth module using the following code:
import asyncio
from bleak import BleakClient
import matplotlib.pyplot as plt
esp32_address = "34:B4:72:EA:77:1E"
data_x = []
data_y = []
async def run(address):
async with BleakClient(address) as client:
x = 0
while True:
value = await client.read_gatt_char("beb5483e-36e1-4688-b7f5-ea07361b26a8")
received = float(value.decode())
data_y.append(received)
data_x.append(x)
x += 1
plt.plot(data_x, data_y)
plt.xlabel('Time')
plt.ylabel('Received Value')
plt.title('Received Data from ESP32')
plt.draw()
plt.pause(0.0001)
loop = asyncio.get_event_loop()
loop.run_until_complete(run(esp32_address))