🛠 Créer un périphérique Bluetooth LE piloté depuis le téléphone

Arduino MKR WiFi 1010 · intermediaire · Schéma : IMG-mkr-wifi-1010-intermediaire-3-mkrwifi-ble-peripheral.svg — MKR WiFi 1010 en périphérique BLE diffusant un service « Led » avec une caractéristique lisible et inscriptible, smartphone exécutant nRF Connect en rôle central.

Objectives

Enable the Bluetooth Low Energy of the NINA-W102 module using the ArduinoBLE library: the board acts as a peripheral, advertising a custom service with a read/write characteristic, and a phone (free nRF Connect app) turns on the D6 LED by writing 0 or 1. The MKR WiFi 1010 is one of the few microcontroller platforms that supports BLE client and host.

Hardware

Complete Code

#include "ArduinoBLE.h"

BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214");
BLEUnsignedCharCharacteristic ledChar(
    "19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);

void setup {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);          // D6
  if (!BLE.begin) { Serial.println("BLE KO"); while (1); }
  BLE.setLocalName("MKRWiFi1010");
  BLE.setAdvertisedService(ledService);
  ledService.addCharacteristic(ledChar);
  BLE.addService(ledService);
  ledChar.writeValue(0);
  BLE.advertise;
  Serial.println("en attente d'un central...");
}

void loop {
  BLEDevice central = BLE.central;
  if (central) {
    Serial.printf("connecte: %s\n", central.address.c_str);
    while (central.connected) {
      if (ledChar.written)
        digitalWrite(LED_BUILTIN, ledChar.value);
      BLE.poll;                        // fait tourner la pile BLE
    }
    Serial.println("deconnecte");
  }
}

Expected Verification

In nRF Connect, the device “MKRWiFi1010” appears during scanning; upon connecting, you find the service and its characteristic, and writing 01 turns on the D6 LED, 00 turns it off. The serial monitor displays the MAC address of the connected central, then “deconnecte” upon disconnection.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials