project Internet of things 2016 UPMC : based on simple controls, it sends pression and temperature from BMP180

Dependencies:   BLE_API BMP180 mbed nRF51822

Committer:
quentin93
Date:
Mon Jan 02 22:10:20 2017 +0000
Revision:
0:05113982163d
project Internet of things 2016 UPMC

Who changed what in which revision?

UserRevisionLine numberNew contents of line
quentin93 0:05113982163d 1 /*
quentin93 0:05113982163d 2
quentin93 0:05113982163d 3 Copyright (c) 2012-2014 RedBearLab
quentin93 0:05113982163d 4
quentin93 0:05113982163d 5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
quentin93 0:05113982163d 6 and associated documentation files (the "Software"), to deal in the Software without restriction,
quentin93 0:05113982163d 7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
quentin93 0:05113982163d 8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
quentin93 0:05113982163d 9 subject to the following conditions:
quentin93 0:05113982163d 10 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
quentin93 0:05113982163d 11
quentin93 0:05113982163d 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
quentin93 0:05113982163d 13 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
quentin93 0:05113982163d 14 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
quentin93 0:05113982163d 15 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
quentin93 0:05113982163d 16 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
quentin93 0:05113982163d 17
quentin93 0:05113982163d 18 */
quentin93 0:05113982163d 19
quentin93 0:05113982163d 20 #include "mbed.h"
quentin93 0:05113982163d 21 #include "ble/BLE.h"
quentin93 0:05113982163d 22 #include "Servo.h"
quentin93 0:05113982163d 23 #include "GattCallbackParamTypes.h"
quentin93 0:05113982163d 24 #include "BMP180.h"
quentin93 0:05113982163d 25
quentin93 0:05113982163d 26 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
quentin93 0:05113982163d 27 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
quentin93 0:05113982163d 28 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
quentin93 0:05113982163d 29
quentin93 0:05113982163d 30 #define TXRX_BUF_LEN 20
quentin93 0:05113982163d 31
quentin93 0:05113982163d 32 #define DIGITAL_OUT_PIN P0_17 //D7
quentin93 0:05113982163d 33 #define DIGITAL_IN_PIN P0_5 //A4
quentin93 0:05113982163d 34 #define PWM_PIN P0_16 //D6
quentin93 0:05113982163d 35 #define SERVO_PIN P0_14 //D10
quentin93 0:05113982163d 36 #define ANALOG_IN_PIN P0_1 //A0
quentin93 0:05113982163d 37
quentin93 0:05113982163d 38 BLE ble;
quentin93 0:05113982163d 39
quentin93 0:05113982163d 40 DigitalOut LED_SET(DIGITAL_OUT_PIN);
quentin93 0:05113982163d 41 DigitalIn BUTTON(DIGITAL_IN_PIN);
quentin93 0:05113982163d 42 PwmOut PWM(PWM_PIN);
quentin93 0:05113982163d 43 AnalogIn ANALOG(ANALOG_IN_PIN);
quentin93 0:05113982163d 44 Servo MYSERVO(SERVO_PIN);
quentin93 0:05113982163d 45
quentin93 0:05113982163d 46 Serial pc(USBTX, USBRX);
quentin93 0:05113982163d 47
quentin93 0:05113982163d 48 static uint8_t analog_enabled = 0;
quentin93 0:05113982163d 49 static uint8_t old_state = 0;
quentin93 0:05113982163d 50 static bool temp_enabled = false;
quentin93 0:05113982163d 51 static bool pres_enabled = false;
quentin93 0:05113982163d 52 I2C i2c(D14, D15);
quentin93 0:05113982163d 53 BMP180 bmp180(&i2c);
quentin93 0:05113982163d 54
quentin93 0:05113982163d 55 // The Nordic UART Service
quentin93 0:05113982163d 56 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
quentin93 0:05113982163d 57 static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
quentin93 0:05113982163d 58 static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
quentin93 0:05113982163d 59 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
quentin93 0:05113982163d 60
quentin93 0:05113982163d 61
quentin93 0:05113982163d 62 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
quentin93 0:05113982163d 63 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
quentin93 0:05113982163d 64
quentin93 0:05113982163d 65 //static uint8_t rx_buf[TXRX_BUF_LEN];
quentin93 0:05113982163d 66 //static uint8_t rx_len=0;
quentin93 0:05113982163d 67
quentin93 0:05113982163d 68
quentin93 0:05113982163d 69 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
quentin93 0:05113982163d 70
quentin93 0:05113982163d 71 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
quentin93 0:05113982163d 72
quentin93 0:05113982163d 73 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
quentin93 0:05113982163d 74
quentin93 0:05113982163d 75 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
quentin93 0:05113982163d 76
quentin93 0:05113982163d 77
quentin93 0:05113982163d 78
quentin93 0:05113982163d 79 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
quentin93 0:05113982163d 80 {
quentin93 0:05113982163d 81 pc.printf("Disconnected \r\n");
quentin93 0:05113982163d 82 pc.printf("Restart advertising \r\n");
quentin93 0:05113982163d 83 ble.startAdvertising();
quentin93 0:05113982163d 84 }
quentin93 0:05113982163d 85
quentin93 0:05113982163d 86 void WrittenHandler(const GattWriteCallbackParams *Handler)
quentin93 0:05113982163d 87 {
quentin93 0:05113982163d 88 uint8_t buf[TXRX_BUF_LEN];
quentin93 0:05113982163d 89 uint16_t bytesRead, index;
quentin93 0:05113982163d 90
quentin93 0:05113982163d 91 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle())
quentin93 0:05113982163d 92 {
quentin93 0:05113982163d 93 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
quentin93 0:05113982163d 94 memset(txPayload, 0, TXRX_BUF_LEN);
quentin93 0:05113982163d 95 memcpy(txPayload, buf, TXRX_BUF_LEN);
quentin93 0:05113982163d 96
quentin93 0:05113982163d 97 for(index=0; index<bytesRead; index++)
quentin93 0:05113982163d 98 pc.putc(buf[index]);
quentin93 0:05113982163d 99
quentin93 0:05113982163d 100 if(buf[0] == 0x01)
quentin93 0:05113982163d 101 {
quentin93 0:05113982163d 102 if(buf[1] == 0x01)
quentin93 0:05113982163d 103 LED_SET = 1;
quentin93 0:05113982163d 104 else
quentin93 0:05113982163d 105 LED_SET = 0;
quentin93 0:05113982163d 106 }
quentin93 0:05113982163d 107 else if(buf[0] == 0xA0)
quentin93 0:05113982163d 108 {
quentin93 0:05113982163d 109 if(buf[1] == 0x01)
quentin93 0:05113982163d 110 analog_enabled = 1;
quentin93 0:05113982163d 111 else
quentin93 0:05113982163d 112 analog_enabled = 0;
quentin93 0:05113982163d 113 }
quentin93 0:05113982163d 114 else if(buf[0] == 0xA1){
quentin93 0:05113982163d 115 if(buf[1] == 0x01){
quentin93 0:05113982163d 116 temp_enabled =true;
quentin93 0:05113982163d 117 }else{
quentin93 0:05113982163d 118 temp_enabled = false;
quentin93 0:05113982163d 119 }
quentin93 0:05113982163d 120 }
quentin93 0:05113982163d 121 else if(buf[0] == 0xA2){
quentin93 0:05113982163d 122 if(buf[1] == 0x01){
quentin93 0:05113982163d 123 pres_enabled =true;
quentin93 0:05113982163d 124 }else{
quentin93 0:05113982163d 125 pres_enabled = false;
quentin93 0:05113982163d 126 }
quentin93 0:05113982163d 127 }
quentin93 0:05113982163d 128
quentin93 0:05113982163d 129
quentin93 0:05113982163d 130
quentin93 0:05113982163d 131 else if(buf[0] == 0x02)
quentin93 0:05113982163d 132 {
quentin93 0:05113982163d 133 float value = (float)buf[1]/255;
quentin93 0:05113982163d 134 PWM = value;
quentin93 0:05113982163d 135 }
quentin93 0:05113982163d 136 else if(buf[0] == 0x03)
quentin93 0:05113982163d 137 {
quentin93 0:05113982163d 138 MYSERVO.write(buf[1]);
quentin93 0:05113982163d 139 }
quentin93 0:05113982163d 140 else if(buf[0] == 0x04)
quentin93 0:05113982163d 141 {
quentin93 0:05113982163d 142 analog_enabled = 0;
quentin93 0:05113982163d 143 PWM = 0;
quentin93 0:05113982163d 144 MYSERVO.write(0);
quentin93 0:05113982163d 145 LED_SET = 0;
quentin93 0:05113982163d 146 old_state = 0;
quentin93 0:05113982163d 147 }
quentin93 0:05113982163d 148
quentin93 0:05113982163d 149 }
quentin93 0:05113982163d 150 }
quentin93 0:05113982163d 151 /*
quentin93 0:05113982163d 152 void uartCB(void)
quentin93 0:05113982163d 153 {
quentin93 0:05113982163d 154 while(pc.readable())
quentin93 0:05113982163d 155 {
quentin93 0:05113982163d 156 rx_buf[rx_len++] = pc.getc();
quentin93 0:05113982163d 157 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
quentin93 0:05113982163d 158 {
quentin93 0:05113982163d 159 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
quentin93 0:05113982163d 160 pc.printf("RecHandler \r\n");
quentin93 0:05113982163d 161 pc.printf("Length: ");
quentin93 0:05113982163d 162 pc.putc(rx_len);
quentin93 0:05113982163d 163 pc.printf("\r\n");
quentin93 0:05113982163d 164 rx_len = 0;
quentin93 0:05113982163d 165 break;
quentin93 0:05113982163d 166 }
quentin93 0:05113982163d 167 }
quentin93 0:05113982163d 168 }
quentin93 0:05113982163d 169 */
quentin93 0:05113982163d 170 void m_status_check_handle(void)
quentin93 0:05113982163d 171 {
quentin93 0:05113982163d 172 uint8_t buf[3];
quentin93 0:05113982163d 173 if (analog_enabled) // if analog reading enabled
quentin93 0:05113982163d 174 {
quentin93 0:05113982163d 175 // Read and send out
quentin93 0:05113982163d 176 float s = ANALOG;
quentin93 0:05113982163d 177 uint16_t value = s*1024;
quentin93 0:05113982163d 178 buf[0] = (0x0B);
quentin93 0:05113982163d 179 buf[1] = (value >> 8);
quentin93 0:05113982163d 180 buf[2] = (value);
quentin93 0:05113982163d 181 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
quentin93 0:05113982163d 182 }
quentin93 0:05113982163d 183 if(temp_enabled){//if temperature reading enabled
quentin93 0:05113982163d 184 float temp;
quentin93 0:05113982163d 185 bmp180.startTemperature();
quentin93 0:05113982163d 186 wait_ms(5); // Wait for conversion to complete
quentin93 0:05113982163d 187 bmp180.getTemperature(&temp);
quentin93 0:05113982163d 188 uint16_t value = temp;
quentin93 0:05113982163d 189 buf[0] = (0x0C);
quentin93 0:05113982163d 190 buf[1] = (value >> 8);
quentin93 0:05113982163d 191 buf[2] = (value);
quentin93 0:05113982163d 192 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
quentin93 0:05113982163d 193 }
quentin93 0:05113982163d 194 if(pres_enabled){//if pressure reading enabled
quentin93 0:05113982163d 195 int pressure;
quentin93 0:05113982163d 196 bmp180.startPressure(BMP180::ULTRA_LOW_POWER);
quentin93 0:05113982163d 197 wait_ms(10); // Wait for conversion to complete
quentin93 0:05113982163d 198 bmp180.getPressure(&pressure);
quentin93 0:05113982163d 199 uint16_t value = pressure;
quentin93 0:05113982163d 200 buf[0] = (0x0D);
quentin93 0:05113982163d 201 buf[1] = (value >> 8);
quentin93 0:05113982163d 202 buf[2] = (value);
quentin93 0:05113982163d 203 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
quentin93 0:05113982163d 204 }
quentin93 0:05113982163d 205
quentin93 0:05113982163d 206 // If digital in changes, report the state
quentin93 0:05113982163d 207 if (BUTTON != old_state)
quentin93 0:05113982163d 208 {
quentin93 0:05113982163d 209 old_state = BUTTON;
quentin93 0:05113982163d 210
quentin93 0:05113982163d 211 if (BUTTON == 1)
quentin93 0:05113982163d 212 {
quentin93 0:05113982163d 213 buf[0] = (0x0A);
quentin93 0:05113982163d 214 buf[1] = (0x01);
quentin93 0:05113982163d 215 buf[2] = (0x00);
quentin93 0:05113982163d 216 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
quentin93 0:05113982163d 217 }
quentin93 0:05113982163d 218 else
quentin93 0:05113982163d 219 {
quentin93 0:05113982163d 220 buf[0] = (0x0A);
quentin93 0:05113982163d 221 buf[1] = (0x00);
quentin93 0:05113982163d 222 buf[2] = (0x00);
quentin93 0:05113982163d 223 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
quentin93 0:05113982163d 224 }
quentin93 0:05113982163d 225 }
quentin93 0:05113982163d 226 }
quentin93 0:05113982163d 227
quentin93 0:05113982163d 228
quentin93 0:05113982163d 229 int main(void)
quentin93 0:05113982163d 230 {
quentin93 0:05113982163d 231 Ticker ticker;
quentin93 0:05113982163d 232 ticker.attach_us(m_status_check_handle, 200000);
quentin93 0:05113982163d 233
quentin93 0:05113982163d 234 ble.init();
quentin93 0:05113982163d 235 ble.onDisconnection(disconnectionCallback);
quentin93 0:05113982163d 236 ble.onDataWritten(WrittenHandler);
quentin93 0:05113982163d 237
quentin93 0:05113982163d 238 pc.baud(9600);
quentin93 0:05113982163d 239 pc.printf("SimpleChat Init \r\n");
quentin93 0:05113982163d 240
quentin93 0:05113982163d 241 //pc.attach( uartCB , pc.RxIrq);
quentin93 0:05113982163d 242
quentin93 0:05113982163d 243 // setup advertising
quentin93 0:05113982163d 244 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
quentin93 0:05113982163d 245 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
quentin93 0:05113982163d 246 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
quentin93 0:05113982163d 247 (const uint8_t *)"Samir", sizeof("Samir") - 1);
quentin93 0:05113982163d 248 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
quentin93 0:05113982163d 249 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
quentin93 0:05113982163d 250 // 100ms; in multiples of 0.625ms.
quentin93 0:05113982163d 251 ble.setAdvertisingInterval(160);
quentin93 0:05113982163d 252
quentin93 0:05113982163d 253 ble.addService(uartService);
quentin93 0:05113982163d 254
quentin93 0:05113982163d 255 ble.startAdvertising();
quentin93 0:05113982163d 256
quentin93 0:05113982163d 257 pc.printf("Advertising Start \r\n");
quentin93 0:05113982163d 258
quentin93 0:05113982163d 259 bmp180.init();
quentin93 0:05113982163d 260
quentin93 0:05113982163d 261 while(1)
quentin93 0:05113982163d 262 {
quentin93 0:05113982163d 263 ble.waitForEvent();
quentin93 0:05113982163d 264 }
quentin93 0:05113982163d 265 }
quentin93 0:05113982163d 266
quentin93 0:05113982163d 267
quentin93 0:05113982163d 268
quentin93 0:05113982163d 269
quentin93 0:05113982163d 270
quentin93 0:05113982163d 271
quentin93 0:05113982163d 272
quentin93 0:05113982163d 273
quentin93 0:05113982163d 274
quentin93 0:05113982163d 275
quentin93 0:05113982163d 276
quentin93 0:05113982163d 277
quentin93 0:05113982163d 278
quentin93 0:05113982163d 279
quentin93 0:05113982163d 280
quentin93 0:05113982163d 281
quentin93 0:05113982163d 282
quentin93 0:05113982163d 283
quentin93 0:05113982163d 284
quentin93 0:05113982163d 285
quentin93 0:05113982163d 286
quentin93 0:05113982163d 287
quentin93 0:05113982163d 288
quentin93 0:05113982163d 289
quentin93 0:05113982163d 290
quentin93 0:05113982163d 291
quentin93 0:05113982163d 292
quentin93 0:05113982163d 293
quentin93 0:05113982163d 294
quentin93 0:05113982163d 295
quentin93 0:05113982163d 296
quentin93 0:05113982163d 297