Hello everyone,
I am currently working on a project that requires sending data from a pressure sensor from one chip to another chip that uses this data to control a servo motor. Currently I have been able to establish a connection between the two boards. However, the pressure data from the sensor on the central device does not seem to match with the one displaying on the peripheral device ( around 800 on central and 43 on peripheral). Please provide any guidance on my code below as I am new to the field.
Central device code:
#include <ArduinoBLE.h>
#include <Wire.h>
const char* deviceServiceUuid = "19B10000-E8F2-537E-4F6C-D104768A1214";
const char* deviceServiceCharacteristicUuid = "19B10000-E8F2-537E-4F6C-D104768A1214";
int pressure = -1;
int oldPressureValue = -1;
void setup() {
Serial.begin(9600);
while (!Serial);
if (!BLE.begin()) {
Serial.println("* Starting Bluetooth® Low Energy module failed!");
while (1);
}
BLE.setLocalName("Pressure sensor (Central)");
BLE.advertise();
Serial.println("Pressure sensor (Central Device)");
Serial.println(" ");
}
void loop() {
connectToPeripheral();
}
void connectToPeripheral(){
BLEDevice peripheral;
Serial.println("- Discovering peripheral device...");
do
{
BLE.scanForUuid(deviceServiceUuid);
peripheral = BLE.available();
} while (!peripheral);
if (peripheral) {
Serial.println("* Peripheral device found!");
Serial.print("* Device MAC address: ");
Serial.println(peripheral.address());
Serial.print("* Device name: ");
Serial.println(peripheral.localName());
Serial.print("* Advertised service UUID: ");
Serial.println(peripheral.advertisedServiceUuid());
Serial.println(" ");
BLE.stopScan();
controlPeripheral(peripheral);
}
}
void controlPeripheral(BLEDevice peripheral) {
Serial.println("- Connecting to peripheral device...");
if (peripheral.connect()) {
Serial.println("* Connected to peripheral device!");
Serial.println(" ");
} else {
Serial.println("* Connection to peripheral device failed!");
Serial.println(" ");
return;
}
Serial.println("- Discovering peripheral device attributes...");
if (peripheral.discoverAttributes()) {
Serial.println("* Peripheral device attributes discovered!");
Serial.println(" ");
} else {
Serial.println("* Peripheral device attributes discovery failed!");
Serial.println(" ");
peripheral.disconnect();
return;
}
BLECharacteristic pressureCharacteristic = peripheral.characteristic(deviceServiceCharacteristicUuid);
if (!pressureCharacteristic) {
Serial.println("* Peripheral device does not have pressure_type characteristic!");
peripheral.disconnect();
return;
} else if (!pressureCharacteristic.canWrite()) {
Serial.println("* Peripheral does not have a writable pressure_type characteristic!");
peripheral.disconnect();
return;
}
while (peripheral.connected()) {
pressure = analogRead(A0);
if (oldPressureValue != pressure) {
oldPressureValue = pressure;
Serial.print("* Writing value to gesture_type characteristic: ");
Serial.println(pressure);
pressureCharacteristic.writeValue((byte)pressure);
Serial.println(" ");
}
}
Serial.println("- Peripheral device disconnected!");
}
Peripheral code:
#include <ArduinoBLE.h>
const char* deviceServiceUuid = "19B10000-E8F2-537E-4F6C-D104768A1214";
const char* deviceServiceCharacteristicUuid = "19B10000-E8F2-537E-4F6C-D104768A1214";
int pressure = -1;
BLEService pressureService(deviceServiceUuid);
BLEByteCharacteristic pressureCharacteristic(deviceServiceCharacteristicUuid, BLERead | BLEWrite);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial);
if (!BLE.begin()) {
Serial.println("- Starting Bluetooth® Low Energy module failed!");
while (1);
}
BLE.setLocalName("Motor control (Peripheral)");
BLE.setAdvertisedService(pressureService);
pressureService.addCharacteristic(pressureCharacteristic);
BLE.addService(pressureService);
pressureCharacteristic.writeValue(-1);
BLE.advertise();
Serial.println("Motor control (Peripheral Device)");
Serial.println(" ");
}
void loop() {
// put your main code here, to run repeatedly:
BLEDevice central = BLE.central();
Serial.println("- Discovering central device...");
delay(5000);
if (central) {
Serial.println("* Connected to central device!");
Serial.print("* Device MAC address: ");
Serial.println(central.address());
Serial.println(" ");
while (central.connected()) {
if (pressureCharacteristic.written()) {
pressure = pressureCharacteristic.value();
//writePressure(pressure);
Serial.println(pressure);
}
}
Serial.println("* Disconnected to central device!");
}
}