🛠 Diffuser ses capteurs en BLE avec ArduinoBLE

Arduino Nicla Sense ME · intermediaire · Schéma : IMG-nicla-sense-me-intermediaire-2-niclasense-ble-arduino.svg — Nicla Sense ME au centre émettant des ondes BLE vers un smartphone affichant une carte de caractéristique température, module ANNA-B112 et puce nRF52832 annotés sous la carte

Objectives

Transform the Nicla Sense ME into a Bluetooth Low Energy peripheral that publishes its sensor temperature via a standard characteristic, using the ArduinoBLE library (v2.1.0, which explicitly lists this board). Hardware precision: BLE is not handled by an STM32WB55 as on other Arduino boards, but by the u-blox ANNA-B112 module, a SiP package that actually contains... an nRF52832 Nordic, which serves as the board's main microcontroller (the module runs on a 1.8 V rail; Bluetooth 5.0 according to the module datasheet, versus "4.2" on the store page, a mention considered obsolete). This same nRF52832 executes your sketch: BLE is therefore native, with no coprocessor.

Hardware

Complete Code

#include "Arduino_BHY2.h"
#include <ArduinoBLE.h>

Sensor temp(SENSOR_ID_TEMP);

BLEService serviceEnv("181A");   // service Environmental Sensing
BLEFloatCharacteristic tempChar("2A6E", BLERead | BLENotify); // Temperature

void setup {
  Serial.begin(115200);
  BHY2.begin;
  temp.begin;

  if (!BLE.begin) {
    Serial.println("Demarrage BLE impossible");
    while (1);
  }
  BLE.setLocalName("NiclaTemp");
  BLE.setAdvertisedService(serviceEnv);
  serviceEnv.addCharacteristic(tempChar);
  BLE.addService(serviceEnv);
  BLE.advertise;
  Serial.println("Peripheral BLE en attente de connexion");
}

void loop {
  static auto printTime = millis;
  BHY2.update;   // pompe les capteurs
  BLE.poll;      // sert la pile BLE
  if (millis - printTime >= 2000) {
    printTime = millis;
    if (temp.dataAvailable) {
      tempChar.writeValue(temp.value);
      Serial.println(String("Temperature diffusee : ") + String(temp.value, 2));
    }
  }
}

Expected Verification

With nRF Connect, the "NiclaTemp" peripheral is visible during scanning; upon connecting, you will find service 0x181A and characteristic 0x2A6E, whose value tracks the board's temperature (enable notify, then blow on the sensor). The module's advertised range reaches 160 m in open conditions (+5 dBm EIRP).

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials