reorganize, make an app?

This commit is contained in:
2022-04-17 20:33:33 +02:00
parent 191df8c418
commit e52ef237af
25 changed files with 945 additions and 20 deletions

View File

@@ -0,0 +1,85 @@
#include <Arduino.h>
#include <ArduinoBLE.h>
// Door switch pin
#define SWITCH_PIN 2
BLEService switchService("180A"); // BLE Switch Service
// BLE Switch Characteristic - custom 128-bit UUID, read by central
BLEByteCharacteristic switchCharacteristic("1234", BLERead | BLEIndicate);
void setup()
{
Serial.begin(9600);
// while (!Serial)
// ;
// begin initialization
if (!BLE.begin())
{
Serial.println("starting Bluetooth® Low Energy failed!");
while (1)
;
}
pinMode(SWITCH_PIN, INPUT);
// set advertised local name and service UUID:
BLE.setLocalName("Nano 33 BLE");
BLE.setAdvertisedService(switchService);
// add the characteristic to the service
switchService.addCharacteristic(switchCharacteristic);
// add service
BLE.addService(switchService);
// set the initial value for the characteristic:
switchCharacteristic.writeValue(0);
// start advertising
BLE.advertise();
Serial.println("BLE Switch Peripheral");
}
void loop()
{
// listen for BLE peripherals to connect:
BLEDevice central = BLE.central();
// if a central is connected to peripheral:
if (central)
{
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address());
digitalWrite(LED_BUILTIN, HIGH); // turn on the LED to indicate the connection
// while the central is still connected to peripheral:
bool oldState = digitalRead(SWITCH_PIN);
while (central.connected())
{
// read the switch state:
bool newState = digitalRead(SWITCH_PIN);
// if the switch state has changed:
if (newState != oldState)
{
// update the characteristic value:
switchCharacteristic.writeValue(newState);
// print the new switch state:
Serial.print("Switch state: ");
Serial.println(newState);
// update the old switch state:
oldState = newState;
}
}
// when the central disconnects, print it out:
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
}
}