BLE ADV Gateway, converts advertisement to proper JSON serial output
Dependencies: BLE_API mbed mbedtls nRF51822
main.cpp@8:46c5e0bfab05, 2017-07-10 (annotated)
- Committer:
- electronichamsters
- Date:
- Mon Jul 10 03:57:10 2017 +0000
- Revision:
- 8:46c5e0bfab05
- Parent:
- 7:0a8bbb6dea16
- Child:
- 9:1ea51b2048a8
saving working sensor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
electronichamsters | 8:46c5e0bfab05 | 1 | /* |
electronichamsters | 8:46c5e0bfab05 | 2 | |
electronichamsters | 8:46c5e0bfab05 | 3 | Eric Tsai |
electronichamsters | 8:46c5e0bfab05 | 4 | todo: hash mac to create unqiue identifier for "my" sensors. |
electronichamsters | 8:46c5e0bfab05 | 5 | |
electronichamsters | 8:46c5e0bfab05 | 6 | |
sunsmile2015 | 0:3dc6e424dba0 | 7 | */ |
sunsmile2015 | 0:3dc6e424dba0 | 8 | |
electronichamsters | 8:46c5e0bfab05 | 9 | //required to call the ecb functions |
electronichamsters | 8:46c5e0bfab05 | 10 | extern "C" |
electronichamsters | 8:46c5e0bfab05 | 11 | { |
electronichamsters | 8:46c5e0bfab05 | 12 | #include "nrf_ecb.h" |
electronichamsters | 8:46c5e0bfab05 | 13 | } |
electronichamsters | 8:46c5e0bfab05 | 14 | |
sunsmile2015 | 0:3dc6e424dba0 | 15 | #include "mbed.h" |
rgrover1 | 5:f4d74a8cad43 | 16 | #include "toolchain.h" |
sunsmile2015 | 0:3dc6e424dba0 | 17 | #include "ble/BLE.h" |
sunsmile2015 | 0:3dc6e424dba0 | 18 | #include "TMP_nrf51/TMP_nrf51.h" |
sunsmile2015 | 0:3dc6e424dba0 | 19 | |
electronichamsters | 8:46c5e0bfab05 | 20 | //comment out when done with debug uart, else eats batteries |
electronichamsters | 8:46c5e0bfab05 | 21 | #define MyDebugEnb 1 |
electronichamsters | 8:46c5e0bfab05 | 22 | |
electronichamsters | 8:46c5e0bfab05 | 23 | //Pin "P0.4" on nRF51822 = mbed "p4". |
electronichamsters | 8:46c5e0bfab05 | 24 | //InterruptIn is pulled-up. GND the pin to activate. |
electronichamsters | 8:46c5e0bfab05 | 25 | |
electronichamsters | 8:46c5e0bfab05 | 26 | // waveshare board ****** |
electronichamsters | 8:46c5e0bfab05 | 27 | //InterruptIn button1(p10); |
electronichamsters | 8:46c5e0bfab05 | 28 | //InterruptIn button2(p11); |
electronichamsters | 8:46c5e0bfab05 | 29 | |
electronichamsters | 8:46c5e0bfab05 | 30 | // purple board ****** |
electronichamsters | 8:46c5e0bfab05 | 31 | InterruptIn button1(p23); |
electronichamsters | 8:46c5e0bfab05 | 32 | InterruptIn button2(p24); |
electronichamsters | 8:46c5e0bfab05 | 33 | |
electronichamsters | 8:46c5e0bfab05 | 34 | |
electronichamsters | 8:46c5e0bfab05 | 35 | //Serial device(p9, p11); // tx, rx, purple board and Rigado |
electronichamsters | 8:46c5e0bfab05 | 36 | #if MyDebugEnb |
electronichamsters | 8:46c5e0bfab05 | 37 | // if you see ~1mA consumption during sleep, that's because uart is enabled. |
electronichamsters | 8:46c5e0bfab05 | 38 | Serial device(p9, p11); //nRF51822 uart : TX=p9. RX=p11 |
electronichamsters | 8:46c5e0bfab05 | 39 | #endif |
electronichamsters | 8:46c5e0bfab05 | 40 | |
electronichamsters | 8:46c5e0bfab05 | 41 | //InterruptIn button(p4); //Pin P0.4 on = mbed "p3"; |
electronichamsters | 8:46c5e0bfab05 | 42 | |
electronichamsters | 8:46c5e0bfab05 | 43 | |
electronichamsters | 8:46c5e0bfab05 | 44 | static Ticker tic_adv; //stop adv |
electronichamsters | 8:46c5e0bfab05 | 45 | static Ticker tic_debounce; //debounce I/O |
electronichamsters | 8:46c5e0bfab05 | 46 | static Ticker tic_periodic; //regular updates |
electronichamsters | 8:46c5e0bfab05 | 47 | //static TMP_nrf51 tempSensor; |
electronichamsters | 8:46c5e0bfab05 | 48 | static bool flag_update_io = false; |
electronichamsters | 8:46c5e0bfab05 | 49 | static bool flag_periodic_call = false; |
electronichamsters | 8:46c5e0bfab05 | 50 | static bool flag_detach_adv_tic = false; |
electronichamsters | 8:46c5e0bfab05 | 51 | static bool flag_set_debounce_tic = false; //not used |
electronichamsters | 8:46c5e0bfab05 | 52 | |
electronichamsters | 8:46c5e0bfab05 | 53 | //static DigitalOut alivenessLED(LED1, 1); |
electronichamsters | 8:46c5e0bfab05 | 54 | float mySensor = 2.0f; |
electronichamsters | 8:46c5e0bfab05 | 55 | |
electronichamsters | 8:46c5e0bfab05 | 56 | |
electronichamsters | 8:46c5e0bfab05 | 57 | /* Optional: Device Name, add for human read-ability */ |
electronichamsters | 8:46c5e0bfab05 | 58 | const static char DEVICE_NAME[] = "CUU"; |
rgrover1 | 5:f4d74a8cad43 | 59 | |
electronichamsters | 8:46c5e0bfab05 | 60 | /* You have up to 26 bytes of advertising data to use. */ |
electronichamsters | 8:46c5e0bfab05 | 61 | /* |
electronichamsters | 8:46c5e0bfab05 | 62 | Advertisement |
electronichamsters | 8:46c5e0bfab05 | 63 | |
electronichamsters | 8:46c5e0bfab05 | 64 | DECvolt:3.11111111,mag:1 |
electronichamsters | 8:46c5e0bfab05 | 65 | AdvData[0-2] = Look for DEC on observer |
electronichamsters | 8:46c5e0bfab05 | 66 | AdvData[3] = beginning of data |
electronichamsters | 8:46c5e0bfab05 | 67 | */ |
electronichamsters | 8:46c5e0bfab05 | 68 | //full with nullls |
electronichamsters | 8:46c5e0bfab05 | 69 | static uint8_t AdvData[] = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0}; /* Example of hex data */ |
electronichamsters | 8:46c5e0bfab05 | 70 | char buffer[10]={0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; //battery analog reading |
electronichamsters | 8:46c5e0bfab05 | 71 | uint8_t magnet_near=0; |
electronichamsters | 8:46c5e0bfab05 | 72 | |
electronichamsters | 8:46c5e0bfab05 | 73 | |
electronichamsters | 8:46c5e0bfab05 | 74 | |
electronichamsters | 8:46c5e0bfab05 | 75 | static uint8_t key[16] = {0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4}; |
electronichamsters | 8:46c5e0bfab05 | 76 | //26 bytes adv data |
electronichamsters | 8:46c5e0bfab05 | 77 | static uint8_t encrypted[26] = {0x0,0x0,0x0,0x1,0x1,0x1,0x2,0x2,0x2,0x3,0x3,0x3,0x4,0x4,0x4,0x5,0x5,0x5,0x6,0x6,0x6,0x7,0x7,0x7,0x8,0x8}; /* Example of hex data */ |
electronichamsters | 8:46c5e0bfab05 | 78 | //static uint8_t key_buf[16] = {0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4}; |
electronichamsters | 8:46c5e0bfab05 | 79 | static uint8_t key_buf[16] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x1, 0x2}; |
electronichamsters | 8:46c5e0bfab05 | 80 | static uint8_t src_buf[16] = {0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4}; |
electronichamsters | 8:46c5e0bfab05 | 81 | static uint8_t des_buf[16] = {0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4}; |
electronichamsters | 8:46c5e0bfab05 | 82 | |
electronichamsters | 8:46c5e0bfab05 | 83 | //const static uint8_t AdvData[] = {"ChangeThisData"}; /* Example of character data */ |
electronichamsters | 8:46c5e0bfab05 | 84 | |
electronichamsters | 8:46c5e0bfab05 | 85 | |
electronichamsters | 8:46c5e0bfab05 | 86 | |
electronichamsters | 8:46c5e0bfab05 | 87 | /* **** NOT USED **** */ |
electronichamsters | 8:46c5e0bfab05 | 88 | //16byte UUID loading happens here |
electronichamsters | 8:46c5e0bfab05 | 89 | //Look at <GapAdvertisingData.h> for rest of definition |
sunsmile2015 | 4:e5fa4c8838db | 90 | struct ApplicationData_t { |
electronichamsters | 8:46c5e0bfab05 | 91 | //Byte 0: AppID High Byte |
electronichamsters | 8:46c5e0bfab05 | 92 | //Byte 1: AppID Low Byte |
electronichamsters | 8:46c5e0bfab05 | 93 | //Byte 2: sensor High Word |
electronichamsters | 8:46c5e0bfab05 | 94 | //Byte 3: |
electronichamsters | 8:46c5e0bfab05 | 95 | //Byte 4: |
electronichamsters | 8:46c5e0bfab05 | 96 | //Byte 5: sensor Low Byte |
electronichamsters | 8:46c5e0bfab05 | 97 | |
electronichamsters | 8:46c5e0bfab05 | 98 | |
electronichamsters | 8:46c5e0bfab05 | 99 | //app ID is 16 bit, (0xFEFE) |
electronichamsters | 8:46c5e0bfab05 | 100 | uint16_t applicationSpecificId; /* An ID used to identify temperature value in the manufacture specific AD data field */ |
electronichamsters | 8:46c5e0bfab05 | 101 | |
electronichamsters | 8:46c5e0bfab05 | 102 | //float = 32-bit. |
electronichamsters | 8:46c5e0bfab05 | 103 | //tsai: change this to uint32_t!!! |
electronichamsters | 8:46c5e0bfab05 | 104 | TMP_nrf51::TempSensorValue_t tmpSensorValue; /* this is a float (32-bit), user data */ |
rgrover1 | 5:f4d74a8cad43 | 105 | } PACKED; |
sunsmile2015 | 0:3dc6e424dba0 | 106 | |
electronichamsters | 8:46c5e0bfab05 | 107 | |
electronichamsters | 8:46c5e0bfab05 | 108 | |
electronichamsters | 8:46c5e0bfab05 | 109 | void debounce_Callback(void) |
electronichamsters | 8:46c5e0bfab05 | 110 | { |
electronichamsters | 8:46c5e0bfab05 | 111 | tic_debounce.detach(); |
electronichamsters | 8:46c5e0bfab05 | 112 | flag_set_debounce_tic = false; //not used |
electronichamsters | 8:46c5e0bfab05 | 113 | flag_update_io = true; //start advertising |
electronichamsters | 8:46c5e0bfab05 | 114 | /* Note that the buttonPressedCallback() executes in interrupt context, so it is safer to access |
electronichamsters | 8:46c5e0bfab05 | 115 | * BLE device API from the main thread. */ |
electronichamsters | 8:46c5e0bfab05 | 116 | //buttonState = PRESSED; |
electronichamsters | 8:46c5e0bfab05 | 117 | } |
electronichamsters | 8:46c5e0bfab05 | 118 | |
electronichamsters | 8:46c5e0bfab05 | 119 | //----- button rising --- |
electronichamsters | 8:46c5e0bfab05 | 120 | void buttonPressedCallback(void) |
sunsmile2015 | 2:b935358da5ba | 121 | { |
electronichamsters | 8:46c5e0bfab05 | 122 | |
electronichamsters | 8:46c5e0bfab05 | 123 | |
electronichamsters | 8:46c5e0bfab05 | 124 | //flag_update_io = true; |
electronichamsters | 8:46c5e0bfab05 | 125 | |
electronichamsters | 8:46c5e0bfab05 | 126 | tic_debounce.attach(debounce_Callback, 1); //ok to attach multiple times, first one wins |
electronichamsters | 8:46c5e0bfab05 | 127 | |
electronichamsters | 8:46c5e0bfab05 | 128 | |
electronichamsters | 8:46c5e0bfab05 | 129 | //buttonState = PRESSED; |
electronichamsters | 8:46c5e0bfab05 | 130 | |
electronichamsters | 8:46c5e0bfab05 | 131 | /* |
electronichamsters | 8:46c5e0bfab05 | 132 | if (flag_set_debounce_tic == false) |
electronichamsters | 8:46c5e0bfab05 | 133 | { |
electronichamsters | 8:46c5e0bfab05 | 134 | flag_set_debounce_tic = true; |
electronichamsters | 8:46c5e0bfab05 | 135 | |
electronichamsters | 8:46c5e0bfab05 | 136 | } |
electronichamsters | 8:46c5e0bfab05 | 137 | */ |
electronichamsters | 8:46c5e0bfab05 | 138 | } |
rgrover1 | 5:f4d74a8cad43 | 139 | |
electronichamsters | 8:46c5e0bfab05 | 140 | //----- button falling --- |
electronichamsters | 8:46c5e0bfab05 | 141 | void buttonReleasedCallback(void) |
electronichamsters | 8:46c5e0bfab05 | 142 | { |
electronichamsters | 8:46c5e0bfab05 | 143 | |
electronichamsters | 8:46c5e0bfab05 | 144 | |
electronichamsters | 8:46c5e0bfab05 | 145 | //flag_update_io = true; |
electronichamsters | 8:46c5e0bfab05 | 146 | |
electronichamsters | 8:46c5e0bfab05 | 147 | tic_debounce.attach(debounce_Callback, 1); |
electronichamsters | 8:46c5e0bfab05 | 148 | |
electronichamsters | 8:46c5e0bfab05 | 149 | |
electronichamsters | 8:46c5e0bfab05 | 150 | |
electronichamsters | 8:46c5e0bfab05 | 151 | } |
electronichamsters | 8:46c5e0bfab05 | 152 | |
electronichamsters | 8:46c5e0bfab05 | 153 | |
electronichamsters | 8:46c5e0bfab05 | 154 | void stop_adv_Callback(void) |
electronichamsters | 8:46c5e0bfab05 | 155 | { |
electronichamsters | 8:46c5e0bfab05 | 156 | //stops advertising after X seconds |
electronichamsters | 8:46c5e0bfab05 | 157 | /* Note that the Callback() executes in interrupt context, so it is safer to do |
rgrover1 | 5:f4d74a8cad43 | 158 | * heavy-weight sensor polling from the main thread (where we should be able to block safely, if needed). */ |
electronichamsters | 8:46c5e0bfab05 | 159 | |
electronichamsters | 8:46c5e0bfab05 | 160 | //tic_adv.detach(); |
electronichamsters | 8:46c5e0bfab05 | 161 | |
electronichamsters | 8:46c5e0bfab05 | 162 | flag_detach_adv_tic = true; |
electronichamsters | 8:46c5e0bfab05 | 163 | //ble.gap().stopAdvertising(); |
electronichamsters | 8:46c5e0bfab05 | 164 | |
electronichamsters | 8:46c5e0bfab05 | 165 | |
electronichamsters | 8:46c5e0bfab05 | 166 | } |
electronichamsters | 8:46c5e0bfab05 | 167 | |
electronichamsters | 8:46c5e0bfab05 | 168 | |
electronichamsters | 8:46c5e0bfab05 | 169 | |
electronichamsters | 8:46c5e0bfab05 | 170 | |
electronichamsters | 8:46c5e0bfab05 | 171 | |
electronichamsters | 8:46c5e0bfab05 | 172 | void periodic_Callback(void) |
electronichamsters | 8:46c5e0bfab05 | 173 | { |
electronichamsters | 8:46c5e0bfab05 | 174 | flag_update_io = true; |
electronichamsters | 8:46c5e0bfab05 | 175 | flag_periodic_call = true; |
sunsmile2015 | 4:e5fa4c8838db | 176 | } |
sunsmile2015 | 4:e5fa4c8838db | 177 | |
rgrover1 | 5:f4d74a8cad43 | 178 | void setupApplicationData(ApplicationData_t &appData) |
sunsmile2015 | 4:e5fa4c8838db | 179 | { |
electronichamsters | 8:46c5e0bfab05 | 180 | // two byte ID: 0xFEFE |
electronichamsters | 8:46c5e0bfab05 | 181 | static const uint16_t APP_SPECIFIC_ID_TEST = 0xFEFE; //2 byte application ID |
andresag | 7:0a8bbb6dea16 | 182 | |
sunsmile2015 | 4:e5fa4c8838db | 183 | appData.applicationSpecificId = APP_SPECIFIC_ID_TEST; |
electronichamsters | 8:46c5e0bfab05 | 184 | //appData.tmpSensorValue = tempSensor.get(); |
electronichamsters | 8:46c5e0bfab05 | 185 | appData.tmpSensorValue = mySensor; |
sunsmile2015 | 0:3dc6e424dba0 | 186 | } |
sunsmile2015 | 0:3dc6e424dba0 | 187 | |
electronichamsters | 8:46c5e0bfab05 | 188 | |
electronichamsters | 8:46c5e0bfab05 | 189 | |
andresag | 7:0a8bbb6dea16 | 190 | /** |
andresag | 7:0a8bbb6dea16 | 191 | * This function is called when the ble initialization process has failled |
andresag | 7:0a8bbb6dea16 | 192 | */ |
andresag | 7:0a8bbb6dea16 | 193 | void onBleInitError(BLE &ble, ble_error_t error) |
andresag | 7:0a8bbb6dea16 | 194 | { |
andresag | 7:0a8bbb6dea16 | 195 | /* Initialization error handling should go here */ |
andresag | 7:0a8bbb6dea16 | 196 | } |
andresag | 7:0a8bbb6dea16 | 197 | |
andresag | 7:0a8bbb6dea16 | 198 | /** |
andresag | 7:0a8bbb6dea16 | 199 | * Callback triggered when the ble initialization process has finished |
andresag | 7:0a8bbb6dea16 | 200 | */ |
andresag | 7:0a8bbb6dea16 | 201 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
sunsmile2015 | 2:b935358da5ba | 202 | { |
andresag | 7:0a8bbb6dea16 | 203 | BLE& ble = params->ble; |
andresag | 7:0a8bbb6dea16 | 204 | ble_error_t error = params->error; |
andresag | 7:0a8bbb6dea16 | 205 | |
andresag | 7:0a8bbb6dea16 | 206 | if (error != BLE_ERROR_NONE) { |
andresag | 7:0a8bbb6dea16 | 207 | /* In case of error, forward the error handling to onBleInitError */ |
andresag | 7:0a8bbb6dea16 | 208 | onBleInitError(ble, error); |
andresag | 7:0a8bbb6dea16 | 209 | return; |
andresag | 7:0a8bbb6dea16 | 210 | } |
andresag | 7:0a8bbb6dea16 | 211 | |
andresag | 7:0a8bbb6dea16 | 212 | /* Ensure that it is the default instance of BLE */ |
andresag | 7:0a8bbb6dea16 | 213 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
andresag | 7:0a8bbb6dea16 | 214 | return; |
andresag | 7:0a8bbb6dea16 | 215 | } |
electronichamsters | 8:46c5e0bfab05 | 216 | |
electronichamsters | 8:46c5e0bfab05 | 217 | /* Set device name characteristic data */ |
electronichamsters | 8:46c5e0bfab05 | 218 | ble.gap().setDeviceName((const uint8_t *) DEVICE_NAME); |
andresag | 7:0a8bbb6dea16 | 219 | |
rgrover1 | 5:f4d74a8cad43 | 220 | /* Setup advertising payload */ |
electronichamsters | 8:46c5e0bfab05 | 221 | //set modes "no EDR", "discoverable" for beacon type advertisements |
rgrover1 | 5:f4d74a8cad43 | 222 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
electronichamsters | 8:46c5e0bfab05 | 223 | |
electronichamsters | 8:46c5e0bfab05 | 224 | //set device profile in ADV/GATT |
electronichamsters | 8:46c5e0bfab05 | 225 | //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER); |
electronichamsters | 8:46c5e0bfab05 | 226 | |
electronichamsters | 8:46c5e0bfab05 | 227 | //set advertising data (ADV data) |
electronichamsters | 8:46c5e0bfab05 | 228 | //ApplicationData_t appData; |
electronichamsters | 8:46c5e0bfab05 | 229 | //setupApplicationData(appData); |
electronichamsters | 8:46c5e0bfab05 | 230 | //in /BLE_API/ble/GapAdvertisingData.h: sets payload (uuid) |
electronichamsters | 8:46c5e0bfab05 | 231 | //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&appData, sizeof(ApplicationData_t)); |
electronichamsters | 8:46c5e0bfab05 | 232 | |
electronichamsters | 8:46c5e0bfab05 | 233 | //from GAP example |
electronichamsters | 8:46c5e0bfab05 | 234 | /* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */ |
electronichamsters | 8:46c5e0bfab05 | 235 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData)); |
electronichamsters | 8:46c5e0bfab05 | 236 | |
electronichamsters | 8:46c5e0bfab05 | 237 | /* Setup advertising parameters: not connectable */ |
electronichamsters | 8:46c5e0bfab05 | 238 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); |
electronichamsters | 8:46c5e0bfab05 | 239 | ble.gap().setAdvertisingInterval(300); //one advertisment every 300ms. Self tickers, so you don't have to worry. |
electronichamsters | 8:46c5e0bfab05 | 240 | |
electronichamsters | 8:46c5e0bfab05 | 241 | |
electronichamsters | 8:46c5e0bfab05 | 242 | |
electronichamsters | 8:46c5e0bfab05 | 243 | //don't start advertising on init. Only advertise on pin interrupt. |
electronichamsters | 8:46c5e0bfab05 | 244 | //ble.gap().startAdvertising(); |
electronichamsters | 8:46c5e0bfab05 | 245 | } |
electronichamsters | 8:46c5e0bfab05 | 246 | |
rgrover1 | 5:f4d74a8cad43 | 247 | |
electronichamsters | 8:46c5e0bfab05 | 248 | //https://developer.mbed.org/users/MarceloSalazar/notebook/measuring-battery-voltage-with-nordic-nrf51x/ |
electronichamsters | 8:46c5e0bfab05 | 249 | void my_analogin_init(void) |
electronichamsters | 8:46c5e0bfab05 | 250 | { |
electronichamsters | 8:46c5e0bfab05 | 251 | NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled; |
electronichamsters | 8:46c5e0bfab05 | 252 | NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) | |
electronichamsters | 8:46c5e0bfab05 | 253 | (ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) | |
electronichamsters | 8:46c5e0bfab05 | 254 | (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) | |
electronichamsters | 8:46c5e0bfab05 | 255 | (ADC_CONFIG_PSEL_Disabled << ADC_CONFIG_PSEL_Pos) | |
electronichamsters | 8:46c5e0bfab05 | 256 | (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos); |
electronichamsters | 8:46c5e0bfab05 | 257 | } |
sunsmile2015 | 2:b935358da5ba | 258 | |
electronichamsters | 8:46c5e0bfab05 | 259 | uint16_t my_analogin_read_u16(void) |
electronichamsters | 8:46c5e0bfab05 | 260 | { |
electronichamsters | 8:46c5e0bfab05 | 261 | NRF_ADC->CONFIG &= ~ADC_CONFIG_PSEL_Msk; |
electronichamsters | 8:46c5e0bfab05 | 262 | NRF_ADC->CONFIG |= ADC_CONFIG_PSEL_Disabled << ADC_CONFIG_PSEL_Pos; |
electronichamsters | 8:46c5e0bfab05 | 263 | NRF_ADC->TASKS_START = 1; |
electronichamsters | 8:46c5e0bfab05 | 264 | |
electronichamsters | 8:46c5e0bfab05 | 265 | |
electronichamsters | 8:46c5e0bfab05 | 266 | //while loop doesn't actually loop until reading comlete, use a wait. |
electronichamsters | 8:46c5e0bfab05 | 267 | while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) {}; |
electronichamsters | 8:46c5e0bfab05 | 268 | wait_ms(2); |
electronichamsters | 8:46c5e0bfab05 | 269 | |
electronichamsters | 8:46c5e0bfab05 | 270 | //save off RESULT before disabling. |
electronichamsters | 8:46c5e0bfab05 | 271 | //uint16_t myresult = (uint16_t)NRF_ADC->RESULT; |
electronichamsters | 8:46c5e0bfab05 | 272 | |
electronichamsters | 8:46c5e0bfab05 | 273 | //disable ADC to lower bat consumption |
electronichamsters | 8:46c5e0bfab05 | 274 | //NRF_ADC->TASKS_STOP = 1; |
electronichamsters | 8:46c5e0bfab05 | 275 | //NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Disabled; //disable to shutdown ADC & lower bat consumption |
electronichamsters | 8:46c5e0bfab05 | 276 | |
electronichamsters | 8:46c5e0bfab05 | 277 | return (uint16_t)NRF_ADC->RESULT; // 10 bit |
electronichamsters | 8:46c5e0bfab05 | 278 | //return myresult; |
sunsmile2015 | 4:e5fa4c8838db | 279 | } |
sunsmile2015 | 4:e5fa4c8838db | 280 | |
sunsmile2015 | 2:b935358da5ba | 281 | int main(void) |
sunsmile2015 | 2:b935358da5ba | 282 | { |
electronichamsters | 8:46c5e0bfab05 | 283 | |
electronichamsters | 8:46c5e0bfab05 | 284 | #if MyDebugEnb |
electronichamsters | 8:46c5e0bfab05 | 285 | device.baud(9600); |
electronichamsters | 8:46c5e0bfab05 | 286 | device.printf("started sensor node 36 "); |
electronichamsters | 8:46c5e0bfab05 | 287 | device.printf("\r\n"); |
electronichamsters | 8:46c5e0bfab05 | 288 | #endif |
electronichamsters | 8:46c5e0bfab05 | 289 | |
electronichamsters | 8:46c5e0bfab05 | 290 | |
electronichamsters | 8:46c5e0bfab05 | 291 | |
electronichamsters | 8:46c5e0bfab05 | 292 | Timer myTimer; //timed advertising |
electronichamsters | 8:46c5e0bfab05 | 293 | myTimer.start(); |
electronichamsters | 8:46c5e0bfab05 | 294 | |
electronichamsters | 8:46c5e0bfab05 | 295 | |
electronichamsters | 8:46c5e0bfab05 | 296 | AdvData[0] = 0x44; //D |
electronichamsters | 8:46c5e0bfab05 | 297 | AdvData[1] = 0x45; //E |
electronichamsters | 8:46c5e0bfab05 | 298 | AdvData[2] = 0x43; //C |
electronichamsters | 8:46c5e0bfab05 | 299 | AdvData[3] = 0x22; //" |
electronichamsters | 8:46c5e0bfab05 | 300 | AdvData[4] = 0x76; //V volt |
electronichamsters | 8:46c5e0bfab05 | 301 | AdvData[5] = 0x6f; //o |
electronichamsters | 8:46c5e0bfab05 | 302 | AdvData[6] = 0x22; //" |
electronichamsters | 8:46c5e0bfab05 | 303 | AdvData[7] = 0x3a; //: |
electronichamsters | 8:46c5e0bfab05 | 304 | AdvData[8] = 0x24; //3 # |
electronichamsters | 8:46c5e0bfab05 | 305 | AdvData[9] = 0x24; //. # |
electronichamsters | 8:46c5e0bfab05 | 306 | AdvData[10] = 0x24; //1 # |
electronichamsters | 8:46c5e0bfab05 | 307 | AdvData[11] = 0x24; //1 # |
electronichamsters | 8:46c5e0bfab05 | 308 | AdvData[12] = 0x2c; //, |
electronichamsters | 8:46c5e0bfab05 | 309 | AdvData[13] = 0x22; //" mag |
electronichamsters | 8:46c5e0bfab05 | 310 | AdvData[14] = 0x6d; //a |
electronichamsters | 8:46c5e0bfab05 | 311 | AdvData[15] = 0x22; //" |
electronichamsters | 8:46c5e0bfab05 | 312 | AdvData[16] = 0x3a; //: |
electronichamsters | 8:46c5e0bfab05 | 313 | AdvData[17] = 0x24; //0 or 1, 30 or 31 |
electronichamsters | 8:46c5e0bfab05 | 314 | |
electronichamsters | 8:46c5e0bfab05 | 315 | button1.fall(buttonPressedCallback); |
electronichamsters | 8:46c5e0bfab05 | 316 | button1.rise(buttonReleasedCallback); |
electronichamsters | 8:46c5e0bfab05 | 317 | button1.mode(PullNone); |
electronichamsters | 8:46c5e0bfab05 | 318 | button1.fall(NULL); |
electronichamsters | 8:46c5e0bfab05 | 319 | button1.rise(NULL); |
sunsmile2015 | 0:3dc6e424dba0 | 320 | |
andresag | 7:0a8bbb6dea16 | 321 | BLE &ble = BLE::Instance(); |
andresag | 7:0a8bbb6dea16 | 322 | ble.init(bleInitComplete); |
electronichamsters | 8:46c5e0bfab05 | 323 | |
electronichamsters | 8:46c5e0bfab05 | 324 | //debug uart |
electronichamsters | 8:46c5e0bfab05 | 325 | //device.baud(115200); |
electronichamsters | 8:46c5e0bfab05 | 326 | float bat_reading; //hold battery voltage reading (Vbg/Vcc) |
electronichamsters | 8:46c5e0bfab05 | 327 | |
electronichamsters | 8:46c5e0bfab05 | 328 | my_analogin_init();//routes band-gap to analog input |
andresag | 7:0a8bbb6dea16 | 329 | |
andresag | 7:0a8bbb6dea16 | 330 | /* SpinWait for initialization to complete. This is necessary because the |
andresag | 7:0a8bbb6dea16 | 331 | * BLE object is used in the main loop below. */ |
electronichamsters | 8:46c5e0bfab05 | 332 | while (ble.hasInitialized() == false) { /* spin loop */ } |
electronichamsters | 8:46c5e0bfab05 | 333 | |
electronichamsters | 8:46c5e0bfab05 | 334 | //every X seconds, sends period update, up to 1800 (30 minutes) |
electronichamsters | 8:46c5e0bfab05 | 335 | tic_periodic.attach(periodic_Callback, 10); |
rgrover1 | 5:f4d74a8cad43 | 336 | |
sunsmile2015 | 0:3dc6e424dba0 | 337 | while (true) { |
electronichamsters | 8:46c5e0bfab05 | 338 | |
electronichamsters | 8:46c5e0bfab05 | 339 | uint16_t mySeconds =(uint16_t)(myTimer.read_ms()/1000); //problem: mySeconds is only 2 byte |
electronichamsters | 8:46c5e0bfab05 | 340 | //reading the ADV value, only goes up to 0-255; |
electronichamsters | 8:46c5e0bfab05 | 341 | //reading the uart: current time in seconds: -1782, goes negative. |
electronichamsters | 8:46c5e0bfab05 | 342 | //need to be able to count 1800 seconds since that's the length of timer. |
electronichamsters | 8:46c5e0bfab05 | 343 | |
electronichamsters | 8:46c5e0bfab05 | 344 | #if MyDebugEnb |
electronichamsters | 8:46c5e0bfab05 | 345 | device.printf("current time in seconds: %d \r\n", mySeconds); |
electronichamsters | 8:46c5e0bfab05 | 346 | #endif |
electronichamsters | 8:46c5e0bfab05 | 347 | //**** set which pin should be interrupt, set pullups *** |
electronichamsters | 8:46c5e0bfab05 | 348 | |
electronichamsters | 8:46c5e0bfab05 | 349 | //set both pins to pull-up, so they're not floating when we read state |
electronichamsters | 8:46c5e0bfab05 | 350 | button1.mode(PullUp); |
electronichamsters | 8:46c5e0bfab05 | 351 | button2.mode(PullUp); |
electronichamsters | 8:46c5e0bfab05 | 352 | |
electronichamsters | 8:46c5e0bfab05 | 353 | //wait_ms(300); //contact settle |
electronichamsters | 8:46c5e0bfab05 | 354 | |
electronichamsters | 8:46c5e0bfab05 | 355 | |
electronichamsters | 8:46c5e0bfab05 | 356 | //AdvData[12] is automatically CR? why? |
electronichamsters | 8:46c5e0bfab05 | 357 | |
electronichamsters | 8:46c5e0bfab05 | 358 | //0x33 0x2E 0x33 0x32 0x13 |
electronichamsters | 8:46c5e0bfab05 | 359 | // 3 . 3 2 CR |
electronichamsters | 8:46c5e0bfab05 | 360 | |
electronichamsters | 8:46c5e0bfab05 | 361 | //expect either button1 or button2 is grounded, b/c using SPDT reed switch |
electronichamsters | 8:46c5e0bfab05 | 362 | //the "common" pin on the reed switch should be on GND |
electronichamsters | 8:46c5e0bfab05 | 363 | uint8_t button1_state = button1.read(); |
electronichamsters | 8:46c5e0bfab05 | 364 | uint8_t button2_state = button2.read(); |
electronichamsters | 8:46c5e0bfab05 | 365 | |
electronichamsters | 8:46c5e0bfab05 | 366 | |
electronichamsters | 8:46c5e0bfab05 | 367 | //let's just update the pins on every wake. Insurance against const drain. |
electronichamsters | 8:46c5e0bfab05 | 368 | //if state == 0, pin is grounded. Unset interrupt and float pin |
electronichamsters | 8:46c5e0bfab05 | 369 | //set the other input |
electronichamsters | 8:46c5e0bfab05 | 370 | if ( (button1_state == 0) && (button2_state == 1) ) |
electronichamsters | 8:46c5e0bfab05 | 371 | { |
electronichamsters | 8:46c5e0bfab05 | 372 | magnet_near = 1; |
electronichamsters | 8:46c5e0bfab05 | 373 | //AdvData[4] = 0x11; //dont' set ADV data directly. Using json now, need spacing |
electronichamsters | 8:46c5e0bfab05 | 374 | //button1.disable_irq() //don't know if disables IRQ on port or pin |
electronichamsters | 8:46c5e0bfab05 | 375 | button1.fall(NULL); //disable interrupt |
electronichamsters | 8:46c5e0bfab05 | 376 | button1.rise(NULL); //disable interrupt |
electronichamsters | 8:46c5e0bfab05 | 377 | button1.mode(PullNone); //float pin to save battery |
electronichamsters | 8:46c5e0bfab05 | 378 | |
electronichamsters | 8:46c5e0bfab05 | 379 | //button2.disable_irq() //don't know if disables IRQ on port or pin |
electronichamsters | 8:46c5e0bfab05 | 380 | button2.fall(buttonReleasedCallback); //disable interrupt |
electronichamsters | 8:46c5e0bfab05 | 381 | button2.rise(buttonReleasedCallback); //disable interrupt |
electronichamsters | 8:46c5e0bfab05 | 382 | button2.mode(PullUp); //float pin to save battery |
electronichamsters | 8:46c5e0bfab05 | 383 | } |
electronichamsters | 8:46c5e0bfab05 | 384 | else if ( (button1_state == 1) && (button2_state == 0) ) //assume other pin is open circuit |
electronichamsters | 8:46c5e0bfab05 | 385 | { |
electronichamsters | 8:46c5e0bfab05 | 386 | magnet_near = 0; |
electronichamsters | 8:46c5e0bfab05 | 387 | //AdvData[4] = 0x22; //dont' set ADV data directly. Using json now, need spacing |
electronichamsters | 8:46c5e0bfab05 | 388 | //button1.disable_irq() //don't know if disables IRQ on port or pin |
electronichamsters | 8:46c5e0bfab05 | 389 | button1.fall(buttonReleasedCallback); //disable interrupt |
electronichamsters | 8:46c5e0bfab05 | 390 | button1.rise(buttonReleasedCallback); //disable interrupt |
electronichamsters | 8:46c5e0bfab05 | 391 | button1.mode(PullUp); //float pin to save battery |
electronichamsters | 8:46c5e0bfab05 | 392 | |
electronichamsters | 8:46c5e0bfab05 | 393 | //button2.disable_irq() //don't know if disables IRQ on port or pin |
electronichamsters | 8:46c5e0bfab05 | 394 | button2.fall(NULL); //disable interrupt |
electronichamsters | 8:46c5e0bfab05 | 395 | button2.rise(NULL); //disable interrupt |
electronichamsters | 8:46c5e0bfab05 | 396 | button2.mode(PullNone); //float pin to save battery |
electronichamsters | 8:46c5e0bfab05 | 397 | } |
electronichamsters | 8:46c5e0bfab05 | 398 | else //odd state, shouldn't happen, suck battery and pullup both pins |
electronichamsters | 8:46c5e0bfab05 | 399 | { |
electronichamsters | 8:46c5e0bfab05 | 400 | magnet_near = 2; |
electronichamsters | 8:46c5e0bfab05 | 401 | //AdvData[4] = 0x33; |
electronichamsters | 8:46c5e0bfab05 | 402 | //button1.disable_irq() //don't know if disables IRQ on port or pin |
electronichamsters | 8:46c5e0bfab05 | 403 | button1.fall(buttonReleasedCallback); //disable interrupt |
electronichamsters | 8:46c5e0bfab05 | 404 | button1.rise(buttonReleasedCallback); //disable interrupt |
electronichamsters | 8:46c5e0bfab05 | 405 | button1.mode(PullUp); //float pin to save battery |
electronichamsters | 8:46c5e0bfab05 | 406 | |
electronichamsters | 8:46c5e0bfab05 | 407 | //button2.disable_irq() //don't know if disables IRQ on port or pin |
electronichamsters | 8:46c5e0bfab05 | 408 | button2.fall(buttonReleasedCallback); //disable interrupt |
electronichamsters | 8:46c5e0bfab05 | 409 | button2.rise(buttonReleasedCallback); //disable interrupt |
electronichamsters | 8:46c5e0bfab05 | 410 | button2.mode(PullUp); //float pin to save battery |
electronichamsters | 8:46c5e0bfab05 | 411 | } |
electronichamsters | 8:46c5e0bfab05 | 412 | |
electronichamsters | 8:46c5e0bfab05 | 413 | |
electronichamsters | 8:46c5e0bfab05 | 414 | if (flag_update_io) { |
andresag | 7:0a8bbb6dea16 | 415 | /* Do blocking calls or whatever hardware-specific action is |
andresag | 7:0a8bbb6dea16 | 416 | * necessary to poll the sensor. */ |
electronichamsters | 8:46c5e0bfab05 | 417 | |
electronichamsters | 8:46c5e0bfab05 | 418 | //analog reading consumes 940uA if not disabled |
electronichamsters | 8:46c5e0bfab05 | 419 | bat_reading = (float)my_analogin_read_u16(); |
electronichamsters | 8:46c5e0bfab05 | 420 | bat_reading = (bat_reading * 3.6) / 1024.0; |
electronichamsters | 8:46c5e0bfab05 | 421 | #if MyDebugEnb |
electronichamsters | 8:46c5e0bfab05 | 422 | device.printf("bat reading: %f \r\n", bat_reading); |
electronichamsters | 8:46c5e0bfab05 | 423 | #endif |
electronichamsters | 8:46c5e0bfab05 | 424 | |
electronichamsters | 8:46c5e0bfab05 | 425 | //memset(&buffer[0], 0, sizeof(buffer)); //clear out buffer |
electronichamsters | 8:46c5e0bfab05 | 426 | //sprintf (buffer, "%f.2", bat_reading); //don't know what i'm doing |
electronichamsters | 8:46c5e0bfab05 | 427 | //sprintf (buffer, "%.2f", bat_reading); |
electronichamsters | 8:46c5e0bfab05 | 428 | //AdvData[8] = buffer[0]; //"3"=0x33 |
electronichamsters | 8:46c5e0bfab05 | 429 | //AdvData[9] = buffer[1]; //"."=0x2E |
electronichamsters | 8:46c5e0bfab05 | 430 | //AdvData[10] = buffer[2];//"3"=0x33 |
electronichamsters | 8:46c5e0bfab05 | 431 | //AdvData[11] = buffer[3];//"2"=0x32 |
electronichamsters | 8:46c5e0bfab05 | 432 | |
electronichamsters | 8:46c5e0bfab05 | 433 | //disable ADC |
electronichamsters | 8:46c5e0bfab05 | 434 | NRF_ADC->TASKS_STOP = 1; |
electronichamsters | 8:46c5e0bfab05 | 435 | NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Disabled; //disable to shutdown ADC & lower bat consumption |
electronichamsters | 8:46c5e0bfab05 | 436 | |
electronichamsters | 8:46c5e0bfab05 | 437 | //*********************************** |
electronichamsters | 8:46c5e0bfab05 | 438 | //form JSON string in ADV_DATA |
electronichamsters | 8:46c5e0bfab05 | 439 | //1) volts starts at AdvData[8] |
electronichamsters | 8:46c5e0bfab05 | 440 | |
electronichamsters | 8:46c5e0bfab05 | 441 | |
electronichamsters | 8:46c5e0bfab05 | 442 | memset(&AdvData[0], 0, sizeof(AdvData)); |
electronichamsters | 8:46c5e0bfab05 | 443 | uint8_t JSON_loc=0; //DEC"vo": <--- |
electronichamsters | 8:46c5e0bfab05 | 444 | |
electronichamsters | 8:46c5e0bfab05 | 445 | AdvData[JSON_loc] = 0x44; //D |
electronichamsters | 8:46c5e0bfab05 | 446 | JSON_loc++; |
electronichamsters | 8:46c5e0bfab05 | 447 | AdvData[JSON_loc] = 0x45; //E |
electronichamsters | 8:46c5e0bfab05 | 448 | JSON_loc++; |
electronichamsters | 8:46c5e0bfab05 | 449 | AdvData[JSON_loc] = 0x43; //C |
electronichamsters | 8:46c5e0bfab05 | 450 | JSON_loc++; |
electronichamsters | 8:46c5e0bfab05 | 451 | AdvData[JSON_loc] = mySeconds & 0xFF; //reserved for timer |
electronichamsters | 8:46c5e0bfab05 | 452 | JSON_loc++; |
electronichamsters | 8:46c5e0bfab05 | 453 | AdvData[JSON_loc] = (mySeconds >> 8) & 0xFF;; //reserved for timer |
electronichamsters | 8:46c5e0bfab05 | 454 | JSON_loc++; |
electronichamsters | 8:46c5e0bfab05 | 455 | AdvData[JSON_loc] = 0x22; //" |
electronichamsters | 8:46c5e0bfab05 | 456 | JSON_loc++; |
electronichamsters | 8:46c5e0bfab05 | 457 | AdvData[JSON_loc] = 0x76; //V volt |
electronichamsters | 8:46c5e0bfab05 | 458 | JSON_loc++; |
electronichamsters | 8:46c5e0bfab05 | 459 | AdvData[JSON_loc] = 0x6f; //o |
electronichamsters | 8:46c5e0bfab05 | 460 | JSON_loc++; |
electronichamsters | 8:46c5e0bfab05 | 461 | AdvData[JSON_loc] = 0x6C; //l |
electronichamsters | 8:46c5e0bfab05 | 462 | JSON_loc++; |
electronichamsters | 8:46c5e0bfab05 | 463 | AdvData[JSON_loc] = 0x74; //t |
electronichamsters | 8:46c5e0bfab05 | 464 | JSON_loc++; |
electronichamsters | 8:46c5e0bfab05 | 465 | AdvData[JSON_loc] = 0x22; //" |
electronichamsters | 8:46c5e0bfab05 | 466 | JSON_loc++; |
electronichamsters | 8:46c5e0bfab05 | 467 | AdvData[JSON_loc] = 0x3a; //: |
electronichamsters | 8:46c5e0bfab05 | 468 | JSON_loc++; |
electronichamsters | 8:46c5e0bfab05 | 469 | |
electronichamsters | 8:46c5e0bfab05 | 470 | |
electronichamsters | 8:46c5e0bfab05 | 471 | |
electronichamsters | 8:46c5e0bfab05 | 472 | //write battery voltage |
electronichamsters | 8:46c5e0bfab05 | 473 | uint8_t total_chars; |
electronichamsters | 8:46c5e0bfab05 | 474 | memset(&buffer[0], 0, sizeof(buffer)); //clear out buffer |
electronichamsters | 8:46c5e0bfab05 | 475 | total_chars = sprintf (buffer, "%.2f", bat_reading); //returns total number of characters |
electronichamsters | 8:46c5e0bfab05 | 476 | |
electronichamsters | 8:46c5e0bfab05 | 477 | #if MyDebugEnb |
electronichamsters | 8:46c5e0bfab05 | 478 | device.printf("char buff: %c%c%c%c \r\n", buffer[0], buffer[1], buffer[2], buffer[3]); |
electronichamsters | 8:46c5e0bfab05 | 479 | device.printf("num chars: %d \r\n", total_chars); |
electronichamsters | 8:46c5e0bfab05 | 480 | #endif |
electronichamsters | 8:46c5e0bfab05 | 481 | |
electronichamsters | 8:46c5e0bfab05 | 482 | for (int i=0; i < total_chars; i++) |
electronichamsters | 8:46c5e0bfab05 | 483 | { |
electronichamsters | 8:46c5e0bfab05 | 484 | AdvData[JSON_loc] = buffer[i]; |
electronichamsters | 8:46c5e0bfab05 | 485 | JSON_loc++; |
electronichamsters | 8:46c5e0bfab05 | 486 | #if MyDebugEnb |
electronichamsters | 8:46c5e0bfab05 | 487 | device.printf("JSON_loc: %d buf[]:%c \r\n", JSON_loc, buffer[i]); |
electronichamsters | 8:46c5e0bfab05 | 488 | #endif |
electronichamsters | 8:46c5e0bfab05 | 489 | } //JSON_loc left at location of next character |
electronichamsters | 8:46c5e0bfab05 | 490 | |
electronichamsters | 8:46c5e0bfab05 | 491 | //DEC"vo":3.11," |
electronichamsters | 8:46c5e0bfab05 | 492 | AdvData[JSON_loc] = 0x2c; //, |
electronichamsters | 8:46c5e0bfab05 | 493 | JSON_loc++; |
electronichamsters | 8:46c5e0bfab05 | 494 | |
electronichamsters | 8:46c5e0bfab05 | 495 | AdvData[JSON_loc] = 0x22; //" start mag |
electronichamsters | 8:46c5e0bfab05 | 496 | JSON_loc++; |
electronichamsters | 8:46c5e0bfab05 | 497 | |
electronichamsters | 8:46c5e0bfab05 | 498 | AdvData[JSON_loc] = 0x6d; //m |
electronichamsters | 8:46c5e0bfab05 | 499 | JSON_loc++; |
electronichamsters | 8:46c5e0bfab05 | 500 | |
electronichamsters | 8:46c5e0bfab05 | 501 | AdvData[JSON_loc] = 0x61; //a |
electronichamsters | 8:46c5e0bfab05 | 502 | JSON_loc++; |
electronichamsters | 8:46c5e0bfab05 | 503 | |
electronichamsters | 8:46c5e0bfab05 | 504 | AdvData[JSON_loc] = 0x67; //g |
electronichamsters | 8:46c5e0bfab05 | 505 | JSON_loc++; |
electronichamsters | 8:46c5e0bfab05 | 506 | |
electronichamsters | 8:46c5e0bfab05 | 507 | if (flag_periodic_call) |
electronichamsters | 8:46c5e0bfab05 | 508 | { |
electronichamsters | 8:46c5e0bfab05 | 509 | //AdvData[JSON_loc] = 0x2f; // "/" |
electronichamsters | 8:46c5e0bfab05 | 510 | //JSON_loc++; |
electronichamsters | 8:46c5e0bfab05 | 511 | AdvData[JSON_loc] = 0x2f; // "/" |
electronichamsters | 8:46c5e0bfab05 | 512 | JSON_loc++; |
electronichamsters | 8:46c5e0bfab05 | 513 | AdvData[JSON_loc] = 0x70; // "p" |
electronichamsters | 8:46c5e0bfab05 | 514 | JSON_loc++; |
electronichamsters | 8:46c5e0bfab05 | 515 | }//end if period call |
electronichamsters | 8:46c5e0bfab05 | 516 | |
electronichamsters | 8:46c5e0bfab05 | 517 | AdvData[JSON_loc] = 0x22; //" |
electronichamsters | 8:46c5e0bfab05 | 518 | JSON_loc++; |
electronichamsters | 8:46c5e0bfab05 | 519 | |
electronichamsters | 8:46c5e0bfab05 | 520 | AdvData[JSON_loc] = 0x3a; //: |
electronichamsters | 8:46c5e0bfab05 | 521 | JSON_loc++; |
electronichamsters | 8:46c5e0bfab05 | 522 | |
electronichamsters | 8:46c5e0bfab05 | 523 | //prep magnet location (1 or 0) for char[] |
electronichamsters | 8:46c5e0bfab05 | 524 | memset(&buffer[0], 0, sizeof(buffer)); //clear out buffer |
electronichamsters | 8:46c5e0bfab05 | 525 | //magnet_near is an integer |
electronichamsters | 8:46c5e0bfab05 | 526 | total_chars = sprintf (buffer, "%d", magnet_near); //returns total number of characters |
electronichamsters | 8:46c5e0bfab05 | 527 | for (int i=0; i < total_chars; i++) |
electronichamsters | 8:46c5e0bfab05 | 528 | { |
electronichamsters | 8:46c5e0bfab05 | 529 | AdvData[JSON_loc] = buffer[i]; |
electronichamsters | 8:46c5e0bfab05 | 530 | JSON_loc++; |
electronichamsters | 8:46c5e0bfab05 | 531 | } //JSON_loc left at location of next character |
electronichamsters | 8:46c5e0bfab05 | 532 | |
electronichamsters | 8:46c5e0bfab05 | 533 | //MUST null terminate for JSON to read correctly, else get intermittent JSON parse errors at gateway |
electronichamsters | 8:46c5e0bfab05 | 534 | //happens when string is shorter than last string, get trash left overs |
electronichamsters | 8:46c5e0bfab05 | 535 | //not really needed after clearning AdvData at start. |
electronichamsters | 8:46c5e0bfab05 | 536 | |
electronichamsters | 8:46c5e0bfab05 | 537 | |
electronichamsters | 8:46c5e0bfab05 | 538 | //AdvData[JSON_loc] = 0x0; //null terminate here |
electronichamsters | 8:46c5e0bfab05 | 539 | |
rgrover1 | 5:f4d74a8cad43 | 540 | ApplicationData_t appData; |
rgrover1 | 5:f4d74a8cad43 | 541 | setupApplicationData(appData); |
electronichamsters | 8:46c5e0bfab05 | 542 | |
electronichamsters | 8:46c5e0bfab05 | 543 | |
electronichamsters | 8:46c5e0bfab05 | 544 | for (int i=0; i<16; i++) |
electronichamsters | 8:46c5e0bfab05 | 545 | { |
electronichamsters | 8:46c5e0bfab05 | 546 | src_buf[i] = AdvData[i+3]; |
electronichamsters | 8:46c5e0bfab05 | 547 | } |
rgrover1 | 5:f4d74a8cad43 | 548 | |
electronichamsters | 8:46c5e0bfab05 | 549 | //nrf_ecb_set_key(key_buf); |
electronichamsters | 8:46c5e0bfab05 | 550 | nrf_ecb_init(); |
electronichamsters | 8:46c5e0bfab05 | 551 | nrf_ecb_set_key(key_buf); |
electronichamsters | 8:46c5e0bfab05 | 552 | bool successful_ecb = nrf_ecb_crypt(des_buf, src_buf); |
electronichamsters | 8:46c5e0bfab05 | 553 | #if MyDebugEnb |
electronichamsters | 8:46c5e0bfab05 | 554 | device.printf("success ecb = %d \r\n", successful_ecb); |
electronichamsters | 8:46c5e0bfab05 | 555 | device.printf("src_buf: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x \r\n", src_buf[0], src_buf[1], src_buf[2], src_buf[3], src_buf[4], src_buf[5], src_buf[6], src_buf[7], src_buf[8], src_buf[9], src_buf[10], src_buf[11], src_buf[12], src_buf[13], src_buf[14], src_buf[15]); |
electronichamsters | 8:46c5e0bfab05 | 556 | device.printf("des_buf: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x \r\n", des_buf[0], des_buf[1], des_buf[2], des_buf[3], des_buf[4], des_buf[5], des_buf[6], des_buf[7], des_buf[8], des_buf[9], des_buf[10], des_buf[11], des_buf[12], des_buf[13], des_buf[14], des_buf[15]); |
electronichamsters | 8:46c5e0bfab05 | 557 | #endif |
electronichamsters | 8:46c5e0bfab05 | 558 | for (int i=0; i<16; i++) |
electronichamsters | 8:46c5e0bfab05 | 559 | { |
electronichamsters | 8:46c5e0bfab05 | 560 | AdvData[i+3] = des_buf[i]; |
electronichamsters | 8:46c5e0bfab05 | 561 | } |
electronichamsters | 8:46c5e0bfab05 | 562 | |
electronichamsters | 8:46c5e0bfab05 | 563 | //ble.gap().updateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *) &appData, sizeof(ApplicationData_t)); |
electronichamsters | 8:46c5e0bfab05 | 564 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData)); |
electronichamsters | 8:46c5e0bfab05 | 565 | |
electronichamsters | 8:46c5e0bfab05 | 566 | flag_update_io = false; |
electronichamsters | 8:46c5e0bfab05 | 567 | flag_periodic_call = false; |
electronichamsters | 8:46c5e0bfab05 | 568 | |
electronichamsters | 8:46c5e0bfab05 | 569 | //GAP::AddressType_t *myType; |
electronichamsters | 8:46c5e0bfab05 | 570 | //GAP::Address_t myAddress |
electronichamsters | 8:46c5e0bfab05 | 571 | //ble_error_t getAddress(Gap::AddressType_t *typeP, Gap::Address_t address) |
electronichamsters | 8:46c5e0bfab05 | 572 | //ble.gap().getAddress(myType, myAddress); |
electronichamsters | 8:46c5e0bfab05 | 573 | //ble.gap().getAddress(Gap::AddressType_t *typeP, Gap::Address_t address); |
electronichamsters | 8:46c5e0bfab05 | 574 | uint8_t mac[6] = {0x0,0x0,0x0,0x0,0x0,0x0}; |
electronichamsters | 8:46c5e0bfab05 | 575 | ble.getAddress(0,mac); //NOTE: last byte of MAC (as shown on phone app) is at mac[0], not mac[6]; |
electronichamsters | 8:46c5e0bfab05 | 576 | #if MyDebugEnb |
electronichamsters | 8:46c5e0bfab05 | 577 | device.printf("mac = "); |
electronichamsters | 8:46c5e0bfab05 | 578 | for (int i=0; i<6; i++) |
electronichamsters | 8:46c5e0bfab05 | 579 | { |
electronichamsters | 8:46c5e0bfab05 | 580 | device.printf("%x:", mac[i]); |
electronichamsters | 8:46c5e0bfab05 | 581 | } |
electronichamsters | 8:46c5e0bfab05 | 582 | device.printf("\r\n"); |
electronichamsters | 8:46c5e0bfab05 | 583 | #endif |
electronichamsters | 8:46c5e0bfab05 | 584 | ble.gap().startAdvertising(); |
electronichamsters | 8:46c5e0bfab05 | 585 | tic_adv.attach(stop_adv_Callback, 2); /* trigger turn off advertisement after X seconds */ |
electronichamsters | 8:46c5e0bfab05 | 586 | |
electronichamsters | 8:46c5e0bfab05 | 587 | /* |
electronichamsters | 8:46c5e0bfab05 | 588 | Timer myTimer; //timed advertising |
electronichamsters | 8:46c5e0bfab05 | 589 | myTimer.start(); |
electronichamsters | 8:46c5e0bfab05 | 590 | uint32_t duration = myTimer.read_ms(); |
electronichamsters | 8:46c5e0bfab05 | 591 | |
electronichamsters | 8:46c5e0bfab05 | 592 | //do this as a ticker instead of keeping processor on |
electronichamsters | 8:46c5e0bfab05 | 593 | while (duration < 15000) //advertise for 1000 ms |
electronichamsters | 8:46c5e0bfab05 | 594 | { |
electronichamsters | 8:46c5e0bfab05 | 595 | duration = myTimer.read_ms(); //read miliseconds |
electronichamsters | 8:46c5e0bfab05 | 596 | } |
electronichamsters | 8:46c5e0bfab05 | 597 | myTimer.stop(); |
electronichamsters | 8:46c5e0bfab05 | 598 | ble.gap().stopAdvertising(); |
electronichamsters | 8:46c5e0bfab05 | 599 | */ |
electronichamsters | 8:46c5e0bfab05 | 600 | |
electronichamsters | 8:46c5e0bfab05 | 601 | }//end flag_update_io |
electronichamsters | 8:46c5e0bfab05 | 602 | |
electronichamsters | 8:46c5e0bfab05 | 603 | |
electronichamsters | 8:46c5e0bfab05 | 604 | /* |
electronichamsters | 8:46c5e0bfab05 | 605 | if (flag_set_debounce_tic == true) |
electronichamsters | 8:46c5e0bfab05 | 606 | { |
electronichamsters | 8:46c5e0bfab05 | 607 | tic_debounce.attach(); |
electronichamsters | 8:46c5e0bfab05 | 608 | //flag_set_debounce_tic = false; |
electronichamsters | 8:46c5e0bfab05 | 609 | |
sunsmile2015 | 0:3dc6e424dba0 | 610 | } |
electronichamsters | 8:46c5e0bfab05 | 611 | */ |
electronichamsters | 8:46c5e0bfab05 | 612 | |
electronichamsters | 8:46c5e0bfab05 | 613 | //if (trigger_Detach_ADV_Tick == false) |
electronichamsters | 8:46c5e0bfab05 | 614 | //{ |
electronichamsters | 8:46c5e0bfab05 | 615 | //} |
electronichamsters | 8:46c5e0bfab05 | 616 | if (flag_detach_adv_tic == true) //Stop Advertising |
electronichamsters | 8:46c5e0bfab05 | 617 | { |
electronichamsters | 8:46c5e0bfab05 | 618 | ble.gap().stopAdvertising(); //may be safer to execute BLE operations in main |
electronichamsters | 8:46c5e0bfab05 | 619 | tic_adv.detach(); |
electronichamsters | 8:46c5e0bfab05 | 620 | flag_detach_adv_tic = false; |
electronichamsters | 8:46c5e0bfab05 | 621 | } |
electronichamsters | 8:46c5e0bfab05 | 622 | //device.printf("Input Voltage: %f\n\r",bat_reading); |
electronichamsters | 8:46c5e0bfab05 | 623 | |
electronichamsters | 8:46c5e0bfab05 | 624 | ble.waitForEvent(); //sleeps until interrupt |
electronichamsters | 8:46c5e0bfab05 | 625 | |
rgrover1 | 5:f4d74a8cad43 | 626 | |
electronichamsters | 8:46c5e0bfab05 | 627 | }//end forever while |
electronichamsters | 8:46c5e0bfab05 | 628 | }//end main |