Dependencies: BLE_API mbed nRF51822
Fork of SensorModulePIR by
main.cpp@3:53db6c57bb61, 2017-05-04 (annotated)
- Committer:
- MisterGiet
- Date:
- Thu May 04 09:58:43 2017 +0000
- Revision:
- 3:53db6c57bb61
- Parent:
- 1:839f9cac1eca
- Child:
- 4:de22f0d73c12
First Version with Panasonic PIR, low power.
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 | 3:53db6c57bb61 | 82 | float Vbat_min = 2.3; //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 | 3:53db6c57bb61 | 94 | return battLevel; |
MisterGiet | 3:53db6c57bb61 | 95 | } |
MisterGiet | 3:53db6c57bb61 | 96 | |
MisterGiet | 3:53db6c57bb61 | 97 | /* BLE CONFIGURATION */ |
waltercolitti | 0:eb280529b0ef | 98 | |
waltercolitti | 0:eb280529b0ef | 99 | BLE ble; |
MisterGiet | 3:53db6c57bb61 | 100 | static const char DEVICE_NAME[] = "SM-PIR"; |
MisterGiet | 3:53db6c57bb61 | 101 | static const uint16_t uuid16_list[] = {GattService::UUID_HEALTH_THERMOMETER_SERVICE}; |
MisterGiet | 3:53db6c57bb61 | 102 | static volatile bool triggerSensorPolling = false; |
MisterGiet | 3:53db6c57bb61 | 103 | static char fwversion[31] = "CAT-1817"; |
MisterGiet | 3:53db6c57bb61 | 104 | static uint8_t batteryLevel = 100; |
MisterGiet | 3:53db6c57bb61 | 105 | static int8_t TxPower = +4; |
waltercolitti | 0:eb280529b0ef | 106 | |
MisterGiet | 3:53db6c57bb61 | 107 | static HealthThermometerService *thermometerServicePtr; |
MisterGiet | 3:53db6c57bb61 | 108 | PresenceDetectionService *presenceService = NULL; |
MisterGiet | 3:53db6c57bb61 | 109 | HumidityMeasureService *humidityService = NULL; |
MisterGiet | 3:53db6c57bb61 | 110 | UARTService *uart; |
MisterGiet | 3:53db6c57bb61 | 111 | const static uint8_t ManufData[] = {0x01,0x02,0x03,0x04,0x05}; // Set up to 26B of advertising data to use fro the Manufacturer data. |
MisterGiet | 3:53db6c57bb61 | 112 | |
MisterGiet | 3:53db6c57bb61 | 113 | // Firmware |
MisterGiet | 3:53db6c57bb61 | 114 | GattCharacteristic fwChars(GattCharacteristic::UUID_FIRMWARE_REVISION_STRING_CHAR, (uint8_t *)fwversion, sizeof(fwversion), sizeof(fwversion),GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
MisterGiet | 3:53db6c57bb61 | 115 | GattCharacteristic *firmwareChars[] = {&fwChars }; |
MisterGiet | 3:53db6c57bb61 | 116 | GattService firmwareService(GattService::UUID_DEVICE_INFORMATION_SERVICE, firmwareChars, sizeof(firmwareChars) / sizeof(GattCharacteristic *)); |
MisterGiet | 3:53db6c57bb61 | 117 | |
MisterGiet | 3:53db6c57bb61 | 118 | //Battery |
MisterGiet | 3:53db6c57bb61 | 119 | GattCharacteristic batteryPercentage(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, (uint8_t *)batteryLevel, sizeof(batteryLevel), sizeof(batteryLevel), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
MisterGiet | 3:53db6c57bb61 | 120 | GattCharacteristic *batteryChars[] = {&batteryPercentage}; |
MisterGiet | 3:53db6c57bb61 | 121 | GattService batteryService(GattService::UUID_BATTERY_SERVICE, batteryChars, sizeof(batteryChars) / sizeof(GattCharacteristic *)); |
MisterGiet | 3:53db6c57bb61 | 122 | |
MisterGiet | 3:53db6c57bb61 | 123 | //Power |
MisterGiet | 3:53db6c57bb61 | 124 | GattCharacteristic TxPowerChar(GattCharacteristic::UUID_TX_POWER_LEVEL_CHAR, (uint8_t*)&TxPower, sizeof(TxPower), sizeof(TxPower), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
MisterGiet | 3:53db6c57bb61 | 125 | GattCharacteristic *charTable[] = {&TxPowerChar}; |
MisterGiet | 3:53db6c57bb61 | 126 | GattService TxPowerService(GattService::UUID_TX_POWER_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
waltercolitti | 0:eb280529b0ef | 127 | |
MisterGiet | 3:53db6c57bb61 | 128 | |
MisterGiet | 3:53db6c57bb61 | 129 | bool presence = false; |
MisterGiet | 3:53db6c57bb61 | 130 | bool presenceL = false; |
MisterGiet | 3:53db6c57bb61 | 131 | uint8_t presence_count = 0; |
MisterGiet | 3:53db6c57bb61 | 132 | bool ledOn = 0; |
MisterGiet | 3:53db6c57bb61 | 133 | static volatile bool triggerPresencePolling = false; |
waltercolitti | 0:eb280529b0ef | 134 | |
MisterGiet | 3:53db6c57bb61 | 135 | void blinkLed (bool enable, int color) |
MisterGiet | 3:53db6c57bb61 | 136 | { |
MisterGiet | 3:53db6c57bb61 | 137 | if (enable) { |
MisterGiet | 3:53db6c57bb61 | 138 | if (color == 1) { |
MisterGiet | 3:53db6c57bb61 | 139 | ledB=0; |
MisterGiet | 3:53db6c57bb61 | 140 | wait(0.2); |
MisterGiet | 3:53db6c57bb61 | 141 | ledB=1; |
MisterGiet | 3:53db6c57bb61 | 142 | wait(0.2); |
MisterGiet | 3:53db6c57bb61 | 143 | } |
MisterGiet | 3:53db6c57bb61 | 144 | if (color == 2) { |
MisterGiet | 3:53db6c57bb61 | 145 | ledR=0; |
MisterGiet | 3:53db6c57bb61 | 146 | wait(0.2); |
MisterGiet | 3:53db6c57bb61 | 147 | ledR=1; |
MisterGiet | 3:53db6c57bb61 | 148 | wait(0.2); |
MisterGiet | 3:53db6c57bb61 | 149 | } |
MisterGiet | 3:53db6c57bb61 | 150 | } else { |
MisterGiet | 3:53db6c57bb61 | 151 | // do nothing if enable=0 |
MisterGiet | 3:53db6c57bb61 | 152 | } |
MisterGiet | 3:53db6c57bb61 | 153 | } |
waltercolitti | 0:eb280529b0ef | 154 | |
MisterGiet | 3:53db6c57bb61 | 155 | /* Restart Advertising on disconnection*/ |
waltercolitti | 0:eb280529b0ef | 156 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
waltercolitti | 0:eb280529b0ef | 157 | { |
MisterGiet | 3:53db6c57bb61 | 158 | SERIAL_DEBUG("Disconnected\r\n"); |
MisterGiet | 3:53db6c57bb61 | 159 | BLE::Instance().gap().startAdvertising(); |
waltercolitti | 0:eb280529b0ef | 160 | } |
waltercolitti | 0:eb280529b0ef | 161 | |
waltercolitti | 0:eb280529b0ef | 162 | /** |
waltercolitti | 0:eb280529b0ef | 163 | * This function is called when the ble initialization process has failed |
waltercolitti | 0:eb280529b0ef | 164 | */ |
waltercolitti | 0:eb280529b0ef | 165 | void onBleInitError(BLE &ble, ble_error_t error) |
waltercolitti | 0:eb280529b0ef | 166 | { |
MisterGiet | 3:53db6c57bb61 | 167 | /* Avoid compiler warnings */ |
MisterGiet | 3:53db6c57bb61 | 168 | (void) ble; |
MisterGiet | 3:53db6c57bb61 | 169 | (void) error; |
waltercolitti | 0:eb280529b0ef | 170 | /* Initialization error handling should go here */ |
waltercolitti | 0:eb280529b0ef | 171 | } |
waltercolitti | 0:eb280529b0ef | 172 | |
MisterGiet | 3:53db6c57bb61 | 173 | |
waltercolitti | 0:eb280529b0ef | 174 | float getTemperature(void) |
waltercolitti | 0:eb280529b0ef | 175 | { |
MisterGiet | 3:53db6c57bb61 | 176 | float temp; |
waltercolitti | 0:eb280529b0ef | 177 | |
MisterGiet | 3:53db6c57bb61 | 178 | if(tempsensor.getTemperature(&temp) != 0) { |
MisterGiet | 3:53db6c57bb61 | 179 | DEBUG("Error getting temperature"); |
MisterGiet | 3:53db6c57bb61 | 180 | temp = -1; |
MisterGiet | 3:53db6c57bb61 | 181 | } |
waltercolitti | 0:eb280529b0ef | 182 | |
MisterGiet | 3:53db6c57bb61 | 183 | // Debugging: |
MisterGiet | 3:53db6c57bb61 | 184 | SERIAL_DEBUG("\nTemperature = %.2f", temp); |
MisterGiet | 3:53db6c57bb61 | 185 | char message[50]; |
MisterGiet | 3:53db6c57bb61 | 186 | sprintf (message, "T=%.2fC\n", temp); |
MisterGiet | 3:53db6c57bb61 | 187 | DEBUG(message); |
waltercolitti | 0:eb280529b0ef | 188 | |
MisterGiet | 3:53db6c57bb61 | 189 | // Adding offset: |
MisterGiet | 3:53db6c57bb61 | 190 | temp = temp + OFFSET; |
MisterGiet | 3:53db6c57bb61 | 191 | |
MisterGiet | 3:53db6c57bb61 | 192 | return temp; |
waltercolitti | 0:eb280529b0ef | 193 | } |
waltercolitti | 0:eb280529b0ef | 194 | |
MisterGiet | 3:53db6c57bb61 | 195 | float getHumidity(void) |
MisterGiet | 3:53db6c57bb61 | 196 | { |
MisterGiet | 3:53db6c57bb61 | 197 | |
MisterGiet | 3:53db6c57bb61 | 198 | float hum; |
MisterGiet | 3:53db6c57bb61 | 199 | if(tempsensor.getHumidity(&hum) != 0) { |
MisterGiet | 3:53db6c57bb61 | 200 | SERIAL_DEBUG("Error getting humidity"); |
MisterGiet | 3:53db6c57bb61 | 201 | hum = -1; |
MisterGiet | 3:53db6c57bb61 | 202 | } |
MisterGiet | 3:53db6c57bb61 | 203 | |
MisterGiet | 3:53db6c57bb61 | 204 | // Debugging: |
MisterGiet | 3:53db6c57bb61 | 205 | SERIAL_DEBUG("\nHumidity = %f", hum); |
MisterGiet | 3:53db6c57bb61 | 206 | char message[50]; |
MisterGiet | 3:53db6c57bb61 | 207 | sprintf (message, "RH=%d\n", (int)hum); |
MisterGiet | 3:53db6c57bb61 | 208 | DEBUG(message); |
MisterGiet | 3:53db6c57bb61 | 209 | |
MisterGiet | 3:53db6c57bb61 | 210 | return hum; |
MisterGiet | 3:53db6c57bb61 | 211 | } |
MisterGiet | 3:53db6c57bb61 | 212 | |
MisterGiet | 3:53db6c57bb61 | 213 | void onDataWrittenCallback(const GattWriteCallbackParams *params) |
waltercolitti | 0:eb280529b0ef | 214 | { |
MisterGiet | 3:53db6c57bb61 | 215 | if (params->data[0] == 1) { |
MisterGiet | 3:53db6c57bb61 | 216 | blinkLed(ledOn,1); |
MisterGiet | 3:53db6c57bb61 | 217 | SERIAL_DEBUG("BLE. Resetting presence\n"); |
MisterGiet | 3:53db6c57bb61 | 218 | presence_count = 0; |
MisterGiet | 3:53db6c57bb61 | 219 | } |
MisterGiet | 3:53db6c57bb61 | 220 | |
MisterGiet | 3:53db6c57bb61 | 221 | if (params->data[0] == 2) { |
MisterGiet | 3:53db6c57bb61 | 222 | blinkLed(1,1); |
MisterGiet | 3:53db6c57bb61 | 223 | blinkLed(1,1); |
MisterGiet | 3:53db6c57bb61 | 224 | SERIAL_DEBUG("BLE. All LEDs ON/OFF\n"); |
MisterGiet | 3:53db6c57bb61 | 225 | ledOn = !ledOn; |
MisterGiet | 3:53db6c57bb61 | 226 | } |
MisterGiet | 3:53db6c57bb61 | 227 | |
MisterGiet | 3:53db6c57bb61 | 228 | if (params->data[0] == 3) { |
MisterGiet | 3:53db6c57bb61 | 229 | blinkLed(1,1); |
MisterGiet | 3:53db6c57bb61 | 230 | blinkLed(1,1); |
MisterGiet | 3:53db6c57bb61 | 231 | blinkLed(1,1); |
MisterGiet | 3:53db6c57bb61 | 232 | SERIAL_DEBUG("BLE. Rebooting sensor\n"); |
MisterGiet | 3:53db6c57bb61 | 233 | wait(3); |
MisterGiet | 3:53db6c57bb61 | 234 | NVIC_SystemReset(); // SW Reset |
MisterGiet | 3:53db6c57bb61 | 235 | } |
waltercolitti | 0:eb280529b0ef | 236 | } |
waltercolitti | 0:eb280529b0ef | 237 | |
waltercolitti | 0:eb280529b0ef | 238 | /** |
waltercolitti | 0:eb280529b0ef | 239 | * Callback triggered when the ble initialization process has finished |
waltercolitti | 0:eb280529b0ef | 240 | */ |
waltercolitti | 0:eb280529b0ef | 241 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
waltercolitti | 0:eb280529b0ef | 242 | { |
MisterGiet | 3:53db6c57bb61 | 243 | SERIAL_DEBUG("BLE. Init \n"); |
MisterGiet | 3:53db6c57bb61 | 244 | |
MisterGiet | 3:53db6c57bb61 | 245 | BLE& ble = params->ble; |
waltercolitti | 0:eb280529b0ef | 246 | ble_error_t error = params->error; |
waltercolitti | 0:eb280529b0ef | 247 | |
waltercolitti | 0:eb280529b0ef | 248 | if (error != BLE_ERROR_NONE) { |
waltercolitti | 0:eb280529b0ef | 249 | /* In case of error, forward the error handling to onBleInitError */ |
waltercolitti | 0:eb280529b0ef | 250 | onBleInitError(ble, error); |
waltercolitti | 0:eb280529b0ef | 251 | return; |
waltercolitti | 0:eb280529b0ef | 252 | } |
waltercolitti | 0:eb280529b0ef | 253 | |
waltercolitti | 0:eb280529b0ef | 254 | /* Ensure that it is the default instance of BLE */ |
waltercolitti | 0:eb280529b0ef | 255 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
waltercolitti | 0:eb280529b0ef | 256 | return; |
waltercolitti | 0:eb280529b0ef | 257 | } |
MisterGiet | 3:53db6c57bb61 | 258 | |
waltercolitti | 0:eb280529b0ef | 259 | //ble.gap().onConnection(connectionCallback); |
MisterGiet | 3:53db6c57bb61 | 260 | ble.gap().onDisconnection(disconnectionCallback); |
waltercolitti | 0:eb280529b0ef | 261 | |
MisterGiet | 3:53db6c57bb61 | 262 | // Set Transmission power: |
MisterGiet | 3:53db6c57bb61 | 263 | ble.setTxPower(TxPower); |
MisterGiet | 3:53db6c57bb61 | 264 | |
MisterGiet | 3:53db6c57bb61 | 265 | //ble.gap().setScanParams(500, 400); |
waltercolitti | 0:eb280529b0ef | 266 | //ble.gap().startScan(advertisementCallback); |
MisterGiet | 3:53db6c57bb61 | 267 | |
waltercolitti | 0:eb280529b0ef | 268 | //BLE server setup |
waltercolitti | 0:eb280529b0ef | 269 | ble.gattServer().onDataWritten(onDataWrittenCallback); |
MisterGiet | 3:53db6c57bb61 | 270 | |
waltercolitti | 0:eb280529b0ef | 271 | //Setup primary services |
MisterGiet | 3:53db6c57bb61 | 272 | presenceService = new PresenceDetectionService(ble, presence_count); |
MisterGiet | 3:53db6c57bb61 | 273 | thermometerServicePtr = new HealthThermometerService(ble, getTemperature(), HealthThermometerService::LOCATION_EAR); |
MisterGiet | 3:53db6c57bb61 | 274 | humidityService = new HumidityMeasureService(ble, getHumidity()); |
MisterGiet | 3:53db6c57bb61 | 275 | ble.addService(batteryService); |
MisterGiet | 3:53db6c57bb61 | 276 | ble.addService(firmwareService); |
MisterGiet | 3:53db6c57bb61 | 277 | uart = new UARTService(ble); |
MisterGiet | 3:53db6c57bb61 | 278 | ble.addService(TxPowerService); |
MisterGiet | 3:53db6c57bb61 | 279 | |
MisterGiet | 3:53db6c57bb61 | 280 | |
MisterGiet | 3:53db6c57bb61 | 281 | /* setup advertising */ |
MisterGiet | 3:53db6c57bb61 | 282 | |
MisterGiet | 3:53db6c57bb61 | 283 | /* Sacrifice 3B of 31B to Advertising Flags */ |
waltercolitti | 0:eb280529b0ef | 284 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
MisterGiet | 3:53db6c57bb61 | 285 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
MisterGiet | 3:53db6c57bb61 | 286 | |
MisterGiet | 3:53db6c57bb61 | 287 | /* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */ |
MisterGiet | 3:53db6c57bb61 | 288 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, ManufData, sizeof(ManufData)); |
MisterGiet | 3:53db6c57bb61 | 289 | ble.gap().accumulateAdvertisingPayloadTxPower(TxPower); |
waltercolitti | 0:eb280529b0ef | 290 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
waltercolitti | 0:eb280529b0ef | 291 | //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::THERMOMETER_EAR); |
MisterGiet | 3:53db6c57bb61 | 292 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER); |
MisterGiet | 3:53db6c57bb61 | 293 | //ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,(const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); |
MisterGiet | 3:53db6c57bb61 | 294 | //ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
waltercolitti | 0:eb280529b0ef | 295 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
waltercolitti | 0:eb280529b0ef | 296 | |
MisterGiet | 3:53db6c57bb61 | 297 | ble.setAdvertisingInterval(1000); /* 1000ms */ |
MisterGiet | 3:53db6c57bb61 | 298 | //ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
MisterGiet | 3:53db6c57bb61 | 299 | ble.startAdvertising(); |
waltercolitti | 0:eb280529b0ef | 300 | } |
waltercolitti | 0:eb280529b0ef | 301 | |
waltercolitti | 0:eb280529b0ef | 302 | void periodicCallbackPresence(void) |
waltercolitti | 0:eb280529b0ef | 303 | { |
MisterGiet | 3:53db6c57bb61 | 304 | triggerPresencePolling = true; |
MisterGiet | 3:53db6c57bb61 | 305 | presence = 1; |
waltercolitti | 0:eb280529b0ef | 306 | } |
waltercolitti | 0:eb280529b0ef | 307 | |
MisterGiet | 3:53db6c57bb61 | 308 | void periodicCallbackSensor (void) |
waltercolitti | 0:eb280529b0ef | 309 | { |
MisterGiet | 3:53db6c57bb61 | 310 | triggerSensorPolling = true; |
waltercolitti | 0:eb280529b0ef | 311 | } |
waltercolitti | 0:eb280529b0ef | 312 | |
waltercolitti | 0:eb280529b0ef | 313 | int main(void) |
MisterGiet | 3:53db6c57bb61 | 314 | { |
MisterGiet | 3:53db6c57bb61 | 315 | SERIAL_DEBUG("Start \n"); |
MisterGiet | 3:53db6c57bb61 | 316 | char pres_message[20]; |
MisterGiet | 3:53db6c57bb61 | 317 | |
MisterGiet | 3:53db6c57bb61 | 318 | blinkLed(1,1); |
MisterGiet | 3:53db6c57bb61 | 319 | |
MisterGiet | 3:53db6c57bb61 | 320 | //Ticker ticker_presence; |
MisterGiet | 3:53db6c57bb61 | 321 | //ticker_presence.attach(periodicCallbackPresence, 0.5); |
MisterGiet | 3:53db6c57bb61 | 322 | |
MisterGiet | 3:53db6c57bb61 | 323 | motion_pin.fall(&periodicCallbackPresence); |
MisterGiet | 3:53db6c57bb61 | 324 | //motion_pin.rise(&periodicCallbackPresence); |
MisterGiet | 3:53db6c57bb61 | 325 | |
waltercolitti | 0:eb280529b0ef | 326 | Ticker ticker_sensor; |
MisterGiet | 3:53db6c57bb61 | 327 | ticker_sensor.attach(periodicCallbackSensor, 60); |
MisterGiet | 3:53db6c57bb61 | 328 | |
waltercolitti | 0:eb280529b0ef | 329 | //BLE instance setup |
waltercolitti | 0:eb280529b0ef | 330 | BLE &bleptr = BLE::Instance(); |
waltercolitti | 0:eb280529b0ef | 331 | bleptr.init(bleInitComplete); |
MisterGiet | 3:53db6c57bb61 | 332 | |
MisterGiet | 3:53db6c57bb61 | 333 | /* |
MisterGiet | 3:53db6c57bb61 | 334 | //SpinWait for initialization to complete. This is necessary because the |
MisterGiet | 3:53db6c57bb61 | 335 | //BLE object is used in the main loop below. |
MisterGiet | 3:53db6c57bb61 | 336 | while (ble.hasInitialized() == false) { } |
MisterGiet | 3:53db6c57bb61 | 337 | */ |
MisterGiet | 3:53db6c57bb61 | 338 | |
waltercolitti | 0:eb280529b0ef | 339 | while (true) { |
MisterGiet | 3:53db6c57bb61 | 340 | |
MisterGiet | 3:53db6c57bb61 | 341 | /* Get Presence data: */ |
MisterGiet | 3:53db6c57bb61 | 342 | if(triggerPresencePolling) { |
MisterGiet | 3:53db6c57bb61 | 343 | triggerPresencePolling = false; |
MisterGiet | 3:53db6c57bb61 | 344 | //presence = motion_pin.read(); |
MisterGiet | 3:53db6c57bb61 | 345 | |
MisterGiet | 3:53db6c57bb61 | 346 | if (presence) { |
MisterGiet | 3:53db6c57bb61 | 347 | presence_count++; |
MisterGiet | 3:53db6c57bb61 | 348 | |
MisterGiet | 3:53db6c57bb61 | 349 | // Debugging: |
MisterGiet | 3:53db6c57bb61 | 350 | if (presence) { |
MisterGiet | 3:53db6c57bb61 | 351 | blinkLed(ledOn,1); |
MisterGiet | 3:53db6c57bb61 | 352 | } |
MisterGiet | 3:53db6c57bb61 | 353 | |
MisterGiet | 3:53db6c57bb61 | 354 | SERIAL_DEBUG("Presence counter: %i\n", presence_count); |
MisterGiet | 3:53db6c57bb61 | 355 | sprintf (pres_message, "Pres=%i\n", presence_count); |
MisterGiet | 3:53db6c57bb61 | 356 | DEBUG(pres_message); |
MisterGiet | 3:53db6c57bb61 | 357 | |
MisterGiet | 3:53db6c57bb61 | 358 | presence=0; |
MisterGiet | 3:53db6c57bb61 | 359 | } |
MisterGiet | 3:53db6c57bb61 | 360 | |
MisterGiet | 3:53db6c57bb61 | 361 | else { |
MisterGiet | 3:53db6c57bb61 | 362 | SERIAL_DEBUG("."); |
MisterGiet | 3:53db6c57bb61 | 363 | } |
MisterGiet | 3:53db6c57bb61 | 364 | } |
MisterGiet | 3:53db6c57bb61 | 365 | |
MisterGiet | 3:53db6c57bb61 | 366 | /* Update BLE services: */ |
waltercolitti | 0:eb280529b0ef | 367 | if (triggerSensorPolling && ble.getGapState().connected) { |
waltercolitti | 0:eb280529b0ef | 368 | triggerSensorPolling = false; |
MisterGiet | 3:53db6c57bb61 | 369 | thermometerServicePtr->updateTemperature(getTemperature()); |
MisterGiet | 3:53db6c57bb61 | 370 | humidityService->updateHumidity(getHumidity()); |
MisterGiet | 3:53db6c57bb61 | 371 | presenceService->updatePresence(presence_count); |
MisterGiet | 3:53db6c57bb61 | 372 | batteryLevel = getBatteryPercent(); |
MisterGiet | 3:53db6c57bb61 | 373 | ble.updateCharacteristicValue(batteryPercentage.getValueAttribute().getHandle(), &batteryLevel, sizeof(batteryLevel)); |
MisterGiet | 3:53db6c57bb61 | 374 | ble.updateCharacteristicValue(TxPowerChar.getValueAttribute().getHandle(), (uint8_t*)&TxPower, 1); |
waltercolitti | 0:eb280529b0ef | 375 | |
MisterGiet | 3:53db6c57bb61 | 376 | } else { |
MisterGiet | 3:53db6c57bb61 | 377 | ble.waitForEvent(); |
waltercolitti | 0:eb280529b0ef | 378 | } |
waltercolitti | 0:eb280529b0ef | 379 | } |
waltercolitti | 0:eb280529b0ef | 380 | } |