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