Adaptation of SMPIR for the Window Sensor Module of the Agile-IoT project.
Dependencies: BLE_API mbed nRF51822
Fork of SensorModulePIR by
main.cpp@21:819c4d225910, 2018-01-29 (annotated)
- Committer:
- MisterGiet
- Date:
- Mon Jan 29 19:31:48 2018 +0000
- Revision:
- 21:819c4d225910
- Parent:
- 20:11eb72daf716
FW:ETT-0318. Solved small bug on battery measurement.
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 | 12:92f008fe602c | 4 | #include "HealthThermometerService.h" |
MisterGiet | 12:92f008fe602c | 5 | #include "BatteryService.h" |
MisterGiet | 12:92f008fe602c | 6 | #include "EnvironmentalService.h" |
MisterGiet | 10:29dbc6d316c3 | 7 | #include "HumidityMeasureService.h" |
waltercolitti | 0:eb280529b0ef | 8 | #include "PresenceDetectionService.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> |
MisterGiet | 13:0dc9a52cff66 | 13 | //#include "DFUService.h" |
MisterGiet | 10:29dbc6d316c3 | 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 | 19:1ecc4b7f83ca | 18 | #define OFFSET ZERO |
MisterGiet | 3:53db6c57bb61 | 19 | |
MisterGiet | 15:cd533d4ef766 | 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 | 15:cd533d4ef766 | 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 | 6:80f2517965bb | 38 | InterruptIn motion_pin(PIN_PRESENCE_RIGHT); |
MisterGiet | 6:80f2517965bb | 39 | DigitalIn pinR(PIN_PRESENCE_RIGHT, PullNone); |
MisterGiet | 6:80f2517965bb | 40 | DigitalOut ledB(PIN_BLED_PCB, 1); |
MisterGiet | 6:80f2517965bb | 41 | DigitalOut ledR(PIN_RLED_PCB, 1); |
MisterGiet | 3:53db6c57bb61 | 42 | |
MisterGiet | 6:80f2517965bb | 43 | bool presenceState = false; |
MisterGiet | 6:80f2517965bb | 44 | int presenceCounter = 0; |
MisterGiet | 6:80f2517965bb | 45 | //uint8_t presenceCounterBLE = 0; |
MisterGiet | 9:97d8ea1feeed | 46 | bool installMode = 0; |
MisterGiet | 5:173ffbf84b3a | 47 | bool battNotify = 1; |
MisterGiet | 5:173ffbf84b3a | 48 | static volatile bool triggerPresencePolling = false; |
MisterGiet | 15:cd533d4ef766 | 49 | bool TESTCONF = 0; |
MisterGiet | 5:173ffbf84b3a | 50 | |
MisterGiet | 5:173ffbf84b3a | 51 | |
MisterGiet | 5:173ffbf84b3a | 52 | /* BLE CONFIGURATION */ |
MisterGiet | 5:173ffbf84b3a | 53 | |
MisterGiet | 5:173ffbf84b3a | 54 | BLE ble; |
MisterGiet | 21:819c4d225910 | 55 | static const char DEVICE_NAME[] = "SM10"; |
MisterGiet | 5:173ffbf84b3a | 56 | static const uint16_t uuid16_list[] = {GattService::UUID_HEALTH_THERMOMETER_SERVICE}; |
MisterGiet | 5:173ffbf84b3a | 57 | static volatile bool triggerSensorPolling = false; |
MisterGiet | 21:819c4d225910 | 58 | static char fwversion[31] = "ETT-0318"; |
MisterGiet | 11:1186eb69af44 | 59 | static uint8_t batteryLevel = 99; |
MisterGiet | 5:173ffbf84b3a | 60 | static int8_t TxPower = +4; |
MisterGiet | 10:29dbc6d316c3 | 61 | //static uint8_t humidity = 99; |
MisterGiet | 13:0dc9a52cff66 | 62 | static uint8_t ManufData[] = {0x01,0x02,0x03,0x04,0x05}; // Set up to 26B of advertising data to use for the Manufacturer data. |
MisterGiet | 12:92f008fe602c | 63 | //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 | 64 | |
MisterGiet | 10:29dbc6d316c3 | 65 | static HealthThermometerService *thermometerServicePtr = NULL;; |
MisterGiet | 10:29dbc6d316c3 | 66 | static EnvironmentalService* humidityTemperatureServicePtr = NULL; |
MisterGiet | 10:29dbc6d316c3 | 67 | static HumidityMeasureService *humidityService = NULL; |
MisterGiet | 10:29dbc6d316c3 | 68 | static PresenceDetectionService *presenceService = NULL; |
MisterGiet | 12:92f008fe602c | 69 | static BatteryService* batteryServicePtr = NULL;; |
MisterGiet | 5:173ffbf84b3a | 70 | UARTService *uart; |
MisterGiet | 5:173ffbf84b3a | 71 | |
MisterGiet | 5:173ffbf84b3a | 72 | // Firmware |
MisterGiet | 5:173ffbf84b3a | 73 | GattCharacteristic fwChars(GattCharacteristic::UUID_FIRMWARE_REVISION_STRING_CHAR, (uint8_t *)fwversion, sizeof(fwversion), sizeof(fwversion),GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
MisterGiet | 5:173ffbf84b3a | 74 | GattCharacteristic *firmwareChars[] = {&fwChars }; |
MisterGiet | 5:173ffbf84b3a | 75 | GattService firmwareService(GattService::UUID_DEVICE_INFORMATION_SERVICE, firmwareChars, sizeof(firmwareChars) / sizeof(GattCharacteristic *)); |
MisterGiet | 5:173ffbf84b3a | 76 | |
MisterGiet | 5:173ffbf84b3a | 77 | //Power |
MisterGiet | 5:173ffbf84b3a | 78 | GattCharacteristic TxPowerChar(GattCharacteristic::UUID_TX_POWER_LEVEL_CHAR, (uint8_t*)&TxPower, sizeof(TxPower), sizeof(TxPower), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
MisterGiet | 5:173ffbf84b3a | 79 | GattCharacteristic *charTable[] = {&TxPowerChar}; |
MisterGiet | 5:173ffbf84b3a | 80 | GattService TxPowerService(GattService::UUID_TX_POWER_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
MisterGiet | 5:173ffbf84b3a | 81 | |
MisterGiet | 3:53db6c57bb61 | 82 | /* BATTERY MEASUREMENT */ |
MisterGiet | 3:53db6c57bb61 | 83 | void my_analogin_init(void) |
MisterGiet | 3:53db6c57bb61 | 84 | { |
MisterGiet | 3:53db6c57bb61 | 85 | NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled; |
MisterGiet | 3:53db6c57bb61 | 86 | NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) | |
MisterGiet | 3:53db6c57bb61 | 87 | (ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) | |
MisterGiet | 3:53db6c57bb61 | 88 | (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) | |
MisterGiet | 3:53db6c57bb61 | 89 | (ADC_CONFIG_PSEL_Disabled << ADC_CONFIG_PSEL_Pos) | |
MisterGiet | 3:53db6c57bb61 | 90 | (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos); |
MisterGiet | 3:53db6c57bb61 | 91 | } |
MisterGiet | 3:53db6c57bb61 | 92 | |
MisterGiet | 3:53db6c57bb61 | 93 | uint16_t my_analogin_read_u16(void) |
MisterGiet | 3:53db6c57bb61 | 94 | { |
MisterGiet | 3:53db6c57bb61 | 95 | NRF_ADC->CONFIG &= ~ADC_CONFIG_PSEL_Msk; |
MisterGiet | 3:53db6c57bb61 | 96 | NRF_ADC->CONFIG |= ADC_CONFIG_PSEL_Disabled << ADC_CONFIG_PSEL_Pos; |
MisterGiet | 3:53db6c57bb61 | 97 | NRF_ADC->TASKS_START = 1; |
MisterGiet | 3:53db6c57bb61 | 98 | while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) {}; |
MisterGiet | 3:53db6c57bb61 | 99 | return (uint16_t)NRF_ADC->RESULT; // 10 bit |
MisterGiet | 3:53db6c57bb61 | 100 | } |
waltercolitti | 0:eb280529b0ef | 101 | |
MisterGiet | 3:53db6c57bb61 | 102 | float getBatteryVolt (void) |
MisterGiet | 3:53db6c57bb61 | 103 | { |
MisterGiet | 3:53db6c57bb61 | 104 | float Vadc = (float)my_analogin_read_u16(); |
MisterGiet | 3:53db6c57bb61 | 105 | float Vref = 1.2; // Internal Vref |
MisterGiet | 3:53db6c57bb61 | 106 | float Vcc = 3 * 4 * Vref * Vadc / 1024.0; |
MisterGiet | 3:53db6c57bb61 | 107 | |
MisterGiet | 13:0dc9a52cff66 | 108 | // Debugging: |
MisterGiet | 13:0dc9a52cff66 | 109 | char message[10]; // 1B per Ascii character |
MisterGiet | 13:0dc9a52cff66 | 110 | sprintf (message, "VCC=%.2f;", Vcc); |
MisterGiet | 13:0dc9a52cff66 | 111 | DEBUG(message); |
MisterGiet | 13:0dc9a52cff66 | 112 | SERIAL_DEBUG(message); |
MisterGiet | 13:0dc9a52cff66 | 113 | |
MisterGiet | 3:53db6c57bb61 | 114 | return Vcc; |
MisterGiet | 3:53db6c57bb61 | 115 | } |
MisterGiet | 3:53db6c57bb61 | 116 | |
MisterGiet | 3:53db6c57bb61 | 117 | int getBatteryPercent () |
MisterGiet | 3:53db6c57bb61 | 118 | { |
MisterGiet | 4:de22f0d73c12 | 119 | float Vbat_min = 2; //2; |
MisterGiet | 3:53db6c57bb61 | 120 | float Vbat_max = 3; |
MisterGiet | 3:53db6c57bb61 | 121 | int battLevel = (int) ( ((getBatteryVolt()-Vbat_min) / (Vbat_max-Vbat_min)) *100); |
MisterGiet | 3:53db6c57bb61 | 122 | |
MisterGiet | 3:53db6c57bb61 | 123 | // To avoid values bigger than 100% |
MisterGiet | 3:53db6c57bb61 | 124 | if (battLevel > 100) { |
MisterGiet | 3:53db6c57bb61 | 125 | battLevel = 100; |
MisterGiet | 3:53db6c57bb61 | 126 | } |
MisterGiet | 3:53db6c57bb61 | 127 | |
MisterGiet | 4:de22f0d73c12 | 128 | // To avoid overflow due to a value lower than Vbat_min |
MisterGiet | 21:819c4d225910 | 129 | /* |
MisterGiet | 4:de22f0d73c12 | 130 | if (battLevel >= 250) { |
MisterGiet | 4:de22f0d73c12 | 131 | battLevel = 0; |
MisterGiet | 21:819c4d225910 | 132 | }*/ |
MisterGiet | 21:819c4d225910 | 133 | |
MisterGiet | 21:819c4d225910 | 134 | // To avoid overflow due to values lower than Vbat_min |
MisterGiet | 21:819c4d225910 | 135 | if (battLevel < 0) { |
MisterGiet | 21:819c4d225910 | 136 | //if ((battLevel >= 130) && (battLevel <=255)) { |
MisterGiet | 21:819c4d225910 | 137 | battLevel = 0; |
MisterGiet | 4:de22f0d73c12 | 138 | } |
MisterGiet | 4:de22f0d73c12 | 139 | |
MisterGiet | 13:0dc9a52cff66 | 140 | // Debugging: |
MisterGiet | 13:0dc9a52cff66 | 141 | char message[10]; // 1B per Ascii character |
MisterGiet | 13:0dc9a52cff66 | 142 | sprintf (message, "BAT=%i;", battLevel); |
MisterGiet | 13:0dc9a52cff66 | 143 | DEBUG(message); |
MisterGiet | 13:0dc9a52cff66 | 144 | SERIAL_DEBUG(message); |
MisterGiet | 13:0dc9a52cff66 | 145 | |
MisterGiet | 3:53db6c57bb61 | 146 | return battLevel; |
MisterGiet | 3:53db6c57bb61 | 147 | } |
MisterGiet | 3:53db6c57bb61 | 148 | |
MisterGiet | 3:53db6c57bb61 | 149 | void blinkLed (bool enable, int color) |
MisterGiet | 3:53db6c57bb61 | 150 | { |
MisterGiet | 3:53db6c57bb61 | 151 | if (enable) { |
MisterGiet | 3:53db6c57bb61 | 152 | if (color == 1) { |
MisterGiet | 3:53db6c57bb61 | 153 | ledB=0; |
MisterGiet | 3:53db6c57bb61 | 154 | wait(0.2); |
MisterGiet | 3:53db6c57bb61 | 155 | ledB=1; |
MisterGiet | 3:53db6c57bb61 | 156 | wait(0.2); |
MisterGiet | 3:53db6c57bb61 | 157 | } |
MisterGiet | 3:53db6c57bb61 | 158 | if (color == 2) { |
MisterGiet | 3:53db6c57bb61 | 159 | ledR=0; |
MisterGiet | 3:53db6c57bb61 | 160 | wait(0.2); |
MisterGiet | 3:53db6c57bb61 | 161 | ledR=1; |
MisterGiet | 3:53db6c57bb61 | 162 | wait(0.2); |
MisterGiet | 3:53db6c57bb61 | 163 | } |
MisterGiet | 3:53db6c57bb61 | 164 | } else { |
MisterGiet | 3:53db6c57bb61 | 165 | // do nothing if enable=0 |
MisterGiet | 3:53db6c57bb61 | 166 | } |
MisterGiet | 3:53db6c57bb61 | 167 | } |
waltercolitti | 0:eb280529b0ef | 168 | |
MisterGiet | 6:80f2517965bb | 169 | uint8_t updatePresenceCounter (int counts) |
MisterGiet | 6:80f2517965bb | 170 | { |
MisterGiet | 6:80f2517965bb | 171 | uint8_t uint8_counter; |
MisterGiet | 6:80f2517965bb | 172 | |
MisterGiet | 6:80f2517965bb | 173 | if (counts>=255) { |
MisterGiet | 6:80f2517965bb | 174 | uint8_counter = 255; |
MisterGiet | 6:80f2517965bb | 175 | |
MisterGiet | 6:80f2517965bb | 176 | } else { |
MisterGiet | 6:80f2517965bb | 177 | uint8_counter = uint8_t(counts); |
MisterGiet | 6:80f2517965bb | 178 | } |
MisterGiet | 6:80f2517965bb | 179 | |
MisterGiet | 12:92f008fe602c | 180 | // Debugging: |
MisterGiet | 12:92f008fe602c | 181 | char message[10]; // 1B per Ascii character |
MisterGiet | 12:92f008fe602c | 182 | sprintf (message, "PRES=%i;", uint8_counter); |
MisterGiet | 12:92f008fe602c | 183 | DEBUG(message); |
MisterGiet | 12:92f008fe602c | 184 | SERIAL_DEBUG(message); |
MisterGiet | 12:92f008fe602c | 185 | |
MisterGiet | 6:80f2517965bb | 186 | return uint8_counter; |
MisterGiet | 6:80f2517965bb | 187 | } |
MisterGiet | 6:80f2517965bb | 188 | |
MisterGiet | 10:29dbc6d316c3 | 189 | float getHumidity(void) |
waltercolitti | 0:eb280529b0ef | 190 | { |
MisterGiet | 10:29dbc6d316c3 | 191 | float hum; |
MisterGiet | 10:29dbc6d316c3 | 192 | if(tempsensor.getHumidity(&hum) != 0) { |
MisterGiet | 10:29dbc6d316c3 | 193 | SERIAL_DEBUG("Error getting humidity"); |
MisterGiet | 10:29dbc6d316c3 | 194 | hum = -1; |
MisterGiet | 10:29dbc6d316c3 | 195 | } |
waltercolitti | 0:eb280529b0ef | 196 | |
MisterGiet | 10:29dbc6d316c3 | 197 | // Debugging: |
MisterGiet | 12:92f008fe602c | 198 | char message[10]; // 1B per Ascii character |
MisterGiet | 13:0dc9a52cff66 | 199 | //sprintf (message, "HUM=%.2f;", hum); |
MisterGiet | 13:0dc9a52cff66 | 200 | sprintf (message, "HUM=%i;",(uint16_t)hum); |
MisterGiet | 12:92f008fe602c | 201 | SERIAL_DEBUG(message); |
MisterGiet | 10:29dbc6d316c3 | 202 | DEBUG(message); |
MisterGiet | 10:29dbc6d316c3 | 203 | |
MisterGiet | 10:29dbc6d316c3 | 204 | return hum; |
waltercolitti | 0:eb280529b0ef | 205 | } |
waltercolitti | 0:eb280529b0ef | 206 | |
waltercolitti | 0:eb280529b0ef | 207 | float getTemperature(void) |
waltercolitti | 0:eb280529b0ef | 208 | { |
MisterGiet | 3:53db6c57bb61 | 209 | float temp; |
MisterGiet | 4:de22f0d73c12 | 210 | |
MisterGiet | 3:53db6c57bb61 | 211 | if(tempsensor.getTemperature(&temp) != 0) { |
MisterGiet | 3:53db6c57bb61 | 212 | DEBUG("Error getting temperature"); |
MisterGiet | 3:53db6c57bb61 | 213 | temp = -1; |
MisterGiet | 3:53db6c57bb61 | 214 | } |
waltercolitti | 0:eb280529b0ef | 215 | |
MisterGiet | 3:53db6c57bb61 | 216 | // Adding offset: |
MisterGiet | 3:53db6c57bb61 | 217 | temp = temp + OFFSET; |
MisterGiet | 3:53db6c57bb61 | 218 | |
MisterGiet | 13:0dc9a52cff66 | 219 | // Debugging: |
MisterGiet | 13:0dc9a52cff66 | 220 | char msg[11]; // 1B per Ascii character |
MisterGiet | 13:0dc9a52cff66 | 221 | char msg2[10]; |
MisterGiet | 13:0dc9a52cff66 | 222 | sprintf (msg, "\nTEM=%.2f;", temp); |
MisterGiet | 13:0dc9a52cff66 | 223 | sprintf (msg2, "OFFSET=%i;",OFFSET); |
MisterGiet | 13:0dc9a52cff66 | 224 | SERIAL_DEBUG(msg); |
MisterGiet | 13:0dc9a52cff66 | 225 | DEBUG(msg); |
MisterGiet | 13:0dc9a52cff66 | 226 | SERIAL_DEBUG(msg2); |
MisterGiet | 13:0dc9a52cff66 | 227 | DEBUG(msg2); |
MisterGiet | 13:0dc9a52cff66 | 228 | |
MisterGiet | 3:53db6c57bb61 | 229 | return temp; |
waltercolitti | 0:eb280529b0ef | 230 | } |
waltercolitti | 0:eb280529b0ef | 231 | |
MisterGiet | 12:92f008fe602c | 232 | void updateManufData () |
MisterGiet | 12:92f008fe602c | 233 | { |
MisterGiet | 13:0dc9a52cff66 | 234 | float temp = getTemperature(); |
MisterGiet | 12:92f008fe602c | 235 | float hum = getHumidity(); |
MisterGiet | 13:0dc9a52cff66 | 236 | |
MisterGiet | 12:92f008fe602c | 237 | ManufData [1]= (uint8_t) temp; |
MisterGiet | 12:92f008fe602c | 238 | ManufData [2]= (uint8_t) ((temp -((uint8_t)temp))*100); |
MisterGiet | 13:0dc9a52cff66 | 239 | ManufData [3]= (uint8_t) hum; |
MisterGiet | 12:92f008fe602c | 240 | ManufData [4]= updatePresenceCounter(presenceCounter); |
MisterGiet | 13:0dc9a52cff66 | 241 | |
MisterGiet | 13:0dc9a52cff66 | 242 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, ManufData, sizeof(ManufData)); |
MisterGiet | 13:0dc9a52cff66 | 243 | |
MisterGiet | 13:0dc9a52cff66 | 244 | // Debugging: |
MisterGiet | 13:0dc9a52cff66 | 245 | char message1[10]; // 1B per Ascii character |
MisterGiet | 13:0dc9a52cff66 | 246 | char message2[10]; // 1B per Ascii character |
MisterGiet | 13:0dc9a52cff66 | 247 | char message3[10]; // 1B per Ascii character |
MisterGiet | 13:0dc9a52cff66 | 248 | char message4[10]; // 1B per Ascii character |
MisterGiet | 13:0dc9a52cff66 | 249 | sprintf (message1, "MD1=%i;", ManufData [1]); |
MisterGiet | 13:0dc9a52cff66 | 250 | sprintf (message2, "MD2=%i;", ManufData [2]); |
MisterGiet | 13:0dc9a52cff66 | 251 | sprintf (message3, "MD3=%i;", ManufData [3]); |
MisterGiet | 13:0dc9a52cff66 | 252 | sprintf (message4, "MD4=%i;", ManufData [4]); |
MisterGiet | 13:0dc9a52cff66 | 253 | SERIAL_DEBUG(message1); |
MisterGiet | 13:0dc9a52cff66 | 254 | SERIAL_DEBUG(message2); |
MisterGiet | 13:0dc9a52cff66 | 255 | SERIAL_DEBUG(message3); |
MisterGiet | 13:0dc9a52cff66 | 256 | SERIAL_DEBUG(message4); |
MisterGiet | 13:0dc9a52cff66 | 257 | DEBUG(message1); |
MisterGiet | 13:0dc9a52cff66 | 258 | DEBUG(message2); |
MisterGiet | 13:0dc9a52cff66 | 259 | DEBUG(message3); |
MisterGiet | 13:0dc9a52cff66 | 260 | DEBUG(message4); |
MisterGiet | 12:92f008fe602c | 261 | } |
MisterGiet | 12:92f008fe602c | 262 | |
MisterGiet | 10:29dbc6d316c3 | 263 | /* Restart Advertising on disconnection*/ |
MisterGiet | 10:29dbc6d316c3 | 264 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
MisterGiet | 3:53db6c57bb61 | 265 | { |
MisterGiet | 13:0dc9a52cff66 | 266 | SERIAL_DEBUG("\nDISCONNECTED\n"); |
MisterGiet | 10:29dbc6d316c3 | 267 | BLE::Instance().gap().startAdvertising(); |
MisterGiet | 10:29dbc6d316c3 | 268 | } |
MisterGiet | 3:53db6c57bb61 | 269 | |
MisterGiet | 13:0dc9a52cff66 | 270 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) |
MisterGiet | 13:0dc9a52cff66 | 271 | { |
MisterGiet | 13:0dc9a52cff66 | 272 | SERIAL_DEBUG("\nCONNECTED\n"); |
MisterGiet | 13:0dc9a52cff66 | 273 | } |
MisterGiet | 13:0dc9a52cff66 | 274 | |
MisterGiet | 10:29dbc6d316c3 | 275 | /** |
MisterGiet | 10:29dbc6d316c3 | 276 | * This function is called when the ble initialization process has failed |
MisterGiet | 10:29dbc6d316c3 | 277 | */ |
MisterGiet | 10:29dbc6d316c3 | 278 | void onBleInitError(BLE &ble, ble_error_t error) |
MisterGiet | 10:29dbc6d316c3 | 279 | { |
MisterGiet | 10:29dbc6d316c3 | 280 | /* Avoid compiler warnings */ |
MisterGiet | 10:29dbc6d316c3 | 281 | (void) ble; |
MisterGiet | 10:29dbc6d316c3 | 282 | (void) error; |
MisterGiet | 10:29dbc6d316c3 | 283 | /* Initialization error handling should go here */ |
MisterGiet | 3:53db6c57bb61 | 284 | } |
MisterGiet | 3:53db6c57bb61 | 285 | |
MisterGiet | 3:53db6c57bb61 | 286 | void onDataWrittenCallback(const GattWriteCallbackParams *params) |
waltercolitti | 0:eb280529b0ef | 287 | { |
MisterGiet | 6:80f2517965bb | 288 | int var = params->data[0]; |
MisterGiet | 6:80f2517965bb | 289 | |
MisterGiet | 6:80f2517965bb | 290 | if (var == 1) { |
MisterGiet | 9:97d8ea1feeed | 291 | blinkLed(installMode,1); |
MisterGiet | 3:53db6c57bb61 | 292 | SERIAL_DEBUG("BLE. Resetting presence\n"); |
MisterGiet | 6:80f2517965bb | 293 | presenceCounter = 0; |
MisterGiet | 6:80f2517965bb | 294 | //presenceCounterBLE = 0; |
MisterGiet | 6:80f2517965bb | 295 | triggerSensorPolling = true; // force update of all BLE services |
MisterGiet | 3:53db6c57bb61 | 296 | } |
MisterGiet | 3:53db6c57bb61 | 297 | |
MisterGiet | 6:80f2517965bb | 298 | if (var == 2) { |
MisterGiet | 3:53db6c57bb61 | 299 | blinkLed(1,1); |
MisterGiet | 3:53db6c57bb61 | 300 | blinkLed(1,1); |
MisterGiet | 3:53db6c57bb61 | 301 | SERIAL_DEBUG("BLE. All LEDs ON/OFF\n"); |
MisterGiet | 9:97d8ea1feeed | 302 | installMode = !installMode; |
MisterGiet | 3:53db6c57bb61 | 303 | } |
MisterGiet | 3:53db6c57bb61 | 304 | |
MisterGiet | 3:53db6c57bb61 | 305 | if (params->data[0] == 3) { |
MisterGiet | 3:53db6c57bb61 | 306 | blinkLed(1,1); |
MisterGiet | 3:53db6c57bb61 | 307 | blinkLed(1,1); |
MisterGiet | 3:53db6c57bb61 | 308 | blinkLed(1,1); |
MisterGiet | 3:53db6c57bb61 | 309 | SERIAL_DEBUG("BLE. Rebooting sensor\n"); |
MisterGiet | 3:53db6c57bb61 | 310 | wait(3); |
MisterGiet | 3:53db6c57bb61 | 311 | NVIC_SystemReset(); // SW Reset |
MisterGiet | 3:53db6c57bb61 | 312 | } |
MisterGiet | 12:92f008fe602c | 313 | |
MisterGiet | 12:92f008fe602c | 314 | if (params->data[0] == 4) { |
MisterGiet | 12:92f008fe602c | 315 | blinkLed(1,1); |
MisterGiet | 12:92f008fe602c | 316 | blinkLed(1,1); |
MisterGiet | 12:92f008fe602c | 317 | blinkLed(1,1); |
MisterGiet | 12:92f008fe602c | 318 | blinkLed(1,1); |
MisterGiet | 12:92f008fe602c | 319 | SERIAL_DEBUG("BLE. Rebooting sensor\n"); |
MisterGiet | 12:92f008fe602c | 320 | /* Enable over-the-air firmware updates. Instantiating DFUSservice introduces a |
MisterGiet | 12:92f008fe602c | 321 | * control characteristic which can be used to trigger the application to |
MisterGiet | 12:92f008fe602c | 322 | * handover control to a resident bootloader. */ |
MisterGiet | 13:0dc9a52cff66 | 323 | //DFUService dfu(ble); |
MisterGiet | 12:92f008fe602c | 324 | } |
waltercolitti | 0:eb280529b0ef | 325 | } |
waltercolitti | 0:eb280529b0ef | 326 | |
waltercolitti | 0:eb280529b0ef | 327 | /** |
waltercolitti | 0:eb280529b0ef | 328 | * Callback triggered when the ble initialization process has finished |
waltercolitti | 0:eb280529b0ef | 329 | */ |
waltercolitti | 0:eb280529b0ef | 330 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
waltercolitti | 0:eb280529b0ef | 331 | { |
MisterGiet | 13:0dc9a52cff66 | 332 | SERIAL_DEBUG("BLE.Init \n"); |
MisterGiet | 3:53db6c57bb61 | 333 | |
MisterGiet | 3:53db6c57bb61 | 334 | BLE& ble = params->ble; |
waltercolitti | 0:eb280529b0ef | 335 | ble_error_t error = params->error; |
waltercolitti | 0:eb280529b0ef | 336 | |
waltercolitti | 0:eb280529b0ef | 337 | if (error != BLE_ERROR_NONE) { |
waltercolitti | 0:eb280529b0ef | 338 | /* In case of error, forward the error handling to onBleInitError */ |
waltercolitti | 0:eb280529b0ef | 339 | onBleInitError(ble, error); |
waltercolitti | 0:eb280529b0ef | 340 | return; |
waltercolitti | 0:eb280529b0ef | 341 | } |
waltercolitti | 0:eb280529b0ef | 342 | |
waltercolitti | 0:eb280529b0ef | 343 | /* Ensure that it is the default instance of BLE */ |
waltercolitti | 0:eb280529b0ef | 344 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
waltercolitti | 0:eb280529b0ef | 345 | return; |
waltercolitti | 0:eb280529b0ef | 346 | } |
MisterGiet | 3:53db6c57bb61 | 347 | |
MisterGiet | 13:0dc9a52cff66 | 348 | ble.gap().onConnection(connectionCallback); |
MisterGiet | 3:53db6c57bb61 | 349 | ble.gap().onDisconnection(disconnectionCallback); |
waltercolitti | 0:eb280529b0ef | 350 | |
MisterGiet | 3:53db6c57bb61 | 351 | // Set Transmission power: |
MisterGiet | 3:53db6c57bb61 | 352 | ble.setTxPower(TxPower); |
MisterGiet | 3:53db6c57bb61 | 353 | |
MisterGiet | 3:53db6c57bb61 | 354 | //ble.gap().setScanParams(500, 400); |
waltercolitti | 0:eb280529b0ef | 355 | //ble.gap().startScan(advertisementCallback); |
MisterGiet | 3:53db6c57bb61 | 356 | |
waltercolitti | 0:eb280529b0ef | 357 | //BLE server setup |
waltercolitti | 0:eb280529b0ef | 358 | ble.gattServer().onDataWritten(onDataWrittenCallback); |
MisterGiet | 3:53db6c57bb61 | 359 | |
waltercolitti | 0:eb280529b0ef | 360 | //Setup primary services |
MisterGiet | 6:80f2517965bb | 361 | presenceService = new PresenceDetectionService(ble, updatePresenceCounter(presenceCounter)); |
MisterGiet | 10:29dbc6d316c3 | 362 | |
MisterGiet | 3:53db6c57bb61 | 363 | thermometerServicePtr = new HealthThermometerService(ble, getTemperature(), HealthThermometerService::LOCATION_EAR); |
MisterGiet | 3:53db6c57bb61 | 364 | humidityService = new HumidityMeasureService(ble, getHumidity()); |
MisterGiet | 10:29dbc6d316c3 | 365 | |
MisterGiet | 10:29dbc6d316c3 | 366 | humidityTemperatureServicePtr = new EnvironmentalService (ble); |
MisterGiet | 10:29dbc6d316c3 | 367 | batteryServicePtr = new BatteryService(ble, batteryLevel); |
MisterGiet | 10:29dbc6d316c3 | 368 | |
MisterGiet | 3:53db6c57bb61 | 369 | ble.addService(firmwareService); |
MisterGiet | 3:53db6c57bb61 | 370 | uart = new UARTService(ble); |
MisterGiet | 10:29dbc6d316c3 | 371 | //ble.addService(TxPowerService); |
MisterGiet | 3:53db6c57bb61 | 372 | |
MisterGiet | 3:53db6c57bb61 | 373 | /* setup advertising */ |
MisterGiet | 3:53db6c57bb61 | 374 | |
MisterGiet | 3:53db6c57bb61 | 375 | /* Sacrifice 3B of 31B to Advertising Flags */ |
waltercolitti | 0:eb280529b0ef | 376 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
MisterGiet | 3:53db6c57bb61 | 377 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
MisterGiet | 3:53db6c57bb61 | 378 | |
MisterGiet | 3:53db6c57bb61 | 379 | /* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */ |
MisterGiet | 3:53db6c57bb61 | 380 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, ManufData, sizeof(ManufData)); |
MisterGiet | 14:5a1467f7cfb2 | 381 | //ble.gap().accumulateAdvertisingPayloadTxPower(TxPower); |
waltercolitti | 0:eb280529b0ef | 382 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
MisterGiet | 3:53db6c57bb61 | 383 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER); |
MisterGiet | 3:53db6c57bb61 | 384 | //ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,(const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); |
MisterGiet | 3:53db6c57bb61 | 385 | //ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
waltercolitti | 0:eb280529b0ef | 386 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
waltercolitti | 0:eb280529b0ef | 387 | |
MisterGiet | 18:9d3dbfacd106 | 388 | ble.setAdvertisingInterval(320); /* 1000ms */ |
MisterGiet | 3:53db6c57bb61 | 389 | ble.startAdvertising(); |
waltercolitti | 0:eb280529b0ef | 390 | } |
waltercolitti | 0:eb280529b0ef | 391 | |
waltercolitti | 0:eb280529b0ef | 392 | void periodicCallbackPresence(void) |
waltercolitti | 0:eb280529b0ef | 393 | { |
MisterGiet | 3:53db6c57bb61 | 394 | triggerPresencePolling = true; |
MisterGiet | 6:80f2517965bb | 395 | presenceState = true; |
waltercolitti | 0:eb280529b0ef | 396 | } |
waltercolitti | 0:eb280529b0ef | 397 | |
MisterGiet | 3:53db6c57bb61 | 398 | void periodicCallbackSensor (void) |
waltercolitti | 0:eb280529b0ef | 399 | { |
MisterGiet | 3:53db6c57bb61 | 400 | triggerSensorPolling = true; |
waltercolitti | 0:eb280529b0ef | 401 | } |
waltercolitti | 0:eb280529b0ef | 402 | |
MisterGiet | 15:cd533d4ef766 | 403 | void periodicCallbackTest (void) |
MisterGiet | 15:cd533d4ef766 | 404 | { |
MisterGiet | 15:cd533d4ef766 | 405 | TESTCONF=!TESTCONF; |
MisterGiet | 15:cd533d4ef766 | 406 | } |
MisterGiet | 15:cd533d4ef766 | 407 | |
MisterGiet | 6:80f2517965bb | 408 | void updateBLEservices () |
MisterGiet | 6:80f2517965bb | 409 | { |
MisterGiet | 15:cd533d4ef766 | 410 | if (TESTCONF) { |
MisterGiet | 20:11eb72daf716 | 411 | thermometerServicePtr->updateTemperature(getTemperature()+80); |
MisterGiet | 20:11eb72daf716 | 412 | humidityTemperatureServicePtr->updateTemperature (getTemperature()+80); |
MisterGiet | 12:92f008fe602c | 413 | |
MisterGiet | 15:cd533d4ef766 | 414 | } else { |
MisterGiet | 15:cd533d4ef766 | 415 | thermometerServicePtr->updateTemperature(getTemperature()); |
MisterGiet | 15:cd533d4ef766 | 416 | humidityTemperatureServicePtr->updateTemperature (getTemperature()); |
MisterGiet | 15:cd533d4ef766 | 417 | } |
MisterGiet | 15:cd533d4ef766 | 418 | |
MisterGiet | 15:cd533d4ef766 | 419 | humidityService->updateHumidity(getHumidity()); |
MisterGiet | 10:29dbc6d316c3 | 420 | humidityTemperatureServicePtr->updateHumidity (getHumidity()); |
MisterGiet | 10:29dbc6d316c3 | 421 | batteryServicePtr->updateBatteryLevel(getBatteryPercent()); |
MisterGiet | 12:92f008fe602c | 422 | presenceService->updatePresence(updatePresenceCounter(presenceCounter)); |
MisterGiet | 6:80f2517965bb | 423 | } |
MisterGiet | 6:80f2517965bb | 424 | |
waltercolitti | 0:eb280529b0ef | 425 | int main(void) |
MisterGiet | 3:53db6c57bb61 | 426 | { |
MisterGiet | 13:0dc9a52cff66 | 427 | SERIAL_DEBUG("\nSTART\n"); |
MisterGiet | 13:0dc9a52cff66 | 428 | DEBUG("\nSTART\n"); |
MisterGiet | 3:53db6c57bb61 | 429 | char pres_message[20]; |
MisterGiet | 3:53db6c57bb61 | 430 | |
MisterGiet | 3:53db6c57bb61 | 431 | blinkLed(1,1); |
MisterGiet | 3:53db6c57bb61 | 432 | |
MisterGiet | 20:11eb72daf716 | 433 | Ticker ticker_sensor, ticker_test; |
MisterGiet | 3:53db6c57bb61 | 434 | |
MisterGiet | 20:11eb72daf716 | 435 | /* Trigger presence every time is detected */ |
MisterGiet | 20:11eb72daf716 | 436 | motion_pin.fall(&periodicCallbackPresence); |
MisterGiet | 15:cd533d4ef766 | 437 | |
MisterGiet | 20:11eb72daf716 | 438 | /* Get temp&hum data every 3 seconds */ |
MisterGiet | 20:11eb72daf716 | 439 | ticker_sensor.attach(periodicCallbackSensor, 3); // 3 sec |
MisterGiet | 20:11eb72daf716 | 440 | |
MisterGiet | 20:11eb72daf716 | 441 | /* If TESTCONF is needed: |
MisterGiet | 20:11eb72daf716 | 442 | * - set the desired room temperature to 30ºC |
MisterGiet | 20:11eb72daf716 | 443 | * - uncomment the following line: */ |
MisterGiet | 21:819c4d225910 | 444 | //ticker_test.attach(periodicCallbackTest, 300); // 5 min |
MisterGiet | 3:53db6c57bb61 | 445 | |
waltercolitti | 0:eb280529b0ef | 446 | //BLE instance setup |
waltercolitti | 0:eb280529b0ef | 447 | BLE &bleptr = BLE::Instance(); |
waltercolitti | 0:eb280529b0ef | 448 | bleptr.init(bleInitComplete); |
MisterGiet | 3:53db6c57bb61 | 449 | |
waltercolitti | 0:eb280529b0ef | 450 | while (true) { |
MisterGiet | 3:53db6c57bb61 | 451 | |
MisterGiet | 3:53db6c57bb61 | 452 | /* Get Presence data: */ |
MisterGiet | 3:53db6c57bb61 | 453 | if(triggerPresencePolling) { |
MisterGiet | 3:53db6c57bb61 | 454 | triggerPresencePolling = false; |
MisterGiet | 3:53db6c57bb61 | 455 | |
MisterGiet | 6:80f2517965bb | 456 | // if presence is detected: |
MisterGiet | 6:80f2517965bb | 457 | if (presenceState) { |
MisterGiet | 10:29dbc6d316c3 | 458 | |
MisterGiet | 6:80f2517965bb | 459 | presenceCounter++; |
MisterGiet | 10:29dbc6d316c3 | 460 | |
MisterGiet | 6:80f2517965bb | 461 | // Update all BLE services for every presence detected: |
MisterGiet | 6:80f2517965bb | 462 | updateBLEservices(); |
MisterGiet | 3:53db6c57bb61 | 463 | |
MisterGiet | 3:53db6c57bb61 | 464 | // Debugging: |
MisterGiet | 9:97d8ea1feeed | 465 | blinkLed(installMode,1); |
MisterGiet | 6:80f2517965bb | 466 | SERIAL_DEBUG("Presence counter: %i\n", presenceCounter); |
MisterGiet | 6:80f2517965bb | 467 | sprintf (pres_message, "Pres=%i\n", presenceCounter); |
MisterGiet | 3:53db6c57bb61 | 468 | DEBUG(pres_message); |
MisterGiet | 3:53db6c57bb61 | 469 | |
MisterGiet | 6:80f2517965bb | 470 | presenceState=0; |
MisterGiet | 3:53db6c57bb61 | 471 | } |
MisterGiet | 3:53db6c57bb61 | 472 | |
MisterGiet | 6:80f2517965bb | 473 | // if no presence: |
MisterGiet | 3:53db6c57bb61 | 474 | else { |
MisterGiet | 3:53db6c57bb61 | 475 | SERIAL_DEBUG("."); |
MisterGiet | 3:53db6c57bb61 | 476 | } |
MisterGiet | 3:53db6c57bb61 | 477 | } |
MisterGiet | 3:53db6c57bb61 | 478 | |
MisterGiet | 13:0dc9a52cff66 | 479 | updateManufData (); // update advertising values |
MisterGiet | 13:0dc9a52cff66 | 480 | |
MisterGiet | 10:29dbc6d316c3 | 481 | /* Update BLE services if connected: */ |
waltercolitti | 0:eb280529b0ef | 482 | if (triggerSensorPolling && ble.getGapState().connected) { |
MisterGiet | 10:29dbc6d316c3 | 483 | |
MisterGiet | 6:80f2517965bb | 484 | // Quanto consuma quando sta leggendo la temperatura? |
waltercolitti | 0:eb280529b0ef | 485 | triggerSensorPolling = false; |
MisterGiet | 6:80f2517965bb | 486 | |
MisterGiet | 10:29dbc6d316c3 | 487 | updateBLEservices(); |
MisterGiet | 12:92f008fe602c | 488 | |
MisterGiet | 3:53db6c57bb61 | 489 | } else { |
MisterGiet | 3:53db6c57bb61 | 490 | ble.waitForEvent(); |
waltercolitti | 0:eb280529b0ef | 491 | } |
MisterGiet | 13:0dc9a52cff66 | 492 | //SERIAL_DEBUG("\n"); |
MisterGiet | 13:0dc9a52cff66 | 493 | //DEBUG("\n"); |
waltercolitti | 0:eb280529b0ef | 494 | } |
MisterGiet | 6:80f2517965bb | 495 | } |