Hello ,
I want ot secure my Nano BLE so only Allow MAC can connect and send data to it.
2 quesiotns:
do I need to setup the mac in the arduino or on the server side?
how do I do it?
for test (and to see it's working) I took the CallbaclLED from the examples
include <ArduinoBLE.h>
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // create service
// create switch characteristic and allow remote device to read and write
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
const int ledPin = LED_BUILTIN; // pin to use for the LED
void setup() {
Serial.begin(9600);
while (!Serial);
pinMode(ledPin, OUTPUT); // use the LED pin as an output
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
// set the local name peripheral advertises
BLE.setLocalName("LEDCallback");
// set the UUID for the service this peripheral advertises
BLE.setAdvertisedService(ledService);
// add the characteristic to the service
ledService.addCharacteristic(switchCharacteristic);
// add service
BLE.addService(ledService);
// assign event handlers for connected, disconnected to peripheral
BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler);
BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
// assign event handlers for characteristic
switchCharacteristic.setEventHandler(BLEWritten, switchCharacteristicWritten);
// set an initial value for the characteristic
switchCharacteristic.setValue(0);
// start advertising
BLE.advertise();
Serial.println(("Bluetooth® device active, waiting for connections..."));
}
void loop() {
// poll for Bluetooth® Low Energy events
BLE.poll();
}
void blePeripheralConnectHandler(BLEDevice central) {
// central connected event handler
Serial.print("Connected event, central: ");
Serial.println(central.address());
}
void blePeripheralDisconnectHandler(BLEDevice central) {
// central disconnected event handler
Serial.print("Disconnected event, central: ");
Serial.println(central.address());
}
void switchCharacteristicWritten(BLEDevice central, BLECharacteristic characteristic) {
// central wrote new value to characteristic, update LED
Serial.print("Characteristic event, written: ");
if (switchCharacteristic.value()) {
Serial.println("LED on");
digitalWrite(ledPin, HIGH);
} else {
Serial.println("LED off");
digitalWrite(ledPin, LOW);
}
}
didn't find any "security" in the ArduinoBLE.h library
*** All I want is to allow my PI4(which I know it's MAC) to connect and be able to cahnge the led status
don't want the arduino to publish anything
Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project See About the Installation & Troubleshooting category.
In BLE the Arduino is usually the peripheral device which is the server.
Pairing is an optional feature in BLE and not supported by the ArduinoBLE library.
Using the MAC address is a bad idea for several reason.
Your PI will happily tell everyone the MAC address.
Many devices like all smartphones and tablets will randomly change the MAC address to avoid tracking of devices. We walk around with them everywhere. Having a fixed MAC address for BLE and WiFi was becoming a security risk and therefore all manufacturers implemented proprietary and non-public algorithms to change that.
This does not scale. You will need to modify your code to add more devices or use a new one.
If you do not care about this you can do the following:
Note: In BLE the central/client initiates connections, reads and writes but the peripheral/server manages access. It can simply disconnect a central.
// change this to your PIs/central device BLE MAC address
#define BLE_CENTRAL_ADDRESS "de:ad:be:ef:12:34"
// add this to the blePeripheralConnectHandler
if ( central.address() == BLE_CENTRAL_ADDRESS )
{
Serial.println( "My central connected!" );
}
else
{
central.disconnect();
}
A more portable solution would be add a characteristic that needs to be written with a secret password. If the password is not entered after a certain amount of time or is wrong you disconnect.
sorry for the late respond
I can see you are right about this:
I want to be able to write to the Arduino so it will turn on external led on D2
I mange to do it by sending "1" \ "0"
but how do I make it connect by password?
can you show me an example ?
this is what I have done :
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ArduinoBLE.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// declare an SSD1306 display object connected to I2C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // create service
// create switch characteristic and allow remote device to read and write
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
const int ledPin = LED_BUILTIN; // pin to use for the LED
const int ToRealyPin = 2; // pin to use for Relay
void setup() {
Serial.begin(9600);
//while (!Serial)
// ;
pinMode(ledPin, OUTPUT); // use the LED pin as an output
pinMode(ToRealyPin, OUTPUT); // use the LED pin as an output
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1)
;
}
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;)
;
}
delay(2000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 10);
// Display static text
display.println("Online");
display.display();
// set the local name peripheral advertises
BLE.setLocalName("BLE-LED");
// set the UUID for the service this peripheral advertises
BLE.setAdvertisedService(ledService);
// add the characteristic to the service
ledService.addCharacteristic(switchCharacteristic);
// add service
BLE.addService(ledService);
// assign event handlers for connected, disconnected to peripheral
BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler);
BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
// assign event handlers for characteristic
switchCharacteristic.setEventHandler(BLEWritten, switchCharacteristicWritten);
// set an initial value for the characteristic
switchCharacteristic.setValue(0);
// start advertising
BLE.advertise();
Serial.println(("Bluetooth® device active, waiting for connections..."));
}
void loop() {
// poll for Bluetooth® Low Energy events
BLE.poll();
}
void blePeripheralConnectHandler(BLEDevice central) {
// central connected event handler
Serial.print("Connected event, central: ");
Serial.println(central.address());
display.setCursor(3, 25);
display.println(central.address());
display.display();
}
void blePeripheralDisconnectHandler(BLEDevice central) {
// central disconnected event handler
Serial.print("Disconnected event, central: ");
Serial.println(central.address());
}
void switchCharacteristicWritten(BLEDevice central, BLECharacteristic characteristic) {
// central wrote new value to characteristic, update LED
Serial.print("Characteristic event, written: ");
if (switchCharacteristic.value()) {
Serial.println("LED on");
digitalWrite(ledPin, HIGH);
digitalWrite(ToRealyPin, HIGH);
display.clearDisplay();
display.setCursor(0, 25);
// Display static text
display.println("LED - ON");
display.display();
} else {
Serial.println("LED off");
digitalWrite(ledPin, LOW);
digitalWrite(ToRealyPin, LOW);
display.clearDisplay();
display.setCursor(0, 25);
// Display static text
display.println("Led - OFF");
display.display();
}
}