🛠 Créer un périphérique Bluetooth LE piloté depuis le téléphone
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
- 1 × Arduino MKR WiFi 1010
- 1 × Android/iOS smartphone with nRF Connect (or the Arduino NINA BLE app)
- ArduinoBLE library installed via the IDE
- Updated NINA firmware (BLE shares the module with WiFi)
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
- Forgetting
BLE.pollin the inner loop: writes from the central are never processed. - Testing BLE and WiFi simultaneously with the standard Arduino firmware: the official firmware is WiFi-oriented; combined usage requires reprogramming the module (and breaks RF certification — official Arduino warning).
- Non-unique custom UUIDs: reuse the prefix
19B1xxxx-E8F2-537E-4F6C-D104768A1214to avoid collisions with standard services. BLE.beginfails (BLE KO): NINA firmware not up to date, or another sketch is still occupying the module — reset the board.
Other tutorials for this board
Code examples for this board
scan-wifi.inoArduinoconnexion-wpa2.inoArduinohttp-get.inoArduinoserveur-web-led.inoArduinohorloge-ntp.inoArduinoudp-diffusion.inoArduino