Communication of data between two Arduino Nano 33 via BLE

Hi everyone, I'm working on a project for which I'm using two Arduino Nano. I'm using an Arduino Nano 33 BLE as a peripheral device and an Arduino Nano 33 BLE Sense as the central device.

The idea is that my Arduino Nano 33 BLE has a temperature sensor (TMP36) connected to the analog input 0 and alimented with the 3.3 V pin of the board. Then the program should read the value of the temperature sensor, convert it in degree celsius and send it via bluetooth low energy.

Then my Arduino Nano 33 BLE Sense should receive those values and after reading them, print them on the serial monitor.

Now the thing that I don't understand is that the two Arduino communicates between them without problems; when I print the temperature on the serial monitor of the peripheral device, I can see the correct value, but on the serial monitor of the central device, i only get 0.00 and I don't find the problem in my code.

The following code is the one of the Peripheral Device (Arduino Nano 33 BLE):

/*
  Peripheral_device.ino

  This program uses the ArduinoBLE library to set-up an Arduino Nano 33 BLE 
  as a peripheral device and specifies a service and a characteristic. 
*/

#include <ArduinoBLE.h>

const char* deviceServiceUuid = "19b10000-e8f2-537e-4f6c-d104768a1214";
const char* deviceServiceCharacteristicUuid = "19b10001-e8f2-537e-4f6c-d104768a1214";

BLEService temperatureService(deviceServiceUuid);
BLEByteCharacteristic temperatureCharacteristic(deviceServiceCharacteristicUuid, BLERead | BLENotify);

float temperatureValue = 0.0;
const int tempPin = A0;

void setup() {
  Serial.begin(9600);
  pinMode(tempPin, INPUT);
  while (!Serial)
    ;

  if (!BLE.begin()) {
    Serial.println("- Starting Bluetooth® Low Energy module failed!");
    while (1)
      ;
  }

  BLE.setLocalName("Arduino Nano 33 BLE (Peripheral)");
  BLE.setAdvertisedService(temperatureService);
  temperatureService.addCharacteristic(temperatureCharacteristic);
  BLE.addService(temperatureService);
  temperatureCharacteristic.writeValue(temperatureValue);
  BLE.advertise();

  Serial.println("Nano 33 BLE (Peripheral Device)");
  Serial.println(" ");
}

void loop() {
  BLEDevice central = BLE.central();
  Serial.println("- Discovering central device...");
  delay(500);
          int sensorValue = analogRead(0);

        float voltage = sensorValue * 3.3;
        voltage /= 1024.0;
        temperatureValue = (voltage - 0.5) * 100;
        temperatureCharacteristic.writeValue(temperatureValue);
        Serial.println(temperatureValue);
  if (central) {
    Serial.println("* Connected to central device!");
    Serial.print("* Device MAC address: ");
    Serial.println(central.address());
    Serial.println(" ");

    while (central.connected()) {
        int sensorValue = analogRead(0);

        float voltage = sensorValue * 3.3;
        voltage /= 1024.0;
        temperatureValue = (voltage - 0.5) * 100;
        temperatureCharacteristic.writeValue(temperatureValue);
      delay(500);
    }
    Serial.println("* Disconnected to central device!");
  }
}

The next one on the other hand, it's the code of the Central Device (Arduino Nano 33 BLE Sense):

/*
  Central_Device.ino

  This program uses the ArduinoBLE library to set-up an Arduino Nano 33 BLE Sense 
  as a central device and looks for a specified service and characteristic in a 
  peripheral device.
*/

#include <ArduinoBLE.h>

const char* deviceServiceUuid = "19b10000-e8f2-537e-4f6c-d104768a1214";
const char* deviceServiceCharacteristicUuid = "19b10001-e8f2-537e-4f6c-d104768a1214";  

void setup() {
  Serial.begin(9600);
  while (!Serial);
  
  
  if (!BLE.begin()) {
    Serial.println("* Starting Bluetooth® Low Energy module failed!");
    while (1);
  }
  
  BLE.setLocalName("Nano 33 BLE (Central)"); 
  BLE.advertise();

  Serial.println("Arduino Nano 33 BLE Sense (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;
  }
  float temperatureValue = 0.0;
  BLECharacteristic temperatureCharacteristic = peripheral.characteristic(deviceServiceCharacteristicUuid);

  while (peripheral.connected()) {
       // temperatureValue = temperatureCharacteristic.read();
       // temperatureCharacteristic.readValue(&temperatureValue, 4);
        Serial.println(temperatureValue);
  }
  Serial.println("- Peripheral device disconnected!");
}

Hoping in an answer I wish everyone a good weekend.
Fraska

1 Like

My advice is to change the characteristic on the peripheral from byte to the float value you actually want to send.

//BLEByteCharacteristic temperatureCharacteristic(deviceServiceCharacteristicUuid, BLERead | BLENotify);

BLEFloatCharacteristic temperatureCharacteristic(deviceServiceCharacteristicUuid, BLERead | BLENotify);

Then on the central use this reading routine to pick up the 4 bytes sent.

while (peripheral.connected()) {
       // temperatureValue = temperatureCharacteristic.read();
        temperatureCharacteristic.readValue(&temperatureValue, 4);
        Serial.println(temperatureValue);
  }

Thank you very much, I hadn't noticed this error and it actually solved my problem. Now everything works perfectly!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.