Silica Sensor Node board sends custom ble advertising for the Gateway board

Dependencies:   X_NUCLEO_IKS01A2_SPI3W

Fork of sensor-node-ble by Roberto Spelta

Getting started with mbed SensorNode BLE

Information

History project:

  • 26/01/2017 - First Release

This project uses the Bluetooth Low Energy in order to send advertaisments every second. These payloads are read by the Visible Things Gateway board, more about it here . Every advertaisments contains sensors data read form the SensorTile and the Silica Sensor Board. For details please read the document in the MAN folder,

The application:

  • reads sensors data (accelerometer, gyroscope, lux, pressure, temperature, proximity, magnetometer)
  • send these data by BLE

You can compile this project in three ways:

1. Using the Online compiler.

Information

Learn how to use the Online compiler reading https://docs.mbed.com/docs/mbed-os-handbook/en/latest/dev_tools/online_comp/ page.

2. Using the compiler on your PC

Information

Learn how to use the mbed-cli reading https://docs.mbed.com/docs/mbed-os-handbook/en/latest/dev_tools/cli/ page.
The name of the machine is SILICA_SENSOR_NODE.

3. Exporting to 3rd party tools (IDE)

Information

Learn how to use the mbed-cli reading https://docs.mbed.com/docs/mbed-os-handbook/en/latest/dev_tools/third_party/ page. We have exported the project for you, please read here

Warning

This example requires a Visible Things Gateway board. If you don't have this board you can use RF Connect app from an Android phone just to see the raw data sent from the SensorNode.

