PIR + LoRa

Dependencies:   BLE_API LMiC SX1276Lib mbed nRF51822 nrf51_rtc

Fork of BLE_PhysicalWeb by Bluetooth Low Energy

Revision:
10:e7ab33223964
Parent:
8:b816829fb712
Child:
11:bbe4157401e2
--- a/main.cpp	Fri Dec 12 14:17:43 2014 +0000
+++ b/main.cpp	Fri Jul 31 14:29:36 2015 +0000
@@ -19,6 +19,11 @@
 #include "URIBeaconConfigService.h"
 #include "DFUService.h"
 #include "DeviceInformationService.h"
+#include "nrf51_rtc/nrf51_rtc.h"
+
+InterruptIn motion(p21);
+DigitalOut led(LED1); 
+Serial pc(USBTX, USBRX); // tx, rx
 
 BLEDevice ble;
 URIBeaconConfigService *uriBeaconConfig;
@@ -28,52 +33,42 @@
     ble.startAdvertising();
 }
 
-void switchCallback(void)
+bool rise_state = false;
+time_t last_rise = 0;
+
+void riseHandler(void)
 {
-    static bool switched = false;
-    if (!switched) {
-        printf("executing switch\r\n");
-        delete uriBeaconConfig;
-        uriBeaconConfig = NULL;
+    led = 0;
+    pc.printf("rise\r\n");
+    last_rise = rtc.time();
+    rise_state = true;
+}
 
-        static const uint8_t BEACON_UUID[] = {0xD8, 0xFE};
-        static const uint8_t urldata[] = {
-            BEACON_UUID[0],
-            BEACON_UUID[1],
-            0x00, // flags
-            0x20, // power
-            0x00, // http://www.
-            'm',
-            'b',
-            'e',
-            'd',
-            0x08, // .".org"
-        };
+void fallHandler(void)
+{
+    led = 1;
+    pc.printf("fall\r\n");
+    last_rise = rtc.time();
+    rise_state = false;
+}
 
-        ble.shutdown();
-        ble.init();
-
-        ble.clearAdvertisingPayload();
-        ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, BEACON_UUID, sizeof(BEACON_UUID));
-        ble.accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, urldata, sizeof(urldata));
-
-        ble.setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
-        ble.setAdvertisingInterval(1600); /* 1s; in multiples of 0.625ms. */
-        ble.startAdvertising();
-
-        switched = true;
-    }
+void lastRiseCallback(void)
+{
+    pc.printf("Current state %d, last_rise %d\r\n", rise_state, rtc.time() - last_rise);
 }
 
 int main(void)
-{
+{    
     Ticker ticker;
-    ticker.attach(switchCallback, 30);
+    ticker.attach(&lastRiseCallback, 10);
+
+    motion.rise(&riseHandler);
+    motion.fall(&fallHandler);
 
     ble.init();
     ble.onDisconnection(disconnectionCallback);
 
-    uriBeaconConfig = new URIBeaconConfigService(ble, "http://www.mbed.org");
+    uriBeaconConfig = new URIBeaconConfigService(ble, "http://goo.gl/K1PDnX");
     if (!uriBeaconConfig->configuredSuccessfully()) {
         error("failed to accommodate URI");
     }
@@ -82,10 +77,34 @@
     uriBeaconConfig->setTxPowerLevels(powerLevels);
     uriBeaconConfig->setTxPowerMode(URIBeaconConfigService::TX_POWER_MODE_LOW);
 
-    /* Setup auxiliary services. */
-    DFUService dfu(ble); /* To allow over-the-air firmware udpates. optional. */
-    DeviceInformationService deviceInfo(ble, "ARM", "URIBeacon2", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); /* optional */
+    static const uint8_t BEACON_UUID[] = {0xD8, 0xFE};
+    static const uint8_t urldata[] = {
+        BEACON_UUID[0],
+        BEACON_UUID[1],
+        0x00, // flags
+        0x20, // power
+        0x00, // http://www. // https://goo.gl/K1PDnX
+        'g',
+        'o',
+        'o',
+        '.',
+        'g',
+        'l',
+        '/',
+        'K',
+        '1',
+        'P',
+        'D',
+        'n',
+        'X',
+    };
 
+    ble.clearAdvertisingPayload();
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, BEACON_UUID, sizeof(BEACON_UUID));
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, urldata, sizeof(urldata));
+
+    ble.setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
+    ble.setAdvertisingInterval(1600); /* 1s; in multiples of 0.625ms. */
     ble.startAdvertising();
 
     while (true) {