Adaptation of SMPIR for the Window Sensor Module of the Agile-IoT project.
Dependencies: BLE_API mbed nRF51822
Fork of SensorModulePIR by
main.cpp@6:80f2517965bb, 2017-05-11 (annotated)
- Committer:
- MisterGiet
- Date:
- Thu May 11 17:24:04 2017 +0000
- Revision:
- 6:80f2517965bb
- Parent:
- 5:173ffbf84b3a
- Child:
- 7:19fe55e82dd9
- Child:
- 9:97d8ea1feeed
Added: realtime update of all BLE services (debug purposes); upper limit (255) control on presence counter;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
waltercolitti | 0:eb280529b0ef | 1 | #include "mbed.h" |
MisterGiet | 3:53db6c57bb61 | 2 | #include "defineGPIOs.h" |
waltercolitti | 0:eb280529b0ef | 3 | #include "BLE.h" |
MisterGiet | 3:53db6c57bb61 | 4 | //#include "HealthThermometerService.h" |
MisterGiet | 3:53db6c57bb61 | 5 | #include "ble/services/HealthThermometerService.h" // added |
waltercolitti | 0:eb280529b0ef | 6 | #include "PresenceDetectionService.h" |
MisterGiet | 3:53db6c57bb61 | 7 | #include "HumidityMeasureService.h" |
waltercolitti | 0:eb280529b0ef | 8 | #include "ble_gatt.h" |
waltercolitti | 0:eb280529b0ef | 9 | #include "Si7020.h" |
MisterGiet | 3:53db6c57bb61 | 10 | #include "UARTService.h" |
MisterGiet | 3:53db6c57bb61 | 11 | #include <string.h> |
waltercolitti | 0:eb280529b0ef | 12 | |
MisterGiet | 3:53db6c57bb61 | 13 | |
MisterGiet | 3:53db6c57bb61 | 14 | // OFFSET TEMPERATURE CONFIGURATIONS: |
MisterGiet | 3:53db6c57bb61 | 15 | #define STANDARD -1 // Squared radar sensor module: -1ºC |
MisterGiet | 3:53db6c57bb61 | 16 | #define ZERO 0 // no offset |
MisterGiet | 3:53db6c57bb61 | 17 | #define OFFSET STANDARD |
MisterGiet | 3:53db6c57bb61 | 18 | |
MisterGiet | 3:53db6c57bb61 | 19 | #define NEED_SERIAL_CONSOLE_OUTPUT 0 |
MisterGiet | 3:53db6c57bb61 | 20 | #if NEED_SERIAL_CONSOLE_OUTPUT |
MisterGiet | 3:53db6c57bb61 | 21 | Serial pc(PIN_TX, PIN_RX); |
MisterGiet | 3:53db6c57bb61 | 22 | #define SERIAL_DEBUG(...) { printf(__VA_ARGS__); } //Defaults to stdio without having to wirte pcUart explicitly |
MisterGiet | 3:53db6c57bb61 | 23 | #else |
MisterGiet | 3:53db6c57bb61 | 24 | #define SERIAL_DEBUG(...) /* nothing */ |
MisterGiet | 3:53db6c57bb61 | 25 | #endif |
MisterGiet | 3:53db6c57bb61 | 26 | |
MisterGiet | 3:53db6c57bb61 | 27 | #define NEED_BLE_OUTPUT 0 // Set this if you need debug messages on the console; |
MisterGiet | 3:53db6c57bb61 | 28 | #if NEED_BLE_OUTPUT |
MisterGiet | 3:53db6c57bb61 | 29 | #define DEBUG(STR) { if (uart) uart->write(STR, strlen(STR)); } |
MisterGiet | 3:53db6c57bb61 | 30 | #else |
MisterGiet | 3:53db6c57bb61 | 31 | #define DEBUG(...) /* nothing */ |
MisterGiet | 3:53db6c57bb61 | 32 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
MisterGiet | 3:53db6c57bb61 | 33 | |
MisterGiet | 3:53db6c57bb61 | 34 | I2C i2c(PIN_SDA, PIN_SCL); |
waltercolitti | 0:eb280529b0ef | 35 | Si7020 tempsensor(&i2c); |
waltercolitti | 0:eb280529b0ef | 36 | |
MisterGiet | 6:80f2517965bb | 37 | InterruptIn motion_pin(PIN_PRESENCE_RIGHT); |
MisterGiet | 6:80f2517965bb | 38 | DigitalIn pinR(PIN_PRESENCE_RIGHT, PullNone); |
MisterGiet | 6:80f2517965bb | 39 | DigitalOut ledB(PIN_BLED_PCB, 1); |
MisterGiet | 6:80f2517965bb | 40 | DigitalOut ledR(PIN_RLED_PCB, 1); |
MisterGiet | 3:53db6c57bb61 | 41 | |
MisterGiet | 6:80f2517965bb | 42 | bool presenceState = false; |
MisterGiet | 6:80f2517965bb | 43 | int presenceCounter = 0; |
MisterGiet | 6:80f2517965bb | 44 | //uint8_t presenceCounterBLE = 0; |
MisterGiet | 5:173ffbf84b3a | 45 | bool ledOn = 0; |
MisterGiet | 5:173ffbf84b3a | 46 | bool battNotify = 1; |
MisterGiet | 5:173ffbf84b3a | 47 | static volatile bool triggerPresencePolling = false; |
MisterGiet | 5:173ffbf84b3a | 48 | |
MisterGiet | 5:173ffbf84b3a | 49 | |
MisterGiet | 5:173ffbf84b3a | 50 | /* BLE CONFIGURATION */ |
MisterGiet | 5:173ffbf84b3a | 51 | |
MisterGiet | 5:173ffbf84b3a | 52 | BLE ble; |
MisterGiet | 6:80f2517965bb | 53 | static const char DEVICE_NAME[] = "SMxx"; |
MisterGiet | 5:173ffbf84b3a | 54 | static const uint16_t uuid16_list[] = {GattService::UUID_HEALTH_THERMOMETER_SERVICE}; |
MisterGiet | 5:173ffbf84b3a | 55 | static volatile bool triggerSensorPolling = false; |
MisterGiet | 6:80f2517965bb | 56 | static char fwversion[31] = "CAT-2017"; |
MisterGiet | 5:173ffbf84b3a | 57 | static uint8_t batteryLevel = 100; |
MisterGiet | 5:173ffbf84b3a | 58 | static int8_t TxPower = +4; |
MisterGiet | 5:173ffbf84b3a | 59 | |
MisterGiet | 5:173ffbf84b3a | 60 | static HealthThermometerService *thermometerServicePtr; |
MisterGiet | 5:173ffbf84b3a | 61 | PresenceDetectionService *presenceService = NULL; |
MisterGiet | 5:173ffbf84b3a | 62 | HumidityMeasureService *humidityService = NULL; |
MisterGiet | 5:173ffbf84b3a | 63 | UARTService *uart; |
MisterGiet | 5:173ffbf84b3a | 64 | const static uint8_t ManufData[] = {0x01,0x02,0x03,0x04,0x05}; // Set up to 26B of advertising data to use for the Manufacturer data. |
MisterGiet | 5:173ffbf84b3a | 65 | |
MisterGiet | 5:173ffbf84b3a | 66 | // Firmware |
MisterGiet | 5:173ffbf84b3a | 67 | GattCharacteristic fwChars(GattCharacteristic::UUID_FIRMWARE_REVISION_STRING_CHAR, (uint8_t *)fwversion, sizeof(fwversion), sizeof(fwversion),GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
MisterGiet | 5:173ffbf84b3a | 68 | GattCharacteristic *firmwareChars[] = {&fwChars }; |
MisterGiet | 5:173ffbf84b3a | 69 | GattService firmwareService(GattService::UUID_DEVICE_INFORMATION_SERVICE, firmwareChars, sizeof(firmwareChars) / sizeof(GattCharacteristic *)); |
MisterGiet | 5:173ffbf84b3a | 70 | |
MisterGiet | 5:173ffbf84b3a | 71 | //Battery |
MisterGiet | 5:173ffbf84b3a | 72 | GattCharacteristic batteryPercentage(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, (uint8_t *)batteryLevel, sizeof(batteryLevel), sizeof(batteryLevel), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
MisterGiet | 5:173ffbf84b3a | 73 | GattCharacteristic *batteryChars[] = {&batteryPercentage}; |
MisterGiet | 5:173ffbf84b3a | 74 | GattService batteryService(GattService::UUID_BATTERY_SERVICE, batteryChars, sizeof(batteryChars) / sizeof(GattCharacteristic *)); |
MisterGiet | 5:173ffbf84b3a | 75 | |
MisterGiet | 5:173ffbf84b3a | 76 | //Power |
MisterGiet | 5:173ffbf84b3a | 77 | GattCharacteristic TxPowerChar(GattCharacteristic::UUID_TX_POWER_LEVEL_CHAR, (uint8_t*)&TxPower, sizeof(TxPower), sizeof(TxPower), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
MisterGiet | 5:173ffbf84b3a | 78 | GattCharacteristic *charTable[] = {&TxPowerChar}; |
MisterGiet | 5:173ffbf84b3a | 79 | GattService TxPowerService(GattService::UUID_TX_POWER_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
MisterGiet | 5:173ffbf84b3a | 80 | |
MisterGiet | 3:53db6c57bb61 | 81 | /* BATTERY MEASUREMENT */ |
MisterGiet | 3:53db6c57bb61 | 82 | void my_analogin_init(void) |
MisterGiet | 3:53db6c57bb61 | 83 | { |
MisterGiet | 3:53db6c57bb61 | 84 | NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled; |
MisterGiet | 3:53db6c57bb61 | 85 | NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) | |
MisterGiet | 3:53db6c57bb61 | 86 | (ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) | |
MisterGiet | 3:53db6c57bb61 | 87 | (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) | |
MisterGiet | 3:53db6c57bb61 | 88 | (ADC_CONFIG_PSEL_Disabled << ADC_CONFIG_PSEL_Pos) | |
MisterGiet | 3:53db6c57bb61 | 89 | (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos); |
MisterGiet | 3:53db6c57bb61 | 90 | } |
MisterGiet | 3:53db6c57bb61 | 91 | |
MisterGiet | 3:53db6c57bb61 | 92 | uint16_t my_analogin_read_u16(void) |
MisterGiet | 3:53db6c57bb61 | 93 | { |
MisterGiet | 3:53db6c57bb61 | 94 | NRF_ADC->CONFIG &= ~ADC_CONFIG_PSEL_Msk; |
MisterGiet | 3:53db6c57bb61 | 95 | NRF_ADC->CONFIG |= ADC_CONFIG_PSEL_Disabled << ADC_CONFIG_PSEL_Pos; |
MisterGiet | 3:53db6c57bb61 | 96 | NRF_ADC->TASKS_START = 1; |
MisterGiet | 3:53db6c57bb61 | 97 | while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) {}; |
MisterGiet | 3:53db6c57bb61 | 98 | return (uint16_t)NRF_ADC->RESULT; // 10 bit |
MisterGiet | 3:53db6c57bb61 | 99 | } |
waltercolitti | 0:eb280529b0ef | 100 | |
MisterGiet | 3:53db6c57bb61 | 101 | float getBatteryVolt (void) |
MisterGiet | 3:53db6c57bb61 | 102 | { |
MisterGiet | 3:53db6c57bb61 | 103 | char Vpower[10]; |
MisterGiet | 3:53db6c57bb61 | 104 | |
MisterGiet | 3:53db6c57bb61 | 105 | float Vadc = (float)my_analogin_read_u16(); |
MisterGiet | 3:53db6c57bb61 | 106 | float Vref = 1.2; // Internal Vref |
MisterGiet | 3:53db6c57bb61 | 107 | float Vcc = 3 * 4 * Vref * Vadc / 1024.0; |
MisterGiet | 3:53db6c57bb61 | 108 | |
MisterGiet | 3:53db6c57bb61 | 109 | sprintf (Vpower, "Vcc=%.2fV, ", Vcc); |
MisterGiet | 3:53db6c57bb61 | 110 | DEBUG(Vpower); |
MisterGiet | 3:53db6c57bb61 | 111 | return Vcc; |
MisterGiet | 3:53db6c57bb61 | 112 | } |
MisterGiet | 3:53db6c57bb61 | 113 | |
MisterGiet | 3:53db6c57bb61 | 114 | int getBatteryPercent () |
MisterGiet | 3:53db6c57bb61 | 115 | { |
MisterGiet | 3:53db6c57bb61 | 116 | char batt_mess[10]; |
MisterGiet | 4:de22f0d73c12 | 117 | float Vbat_min = 2; //2; |
MisterGiet | 3:53db6c57bb61 | 118 | float Vbat_max = 3; |
MisterGiet | 3:53db6c57bb61 | 119 | int battLevel = (int) ( ((getBatteryVolt()-Vbat_min) / (Vbat_max-Vbat_min)) *100); |
MisterGiet | 3:53db6c57bb61 | 120 | |
MisterGiet | 3:53db6c57bb61 | 121 | sprintf (batt_mess, "Vbatt=%i, ", battLevel); |
MisterGiet | 3:53db6c57bb61 | 122 | DEBUG(batt_mess); |
MisterGiet | 3:53db6c57bb61 | 123 | |
MisterGiet | 3:53db6c57bb61 | 124 | // To avoid values bigger than 100% |
MisterGiet | 3:53db6c57bb61 | 125 | if (battLevel > 100) { |
MisterGiet | 3:53db6c57bb61 | 126 | battLevel = 100; |
MisterGiet | 3:53db6c57bb61 | 127 | } |
MisterGiet | 3:53db6c57bb61 | 128 | |
MisterGiet | 4:de22f0d73c12 | 129 | // To avoid overflow due to a value lower than Vbat_min |
MisterGiet | 4:de22f0d73c12 | 130 | if (battLevel >= 250) { |
MisterGiet | 4:de22f0d73c12 | 131 | battLevel = 0; |
MisterGiet | 4:de22f0d73c12 | 132 | } |
MisterGiet | 4:de22f0d73c12 | 133 | |
MisterGiet | 3:53db6c57bb61 | 134 | return battLevel; |
MisterGiet | 3:53db6c57bb61 | 135 | } |
MisterGiet | 3:53db6c57bb61 | 136 | |
MisterGiet | 3:53db6c57bb61 | 137 | void blinkLed (bool enable, int color) |
MisterGiet | 3:53db6c57bb61 | 138 | { |
MisterGiet | 3:53db6c57bb61 | 139 | if (enable) { |
MisterGiet | 3:53db6c57bb61 | 140 | if (color == 1) { |
MisterGiet | 3:53db6c57bb61 | 141 | ledB=0; |
MisterGiet | 3:53db6c57bb61 | 142 | wait(0.2); |
MisterGiet | 3:53db6c57bb61 | 143 | ledB=1; |
MisterGiet | 3:53db6c57bb61 | 144 | wait(0.2); |
MisterGiet | 3:53db6c57bb61 | 145 | } |
MisterGiet | 3:53db6c57bb61 | 146 | if (color == 2) { |
MisterGiet | 3:53db6c57bb61 | 147 | ledR=0; |
MisterGiet | 3:53db6c57bb61 | 148 | wait(0.2); |
MisterGiet | 3:53db6c57bb61 | 149 | ledR=1; |
MisterGiet | 3:53db6c57bb61 | 150 | wait(0.2); |
MisterGiet | 3:53db6c57bb61 | 151 | } |
MisterGiet | 3:53db6c57bb61 | 152 | } else { |
MisterGiet | 3:53db6c57bb61 | 153 | // do nothing if enable=0 |
MisterGiet | 3:53db6c57bb61 | 154 | } |
MisterGiet | 3:53db6c57bb61 | 155 | } |
waltercolitti | 0:eb280529b0ef | 156 | |
MisterGiet | 6:80f2517965bb | 157 | uint8_t updatePresenceCounter (int counts) |
MisterGiet | 6:80f2517965bb | 158 | { |
MisterGiet | 6:80f2517965bb | 159 | uint8_t uint8_counter; |
MisterGiet | 6:80f2517965bb | 160 | |
MisterGiet | 6:80f2517965bb | 161 | if (counts>=255) { |
MisterGiet | 6:80f2517965bb | 162 | uint8_counter = 255; |
MisterGiet | 6:80f2517965bb | 163 | |
MisterGiet | 6:80f2517965bb | 164 | } else { |
MisterGiet | 6:80f2517965bb | 165 | uint8_counter = uint8_t(counts); |
MisterGiet | 6:80f2517965bb | 166 | } |
MisterGiet | 6:80f2517965bb | 167 | |
MisterGiet | 6:80f2517965bb | 168 | return uint8_counter; |
MisterGiet | 6:80f2517965bb | 169 | } |
MisterGiet | 6:80f2517965bb | 170 | |
MisterGiet | 3:53db6c57bb61 | 171 | /* Restart Advertising on disconnection*/ |
waltercolitti | 0:eb280529b0ef | 172 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
waltercolitti | 0:eb280529b0ef | 173 | { |
MisterGiet | 3:53db6c57bb61 | 174 | SERIAL_DEBUG("Disconnected\r\n"); |
MisterGiet | 3:53db6c57bb61 | 175 | BLE::Instance().gap().startAdvertising(); |
waltercolitti | 0:eb280529b0ef | 176 | } |
waltercolitti | 0:eb280529b0ef | 177 | |
waltercolitti | 0:eb280529b0ef | 178 | /** |
waltercolitti | 0:eb280529b0ef | 179 | * This function is called when the ble initialization process has failed |
waltercolitti | 0:eb280529b0ef | 180 | */ |
waltercolitti | 0:eb280529b0ef | 181 | void onBleInitError(BLE &ble, ble_error_t error) |
waltercolitti | 0:eb280529b0ef | 182 | { |
MisterGiet | 3:53db6c57bb61 | 183 | /* Avoid compiler warnings */ |
MisterGiet | 3:53db6c57bb61 | 184 | (void) ble; |
MisterGiet | 3:53db6c57bb61 | 185 | (void) error; |
waltercolitti | 0:eb280529b0ef | 186 | /* Initialization error handling should go here */ |
waltercolitti | 0:eb280529b0ef | 187 | } |
waltercolitti | 0:eb280529b0ef | 188 | |
MisterGiet | 3:53db6c57bb61 | 189 | |
waltercolitti | 0:eb280529b0ef | 190 | float getTemperature(void) |
waltercolitti | 0:eb280529b0ef | 191 | { |
MisterGiet | 3:53db6c57bb61 | 192 | float temp; |
MisterGiet | 4:de22f0d73c12 | 193 | |
MisterGiet | 3:53db6c57bb61 | 194 | if(tempsensor.getTemperature(&temp) != 0) { |
MisterGiet | 3:53db6c57bb61 | 195 | DEBUG("Error getting temperature"); |
MisterGiet | 3:53db6c57bb61 | 196 | temp = -1; |
MisterGiet | 3:53db6c57bb61 | 197 | } |
waltercolitti | 0:eb280529b0ef | 198 | |
MisterGiet | 3:53db6c57bb61 | 199 | // Debugging: |
MisterGiet | 3:53db6c57bb61 | 200 | SERIAL_DEBUG("\nTemperature = %.2f", temp); |
MisterGiet | 3:53db6c57bb61 | 201 | char message[50]; |
MisterGiet | 3:53db6c57bb61 | 202 | sprintf (message, "T=%.2fC\n", temp); |
MisterGiet | 3:53db6c57bb61 | 203 | DEBUG(message); |
MisterGiet | 4:de22f0d73c12 | 204 | |
MisterGiet | 3:53db6c57bb61 | 205 | // Adding offset: |
MisterGiet | 3:53db6c57bb61 | 206 | temp = temp + OFFSET; |
MisterGiet | 3:53db6c57bb61 | 207 | |
MisterGiet | 3:53db6c57bb61 | 208 | return temp; |
waltercolitti | 0:eb280529b0ef | 209 | } |
waltercolitti | 0:eb280529b0ef | 210 | |
MisterGiet | 3:53db6c57bb61 | 211 | float getHumidity(void) |
MisterGiet | 3:53db6c57bb61 | 212 | { |
MisterGiet | 3:53db6c57bb61 | 213 | |
MisterGiet | 3:53db6c57bb61 | 214 | float hum; |
MisterGiet | 3:53db6c57bb61 | 215 | if(tempsensor.getHumidity(&hum) != 0) { |
MisterGiet | 3:53db6c57bb61 | 216 | SERIAL_DEBUG("Error getting humidity"); |
MisterGiet | 3:53db6c57bb61 | 217 | hum = -1; |
MisterGiet | 3:53db6c57bb61 | 218 | } |
MisterGiet | 3:53db6c57bb61 | 219 | |
MisterGiet | 3:53db6c57bb61 | 220 | // Debugging: |
MisterGiet | 3:53db6c57bb61 | 221 | SERIAL_DEBUG("\nHumidity = %f", hum); |
MisterGiet | 3:53db6c57bb61 | 222 | char message[50]; |
MisterGiet | 3:53db6c57bb61 | 223 | sprintf (message, "RH=%d\n", (int)hum); |
MisterGiet | 3:53db6c57bb61 | 224 | DEBUG(message); |
MisterGiet | 3:53db6c57bb61 | 225 | |
MisterGiet | 3:53db6c57bb61 | 226 | return hum; |
MisterGiet | 3:53db6c57bb61 | 227 | } |
MisterGiet | 3:53db6c57bb61 | 228 | |
MisterGiet | 3:53db6c57bb61 | 229 | void onDataWrittenCallback(const GattWriteCallbackParams *params) |
waltercolitti | 0:eb280529b0ef | 230 | { |
MisterGiet | 6:80f2517965bb | 231 | int var = params->data[0]; |
MisterGiet | 6:80f2517965bb | 232 | |
MisterGiet | 6:80f2517965bb | 233 | if (var == 1) { |
MisterGiet | 3:53db6c57bb61 | 234 | blinkLed(ledOn,1); |
MisterGiet | 3:53db6c57bb61 | 235 | SERIAL_DEBUG("BLE. Resetting presence\n"); |
MisterGiet | 6:80f2517965bb | 236 | presenceCounter = 0; |
MisterGiet | 6:80f2517965bb | 237 | //presenceCounterBLE = 0; |
MisterGiet | 6:80f2517965bb | 238 | triggerSensorPolling = true; // force update of all BLE services |
MisterGiet | 3:53db6c57bb61 | 239 | } |
MisterGiet | 3:53db6c57bb61 | 240 | |
MisterGiet | 6:80f2517965bb | 241 | if (var == 2) { |
MisterGiet | 3:53db6c57bb61 | 242 | blinkLed(1,1); |
MisterGiet | 3:53db6c57bb61 | 243 | blinkLed(1,1); |
MisterGiet | 3:53db6c57bb61 | 244 | SERIAL_DEBUG("BLE. All LEDs ON/OFF\n"); |
MisterGiet | 3:53db6c57bb61 | 245 | ledOn = !ledOn; |
MisterGiet | 3:53db6c57bb61 | 246 | } |
MisterGiet | 3:53db6c57bb61 | 247 | |
MisterGiet | 3:53db6c57bb61 | 248 | if (params->data[0] == 3) { |
MisterGiet | 3:53db6c57bb61 | 249 | blinkLed(1,1); |
MisterGiet | 3:53db6c57bb61 | 250 | blinkLed(1,1); |
MisterGiet | 3:53db6c57bb61 | 251 | blinkLed(1,1); |
MisterGiet | 3:53db6c57bb61 | 252 | SERIAL_DEBUG("BLE. Rebooting sensor\n"); |
MisterGiet | 3:53db6c57bb61 | 253 | wait(3); |
MisterGiet | 3:53db6c57bb61 | 254 | NVIC_SystemReset(); // SW Reset |
MisterGiet | 3:53db6c57bb61 | 255 | } |
waltercolitti | 0:eb280529b0ef | 256 | } |
waltercolitti | 0:eb280529b0ef | 257 | |
waltercolitti | 0:eb280529b0ef | 258 | /** |
waltercolitti | 0:eb280529b0ef | 259 | * Callback triggered when the ble initialization process has finished |
waltercolitti | 0:eb280529b0ef | 260 | */ |
waltercolitti | 0:eb280529b0ef | 261 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
waltercolitti | 0:eb280529b0ef | 262 | { |
MisterGiet | 3:53db6c57bb61 | 263 | SERIAL_DEBUG("BLE. Init \n"); |
MisterGiet | 3:53db6c57bb61 | 264 | |
MisterGiet | 3:53db6c57bb61 | 265 | BLE& ble = params->ble; |
waltercolitti | 0:eb280529b0ef | 266 | ble_error_t error = params->error; |
waltercolitti | 0:eb280529b0ef | 267 | |
waltercolitti | 0:eb280529b0ef | 268 | if (error != BLE_ERROR_NONE) { |
waltercolitti | 0:eb280529b0ef | 269 | /* In case of error, forward the error handling to onBleInitError */ |
waltercolitti | 0:eb280529b0ef | 270 | onBleInitError(ble, error); |
waltercolitti | 0:eb280529b0ef | 271 | return; |
waltercolitti | 0:eb280529b0ef | 272 | } |
waltercolitti | 0:eb280529b0ef | 273 | |
waltercolitti | 0:eb280529b0ef | 274 | /* Ensure that it is the default instance of BLE */ |
waltercolitti | 0:eb280529b0ef | 275 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
waltercolitti | 0:eb280529b0ef | 276 | return; |
waltercolitti | 0:eb280529b0ef | 277 | } |
MisterGiet | 3:53db6c57bb61 | 278 | |
waltercolitti | 0:eb280529b0ef | 279 | //ble.gap().onConnection(connectionCallback); |
MisterGiet | 3:53db6c57bb61 | 280 | ble.gap().onDisconnection(disconnectionCallback); |
waltercolitti | 0:eb280529b0ef | 281 | |
MisterGiet | 3:53db6c57bb61 | 282 | // Set Transmission power: |
MisterGiet | 3:53db6c57bb61 | 283 | ble.setTxPower(TxPower); |
MisterGiet | 3:53db6c57bb61 | 284 | |
MisterGiet | 3:53db6c57bb61 | 285 | //ble.gap().setScanParams(500, 400); |
waltercolitti | 0:eb280529b0ef | 286 | //ble.gap().startScan(advertisementCallback); |
MisterGiet | 3:53db6c57bb61 | 287 | |
waltercolitti | 0:eb280529b0ef | 288 | //BLE server setup |
waltercolitti | 0:eb280529b0ef | 289 | ble.gattServer().onDataWritten(onDataWrittenCallback); |
MisterGiet | 3:53db6c57bb61 | 290 | |
waltercolitti | 0:eb280529b0ef | 291 | //Setup primary services |
MisterGiet | 6:80f2517965bb | 292 | presenceService = new PresenceDetectionService(ble, updatePresenceCounter(presenceCounter)); |
MisterGiet | 3:53db6c57bb61 | 293 | thermometerServicePtr = new HealthThermometerService(ble, getTemperature(), HealthThermometerService::LOCATION_EAR); |
MisterGiet | 3:53db6c57bb61 | 294 | humidityService = new HumidityMeasureService(ble, getHumidity()); |
MisterGiet | 3:53db6c57bb61 | 295 | ble.addService(batteryService); |
MisterGiet | 3:53db6c57bb61 | 296 | ble.addService(firmwareService); |
MisterGiet | 3:53db6c57bb61 | 297 | uart = new UARTService(ble); |
MisterGiet | 3:53db6c57bb61 | 298 | ble.addService(TxPowerService); |
MisterGiet | 3:53db6c57bb61 | 299 | |
MisterGiet | 3:53db6c57bb61 | 300 | |
MisterGiet | 3:53db6c57bb61 | 301 | /* setup advertising */ |
MisterGiet | 3:53db6c57bb61 | 302 | |
MisterGiet | 3:53db6c57bb61 | 303 | /* Sacrifice 3B of 31B to Advertising Flags */ |
waltercolitti | 0:eb280529b0ef | 304 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
MisterGiet | 3:53db6c57bb61 | 305 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
MisterGiet | 3:53db6c57bb61 | 306 | |
MisterGiet | 3:53db6c57bb61 | 307 | /* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */ |
MisterGiet | 3:53db6c57bb61 | 308 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, ManufData, sizeof(ManufData)); |
MisterGiet | 3:53db6c57bb61 | 309 | ble.gap().accumulateAdvertisingPayloadTxPower(TxPower); |
waltercolitti | 0:eb280529b0ef | 310 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
waltercolitti | 0:eb280529b0ef | 311 | //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::THERMOMETER_EAR); |
MisterGiet | 3:53db6c57bb61 | 312 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER); |
MisterGiet | 3:53db6c57bb61 | 313 | //ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,(const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); |
MisterGiet | 3:53db6c57bb61 | 314 | //ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
waltercolitti | 0:eb280529b0ef | 315 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
waltercolitti | 0:eb280529b0ef | 316 | |
MisterGiet | 3:53db6c57bb61 | 317 | ble.setAdvertisingInterval(1000); /* 1000ms */ |
MisterGiet | 3:53db6c57bb61 | 318 | //ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
MisterGiet | 3:53db6c57bb61 | 319 | ble.startAdvertising(); |
waltercolitti | 0:eb280529b0ef | 320 | } |
waltercolitti | 0:eb280529b0ef | 321 | |
waltercolitti | 0:eb280529b0ef | 322 | void periodicCallbackPresence(void) |
waltercolitti | 0:eb280529b0ef | 323 | { |
MisterGiet | 3:53db6c57bb61 | 324 | triggerPresencePolling = true; |
MisterGiet | 6:80f2517965bb | 325 | presenceState = true; |
waltercolitti | 0:eb280529b0ef | 326 | } |
waltercolitti | 0:eb280529b0ef | 327 | |
MisterGiet | 3:53db6c57bb61 | 328 | void periodicCallbackSensor (void) |
waltercolitti | 0:eb280529b0ef | 329 | { |
MisterGiet | 3:53db6c57bb61 | 330 | triggerSensorPolling = true; |
waltercolitti | 0:eb280529b0ef | 331 | } |
waltercolitti | 0:eb280529b0ef | 332 | |
MisterGiet | 6:80f2517965bb | 333 | void updateBLEservices () |
MisterGiet | 6:80f2517965bb | 334 | { |
MisterGiet | 6:80f2517965bb | 335 | thermometerServicePtr->updateTemperature(getTemperature()); |
MisterGiet | 6:80f2517965bb | 336 | humidityService->updateHumidity(getHumidity()); |
MisterGiet | 6:80f2517965bb | 337 | presenceService->updatePresence(updatePresenceCounter (presenceCounter)); |
MisterGiet | 6:80f2517965bb | 338 | batteryLevel = getBatteryPercent(); |
MisterGiet | 6:80f2517965bb | 339 | ble.updateCharacteristicValue(batteryPercentage.getValueAttribute().getHandle(), &batteryLevel, sizeof(batteryLevel)); |
MisterGiet | 6:80f2517965bb | 340 | ble.updateCharacteristicValue(TxPowerChar.getValueAttribute().getHandle(), (uint8_t*)&TxPower, 1); |
MisterGiet | 6:80f2517965bb | 341 | } |
MisterGiet | 6:80f2517965bb | 342 | |
waltercolitti | 0:eb280529b0ef | 343 | int main(void) |
MisterGiet | 3:53db6c57bb61 | 344 | { |
MisterGiet | 3:53db6c57bb61 | 345 | SERIAL_DEBUG("Start \n"); |
MisterGiet | 3:53db6c57bb61 | 346 | char pres_message[20]; |
MisterGiet | 3:53db6c57bb61 | 347 | |
MisterGiet | 3:53db6c57bb61 | 348 | blinkLed(1,1); |
MisterGiet | 3:53db6c57bb61 | 349 | |
MisterGiet | 3:53db6c57bb61 | 350 | //Ticker ticker_presence; |
MisterGiet | 3:53db6c57bb61 | 351 | //ticker_presence.attach(periodicCallbackPresence, 0.5); |
MisterGiet | 3:53db6c57bb61 | 352 | |
MisterGiet | 3:53db6c57bb61 | 353 | motion_pin.fall(&periodicCallbackPresence); |
MisterGiet | 3:53db6c57bb61 | 354 | |
waltercolitti | 0:eb280529b0ef | 355 | Ticker ticker_sensor; |
MisterGiet | 6:80f2517965bb | 356 | ticker_sensor.attach(periodicCallbackSensor, 300); // 5 min |
MisterGiet | 3:53db6c57bb61 | 357 | |
waltercolitti | 0:eb280529b0ef | 358 | //BLE instance setup |
waltercolitti | 0:eb280529b0ef | 359 | BLE &bleptr = BLE::Instance(); |
waltercolitti | 0:eb280529b0ef | 360 | bleptr.init(bleInitComplete); |
MisterGiet | 3:53db6c57bb61 | 361 | |
MisterGiet | 3:53db6c57bb61 | 362 | /* |
MisterGiet | 3:53db6c57bb61 | 363 | //SpinWait for initialization to complete. This is necessary because the |
MisterGiet | 3:53db6c57bb61 | 364 | //BLE object is used in the main loop below. |
MisterGiet | 3:53db6c57bb61 | 365 | while (ble.hasInitialized() == false) { } |
MisterGiet | 3:53db6c57bb61 | 366 | */ |
MisterGiet | 3:53db6c57bb61 | 367 | |
waltercolitti | 0:eb280529b0ef | 368 | while (true) { |
MisterGiet | 3:53db6c57bb61 | 369 | |
MisterGiet | 3:53db6c57bb61 | 370 | /* Get Presence data: */ |
MisterGiet | 3:53db6c57bb61 | 371 | if(triggerPresencePolling) { |
MisterGiet | 3:53db6c57bb61 | 372 | triggerPresencePolling = false; |
MisterGiet | 3:53db6c57bb61 | 373 | |
MisterGiet | 6:80f2517965bb | 374 | // if presence is detected: |
MisterGiet | 6:80f2517965bb | 375 | if (presenceState) { |
MisterGiet | 6:80f2517965bb | 376 | |
MisterGiet | 6:80f2517965bb | 377 | presenceCounter++; |
MisterGiet | 6:80f2517965bb | 378 | |
MisterGiet | 6:80f2517965bb | 379 | // Update all BLE services for every presence detected: |
MisterGiet | 6:80f2517965bb | 380 | updateBLEservices(); |
MisterGiet | 3:53db6c57bb61 | 381 | |
MisterGiet | 3:53db6c57bb61 | 382 | // Debugging: |
MisterGiet | 6:80f2517965bb | 383 | blinkLed(ledOn,1); |
MisterGiet | 6:80f2517965bb | 384 | SERIAL_DEBUG("Presence counter: %i\n", presenceCounter); |
MisterGiet | 6:80f2517965bb | 385 | sprintf (pres_message, "Pres=%i\n", presenceCounter); |
MisterGiet | 3:53db6c57bb61 | 386 | DEBUG(pres_message); |
MisterGiet | 3:53db6c57bb61 | 387 | |
MisterGiet | 6:80f2517965bb | 388 | presenceState=0; |
MisterGiet | 3:53db6c57bb61 | 389 | } |
MisterGiet | 3:53db6c57bb61 | 390 | |
MisterGiet | 6:80f2517965bb | 391 | // if no presence: |
MisterGiet | 3:53db6c57bb61 | 392 | else { |
MisterGiet | 3:53db6c57bb61 | 393 | SERIAL_DEBUG("."); |
MisterGiet | 3:53db6c57bb61 | 394 | } |
MisterGiet | 3:53db6c57bb61 | 395 | } |
MisterGiet | 3:53db6c57bb61 | 396 | |
MisterGiet | 3:53db6c57bb61 | 397 | /* Update BLE services: */ |
waltercolitti | 0:eb280529b0ef | 398 | if (triggerSensorPolling && ble.getGapState().connected) { |
MisterGiet | 6:80f2517965bb | 399 | |
MisterGiet | 6:80f2517965bb | 400 | // Quanto consuma quando sta leggendo la temperatura? |
waltercolitti | 0:eb280529b0ef | 401 | triggerSensorPolling = false; |
MisterGiet | 6:80f2517965bb | 402 | |
MisterGiet | 6:80f2517965bb | 403 | //updateBLEservices(); |
MisterGiet | 3:53db6c57bb61 | 404 | thermometerServicePtr->updateTemperature(getTemperature()); |
MisterGiet | 3:53db6c57bb61 | 405 | humidityService->updateHumidity(getHumidity()); |
MisterGiet | 6:80f2517965bb | 406 | presenceService->updatePresence(updatePresenceCounter(presenceCounter)); |
MisterGiet | 3:53db6c57bb61 | 407 | batteryLevel = getBatteryPercent(); |
MisterGiet | 3:53db6c57bb61 | 408 | ble.updateCharacteristicValue(batteryPercentage.getValueAttribute().getHandle(), &batteryLevel, sizeof(batteryLevel)); |
MisterGiet | 3:53db6c57bb61 | 409 | ble.updateCharacteristicValue(TxPowerChar.getValueAttribute().getHandle(), (uint8_t*)&TxPower, 1); |
waltercolitti | 0:eb280529b0ef | 410 | |
MisterGiet | 4:de22f0d73c12 | 411 | // Red LED blinks if low battery, at every connection |
MisterGiet | 4:de22f0d73c12 | 412 | if (batteryLevel <= 10) { |
MisterGiet | 5:173ffbf84b3a | 413 | blinkLed(battNotify,2); |
MisterGiet | 4:de22f0d73c12 | 414 | } |
MisterGiet | 4:de22f0d73c12 | 415 | |
MisterGiet | 3:53db6c57bb61 | 416 | } else { |
MisterGiet | 3:53db6c57bb61 | 417 | ble.waitForEvent(); |
waltercolitti | 0:eb280529b0ef | 418 | } |
waltercolitti | 0:eb280529b0ef | 419 | } |
MisterGiet | 6:80f2517965bb | 420 | } |