PIR + LoRa
Dependencies: BLE_API LMiC SX1276Lib mbed nRF51822 nrf51_rtc
Fork of BLE_PhysicalWeb by
main.cpp@10:e7ab33223964, 2015-07-31 (annotated)
- Committer:
- janjongboom
- Date:
- Fri Jul 31 14:29:36 2015 +0000
- Revision:
- 10:e7ab33223964
- Parent:
- 8:b816829fb712
- Child:
- 11:bbe4157401e2
motion sensor + ble
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 0:790a27ffc99b | 1 | /* mbed Microcontroller Library |
rgrover1 | 0:790a27ffc99b | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 0:790a27ffc99b | 3 | * |
rgrover1 | 0:790a27ffc99b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 0:790a27ffc99b | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 0:790a27ffc99b | 6 | * You may obtain a copy of the License at |
rgrover1 | 0:790a27ffc99b | 7 | * |
rgrover1 | 0:790a27ffc99b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 0:790a27ffc99b | 9 | * |
rgrover1 | 0:790a27ffc99b | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 0:790a27ffc99b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 0:790a27ffc99b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 0:790a27ffc99b | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 0:790a27ffc99b | 14 | * limitations under the License. |
rgrover1 | 0:790a27ffc99b | 15 | */ |
rgrover1 | 0:790a27ffc99b | 16 | |
rgrover1 | 0:790a27ffc99b | 17 | #include "mbed.h" |
rgrover1 | 0:790a27ffc99b | 18 | #include "BLEDevice.h" |
rgrover1 | 7:6faec5978fb4 | 19 | #include "URIBeaconConfigService.h" |
rgrover1 | 6:31b65d4ea67d | 20 | #include "DFUService.h" |
rgrover1 | 6:31b65d4ea67d | 21 | #include "DeviceInformationService.h" |
janjongboom | 10:e7ab33223964 | 22 | #include "nrf51_rtc/nrf51_rtc.h" |
janjongboom | 10:e7ab33223964 | 23 | |
janjongboom | 10:e7ab33223964 | 24 | InterruptIn motion(p21); |
janjongboom | 10:e7ab33223964 | 25 | DigitalOut led(LED1); |
janjongboom | 10:e7ab33223964 | 26 | Serial pc(USBTX, USBRX); // tx, rx |
rgrover1 | 0:790a27ffc99b | 27 | |
rgrover1 | 0:790a27ffc99b | 28 | BLEDevice ble; |
rgrover1 | 8:b816829fb712 | 29 | URIBeaconConfigService *uriBeaconConfig; |
rgrover1 | 0:790a27ffc99b | 30 | |
rgrover1 | 6:31b65d4ea67d | 31 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
rgrover1 | 6:31b65d4ea67d | 32 | { |
rgrover1 | 6:31b65d4ea67d | 33 | ble.startAdvertising(); |
rgrover1 | 6:31b65d4ea67d | 34 | } |
rgrover1 | 0:790a27ffc99b | 35 | |
janjongboom | 10:e7ab33223964 | 36 | bool rise_state = false; |
janjongboom | 10:e7ab33223964 | 37 | time_t last_rise = 0; |
janjongboom | 10:e7ab33223964 | 38 | |
janjongboom | 10:e7ab33223964 | 39 | void riseHandler(void) |
rgrover1 | 8:b816829fb712 | 40 | { |
janjongboom | 10:e7ab33223964 | 41 | led = 0; |
janjongboom | 10:e7ab33223964 | 42 | pc.printf("rise\r\n"); |
janjongboom | 10:e7ab33223964 | 43 | last_rise = rtc.time(); |
janjongboom | 10:e7ab33223964 | 44 | rise_state = true; |
janjongboom | 10:e7ab33223964 | 45 | } |
rgrover1 | 8:b816829fb712 | 46 | |
janjongboom | 10:e7ab33223964 | 47 | void fallHandler(void) |
janjongboom | 10:e7ab33223964 | 48 | { |
janjongboom | 10:e7ab33223964 | 49 | led = 1; |
janjongboom | 10:e7ab33223964 | 50 | pc.printf("fall\r\n"); |
janjongboom | 10:e7ab33223964 | 51 | last_rise = rtc.time(); |
janjongboom | 10:e7ab33223964 | 52 | rise_state = false; |
janjongboom | 10:e7ab33223964 | 53 | } |
rgrover1 | 8:b816829fb712 | 54 | |
janjongboom | 10:e7ab33223964 | 55 | void lastRiseCallback(void) |
janjongboom | 10:e7ab33223964 | 56 | { |
janjongboom | 10:e7ab33223964 | 57 | pc.printf("Current state %d, last_rise %d\r\n", rise_state, rtc.time() - last_rise); |
rgrover1 | 8:b816829fb712 | 58 | } |
rgrover1 | 8:b816829fb712 | 59 | |
rgrover1 | 0:790a27ffc99b | 60 | int main(void) |
janjongboom | 10:e7ab33223964 | 61 | { |
rgrover1 | 8:b816829fb712 | 62 | Ticker ticker; |
janjongboom | 10:e7ab33223964 | 63 | ticker.attach(&lastRiseCallback, 10); |
janjongboom | 10:e7ab33223964 | 64 | |
janjongboom | 10:e7ab33223964 | 65 | motion.rise(&riseHandler); |
janjongboom | 10:e7ab33223964 | 66 | motion.fall(&fallHandler); |
rgrover1 | 8:b816829fb712 | 67 | |
rgrover1 | 0:790a27ffc99b | 68 | ble.init(); |
rgrover1 | 6:31b65d4ea67d | 69 | ble.onDisconnection(disconnectionCallback); |
rgrover1 | 0:790a27ffc99b | 70 | |
janjongboom | 10:e7ab33223964 | 71 | uriBeaconConfig = new URIBeaconConfigService(ble, "http://goo.gl/K1PDnX"); |
rgrover1 | 8:b816829fb712 | 72 | if (!uriBeaconConfig->configuredSuccessfully()) { |
rgrover1 | 6:31b65d4ea67d | 73 | error("failed to accommodate URI"); |
rgrover1 | 6:31b65d4ea67d | 74 | } |
rgrover1 | 7:6faec5978fb4 | 75 | /* optional use of the API offered by URIBeaconConfigService */ |
rgrover1 | 7:6faec5978fb4 | 76 | const int8_t powerLevels[] = {-20, -4, 0, 10}; |
rgrover1 | 8:b816829fb712 | 77 | uriBeaconConfig->setTxPowerLevels(powerLevels); |
rgrover1 | 8:b816829fb712 | 78 | uriBeaconConfig->setTxPowerMode(URIBeaconConfigService::TX_POWER_MODE_LOW); |
rgrover1 | 0:790a27ffc99b | 79 | |
janjongboom | 10:e7ab33223964 | 80 | static const uint8_t BEACON_UUID[] = {0xD8, 0xFE}; |
janjongboom | 10:e7ab33223964 | 81 | static const uint8_t urldata[] = { |
janjongboom | 10:e7ab33223964 | 82 | BEACON_UUID[0], |
janjongboom | 10:e7ab33223964 | 83 | BEACON_UUID[1], |
janjongboom | 10:e7ab33223964 | 84 | 0x00, // flags |
janjongboom | 10:e7ab33223964 | 85 | 0x20, // power |
janjongboom | 10:e7ab33223964 | 86 | 0x00, // http://www. // https://goo.gl/K1PDnX |
janjongboom | 10:e7ab33223964 | 87 | 'g', |
janjongboom | 10:e7ab33223964 | 88 | 'o', |
janjongboom | 10:e7ab33223964 | 89 | 'o', |
janjongboom | 10:e7ab33223964 | 90 | '.', |
janjongboom | 10:e7ab33223964 | 91 | 'g', |
janjongboom | 10:e7ab33223964 | 92 | 'l', |
janjongboom | 10:e7ab33223964 | 93 | '/', |
janjongboom | 10:e7ab33223964 | 94 | 'K', |
janjongboom | 10:e7ab33223964 | 95 | '1', |
janjongboom | 10:e7ab33223964 | 96 | 'P', |
janjongboom | 10:e7ab33223964 | 97 | 'D', |
janjongboom | 10:e7ab33223964 | 98 | 'n', |
janjongboom | 10:e7ab33223964 | 99 | 'X', |
janjongboom | 10:e7ab33223964 | 100 | }; |
rgrover1 | 6:31b65d4ea67d | 101 | |
janjongboom | 10:e7ab33223964 | 102 | ble.clearAdvertisingPayload(); |
janjongboom | 10:e7ab33223964 | 103 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, BEACON_UUID, sizeof(BEACON_UUID)); |
janjongboom | 10:e7ab33223964 | 104 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, urldata, sizeof(urldata)); |
janjongboom | 10:e7ab33223964 | 105 | |
janjongboom | 10:e7ab33223964 | 106 | ble.setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); |
janjongboom | 10:e7ab33223964 | 107 | ble.setAdvertisingInterval(1600); /* 1s; in multiples of 0.625ms. */ |
rgrover1 | 0:790a27ffc99b | 108 | ble.startAdvertising(); |
rgrover1 | 0:790a27ffc99b | 109 | |
rgrover1 | 0:790a27ffc99b | 110 | while (true) { |
rgrover1 | 0:790a27ffc99b | 111 | ble.waitForEvent(); |
rgrover1 | 0:790a27ffc99b | 112 | } |
rgrover1 | 0:790a27ffc99b | 113 | } |