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.
Revision 7:2b5ed57b088c, committed 2018-08-06
- Comitter:
- Davidroid
- Date:
- Mon Aug 06 09:57:49 2018 +0000
- Parent:
- 6:01d49589410e
- Commit message:
- Correct Custom Service UUID
Changed in this revision
| source/CustomService.h | Show annotated file Show diff for this revision Revisions of this file |
| source/main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/source/CustomService.h Tue May 22 16:35:05 2018 +0000
+++ b/source/CustomService.h Mon Aug 06 09:57:49 2018 +0000
@@ -50,8 +50,7 @@
/* Definitions ---------------------------------------------------------------*/
#define TIMESTAMP_LENGTH (sizeof(uint16_t))
-#define STATE_LENGTH (sizeof(uint8_t))
-#define COMMAND_LENGTH (sizeof(uint8_t))
+#define SWITCH_LENGTH (sizeof(uint8_t))
#define PRESSURE_LENGTH (sizeof(uint32_t))
#define HUMIDITY_LENGTH (sizeof(uint16_t))
#define TEMPERATURE_LENGTH (sizeof(uint16_t))
@@ -59,8 +58,8 @@
#define GYROSCOPE_LENGTH (sizeof(uint16_t))
#define MAGNETOMETER_LENGTH (sizeof(uint16_t))
-#define STATE_DATA_LENGTH (TIMESTAMP_LENGTH + STATE_LENGTH)
-#define COMMAND_DATA_LENGTH (COMMAND_LENGTH)
+#define SWITCH_DATA_INDEX (TIMESTAMP_LENGTH)
+#define SWITCH_DATA_LENGTH (TIMESTAMP_LENGTH + SWITCH_LENGTH)
#define ENV_SENSORS_DATA_LENGTH (TIMESTAMP_LENGTH + PRESSURE_LENGTH + HUMIDITY_LENGTH + TEMPERATURE_LENGTH)
#define INE_SENSORS_DATA_LENGTH (TIMESTAMP_LENGTH + (3 * ACCELEROMETER_LENGTH) + (3 * GYROSCOPE_LENGTH) + (3 * MAGNETOMETER_LENGTH))
@@ -78,7 +77,7 @@
/* Constants -----------------------------------------------------------------*/
-const UUID::LongUUIDBytes_t CUSTOM_SERVICE_UUID = {0x00,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
+const UUID::LongUUIDBytes_t CUSTOM_SERVICE_UUID = {0x00,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0x9a,0xb4,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
const UUID::LongUUIDBytes_t CUSTOM_SWITCH_CHARACTERISTIC_UUID = {0x20,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
const UUID::LongUUIDBytes_t CUSTOM_ENV_SENSORS_CHARACTERISTIC_UUID = {0x00,0x1c,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
const UUID::LongUUIDBytes_t CUSTOM_INE_SENSORS_CHARACTERISTIC_UUID = {0x00,0xe0,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
@@ -98,7 +97,7 @@
CustomService(BLEDevice &_ble) :
ble(_ble),
- state_command(CUSTOM_SWITCH_CHARACTERISTIC_UUID, packed_state_command, STATE_DATA_LENGTH, STATE_DATA_LENGTH,
+ state_command(CUSTOM_SWITCH_CHARACTERISTIC_UUID, packed_state_command, SWITCH_DATA_LENGTH, SWITCH_DATA_LENGTH,
/*GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | */GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
env_sensors(CUSTOM_ENV_SENSORS_CHARACTERISTIC_UUID, packed_env_sensors, ENV_SENSORS_DATA_LENGTH, ENV_SENSORS_DATA_LENGTH,
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
@@ -108,16 +107,16 @@
GattCharacteristic *char_table[] = {&state_command, &env_sensors, &ine_sensors};
GattService iot_service(CUSTOM_SERVICE_UUID, char_table, sizeof(char_table) / sizeof(GattCharacteristic *));
ble.addService(iot_service);
- memset (packed_state_command, 0, STATE_DATA_LENGTH);
+ memset (packed_state_command, 0, SWITCH_DATA_LENGTH);
memset (packed_env_sensors, 0, ENV_SENSORS_DATA_LENGTH);
memset (packed_ine_sensors, 0, INE_SENSORS_DATA_LENGTH);
}
void send_state(uint16_t time_stamp, uint8_t current_state) {
- memset (packed_state_command, 0, STATE_DATA_LENGTH);
+ memset (packed_state_command, 0, SWITCH_DATA_LENGTH);
STORE_LE_16(packed_state_command, time_stamp);
- packed_state_command[TIMESTAMP_LENGTH] = current_state;
- ble.gattServer().write(get_state_command_handle(), (uint8_t *) &packed_state_command, STATE_DATA_LENGTH, 0);
+ packed_state_command[SWITCH_DATA_INDEX] = current_state;
+ ble.gattServer().write(get_state_command_handle(), (uint8_t *) &packed_state_command, SWITCH_DATA_LENGTH, 0);
}
void send_env_sensors(
@@ -184,7 +183,7 @@
GattCharacteristic state_command;
GattCharacteristic env_sensors;
GattCharacteristic ine_sensors;
- uint8_t packed_state_command[STATE_DATA_LENGTH];
+ uint8_t packed_state_command[SWITCH_DATA_LENGTH];
uint8_t packed_env_sensors[ENV_SENSORS_DATA_LENGTH];
uint8_t packed_ine_sensors[INE_SENSORS_DATA_LENGTH];
};
--- a/source/main.cpp Tue May 22 16:35:05 2018 +0000
+++ b/source/main.cpp Mon Aug 06 09:57:49 2018 +0000
@@ -67,7 +67,8 @@
/* Variables -----------------------------------------------------------------*/
-/* Button state. */
+/* Switch and toggle state. */
+CustomService::switch_state_t switch_state;
bool toggle_state;
/* LED to indicate system state. */
@@ -99,6 +100,7 @@
/* Custom service related functions ------------------------------------------*/
void button_callback(void) {
+ switch_state = (CustomService::switch_state_t) (CustomService::SWITCH_ON - switch_state);
toggle_state = true;
}
@@ -155,6 +157,13 @@
*/
void on_data_read_callback(const GattReadCallbackParams *params) {
/* Reading characteristic and sending it via bluetooth. */
+/*
+ if (params->handle == custom_service->get_state_command_handle()) {
+ if (BLE::Instance().getGapState().connected) {
+ event_queue.call(Callback<void(uint16_t, uint8_t)>(custom_service, &CustomService::send_state), time_stamp++, led_state.read() ? CustomService::SWITCH_ON : CustomService::SWITCH_OFF);
+ }
+ } else
+*/
if (params->handle == custom_service->get_env_sensors_handle()) {
if (BLE::Instance().getGapState().connected) {
get_and_send_env_sensors();
@@ -176,7 +185,7 @@
void on_data_written_callback(const GattWriteCallbackParams *params) {
/* Receiving data via bluetooth and writing it to the characteristic. */
if (params->handle == custom_service->get_state_command_handle()) {
- led_state.write(((CustomService::switch_state_t) ((uint8_t *) (params->data))[0]));
+ led_state.write(((CustomService::switch_state_t) ((uint8_t *) (params->data))[SWITCH_DATA_INDEX]));
}
}
@@ -188,7 +197,7 @@
/* Reading toggle command and sending it via bluetooth, if needed. */
if (toggle_state) {
if (BLE::Instance().getGapState().connected) {
- event_queue.call(Callback<void(uint16_t, uint8_t)>(custom_service, &CustomService::send_state), time_stamp++, toggle_state ? TOGGLE_ON : TOGGLE_OFF);
+ event_queue.call(Callback<void(uint16_t, uint8_t)>(custom_service, &CustomService::send_state), time_stamp++, switch_state);
toggle_state = false;
}
}
