I have a project that I personally want to do. It requires a proof of concept timing work where the optimum transmission source would be BLE. I have a Lafvin ESP32 basic starter kit, I have installed ESP32 by Espressif Systems for this.
My phone has nRF Connect to help test and troubleshoot the situation.
The code I current have is this:
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
// Define your BLE service and characteristic UUIDs
#define SERVICE_UUID "12345678-1234-1234-1234-123456789012"
#define CHARACTERISTIC_UUID "87654321-4321-4321-4321-210987654321"
BLECharacteristic *pCharacteristic;
bool deviceConnected = false;
#define LED_PIN 2 // Change if you're using a different pin for LED
// Define the BLE callbacks
class MyCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer *pServer) {
Serial.println("Client connected.");
}
void onDisconnect(BLEServer *pServer) {
Serial.println("Client disconnected.");
}
void onWrite(BLECharacteristic *pCharacteristic) {
// Use String instead of std::string for compatibility with Arduino
String rxValue = pCharacteristic->getValue().c_str();
Serial.print("Received value: ");
Serial.println(rxValue);
}
};
void setup() {
// Start the Serial communication for debugging
Serial.begin(115200);
delay(1000); // Wait for the serial monitor to open
// Set LED pin as OUTPUT
pinMode(LED_PIN, OUTPUT);
// Initialize BLE
BLEDevice::init("ESP32_BLE_Device");
// Create the BLE Server
BLEServer *pServer = BLEDevice::createServer();
// Set the server callbacks
pServer->setCallbacks(new MyCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create the BLE Characteristic
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY
);
// Start the service
pService->start();
// Start advertising the BLE device
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
Serial.println("Waiting for a client connection...");
}
void loop() {
// If a device is connected, turn on the LED (just as an indicator)
if (deviceConnected) {
digitalWrite(LED_PIN, HIGH); // Turn on LED
} else {
digitalWrite(LED_PIN, LOW); // Turn off LED
}
delay(1000);
}
I pulled all of my bells and whistles to try to get this to work. An earlier iteration, now lost, did partially work. However, I was never able to get the nRF Connect to send information I could see working in the Serial Monitor of the Arduino IDE.
My goals are:
- Connect my laptop to the ESP32 via Micro USB, then connect my phone to the ESP32 via BLE Bluetooth.
- Have the blue light on when there is a connection to the ESP32, and flashing when data is being transmitted either way (other than keeping the connection going).
- Be able to confirm that it is working.
I wrote some proprietary software using python on my laptop, I will also create an APK for my phone with proprietary software that will interact with the laptop via the ESP32. The ESP32 is being used specifically for testing the speed of the connection, how much throughput per x time, how many error packages, how many headers, etc. All of the data collection is on the laptop and phone, the ESP32 is specifically just a bluetooth BLE 'modem' in the preferred configuration.
Any advise would be greatly appreciated.