Something in my code does not work as expected. In my Ino file, I first initialise the BLE (and the sensor) during setup and then loop the BLE which uses IMU data to send this. At least, that is what I want to.
Currently, I am able to connect the devices (the board of the Arduino code and a micropython board) and to test, I connected my android phone with my Arduino board which works fine. However, sending data is not succesful. If I read the data of the characteristic on my phone, it is always 0. On my micropython board, I cannot read the characteristic. (In Arduino is the server, in micropython the client (but I have central/peripheral notes as well)
Therefore, I tried printing the characteristic as you can see in the code below. The current output gives: This is no movement: 1. However, in my opinion, this comment should give characteristic 0.
What am I doing wrong? Thanks
This is my cpp file of the BLE code, in which I guess the mistake(s) are:
#include "BLE.h"
// Client
BLE_Class::BLE_Class() : IMUService("0x183E"), IMUCharacteristic("0x0541", BLERead | BLEWrite | BLENotify){
}
// functions for the BLE connection
void BLE_Class::BLE_initialisation() {
Serial.begin(9600);
while (!Serial);
//pinMode(ledPin, OUTPUT);
// if failed to initialise, print
if (!BLE.begin()) {
Serial.println("Starting BLE module failed.");
while (1);
}
// set names
BLE.setLocalName("Plushie");
BLE.setAdvertisedService(IMUService);
// add characteristic to service
IMUService.addCharacteristic(IMUCharacteristic);
// add service
BLE.addService(IMUService);
BLE.advertise();
Serial.println("BLE server is discoverable");
}
void BLE_Class::BLE_loop() {
BLEDevice central = BLE.central();
IMU_check = sense.scan_sensors();
if (central) {
Serial.print("Connected to client: ");
Serial.println(central.address());
while(central.connected()) {
BLE.advertise();
if (IMU_check == 1){
//Serial.print("Movement detected");
IMUCharacteristic.writeValue(1);
Serial.print("this is movement");
Serial.println(IMUCharacteristic);
BLE.advertise();
}
else if (IMU_check == 0){
IMUCharacteristic.writeValue(0);
Serial.print("This is no movement:");
Serial.println(IMUCharacteristic);
}
}
Serial.print(F("Disconnected from client: "));
Serial.println(central.address());
}
}