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: X_NUCLEO_IKS01A2
Fork of Node_BLE_Switch_Device by
Bluetooth Low Energy enabled device with environmental and inertial features, i.e. "Pressure", "Humidity", "Temperature", "Accelerometer", "Gyroscope", and "Magnetometer", plus "Switch" feature, compatible with BlueST Protocol.
Diff: source/main.cpp
- Revision:
- 6:01d49589410e
- Parent:
- 5:445024130101
- Child:
- 7:2b5ed57b088c
diff -r 445024130101 -r 01d49589410e source/main.cpp
--- a/source/main.cpp Fri May 18 18:41:27 2018 +0000
+++ b/source/main.cpp Tue May 22 16:35:05 2018 +0000
@@ -74,8 +74,8 @@
DigitalOut led_state(LED1); //Conflicts with SPI CLK on D13.
/* Bluetooth. */
-const static char DEVICE_NAME[] = "SENSORS_DEVICE";
-const static uint8_t MANUFACTURER_SPECIFIC_DATA[]= {0x01,0x80,0x20,0x1c,0x00,0x00};
+const static char DEVICE_NAME[] = "IOT_DEVICE";
+const static uint8_t MANUFACTURER_SPECIFIC_DATA[]= {0x01,0x80,0x20,0xfc,0x00,0x00};
static EventQueue event_queue(/* event count */ 10 * EVENTS_EVENT_SIZE);
CustomService *custom_service;
@@ -86,12 +86,13 @@
static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
/* Components. */
-//static LSM303AGRMagSensor *magnetometer = mems_expansion_board->magnetometer;
-static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor;
-static LPS22HBSensor *press_temp = mems_expansion_board->pt_sensor;
-//static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro;
-//static LSM303AGRAccSensor *accelerometer = mems_expansion_board->accelerometer;
+static HTS221Sensor *hum_tem = mems_expansion_board->ht_sensor;
+static LPS22HBSensor *pre_tem = mems_expansion_board->pt_sensor;
+static LSM6DSLSensor *acc_gyr = mems_expansion_board->acc_gyro;
+static LSM303AGRMagSensor *mag = mems_expansion_board->magnetometer;
+//static LSM303AGRAccSensor *acc = mems_expansion_board->accelerometer;
+/* Timestamp. */
static uint16_t time_stamp = 0;
@@ -101,10 +102,39 @@
toggle_state = true;
}
-void get_sensors(float *pressure, float *humidity, float *temperature) {
- press_temp->get_pressure(pressure);
- hum_temp->get_humidity(humidity);
- hum_temp->get_temperature(temperature);
+void get_and_send_env_sensors() {
+ float pressure, humidity, temperature;
+
+ pre_tem->get_pressure(&pressure);
+ hum_tem->get_humidity(&humidity);
+ hum_tem->get_temperature(&temperature);
+
+ event_queue.call(Callback<void(
+ uint16_t,
+ float, float, float
+ )>(custom_service, &CustomService::send_env_sensors),
+ time_stamp++,
+ pressure, humidity, temperature
+ );
+ //printf("Env Sensors: (%d) %d %d %d\r\n", time_stamp, pressure, humidity, temperature);
+}
+
+void get_and_send_ine_sensors() {
+ static axes_t accelerometer, gyroscope, magnetometer;
+
+ acc_gyr->get_x_axes((int32_t *) &accelerometer);
+ acc_gyr->get_g_axes((int32_t *) &gyroscope);
+ mag->get_m_axes((int32_t *) &magnetometer);
+ //accelerometer->get_x_axes((int32_t *) (&accelerometer));
+
+ event_queue.call(Callback<void(
+ uint16_t,
+ axes_t*, axes_t*, axes_t*
+ )>(custom_service, &CustomService::send_ine_sensors),
+ time_stamp++,
+ &accelerometer, &gyroscope, &magnetometer
+ );
+ //printf("Ine Sensors: (%d) %d %d %d %d %d %d %d %d %d\r\n", time_stamp, accelerometer.x, accelerometer.y, accelerometer.z, gyroscope.x, gyroscope.y, gyroscope.z, magnetometer.x, magnetometer.y, magnetometer.z);
}
@@ -125,12 +155,13 @@
*/
void on_data_read_callback(const GattReadCallbackParams *params) {
/* Reading characteristic and sending it via bluetooth. */
- if (params->handle == custom_service->get_sensors_handle()) {
+ if (params->handle == custom_service->get_env_sensors_handle()) {
if (BLE::Instance().getGapState().connected) {
- float pressure, humidity, temperature;
- get_sensors(&pressure, &humidity, &temperature);
- event_queue.call(Callback<void(uint16_t, uint32_t, uint16_t, uint16_t)>(custom_service, &CustomService::send_sensors), time_stamp++, (uint32_t) (pressure * 10), (uint16_t) (humidity * 10), (uint16_t) (temperature * 10));
- //printf("Sensors: (%d) %d %d %d\r\n", time_stamp, (uint32_t) (pressure * 100), (uint16_t) (humidity * 10), (uint16_t) (temperature * 10));
+ get_and_send_env_sensors();
+ }
+ } else if (params->handle == custom_service->get_ine_sensors_handle()) {
+ if (BLE::Instance().getGapState().connected) {
+ get_and_send_ine_sensors();
}
}
}
@@ -164,10 +195,8 @@
/* Reading sensors and sending data via bluetooth. */
if (BLE::Instance().getGapState().connected) {
- float pressure, humidity, temperature;
- get_sensors(&pressure, &humidity, &temperature);
- event_queue.call(Callback<void(uint16_t, uint32_t, uint16_t, uint16_t)>(custom_service, &CustomService::send_sensors), time_stamp++, (uint32_t) (pressure * 10), (uint16_t) (humidity * 10), (uint16_t) (temperature * 10));
- //printf("Sensors: (%d) %d %d %d\r\n", time_stamp, (uint32_t) (pressure * 100), (uint16_t) (humidity * 10), (uint16_t) (temperature * 10));
+ get_and_send_env_sensors();
+ get_and_send_ine_sensors();
}
}
@@ -243,8 +272,12 @@
toggle_state = false;
/* Enabling sensors. */
- hum_temp->enable();
- press_temp->enable();
+ hum_tem->enable();
+ pre_tem->enable();
+ acc_gyr->enable_x();
+ acc_gyr->enable_g();
+ mag->enable();
+ //acc->enable();
/* Start. */
event_queue.dispatch_forever();
