/* lp_BLE_DoorBell Note: need long timer so (c)2024 Forward Computing and Control Pty. Ltd. This code is not warranted to be fit for any purpose. You may only use it at your own risk. This code may be freely used for both private and commercial use Provide this copyright is maintained. */ const char LOCAL_NAME[] = "Front_Door"; // Bedroom 1 window 1, (max 24char) // ble localName not actually set instead set name in advert data int led = 11; // Use P0.11 has the 'led' pin, pull to gnd to turn on // OR use 7 if using YJ-16013-NRF52832 const int comparatorPin = A0; // P0.02 for debounce, comparators can be on pins A0 to A7 only unsigned short ADVERT_INTERVAL = 100; // 100 gives about 8 packets in the 1 secs unsigned short ADVERT_TIMEOUT = 1; // length of advertising in secs #include "BLEPeripheral.h" #include // Current consumption for this sketch checking microswitch every second for a change in state adds ~0.33uA avg for microswitch check each sec // see BLE_ADVERT_MS and BLE_NO_ADVERT_MS settings below // ----------------------------------- // Crystal LowFreq clock: no advertising but still running 6.64mV => ~2.66uA BLEPeripheral ble; // BLE_SCAN_DATA_MAX_VALUE_LENGTH == 29 in BLEDeviceLimits.h // to this is added length and type for total of 31 == max advertisize packet size // here we also set flags for not connectable which uses 3 bytes length(1), type(1), flags(1) // so MAX_SCAN_DATA_LEN reduced to 26 const size_t MAX_DATA_LEN = 26; const unsigned MAX_LOCAL_NAME_LEN = MAX_DATA_LEN; // this can be split between say device NAME and Manufacturing Data // but that uses another two bytes for the length and type bytes for the Manufacturing Data // // For simplicity and readability here all the data is added to the NAME as ascii date comma separated // If using NAME and Manufacturing Data then sending data as binary in Manufacturing Data allows for more data. // // Note could also just have Manfacturing Data with out a device NAME but easier to read data in the name with scanning apps // called when pin changes state, pinState is state detected, HIGH or LOW // always triggers LOW first, but only interested in HIGH so initial trigger just ignored void handlePinLevelChange(int pinState) { if (pinState == HIGH) { // low to high advertising = true; digitalWrite(led, LOW); // turn led on ble.startAdvertising(); } else { digitalWrite(led, HIGH); // turn led off } } void setup() { pinMode(led, OUTPUT); // initialize the digital pin as an output. digitalWrite(led, HIGH); // turn off pinMode(comparatorPin, INPUT_PULLDOWN); // set to pulldown since pressing switch will apply +3v3 to the input // set compare pin with INPUT_PULLUP or INPUT_PULLDOWN to prevent floating pin triggers lp_comparator_start(comparatorPin, REF_4_16Vdd, handlePinLevelChange); // always triggers pin LOW first, then if pin HIGH, will trigger HIGH // set advertised name ble.setConnectable(false); ble.setTxPower(+4); ble.setAdvertisingInterval(ADVERT_INTERVAL); ble.setAdvertisedName(LOCAL_NAME); // ble.setAdvertisingTimeout(ADVERT_TIMEOUT); ble.begin(); } void loop() { sleep(); // sleep here // some event woke up loop() // go back to sleep }