|
| 1 | +/* |
| 2 | +
|
| 3 | + BleSensors_SensiBLE |
| 4 | +
|
| 5 | + This sketch provides an example of using the following SensiBLE features: |
| 6 | + - Environment sensors (humidity, temperature, pressure) |
| 7 | + - Motion sensors (accelerometer and gyroscope) |
| 8 | + - BLE communication |
| 9 | + |
| 10 | + You can use BlueNRG application provided by STMICROELECTRONICS to test this sketch. |
| 11 | + The name of bluetooth device is "BlueNRG". Scan devices with the application and |
| 12 | + connect your device. |
| 13 | +
|
| 14 | + Updating periods: |
| 15 | + Accelerometer and gyroscope values via BLE - 100 ms |
| 16 | + Environmental sensors - 1 second |
| 17 | + Printing values to the serial therminal - 1 second |
| 18 | +
|
| 19 | + More information about SensiBLE at https://www.sensiedge.com/ |
| 20 | +
|
| 21 | + */ |
| 22 | + |
| 23 | +#include <SPI.h> |
| 24 | +#include <SPBTLE_RF.h> |
| 25 | +#include <sensor_service.h> |
| 26 | + |
| 27 | +#include <LSM6DS3Sensor.h> |
| 28 | +#include <HTS221Sensor.h> |
| 29 | +#include <LPS25HBSensor.h> |
| 30 | + |
| 31 | +#define PIN_LED_GRN (13) |
| 32 | +#define PIN_SPI_MOSI (11) |
| 33 | +#define PIN_SPI_MISO (12) |
| 34 | +#define PIN_SPI_SCK (3) |
| 35 | + |
| 36 | +#define PIN_SPI_nCS (A1) |
| 37 | +#define PIN_SPI_RESET (7) |
| 38 | +#define PIN_SPI_IRQ (A0) |
| 39 | + |
| 40 | +#define PIN_BLE_LED (0xFF) |
| 41 | + |
| 42 | +#define SerialPort Serial |
| 43 | +#define DEV_I2C Wire |
| 44 | + |
| 45 | +// Configure BTLE_SPI |
| 46 | +SPIClass BTLE_SPI(PIN_SPI_MOSI, PIN_SPI_MISO, PIN_SPI_SCK); |
| 47 | + |
| 48 | +// Configure BTLE pins |
| 49 | +SPBTLERFClass BTLE(&BTLE_SPI, PIN_SPI_nCS, PIN_SPI_IRQ, PIN_SPI_RESET, PIN_BLE_LED); |
| 50 | + |
| 51 | +const char *name = "BlueNRG"; |
| 52 | +uint8_t SERVER_BDADDR[] = {0x12, 0x34, 0x00, 0xE1, 0x80, 0x03}; |
| 53 | + |
| 54 | +// Pointers for sensors objects |
| 55 | +LSM6DS3Sensor *AccGyr; |
| 56 | +HTS221Sensor *HumTemp; |
| 57 | +LPS25HBSensor *PressTemp; |
| 58 | + |
| 59 | +// Variables for sensors data |
| 60 | +float humidity, temperature; |
| 61 | +float pressure, temperatureP; |
| 62 | +int32_t accelerometer[3]; |
| 63 | +int32_t gyroscope[3]; |
| 64 | +AxesRaw_t axes_data; |
| 65 | + |
| 66 | +// Variables for update periods calculation |
| 67 | +uint32_t prevUpdMsec = 0; |
| 68 | +uint32_t prevLedMsec = 0; |
| 69 | +uint32_t envSensorsSkipCnt = 0; |
| 70 | +uint32_t printSkipCnt = 0; |
| 71 | + |
| 72 | +void setup() { |
| 73 | + int ret; |
| 74 | + |
| 75 | + pinMode(PIN_LED_GRN, OUTPUT); |
| 76 | + |
| 77 | + SerialPort.begin(115200); |
| 78 | + |
| 79 | + if(BTLE.begin() == SPBTLERF_ERROR) |
| 80 | + { |
| 81 | + SerialPort.println("Bluetooth module configuration error!"); |
| 82 | + while(1); |
| 83 | + } |
| 84 | + |
| 85 | + if(SensorService.begin(name, SERVER_BDADDR)) |
| 86 | + { |
| 87 | + SerialPort.println("Sensor service configuration error!"); |
| 88 | + while(1); |
| 89 | + } |
| 90 | + |
| 91 | + /* Configure the User Button in GPIO Mode */ |
| 92 | + pinMode(USER_BTN, INPUT); |
| 93 | + |
| 94 | + ret = SensorService.Add_Acc_Service(); |
| 95 | + |
| 96 | + if(ret == BLE_STATUS_SUCCESS) |
| 97 | + SerialPort.println("Acc service added successfully."); |
| 98 | + else |
| 99 | + SerialPort.println("Error while adding Acc service."); |
| 100 | + |
| 101 | + ret = SensorService.Add_Environmental_Sensor_Service(); |
| 102 | + |
| 103 | + if(ret == BLE_STATUS_SUCCESS) |
| 104 | + SerialPort.println("Environmental Sensor service added successfully."); |
| 105 | + else |
| 106 | + SerialPort.println("Error while adding Environmental Sensor service."); |
| 107 | + |
| 108 | + /* Instantiate Timer Service with two characteristics: |
| 109 | + * - seconds characteristic (Readable only) |
| 110 | + * - minutes characteristics (Readable and Notifiable ) |
| 111 | + */ |
| 112 | + ret = SensorService.Add_Time_Service(); |
| 113 | + |
| 114 | + if(ret == BLE_STATUS_SUCCESS) |
| 115 | + SerialPort.println("Time service added successfully."); |
| 116 | + else |
| 117 | + SerialPort.println("Error while adding Time service."); |
| 118 | + |
| 119 | + // Initialize I2C bus. |
| 120 | + DEV_I2C.begin(); |
| 121 | + SerialPort.println("Init I2C"); |
| 122 | + |
| 123 | + // Initlialize Components. |
| 124 | + AccGyr = new LSM6DS3Sensor(&DEV_I2C, LSM6DS3_ACC_GYRO_I2C_ADDRESS_LOW); |
| 125 | + AccGyr->Enable_X(); |
| 126 | + AccGyr->Enable_G(); |
| 127 | + SerialPort.println("Init Acc&Gyr"); |
| 128 | + |
| 129 | + HumTemp = new HTS221Sensor (&DEV_I2C); |
| 130 | + HumTemp->Enable(); |
| 131 | + SerialPort.println("Init Hum&Temp"); |
| 132 | + |
| 133 | + PressTemp = new LPS25HBSensor (&DEV_I2C); |
| 134 | + PressTemp->Enable(); |
| 135 | + SerialPort.println("Init Pressure&Temp"); |
| 136 | +} |
| 137 | + |
| 138 | +void loop() { |
| 139 | + |
| 140 | + Led_Blink(); |
| 141 | + |
| 142 | + BTLE.update(); |
| 143 | + |
| 144 | + // Blink LED depending on the connection state |
| 145 | + if(SensorService.isConnected() != TRUE) |
| 146 | + { |
| 147 | + // Keep the Bluetooth module in discoverable mode |
| 148 | + SensorService.setConnectable(); |
| 149 | + } |
| 150 | + |
| 151 | + // Update sensors values |
| 152 | + if((millis() - prevUpdMsec) >= 100) |
| 153 | + { |
| 154 | + prevUpdMsec = millis(); |
| 155 | + // Update environment data every N cycles |
| 156 | + if(++envSensorsSkipCnt >= 10) |
| 157 | + { |
| 158 | + envSensorsSkipCnt = 0; |
| 159 | + // Read humidity and temperature. |
| 160 | + HumTemp->GetHumidity(&humidity); |
| 161 | + HumTemp->GetTemperature(&temperature); |
| 162 | + |
| 163 | + // Read pressure and temperature. |
| 164 | + PressTemp->GetPressure(&pressure); |
| 165 | + PressTemp->GetTemperature(&temperatureP); |
| 166 | + } |
| 167 | + |
| 168 | + // Read accelerometer and gyroscope |
| 169 | + AccGyr->Get_X_Axes(accelerometer); |
| 170 | + AccGyr->Get_G_Axes(gyroscope); |
| 171 | + |
| 172 | + if(++printSkipCnt >= 10) |
| 173 | + { |
| 174 | + printSkipCnt = 0; |
| 175 | + // Print sensors values to serial port |
| 176 | + Print_Sensors(); |
| 177 | + } |
| 178 | + |
| 179 | + // Update BLE characteristics |
| 180 | + if(SensorService.isConnected() == TRUE) |
| 181 | + { |
| 182 | + // Update time |
| 183 | + SensorService.Update_Time_Characteristics(); |
| 184 | + |
| 185 | + // Update environnemental data |
| 186 | + SensorService.Temp_Update((temperature*10 + temperatureP*10)/2); |
| 187 | + SensorService.Press_Update(pressure*100); |
| 188 | + SensorService.Humidity_Update(humidity*10); |
| 189 | + |
| 190 | + // Update accelerometer data |
| 191 | + axes_data.AXIS_X = accelerometer[0]; |
| 192 | + axes_data.AXIS_Y = accelerometer[1]; |
| 193 | + axes_data.AXIS_Z = accelerometer[2]; |
| 194 | + SensorService.Acc_Update(&axes_data); |
| 195 | + } |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +void Print_Sensors() |
| 200 | +{ |
| 201 | + // Print HTS221 data |
| 202 | + SerialPort.println("------------------------"); |
| 203 | + SerialPort.println("------------------------"); |
| 204 | + SerialPort.print("Hum[%]: "); |
| 205 | + SerialPort.println(humidity, 2); |
| 206 | + SerialPort.print("Temp[C] (HTS221): "); |
| 207 | + SerialPort.println(temperature, 2); |
| 208 | + // Print LPS25 data |
| 209 | + SerialPort.print("Pres[hPa]: "); |
| 210 | + SerialPort.println(pressure, 2); |
| 211 | + SerialPort.print("TempP[C] (LPS25): "); |
| 212 | + SerialPort.println(temperatureP, 2); |
| 213 | + // Print accelerometer data |
| 214 | + SerialPort.println("------------------------"); |
| 215 | + SerialPort.println("Acc"); |
| 216 | + SerialPort.print("X: "); |
| 217 | + SerialPort.println(accelerometer[0]); |
| 218 | + SerialPort.print("Y: "); |
| 219 | + SerialPort.println(accelerometer[1]); |
| 220 | + SerialPort.print("Z: "); |
| 221 | + SerialPort.println(accelerometer[2]); |
| 222 | + // Print gyroscope data |
| 223 | + SerialPort.println("------------------------"); |
| 224 | + SerialPort.println("Gyr"); |
| 225 | + SerialPort.print("X: "); |
| 226 | + SerialPort.println(gyroscope[0]); |
| 227 | + SerialPort.print("Y: "); |
| 228 | + SerialPort.println(gyroscope[1]); |
| 229 | + SerialPort.print("Z: "); |
| 230 | + SerialPort.println(gyroscope[2]); |
| 231 | +} |
| 232 | + |
| 233 | +void Led_Blink() |
| 234 | +{ |
| 235 | + // Blink LED depending on the connection state |
| 236 | + if(SensorService.isConnected() == TRUE) |
| 237 | + { |
| 238 | + if(millis() - prevLedMsec > 1000) |
| 239 | + { |
| 240 | + prevLedMsec = millis(); |
| 241 | + digitalWrite(PIN_LED_GRN, HIGH); |
| 242 | + delay(10); |
| 243 | + digitalWrite(PIN_LED_GRN, LOW); |
| 244 | + } |
| 245 | + } |
| 246 | + else |
| 247 | + { |
| 248 | + if(millis() - prevLedMsec > 300) |
| 249 | + { |
| 250 | + prevLedMsec = millis(); |
| 251 | + digitalWrite(PIN_LED_GRN, HIGH); |
| 252 | + delay(10); |
| 253 | + digitalWrite(PIN_LED_GRN, LOW); |
| 254 | + } |
| 255 | + } |
| 256 | +} |
0 commit comments