PIR sensor on BLE nano with BEACON service

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_PIR_BEACON by wattx

Revision:
2:b8632b29304e
Parent:
1:628464a3082e
Child:
3:4d6652dc7b43
--- a/main.cpp	Fri Sep 09 08:56:56 2016 +0000
+++ b/main.cpp	Fri Sep 09 10:01:48 2016 +0000
@@ -19,53 +19,43 @@
 
 Ticker timer;
 
-DigitalIn myInputPin(P0_10);
+DigitalIn pir_signal_pin(P0_10);
 
 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
 
 /* Optional: Device Name, add for human read-ability */
-const static char     DEVICE_NAME[] = "BLE_PIR_1";
+const static char     DEVICE_NAME[] = "BLEBEACON";
 
 /* You have up to 26 bytes of advertising data to use. */
-uint8_t AdvData[] = {0x01,0x02,0x00,0x01,0x12};   /* Example of hex data */
-//const static uint8_t AdvData[] = {"ChangeThisData"};         /* Example of character data */
+uint8_t AdvData[] = {0x01,0x02,0x00,0x04,0x12};   /* Example of hex data */
+
+const uint8_t SERVICE_UUID[] = {0xAA, 0xFE};
 
 
-class AdvertisementPacket {
+class AdvertisementPacketHandler {
 public:
-    AdvertisementPacket(BLE&);
-    void updateAdvertisementPacket(){
+    AdvertisementPacketHandler(BLE&);
+    void updatePIRValue(){
         
-        AdvData[1] = myInputPin;
+        AdvData[1] = pir_signal_pin;
         
         ble.gap().clearAdvertisingPayload();
-        /* Sacrifice 3B of 31B to Advertising Flags */
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
-    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
-    //ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
- 
-    /* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData));
- 
-    /* Optional: Add name to device */
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
-        //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
-        //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData));
-        };
+        ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
+        ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, SERVICE_UUID, sizeof(SERVICE_UUID));
+        ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, AdvData, sizeof(AdvData) );
+        ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+        ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
+        ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData));
+    };
 private:
     BLE &ble;
 };
 
-AdvertisementPacket::AdvertisementPacket(BLE &bleIn): ble(bleIn){
+AdvertisementPacketHandler::AdvertisementPacketHandler(BLE &bleIn): ble(bleIn){
         
 };
-/*
 
-/* Optional: Restart advertising when peer disconnects */
-void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
-{
-    BLE::Instance().gap().startAdvertising();
-}
+
 /**
  * This function is called when the ble initialization process has failed
  */
@@ -99,45 +89,32 @@
     
     /* Set device name characteristic data */
     ble.gap().setDeviceName((const uint8_t *) DEVICE_NAME);
-
-    /* Optional: add callback for disconnection */
-    ble.gap().onDisconnection(disconnectionCallback);
-    
     
      /* Configure advertisements */
     //ble.gap().setTxPower(radioPowerLevels[txPowerMode]);
-    
-    //ble.gap().setAdvertisingInterval(beaconPeriod);
+    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
+    /* Set advertising interval. Longer interval == longer battery life */
+    ble.gap().setAdvertisingInterval(1000); /* 1000ms */
     //ble.gap().onRadioNotification(this, &EddystoneService::radioNotificationCallback);
     //ble.gap().initRadioNotification();
     
-    
     /* Sacrifice 3B of 31B to Advertising Flags */
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
-    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
-    //ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
- 
+
     /* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData));
- 
+
     /* Optional: Add name to device */
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
-
-
-    /* Set advertising interval. Longer interval == longer battery life */
-    ble.gap().setAdvertisingInterval(1000); /* 1000ms */
-    
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, SERVICE_UUID, sizeof(SERVICE_UUID));
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, AdvData, sizeof(AdvData) );
 
     /* Start advertising */
     ble.gap().startAdvertising();
 }
 
 int main(void)
-{
-    
-    
-    
- 
+{ 
     /* Initialize BLE baselayer, always do this first! */
     ble.init(bleInitComplete);
 
@@ -145,10 +122,9 @@
      * BLE object is used in the main loop below. */
     while (!ble.hasInitialized()) { /* spin loop */ }
     
-    AdvertisementPacket f(ble);
+    AdvertisementPacketHandler f(ble);
 
-    timer.attach( &f, &AdvertisementPacket::updateAdvertisementPacket, 1.0); 
-
+    timer.attach( &f, &AdvertisementPacketHandler::updatePIRValue, 1.0); 
     while (true) {
         ble.waitForEvent();
     }