Committer:
rspelta
Date:
Fri Jan 26 18:26:48 2018 +0100
Revision:
5:26b07c500aa2
Parent:
3:94aee6f716b7
fix comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rspelta 0:6a77043d1ee1 1 /* mbed Microcontroller Library
rspelta 0:6a77043d1ee1 2 * Copyright (c) 2006-2015 ARM Limited
rspelta 0:6a77043d1ee1 3 *
rspelta 0:6a77043d1ee1 4 * Licensed under the Apache License, Version 2.0 (the "License");
rspelta 0:6a77043d1ee1 5 * you may not use this file except in compliance with the License.
rspelta 0:6a77043d1ee1 6 * You may obtain a copy of the License at
rspelta 0:6a77043d1ee1 7 *
rspelta 0:6a77043d1ee1 8 * http://www.apache.org/licenses/LICENSE-2.0
rspelta 0:6a77043d1ee1 9 *
rspelta 0:6a77043d1ee1 10 * Unless required by applicable law or agreed to in writing, software
rspelta 0:6a77043d1ee1 11 * distributed under the License is distributed on an "AS IS" BASIS,
rspelta 0:6a77043d1ee1 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rspelta 0:6a77043d1ee1 13 * See the License for the specific language governing permissions and
rspelta 0:6a77043d1ee1 14 * limitations under the License.
rspelta 0:6a77043d1ee1 15 */
rspelta 0:6a77043d1ee1 16
rspelta 0:6a77043d1ee1 17 #include <events/mbed_events.h>
rspelta 0:6a77043d1ee1 18 #include <mbed.h>
rspelta 0:6a77043d1ee1 19 #include "ble/BLE.h"
rspelta 0:6a77043d1ee1 20 #include "ble/Gap.h"
rspelta 0:6a77043d1ee1 21 #include "ble/services/advGateway.h"
rspelta 0:6a77043d1ee1 22
rspelta 0:6a77043d1ee1 23 #include "LPS22HBSensor.h"
rspelta 0:6a77043d1ee1 24 #include "LSM6DSLSensor.h"
rspelta 0:6a77043d1ee1 25 #include "LSM303AGRMagSensor.h"
rspelta 0:6a77043d1ee1 26 #include "LSM303AGRAccSensor.h"
rspelta 0:6a77043d1ee1 27 #include "SPI3W.h"
rspelta 3:94aee6f716b7 28 #include "VL6180X.h"
rspelta 3:94aee6f716b7 29
rspelta 3:94aee6f716b7 30 #define VERSION_SW "1.0"
rspelta 1:f355c8472bdf 31
rspelta 1:f355c8472bdf 32 SPI3W sens_intf(PB_15, NC, PB_13); // 3W mosi, sclk on Nucleo L476 same as BTLE
rspelta 1:f355c8472bdf 33 LPS22HBSensor press_temp(&sens_intf, PA_3); // on SensorTile L476JG
rspelta 1:f355c8472bdf 34 LSM6DSLSensor acc_gyro(&sens_intf, PB_12, NC, PA_2 ); // on SensorTile L476JG
rspelta 1:f355c8472bdf 35 LSM303AGRMagSensor mag(&sens_intf, PB_1 ); //on SensorTile L476JG
rspelta 3:94aee6f716b7 36 LSM303AGRAccSensor acc(&sens_intf, PC_4 ); //on SensorTile L476JG
rspelta 3:94aee6f716b7 37 I2C _i2c(I2C_SDA, I2C_SCL);
rspelta 3:94aee6f716b7 38 VL6180x sensor(&_i2c, VL6180X_ADDRESS); //on Sensor VL6180X
rspelta 3:94aee6f716b7 39
rspelta 1:f355c8472bdf 40 BLE &ble = BLE::Instance();
rspelta 1:f355c8472bdf 41
rspelta 1:f355c8472bdf 42 /* FORMAT
rspelta 1:f355c8472bdf 43 0x19, 0x10 CompanyID
rspelta 0:6a77043d1ee1 44 0x58 Temperature
rspelta 0:6a77043d1ee1 45 T signed value from -20 to +40 celsius
rspelta 2:934cd97bfe1a 46 0xB8 Pressure
rspelta 3:94aee6f716b7 47 H 2’complement value in units of 1mbar
rspelta 0:6a77043d1ee1 48 0x18 Accelerometer
rspelta 0:6a77043d1ee1 49 X 2’complement value in units of 0.01g
rspelta 0:6a77043d1ee1 50 Y 2’complement value in units of 0.01g
rspelta 0:6a77043d1ee1 51 Z 2’complement value in units of 0.01g
rspelta 0:6a77043d1ee1 52 0x38 Magnetometer
rspelta 0:6a77043d1ee1 53 X 2’ complement value in units of 10-6T
rspelta 0:6a77043d1ee1 54 Y 2’ complement value in units of 10-6T
rspelta 0:6a77043d1ee1 55 Z 2’ complement value in units of 10-6T
rspelta 1:f355c8472bdf 56 0x28 Gyroscope (it is not showed on the gateway)
rspelta 0:6a77043d1ee1 57 X 2’ complement value in units od 0.1deg/s
rspelta 0:6a77043d1ee1 58 Y 2’ complement value in units od 0.1deg/s
rspelta 0:6a77043d1ee1 59 Z 2’ complement value in units od 0.1deg/s
rspelta 0:6a77043d1ee1 60 0x48 Ambient light & Proximity
rspelta 3:94aee6f716b7 61 A units ok klux
rspelta 3:94aee6f716b7 62 P mm
rspelta 0:6a77043d1ee1 63 */
rspelta 2:934cd97bfe1a 64 uint8_t uuid[] = { 0x19, 0x10, 0x58, 0x00, 0xBB, 0x00, 0x18, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00};
rspelta 0:6a77043d1ee1 65
rspelta 0:6a77043d1ee1 66 static advGateway* advGatewayPtr;
rspelta 0:6a77043d1ee1 67
rspelta 0:6a77043d1ee1 68 static EventQueue eventQueue(/* event count */ 4 * EVENTS_EVENT_SIZE);
rspelta 0:6a77043d1ee1 69
rspelta 0:6a77043d1ee1 70 /**
rspelta 0:6a77043d1ee1 71 * This function is called when the ble initialization process has failled
rspelta 0:6a77043d1ee1 72 */
rspelta 0:6a77043d1ee1 73 void onBleInitError(BLE &ble, ble_error_t error)
rspelta 0:6a77043d1ee1 74 {
rspelta 0:6a77043d1ee1 75 /* Initialization error handling should go here */
rspelta 0:6a77043d1ee1 76 }
rspelta 0:6a77043d1ee1 77
rspelta 0:6a77043d1ee1 78 void printMacAddress()
rspelta 0:6a77043d1ee1 79 {
rspelta 0:6a77043d1ee1 80 /* Print out device MAC address to the console*/
rspelta 0:6a77043d1ee1 81 Gap::AddressType_t addr_type;
rspelta 0:6a77043d1ee1 82 Gap::Address_t address;
rspelta 0:6a77043d1ee1 83 BLE::Instance().gap().getAddress(&addr_type, address);
rspelta 0:6a77043d1ee1 84 printf("DEVICE MAC ADDRESS: ");
rspelta 0:6a77043d1ee1 85 for (int i = 5; i >= 1; i--){
rspelta 0:6a77043d1ee1 86 printf("%02x:", address[i]);
rspelta 0:6a77043d1ee1 87 }
rspelta 0:6a77043d1ee1 88 printf("%02x\r\n", address[0]);
rspelta 0:6a77043d1ee1 89 }
rspelta 0:6a77043d1ee1 90
rspelta 0:6a77043d1ee1 91 /**
rspelta 0:6a77043d1ee1 92 * Callback triggered when the ble initialization process has finished
rspelta 0:6a77043d1ee1 93 */
rspelta 0:6a77043d1ee1 94 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
rspelta 0:6a77043d1ee1 95 {
rspelta 0:6a77043d1ee1 96 BLE& ble = params->ble;
rspelta 0:6a77043d1ee1 97 ble_error_t error = params->error;
rspelta 0:6a77043d1ee1 98
rspelta 0:6a77043d1ee1 99 if (error != BLE_ERROR_NONE) {
rspelta 0:6a77043d1ee1 100 /* In case of error, forward the error handling to onBleInitError */
rspelta 0:6a77043d1ee1 101 onBleInitError(ble, error);
rspelta 0:6a77043d1ee1 102 return;
rspelta 0:6a77043d1ee1 103 }
rspelta 0:6a77043d1ee1 104
rspelta 0:6a77043d1ee1 105 /* Ensure that it is the default instance of BLE */
rspelta 0:6a77043d1ee1 106 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
rspelta 0:6a77043d1ee1 107 return;
rspelta 0:6a77043d1ee1 108 }
rspelta 0:6a77043d1ee1 109
rspelta 0:6a77043d1ee1 110 advGatewayPtr = new advGateway(ble, uuid);
rspelta 1:f355c8472bdf 111
rspelta 0:6a77043d1ee1 112 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
rspelta 0:6a77043d1ee1 113 ble.gap().startAdvertising();
rspelta 0:6a77043d1ee1 114
rspelta 0:6a77043d1ee1 115 printMacAddress();
rspelta 0:6a77043d1ee1 116 }
rspelta 0:6a77043d1ee1 117
rspelta 0:6a77043d1ee1 118 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
rspelta 0:6a77043d1ee1 119 BLE &ble = BLE::Instance();
rspelta 0:6a77043d1ee1 120 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
rspelta 0:6a77043d1ee1 121 }
rspelta 0:6a77043d1ee1 122
rspelta 0:6a77043d1ee1 123 void readSensors(void)
rspelta 0:6a77043d1ee1 124 {
rspelta 3:94aee6f716b7 125 float temp, press, lux, prox;
rspelta 0:6a77043d1ee1 126 int32_t mag_axes[3], acc_axes[3], gyr_axes[3];
rspelta 0:6a77043d1ee1 127
rspelta 0:6a77043d1ee1 128 press_temp.get_temperature(&temp);
rspelta 0:6a77043d1ee1 129 uuid[3] = (int8_t)temp;
rspelta 0:6a77043d1ee1 130
rspelta 2:934cd97bfe1a 131 press_temp.get_pressure(&press);
rspelta 2:934cd97bfe1a 132 uuid[5] = (int8_t)(press/8);
rspelta 2:934cd97bfe1a 133 //printf("LPS22HB: [temp] %7f C, [press] %f mbar\r\n", temp, press);
rspelta 0:6a77043d1ee1 134
rspelta 0:6a77043d1ee1 135 mag.get_m_axes(mag_axes);
rspelta 1:f355c8472bdf 136 uuid[11] = (int8_t)((mag_axes[0]*1.5)/10);
rspelta 1:f355c8472bdf 137 uuid[12] = (int8_t)((mag_axes[1]*1.5)/10);
rspelta 1:f355c8472bdf 138 uuid[13] = (int8_t)((mag_axes[2]*1.5)/10);
rspelta 1:f355c8472bdf 139 //printf("LSM303AGR [mag/mgauss]: %d, %d, %d\r\n", uuid[11], uuid[12], uuid[13]); //*1.5=1mG
rspelta 0:6a77043d1ee1 140
rspelta 1:f355c8472bdf 141 acc_gyro.get_x_axes(acc_axes);
rspelta 1:f355c8472bdf 142 uuid[7] = (int8_t)((acc_axes[0]*100)/1024);
rspelta 1:f355c8472bdf 143 uuid[8] = (int8_t)((acc_axes[1]*100)/1024);
rspelta 1:f355c8472bdf 144 uuid[9] = (int8_t)((acc_axes[2]*100)/1024);
rspelta 1:f355c8472bdf 145 //printf("LSM6DSL [acc/mg]: %02x, %02x, %02x\r\n", acc_axes[0], acc_axes[1],acc_axes[2]); // 1024 = 1G
rspelta 0:6a77043d1ee1 146
rspelta 1:f355c8472bdf 147 acc_gyro.get_g_axes(gyr_axes); // not used by the gateway
rspelta 0:6a77043d1ee1 148 uuid[15] = (int8_t)gyr_axes[0];
rspelta 0:6a77043d1ee1 149 uuid[16] = (int8_t)gyr_axes[1];
rspelta 0:6a77043d1ee1 150 uuid[17] = (int8_t)gyr_axes[2];
rspelta 1:f355c8472bdf 151 //printf("LSM6DSL [gyro/mdps]: %6ld, %6ld, %6ld\r\n", uuid[15], uuid[16], uuid[17]);
rspelta 3:94aee6f716b7 152
rspelta 3:94aee6f716b7 153 lux = sensor.getAmbientLight(GAIN_1);
rspelta 3:94aee6f716b7 154 prox = sensor.getDistance();
rspelta 3:94aee6f716b7 155 uuid[19] = (int8_t)(lux/10);
rspelta 3:94aee6f716b7 156 // uuid[20] = (int8_t)prox; not used, mm unit
rspelta 0:6a77043d1ee1 157 }
rspelta 0:6a77043d1ee1 158
rspelta 0:6a77043d1ee1 159 void getDataSensors( void )
rspelta 0:6a77043d1ee1 160 {
rspelta 0:6a77043d1ee1 161 ble.gap().stopAdvertising();
rspelta 0:6a77043d1ee1 162 readSensors();
rspelta 0:6a77043d1ee1 163 advGatewayPtr->updatePayload( uuid );
rspelta 0:6a77043d1ee1 164 ble.gap().startAdvertising();
rspelta 0:6a77043d1ee1 165 }
rspelta 0:6a77043d1ee1 166
rspelta 0:6a77043d1ee1 167 int main()
rspelta 0:6a77043d1ee1 168 {
rspelta 3:94aee6f716b7 169 VL6180xIdentification identification;
rspelta 3:94aee6f716b7 170
rspelta 3:94aee6f716b7 171 printf("Silica Sensor Node BLE v%s\n\r", VERSION_SW);
rspelta 3:94aee6f716b7 172
rspelta 3:94aee6f716b7 173 sensor.VL6180xInit();
rspelta 3:94aee6f716b7 174 sensor.VL6180xDefautSettings(); //Load default settings to get started.
rspelta 3:94aee6f716b7 175
rspelta 3:94aee6f716b7 176 press_temp.enable();
rspelta 0:6a77043d1ee1 177
rspelta 0:6a77043d1ee1 178 acc_gyro.enable_x();
rspelta 0:6a77043d1ee1 179 acc_gyro.enable_g();
rspelta 0:6a77043d1ee1 180 mag.enable();
rspelta 0:6a77043d1ee1 181 acc.enable();
rspelta 0:6a77043d1ee1 182
rspelta 0:6a77043d1ee1 183 uint8_t id22=0, idLSM6=0, id303mag=0, id303acc=0;
rspelta 0:6a77043d1ee1 184 press_temp.read_id(&id22);
rspelta 0:6a77043d1ee1 185 acc_gyro.read_id(&idLSM6);
rspelta 0:6a77043d1ee1 186 mag.read_id(&id303mag);
rspelta 0:6a77043d1ee1 187 acc.read_id(&id303acc);
rspelta 3:94aee6f716b7 188 sensor.getIdentification(&identification);
rspelta 0:6a77043d1ee1 189
rspelta 3:94aee6f716b7 190 printf("LS303acc ID %x LS303mag ID %x LSM6DSL ID %x LPS22HB ID %x VL6180x ID %x\r\n", id303acc, id303mag, idLSM6, id22, identification.idModel);
rspelta 0:6a77043d1ee1 191
rspelta 1:f355c8472bdf 192 eventQueue.call_every( 500, getDataSensors );
rspelta 1:f355c8472bdf 193 ble.onEventsToProcess(scheduleBleEventsProcessing);
rspelta 1:f355c8472bdf 194 ble.init(bleInitComplete);
rspelta 0:6a77043d1ee1 195
rspelta 1:f355c8472bdf 196 eventQueue.dispatch_forever();
rspelta 0:6a77043d1ee1 197
rspelta 1:f355c8472bdf 198 return 0;
rspelta 0:6a77043d1ee1 199 }