Étape 4: Exploitation du BMP180
Versez les lires les valeurs capté par le BMP 180 il faut modifier le projet précédemment imported. Il faut un faire ONU incluent BMP085.h qui est le en-tête correspondant à la classe contenant les méthodes propre à capteur de notre-Dame.
Le code est composé de 2 fonctions directeur principal :
- WrittenHandler: Lit le tampon envoyé par la framboise au RedBearLab afin de setter ONU booléen permettant de savoir quel module est solicité normale à la framboise
- m_status_check_handle : Cette fonction est appelé périodiquement, elle verifier si une demande de relevé a été faite au capteur si oui elle envoi la valeur lu par le capteur à framboise la via la la méthode updateCharacteristicValue
La périodicité d'appelle à la fonction m_status_check_handle est réglé par l’objet ticker grâce à la méthode attach_us.
#include "mbed.h" #include "ble/BLE.h" #include "ble/FunctionPointerWithContext.h" #include "Servo.h" #include "GattCallbackParamTypes.h" #include "BMP085.h"</p><p>#define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ #define TXRX_BUF_LEN 20 #define DIGITAL_OUT_PIN P0_17 //D7 #define DIGITAL_IN_PIN P0_5 //A4 #define PWM_PIN P0_16 //D6 #define SERVO_PIN P0_14 //D10 #define ANALOG_IN_PIN P0_1 //A0 BLE ble; DigitalOut LED_SET(DIGITAL_OUT_PIN); DigitalIn BUTTON(DIGITAL_IN_PIN); PwmOut PWM(PWM_PIN); AnalogIn ANALOG(ANALOG_IN_PIN); Servo MYSERVO(SERVO_PIN); BMP085 myCaptor(P0_29, P0_28); Serial pc(USBTX, USBRX); static uint8_t analog_enabled = 0; static uint8_t captor_enabled = 0; static uint8_t captor_enabled_for_pressure = 0; // The Nordic UART Service static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};</p><p>uint8_t txPayload[TXRX_BUF_LEN] = {0,}; uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; GattCharacteristic txCharacteristic(uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); GattCharacteristic rxCharacteristic(uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { pc.printf("Disconnected \r\n"); pc.printf("Restart advertising \r\n"); ble.startAdvertising(); } void confirmationHandler(uint16_t Handler) { if (captor_enabled) captor_enabled = false; } void WrittenHandler(const GattWriteCallbackParams *Handler) { uint8_t buf[TXRX_BUF_LEN]; uint16_t bytesRead, index; if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) { ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead); memset(txPayload, 0, TXRX_BUF_LEN); memcpy(txPayload, buf, TXRX_BUF_LEN); for(index=0; index<bytesread; index++)="" pc.putc(buf[index]);="" if(buf[0]="=" 0x01)="" {="" if(buf[1]="=" 0x02)="" while(1)="" led_set="!LED_SET;" wait(0.25);="" }="" else="" 0xa0)="" analog_enabled="1;" captor_enabled="1;" 0x03)="" captor_enabled_for_pressure="1;" float="" value="(float)buf[1]/255;" pwm="value;" }<="" p=""></bytesread;></p><p>void m_status_check_handle(void) { uint8_t buf[4]; if (analog_enabled) // if analog reading enabled { // Read and send out float s = ANALOG; uint16_t value = s*1024; buf[0] = (0x0B); buf[1] = (value >> 8); buf[2] = (value); ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); } if (captor_enabled) // if analog reading enabled { // Read and send out captor_enabled = false; myCaptor.update(); float s = myCaptor.get_temperature() * 100; int value = s; buf[0] = (value >> 24); buf[1] = (value >> 16); buf[2] = (value >> 8); buf[3] = value; //for(int i = 0; i < 200000; ++i) ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 4); } // add to get pressure if (captor_enabled_for_pressure) { // Read and send out captor_enabled_for_pressure = false; myCaptor.update(); float s = myCaptor.get_pressure() * 100; uint32_t value = s; buf[0] = (value >> 24); buf[1] = (value >> 16); buf[2] = (value >> 8); buf[3] = value; ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 4); } }</p><p>int main(void) { Ticker ticker; ticker.attach_us(m_status_check_handle, 200000); ble.init(); ble.onDisconnection(disconnectionCallback); ble.onDataWritten(WrittenHandler); ble.onConfirmationReceived(confirmationHandler); pc.baud(9600); pc.printf("SimpleChat Init \r\n"); // setup advertising ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)"Mustafa", sizeof("Mustafa") - 1); ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); // 100ms; in multiples of 0.625ms. ble.setAdvertisingInterval(160); ble.addService(uartService); ble.startAdvertising(); pc.printf("Advertising Start \r\n"); while(1) { ble.waitForEvent(); } }