Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BSP_B-L475E-IOT01
Revision 39:ddabcba6eff1, committed 2019-07-10
- Comitter:
- samilive2011
- Date:
- Wed Jul 10 20:09:49 2019 +0000
- Parent:
- 38:4bdc241ba9a6
- Commit message:
- Sending data over BLE
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BSP_B-L475E-IOT01.lib Wed Jul 10 20:09:49 2019 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/teams/ST/code/BSP_B-L475E-IOT01/#0c70bc6d2dc0
--- a/source/LEDService.h Wed Jul 26 14:47:11 2017 +0200
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,43 +0,0 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2013 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef __BLE_LED_SERVICE_H__
-#define __BLE_LED_SERVICE_H__
-
-class LEDService {
-public:
- const static uint16_t LED_SERVICE_UUID = 0xA000;
- const static uint16_t LED_STATE_CHARACTERISTIC_UUID = 0xA001;
-
- LEDService(BLEDevice &_ble, bool initialValueForLEDCharacteristic) :
- ble(_ble), ledState(LED_STATE_CHARACTERISTIC_UUID, &initialValueForLEDCharacteristic)
- {
- GattCharacteristic *charTable[] = {&ledState};
- GattService ledService(LED_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
- ble.addService(ledService);
- }
-
- GattAttribute::Handle_t getValueHandle() const
- {
- return ledState.getValueHandle();
- }
-
-private:
- BLEDevice &ble;
- ReadWriteGattCharacteristic<bool> ledState;
-};
-
-#endif /* #ifndef __BLE_LED_SERVICE_H__ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/source/Sensorservice.h Wed Jul 10 20:09:49 2019 +0000
@@ -0,0 +1,174 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2013 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __BLE_ENVIRONMENTAL_SERVICE_H__
+#define __BLE_ENVIRONMENTAL_SERVICE_H__
+
+#include "ble/BLE.h"
+
+/**
+* @class EnvironmentalService
+* @brief BLE Environmental Service. This service provides temperature, humidity and pressure measurement.
+* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.environmental_sensing.xml
+* Temperature: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature.xml
+* Humidity: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.humidity.xml
+* Pressure: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.pressure.xml
+*/
+class EnvironmentalService {
+public:
+ typedef int16_t TemperatureType_t;
+ typedef uint16_t HumidityType_t;
+ typedef uint32_t PressureType_t;
+ typedef uint8_t TimeType_t;
+ typedef uint8_t writeValue_t[10];
+ typedef uint8_t readValue_t[10];
+ typedef int16_t Magneto_t[3];
+ typedef int16_t ACCELERO_t[3];
+ typedef float Gyro_t[3];
+
+ const static uint16_t READ_SERVICE_CHARACTERISTIC_UUID = 0xA001;
+ const static uint16_t WRITE_SERVICE_CHARACTERISTIC_UUID = 0xA002;
+ const static uint16_t MAGNETO_SERVICE_CHARACTERISTIC_UUID = 0xA003;
+ const static uint16_t GYRO_SERVICE_CHARACTERISTIC_UUID = 0xA004;
+ const static uint16_t ACCELERO_SERVICE_CHARACTERISTIC_UUID = 0xA005;
+
+ /**
+ * @brief EnvironmentalService constructor.
+ * @param ble Reference to BLE device.
+ * @param temperature_en Enable this characteristic.
+ * @param humidity_en Enable this characteristic.
+ * @param pressure_en Enable this characteristic.
+ */
+ EnvironmentalService(BLE& _ble) :
+ ble(_ble),
+ temperatureCharacteristic(GattCharacteristic::UUID_TEMPERATURE_CHAR, &temperature),
+ humidityCharacteristic(GattCharacteristic::UUID_HUMIDITY_CHAR, &humidity),
+ pressureCharacteristic(GattCharacteristic::UUID_PRESSURE_CHAR, &pressure),
+ timeCharacteristic(GattCharacteristic::UUID_CURRENT_TIME_CHAR , &timeNow),
+ magnetoCharacteristic(MAGNETO_SERVICE_CHARACTERISTIC_UUID, &magneto),
+ acceleroCharacteristic(ACCELERO_SERVICE_CHARACTERISTIC_UUID, &accelero),
+ gyroCharacteristic(GYRO_SERVICE_CHARACTERISTIC_UUID, &gyro),
+ readState(READ_SERVICE_CHARACTERISTIC_UUID, &readValue)
+
+ {
+ static bool serviceAdded = false; /* We should only ever need to add the information service once. */
+ if (serviceAdded) {
+ return;
+ }
+
+ GattCharacteristic *charTable[] = { &humidityCharacteristic,
+ &pressureCharacteristic,
+ &temperatureCharacteristic,
+ &timeCharacteristic,
+ &readState,
+ &magnetoCharacteristic,
+ &acceleroCharacteristic,
+ &gyroCharacteristic };
+ GattService environmentalService(GattService::UUID_ENVIRONMENTAL_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
+
+ ble.gattServer().addService(environmentalService);
+ serviceAdded = true;
+ }
+
+ /**
+ * @brief Update humidity characteristic.
+ * @param newHumidityVal New humidity measurement.
+ */
+ void updateHumidity(HumidityType_t newHumidityVal)
+ {
+ humidity = (HumidityType_t) (newHumidityVal*100);
+ ble.gattServer().write(humidityCharacteristic.getValueHandle(), (uint8_t *) &humidity, sizeof(HumidityType_t));
+ }
+
+ void updateTime(TimeType_t newTimeVal)
+ {
+ timeNow = (TimeType_t) newTimeVal;
+ ble.gattServer().write(timeCharacteristic.getValueHandle(), (uint8_t *) &timeNow, sizeof(newTimeVal));
+ }
+
+ /**
+ * @brief Update pressure characteristic.
+ * @param newPressureVal New pressure measurement.
+ */
+ void updatePressure(PressureType_t newPressureVal)
+ {
+ pressure = (PressureType_t) (newPressureVal * 10);
+ ble.gattServer().write(pressureCharacteristic.getValueHandle(), (uint8_t *) &pressure, sizeof(PressureType_t));
+ }
+
+ /**
+ * @brief Update temperature characteristic.
+ * @param newTemperatureVal New temperature measurement.
+ */
+ void updateTemperature(float newTemperatureVal)
+ {
+ temperature = (TemperatureType_t) (newTemperatureVal * 100);
+ ble.gattServer().write(temperatureCharacteristic.getValueHandle(), (uint8_t *) &temperature, sizeof(TemperatureType_t));
+ }
+
+ void updateMagneto(Magneto_t newMagnetoVal)
+ {
+ ble.gattServer().write(magnetoCharacteristic.getValueHandle(), (uint8_t *) &magneto[0], sizeof(int16_t));
+ ble.gattServer().write(magnetoCharacteristic.getValueHandle(), (uint8_t *) &magneto[1], sizeof(int16_t));
+ ble.gattServer().write(magnetoCharacteristic.getValueHandle(), (uint8_t *) &magneto[2], sizeof(int16_t));
+ }
+
+ void updateAccelero(ACCELERO_t newAcceleroVal)
+ {
+ ble.gattServer().write(acceleroCharacteristic.getValueHandle(), (uint8_t *) &accelero[0], sizeof(int16_t));
+ ble.gattServer().write(acceleroCharacteristic.getValueHandle(), (uint8_t *) &accelero[1], sizeof(int16_t));
+ ble.gattServer().write(acceleroCharacteristic.getValueHandle(), (uint8_t *) &accelero[2], sizeof(int16_t));
+ }
+
+ void updateGyro(Gyro_t newGyroVal)
+ {
+ ble.gattServer().write(gyroCharacteristic.getValueHandle(), (uint8_t *) &gyro[0], sizeof(float));
+ ble.gattServer().write(gyroCharacteristic.getValueHandle(), (uint8_t *) &gyro[1], sizeof(float));
+ ble.gattServer().write(gyroCharacteristic.getValueHandle(), (uint8_t *) &gyro[2], sizeof(float));
+ }
+
+ GattAttribute::Handle_t getValueHandle() const
+ {
+ return readState.getValueHandle();
+ }
+
+
+
+
+private:
+ BLE& ble;
+
+ TemperatureType_t temperature;
+ HumidityType_t humidity;
+ PressureType_t pressure;
+ TimeType_t timeNow;
+ writeValue_t writeValue;
+ readValue_t readValue;
+ Magneto_t magneto;
+ ACCELERO_t accelero;
+ Gyro_t gyro;
+
+ ReadOnlyGattCharacteristic<TemperatureType_t> temperatureCharacteristic;
+ ReadOnlyGattCharacteristic<HumidityType_t> humidityCharacteristic;
+ ReadOnlyGattCharacteristic<PressureType_t> pressureCharacteristic;
+ ReadOnlyGattCharacteristic<TimeType_t> timeCharacteristic;
+ ReadOnlyGattCharacteristic<readValue_t> readState;
+ ReadOnlyGattCharacteristic<Magneto_t> magnetoCharacteristic;
+ ReadOnlyGattCharacteristic<ACCELERO_t> acceleroCharacteristic;
+ ReadOnlyGattCharacteristic<Gyro_t> gyroCharacteristic;
+};
+
+#endif /* #ifndef __BLE_ENVIRONMENTAL_SERVICE_H__*/
\ No newline at end of file
--- a/source/main.cpp Wed Jul 26 14:47:11 2017 +0200
+++ b/source/main.cpp Wed Jul 10 20:09:49 2019 +0000
@@ -1,33 +1,45 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2013 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
#include <events/mbed_events.h>
#include <mbed.h>
#include "ble/BLE.h"
-#include "LEDService.h"
+#include "Sensorservice.h"
+#include "stm32l475e_iot01_tsensor.h"
+#include "stm32l475e_iot01_hsensor.h"
+#include "stm32l475e_iot01_magneto.h"
+#include "stm32l475e_iot01_gyro.h"
+#include "stm32l475e_iot01_accelero.h"
+#include "stm32l475e_iot01_psensor.h"
+
+
+EnvironmentalService *SensorPtr;
+static float currentTemperature = 0.0;
+static float currentHumidity = 0.0;
+static float currentPressure = 0.0;
+static int16_t MagnetoXYZ[3] ={0};
+static int16_t AcceleroXYZ[3] ={0};
+static float GyroXYZ[3] ={0};
+
+Serial pc(USBTX,USBRX);
DigitalOut alivenessLED(LED1, 0);
DigitalOut actuatedLED(LED2, 0);
+DigitalOut led(LED1, 1);
+uint16_t customServiceUUID = 0xA000; /*Service*/
+uint16_t readCharUUID = 0xA001; /*Read*/
+uint16_t writeCharUUID = 0xA002; /*Write*/
-const static char DEVICE_NAME[] = "LED";
-static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID};
+const static char DEVICE_NAME[] = "Projet";
+static const uint16_t uuid16_list[] = {GattService::UUID_ENVIRONMENTAL_SERVICE};
+
-static EventQueue eventQueue(/* event count */ 10 * EVENTS_EVENT_SIZE);
+static uint8_t writeValue[10] = {0};
+WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue);
+
-LEDService *ledServicePtr;
+GattCharacteristic *characteristics[] = {&writeChar};
+GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
+
+static EventQueue eventQueue(10 * EVENTS_EVENT_SIZE);
+
void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
{
@@ -35,62 +47,92 @@
BLE::Instance().gap().startAdvertising();
}
-void blinkCallback(void)
-{
- alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */
+
+void updateTSensorValue(void) {
+ currentTemperature = BSP_TSENSOR_ReadTemp();
+ currentHumidity = BSP_HSENSOR_ReadHumidity();
+ currentPressure = BSP_PSENSOR_ReadPressure();
+ BSP_MAGNETO_GetXYZ(MagnetoXYZ);
+ BSP_GYRO_GetXYZ(GyroXYZ);
+ BSP_ACCELERO_AccGetXYZ(AcceleroXYZ);
+ SensorPtr->updateTemperature(currentTemperature);
+ SensorPtr->updateHumidity(currentHumidity);
+ SensorPtr->updatePressure(currentPressure);
+ SensorPtr->updateAccelero(AcceleroXYZ);
+ SensorPtr->updateMagneto(MagnetoXYZ);
+ SensorPtr->updateGyro(GyroXYZ);
}
-/**
- * This callback allows the LEDService to receive updates to the ledState Characteristic.
- *
- * @param[in] params
- * Information about the characterisitc being updated.
- */
-void onDataWrittenCallback(const GattWriteCallbackParams *params) {
- if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) {
- actuatedLED = *(params->data);
+
+void blinkCallback(void)
+{
+ alivenessLED = !alivenessLED;
+ if (BLE::Instance().gap().getState().connected) {
+ eventQueue.call(updateTSensorValue);
}
}
-/**
- * This function is called when the ble initialization process has failled
- */
+/********Lecture des donées et traitement selon leurs taille*******/
+void onDataWrittenCallback(const GattWriteCallbackParams *params) {
+ if ((params->handle == writeChar.getValueHandle())) {
+ if(params->len == 1) {
+ actuatedLED = *(params->data);
+ pc.printf("Data received: length = %d\n\r",params->len);
+ if(actuatedLED == 1){
+ pc.printf("LED is on\n\r");
+ }
+ else{
+ pc.printf("LED is off\n\r");
+ }
+ for(int x=0; x < params->len; x++) {
+ pc.printf("%x", params->data[x]);
+ pc.printf("\n\r");
+ }
+ }
+ else {
+ pc.printf("Data received: length = %d, data = 0x",params->len);
+ for(int x=0; x < params->len; x++) {
+ pc.printf("%x", params->data[x]);
+ }
+ pc.printf("\n\r");
+ }
+
+ }
+}
+
+
void onBleInitError(BLE &ble, ble_error_t error)
{
- /* Initialization error handling should go here */
+ pc.printf("Erreur\n\r");
}
-/**
- * Callback triggered when the ble initialization process has finished
- */
+
void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
{
BLE& ble = params->ble;
ble_error_t error = params->error;
if (error != BLE_ERROR_NONE) {
- /* In case of error, forward the error handling to onBleInitError */
onBleInitError(ble, error);
return;
}
- /* Ensure that it is the default instance of BLE */
if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
return;
}
ble.gap().onDisconnection(disconnectionCallback);
+ SensorPtr = new EnvironmentalService(ble);
ble.gattServer().onDataWritten(onDataWrittenCallback);
- bool initialValueForLEDCharacteristic = false;
- ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic);
- /* setup advertising */
+/************************** advertising***********************/
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
+ ble.addService(customService);
ble.gap().startAdvertising();
}
@@ -99,9 +141,19 @@
eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
}
+/******************************MAIN*******************************/
+
int main()
{
+ BSP_TSENSOR_Init();
+ BSP_HSENSOR_Init();
+ BSP_PSENSOR_Init();
+ BSP_MAGNETO_Init();
+ BSP_GYRO_Init();
+ BSP_ACCELERO_Init();
+ pc.printf("BLE is Working\n\r");
eventQueue.call_every(500, blinkCallback);
+
BLE &ble = BLE::Instance();
ble.onEventsToProcess(scheduleBleEventsProcessing);