Upload files to "BLE_client_values_record"

This commit is contained in:
Adema 2025-04-03 13:12:38 +02:00
parent 3e3c5bbb79
commit f49a7f8a0a

View File

@ -0,0 +1,83 @@
#include <NimBLEDevice.h>
#define SERVICE_UUID "12345678-1234-5678-1234-56789abcdef0"
#define CHARACTERISTIC_UUID "87654321-4321-6789-4321-fedcba987654"
NimBLEClient* pClient = nullptr;
BLERemoteCharacteristic* pRemoteCharacteristic = nullptr;
// Callback function for receiving notifications
static void notifyCallback(BLERemoteCharacteristic* pBLERemoteCharacteristic,
uint8_t* pData, size_t length, bool isNotify) {
if (length == 8) { // Expecting 4x uint16_t (8 bytes total)
uint16_t values[4];
memcpy(values, pData, sizeof(values)); // Copy received bytes into uint16_t array
// Send data to PC as CSV format
Serial.printf("%d,%d,%d,%d\n", values[0], values[1], values[2], values[3]);
} else {
Serial.println("Invalid data length!");
}
}
class MyClientCallbacks : public NimBLEClientCallbacks {
void onConnect(NimBLEClient* pClient) {
Serial.println("Connected to Server!");
Serial.printf("Negotiated MTU: %d\n", pClient->getMTU());
}
void onDisconnect(NimBLEClient* pClient) {
Serial.println("Disconnected from Server!");
}
};
void connectToServer() {
Serial.println("Initializing BLE...");
NimBLEDevice::init("");
pClient = NimBLEDevice::createClient();
if (!pClient) {
Serial.println("Failed to create client!");
return;
}
pClient->setClientCallbacks(new MyClientCallbacks());
Serial.println("Connecting to server...");
// Replace with actual server MAC address
NimBLEAddress address(std::string("64:e8:33:85:36:ee"), BLE_ADDR_PUBLIC);
if (!pClient->connect(address)) {
Serial.println("❌ Connection failed!");
return;
}
Serial.println("✅ Connected!");
BLERemoteService* pRemoteService = pClient->getService(SERVICE_UUID);
if (!pRemoteService) {
Serial.println("❌ Failed to find service!");
pClient->disconnect();
return;
}
pRemoteCharacteristic = pRemoteService->getCharacteristic(CHARACTERISTIC_UUID);
if (!pRemoteCharacteristic) {
Serial.println("❌ Failed to find characteristic!");
pClient->disconnect();
return;
}
Serial.println("✅ Subscribing to notifications...");
pRemoteCharacteristic->subscribe(true, notifyCallback);
}
void setup() {
Serial.begin(115200);
delay(1000);
connectToServer();
}
void loop() {
delay(1000);
}