Skip to content

Commit 78a7774

Browse files
authored
Merge pull request #2 from droid-technologies/SensiBLE
SensiBLE example was added
2 parents 44bafc5 + b566499 commit 78a7774

File tree

2 files changed

+266
-0
lines changed

2 files changed

+266
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ a swipe movement respectively from left to right and from right to left.
1818
* MemsMotorControl_IKS01A2_IHM02A1: This application provides a simple example of usage of a NUCLEO board (it was tested with NUCLEO-F401RE)
1919
with X-NUCLEO-IKS01A2 and X-NUCLEO-IHM02A1 Expansion Boards. The application allows to run a motor clockwise or counter-clockwise rotating the
2020
stack of boards accordingly; the speed of motor increases when the angle of rotation increases as well.
21+
* BleSensors_SensiBLE: This application provides an example of usage of a SensiBLE board. The application uses BLE along with environmental and motion sensors (humidity, temperature, pressure, accelerometer, gyroscope). The user can connect an Android or IOS device with BlueNRG application to SensiBLE and see sensors data. The data are also printed to the serial port.
2122

2223

2324
## Dependencies
@@ -33,6 +34,9 @@ The FP_Examples library requires the following STM32duino libraries:
3334
* STM32duino X-NUCLEO-53L0A1: https://github.com/stm32duino/X-NUCLEO-53L0A1
3435
* STM32duino X-NUCLEO-IHM02A1: https://github.com/stm32duino/X-NUCLEO-IHM02A1
3536
* STM32duino X-NUCLEO-LED61A1: https://github.com/stm32duino/X-NUCLEO-LED61A1
37+
* STM32duino HTS221: https://github.com/stm32duino/HTS221
38+
* STM32duino LPS25HB: https://github.com/stm32duino/LPS25HB
39+
* STM32duino LSM6DS3: https://github.com/stm32duino/LSM6DS3
3640

3741

3842
## Documentation
@@ -47,6 +51,9 @@ https://github.com/stm32duino/X-NUCLEO-6180XA1
4751
https://github.com/stm32duino/X-NUCLEO-53L0A1
4852
https://github.com/stm32duino/X-NUCLEO-IHM02A1
4953
https://github.com/stm32duino/X-NUCLEO-LED61A1
54+
https://github.com/stm32duino/HTS221
55+
https://github.com/stm32duino/LPS25HB
56+
https://github.com/stm32duino/LSM6DS3
5057

5158
The datasheets of the several components are available at
5259
http://www.st.com/content/st_com/en/products/mems-and-sensors/inemo-inertial-modules/lsm6dsl.html
@@ -55,6 +62,9 @@ http://www.st.com/content/st_com/en/products/imaging-and-photonics-solutions/pro
5562
http://www.st.com/content/st_com/en/products/wireless-connectivity/bluetooth-bluetooth-low-energy/spbtle-rf.html
5663
http://www.st.com/content/st_com/en/products/motor-drivers/stepper-motor-drivers/l6470.html
5764
http://www.st.com/content/st_com/en/products/power-management/led-drivers/boost-current-regulators-for-led/led6001.html
65+
http://www.st.com/content/st_com/en/products/mems-and-sensors/humidity-sensors/hts221.html
66+
http://www.st.com/content/st_com/en/products/mems-and-sensors/pressure-sensors/lps25hb.html
67+
http://www.st.com/content/st_com/en/products/mems-and-sensors/inemo-inertial-modules/lsm6ds3.html
5868

5969

6070

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
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

Comments
 (0)