Buongiorno
Ho questo codice. Quando la periferica e abbastanza vicina accende il led. Dopo la Pausa lo spegne.
#include <ArduinoBLE.h>
const char* serial = "e5:20:bb:11:12:4b";
const char* serial2 = "fe:32:6b:dd:1e:9d";
const int ledPin = LED_BUILTIN;
unsigned long t0, dt;
int Pausa = 10000 ;
int Dista = 40 ;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
while (!Serial);
//begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
Serial.println("Bluetooth® Low Energy Central scan");
// start scanning for peripheral
BLE.scan(1);
}
void loop() {
Cerca();
dt = millis() - t0;
if ( dt >= Pausa ) {
t0 = millis();
Chiudi();
}
}
void Cerca() {
// check if a peripheral has been discovered
BLEDevice peripheral = BLE.available();
if (peripheral) {
// discovered a peripheral
Serial.println("Discovered a peripheral");
Serial.println("-----------------------");
// print address
Serial.print("Address: ");
Serial.println(peripheral.address());
// print the local name, if present
if (peripheral.hasLocalName()) {
Serial.print("Local Name: ");
Serial.println(peripheral.localName());
}
// print the advertised service UUIDs, if present
if (peripheral.hasAdvertisedServiceUuid()) {
Serial.print("Service UUIDs: ");
for (int i = 0; i < peripheral.advertisedServiceUuidCount(); i++) {
Serial.print(peripheral.advertisedServiceUuid(i));
Serial.print(" ");
}
Serial.println();
}
// print the RSSI
Serial.print("RSSI: ");
Serial.println(peripheral.rssi());
Serial.println();
if (peripheral.address()==serial && peripheral.rssi()>(-Dista))
{
Apri();
}
if (peripheral.address()==serial2 && peripheral.rssi()>(-Dista))
{
Apri();
}
}
}
void Apri(){
digitalWrite(ledPin,HIGH);
Serial.println("aperto ");
}
void Chiudi(){
digitalWrite(ledPin,LOW);
Serial.println("chiudi ");
}
Con IDE di arduino aperto funziona. Se uso un alimentatore non funziona piu.
Qualcuno ha qualche idea di come farlo funzionare senza dover avere l'IDE aperto.
Grazie