a sensor hub for monitoring home environment

Dependencies:   HTS221 LIS3MDL LPS22HB LSM303AGR LSM6DSL VL53L0X picojson

Fork of HelloWorld_ST_Sensors by ST

Committer:
nikapov
Date:
Thu Sep 07 12:31:15 2017 +0000
Revision:
0:6a97487a0ab3
Child:
1:0f7838527fef
First version.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nikapov 0:6a97487a0ab3 1 /**
nikapov 0:6a97487a0ab3 2 ******************************************************************************
nikapov 0:6a97487a0ab3 3 * @file main.cpp
nikapov 0:6a97487a0ab3 4 * @author CLab
nikapov 0:6a97487a0ab3 5 * @version V1.0.0
nikapov 0:6a97487a0ab3 6 * @date 5-September-2017
nikapov 0:6a97487a0ab3 7 * @brief Simple Example application for using X_NUCLEO_IKS01A2
nikapov 0:6a97487a0ab3 8 * MEMS Inertial & Environmental Sensor Nucleo expansion and
nikapov 0:6a97487a0ab3 9 * B-L475E-IOT01A2 boards.
nikapov 0:6a97487a0ab3 10 ******************************************************************************
nikapov 0:6a97487a0ab3 11 * @attention
nikapov 0:6a97487a0ab3 12 *
nikapov 0:6a97487a0ab3 13 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
nikapov 0:6a97487a0ab3 14 *
nikapov 0:6a97487a0ab3 15 * Redistribution and use in source and binary forms, with or without modification,
nikapov 0:6a97487a0ab3 16 * are permitted provided that the following conditions are met:
nikapov 0:6a97487a0ab3 17 * 1. Redistributions of source code must retain the above copyright notice,
nikapov 0:6a97487a0ab3 18 * this list of conditions and the following disclaimer.
nikapov 0:6a97487a0ab3 19 * 2. Redistributions in binary form must reproduce the above copyright notice,
nikapov 0:6a97487a0ab3 20 * this list of conditions and the following disclaimer in the documentation
nikapov 0:6a97487a0ab3 21 * and/or other materials provided with the distribution.
nikapov 0:6a97487a0ab3 22 * 3. Neither the name of STMicroelectronics nor the names of its contributors
nikapov 0:6a97487a0ab3 23 * may be used to endorse or promote products derived from this software
nikapov 0:6a97487a0ab3 24 * without specific prior written permission.
nikapov 0:6a97487a0ab3 25 *
nikapov 0:6a97487a0ab3 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
nikapov 0:6a97487a0ab3 27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
nikapov 0:6a97487a0ab3 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
nikapov 0:6a97487a0ab3 29 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
nikapov 0:6a97487a0ab3 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
nikapov 0:6a97487a0ab3 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
nikapov 0:6a97487a0ab3 32 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
nikapov 0:6a97487a0ab3 33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
nikapov 0:6a97487a0ab3 34 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
nikapov 0:6a97487a0ab3 35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
nikapov 0:6a97487a0ab3 36 *
nikapov 0:6a97487a0ab3 37 ******************************************************************************
nikapov 0:6a97487a0ab3 38 */
nikapov 0:6a97487a0ab3 39
nikapov 0:6a97487a0ab3 40 /* Includes */
nikapov 0:6a97487a0ab3 41 #include "mbed.h"
nikapov 0:6a97487a0ab3 42 #include "HTS221Sensor.h"
nikapov 0:6a97487a0ab3 43 #include "LPS22HBSensor.h"
nikapov 0:6a97487a0ab3 44 #include "LSM6DSLSensor.h"
nikapov 0:6a97487a0ab3 45
nikapov 0:6a97487a0ab3 46 #ifdef TARGET_DISCO_L475VG_IOT01A
nikapov 0:6a97487a0ab3 47 #include "lis3mdl_class.h"
nikapov 0:6a97487a0ab3 48 #else // X-Nucleo-IKS01A2
nikapov 0:6a97487a0ab3 49 #include "LSM303AGRMagSensor.h"
nikapov 0:6a97487a0ab3 50 #endif
nikapov 0:6a97487a0ab3 51
nikapov 0:6a97487a0ab3 52
nikapov 0:6a97487a0ab3 53 /* Retrieve the composing elements of the expansion board */
nikapov 0:6a97487a0ab3 54
nikapov 0:6a97487a0ab3 55 #ifdef TARGET_DISCO_L475VG_IOT01A
nikapov 0:6a97487a0ab3 56 static DevI2C devI2c = DevI2C(PB_11,PB_10);
nikapov 0:6a97487a0ab3 57 #else // X-Nucleo-IKS01A2
nikapov 0:6a97487a0ab3 58 static DevI2C devI2c = DevI2C(D14,D15);
nikapov 0:6a97487a0ab3 59 #endif
nikapov 0:6a97487a0ab3 60
nikapov 0:6a97487a0ab3 61 static HTS221Sensor hum_temp = HTS221Sensor(devI2c);
nikapov 0:6a97487a0ab3 62 static LPS22HBSensor press_temp = LPS22HBSensor(devI2c);
nikapov 0:6a97487a0ab3 63
nikapov 0:6a97487a0ab3 64 #ifdef TARGET_DISCO_L475VG_IOT01A
nikapov 0:6a97487a0ab3 65 static LSM6DSLSensor acc_gyro = LSM6DSLSensor(devI2c,PD_11,NC,LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW); // low address
nikapov 0:6a97487a0ab3 66 static LIS3MDL magnetometer = LIS3MDL(devI2c);
nikapov 0:6a97487a0ab3 67 #else // X-NUCLEO_IKS01A2
nikapov 0:6a97487a0ab3 68 static LSM6DSLSensor acc_gyro = LSM6DSLSensor(devI2c,D4,D5); // high address (default)
nikapov 0:6a97487a0ab3 69 static LSM303AGRMagSensor magnetometer = LSM303AGRMagSensor(devI2c);
nikapov 0:6a97487a0ab3 70 #endif
nikapov 0:6a97487a0ab3 71
nikapov 0:6a97487a0ab3 72 /* Helper function for printing floats & doubles */
nikapov 0:6a97487a0ab3 73 static char *print_double(char* str, double v, int decimalDigits=2)
nikapov 0:6a97487a0ab3 74 {
nikapov 0:6a97487a0ab3 75 int i = 1;
nikapov 0:6a97487a0ab3 76 int intPart, fractPart;
nikapov 0:6a97487a0ab3 77 int len;
nikapov 0:6a97487a0ab3 78 char *ptr;
nikapov 0:6a97487a0ab3 79
nikapov 0:6a97487a0ab3 80 /* prepare decimal digits multiplicator */
nikapov 0:6a97487a0ab3 81 for (;decimalDigits!=0; i*=10, decimalDigits--);
nikapov 0:6a97487a0ab3 82
nikapov 0:6a97487a0ab3 83 /* calculate integer & fractinal parts */
nikapov 0:6a97487a0ab3 84 intPart = (int)v;
nikapov 0:6a97487a0ab3 85 fractPart = (int)((v-(double)(int)v)*i);
nikapov 0:6a97487a0ab3 86
nikapov 0:6a97487a0ab3 87 /* fill in integer part */
nikapov 0:6a97487a0ab3 88 sprintf(str, "%i.", intPart);
nikapov 0:6a97487a0ab3 89
nikapov 0:6a97487a0ab3 90 /* prepare fill in of fractional part */
nikapov 0:6a97487a0ab3 91 len = strlen(str);
nikapov 0:6a97487a0ab3 92 ptr = &str[len];
nikapov 0:6a97487a0ab3 93
nikapov 0:6a97487a0ab3 94 /* fill in leading fractional zeros */
nikapov 0:6a97487a0ab3 95 for (i/=10;i>1; i/=10, ptr++) {
nikapov 0:6a97487a0ab3 96 if (fractPart >= i) {
nikapov 0:6a97487a0ab3 97 break;
nikapov 0:6a97487a0ab3 98 }
nikapov 0:6a97487a0ab3 99 *ptr = '0';
nikapov 0:6a97487a0ab3 100 }
nikapov 0:6a97487a0ab3 101
nikapov 0:6a97487a0ab3 102 /* fill in (rest of) fractional part */
nikapov 0:6a97487a0ab3 103 sprintf(ptr, "%i", fractPart);
nikapov 0:6a97487a0ab3 104
nikapov 0:6a97487a0ab3 105 return str;
nikapov 0:6a97487a0ab3 106 }
nikapov 0:6a97487a0ab3 107
nikapov 0:6a97487a0ab3 108 /* Simple main function */
nikapov 0:6a97487a0ab3 109 int main() {
nikapov 0:6a97487a0ab3 110 uint8_t id;
nikapov 0:6a97487a0ab3 111 float value1, value2;
nikapov 0:6a97487a0ab3 112 char buffer1[32], buffer2[32];
nikapov 0:6a97487a0ab3 113 int32_t axes[3];
nikapov 0:6a97487a0ab3 114
nikapov 0:6a97487a0ab3 115 /* Init all sensors with default params */
nikapov 0:6a97487a0ab3 116 hum_temp.init(NULL);
nikapov 0:6a97487a0ab3 117 press_temp.init(NULL);
nikapov 0:6a97487a0ab3 118 magnetometer.init(NULL);
nikapov 0:6a97487a0ab3 119 acc_gyro.init(NULL);
nikapov 0:6a97487a0ab3 120
nikapov 0:6a97487a0ab3 121 /* Enable all sensors */
nikapov 0:6a97487a0ab3 122 hum_temp.enable();
nikapov 0:6a97487a0ab3 123 press_temp.enable();
nikapov 0:6a97487a0ab3 124 #ifndef TARGET_DISCO_L475VG_IOT01A // X-NUCLEO-IKS01A2
nikapov 0:6a97487a0ab3 125 magnetometer.enable();
nikapov 0:6a97487a0ab3 126 #endif
nikapov 0:6a97487a0ab3 127 acc_gyro.enable_x();
nikapov 0:6a97487a0ab3 128 acc_gyro.enable_g();
nikapov 0:6a97487a0ab3 129
nikapov 0:6a97487a0ab3 130 printf("\r\n--- Starting new run ---\r\n");
nikapov 0:6a97487a0ab3 131
nikapov 0:6a97487a0ab3 132 hum_temp.read_id(&id);
nikapov 0:6a97487a0ab3 133 printf("HTS221 humidity & temperature = 0x%X\r\n", id);
nikapov 0:6a97487a0ab3 134 press_temp.read_id(&id);
nikapov 0:6a97487a0ab3 135 printf("LPS22HB pressure & temperature = 0x%X\r\n", id);
nikapov 0:6a97487a0ab3 136 magnetometer.read_id(&id);
nikapov 0:6a97487a0ab3 137 #ifdef TARGET_DISCO_L475VG_IOT01A
nikapov 0:6a97487a0ab3 138 printf("LIS3MDL magnetometer = 0x%X\r\n", id);
nikapov 0:6a97487a0ab3 139 #else // X-NUCLEO-IKS01A2
nikapov 0:6a97487a0ab3 140 printf("LSM303AGR magnetometer = 0x%X\r\n", id);
nikapov 0:6a97487a0ab3 141 #endif
nikapov 0:6a97487a0ab3 142 acc_gyro.read_id(&id);
nikapov 0:6a97487a0ab3 143 printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id);
nikapov 0:6a97487a0ab3 144
nikapov 0:6a97487a0ab3 145 while(1) {
nikapov 0:6a97487a0ab3 146 printf("\r\n");
nikapov 0:6a97487a0ab3 147
nikapov 0:6a97487a0ab3 148 hum_temp.get_temperature(&value1);
nikapov 0:6a97487a0ab3 149 hum_temp.get_humidity(&value2);
nikapov 0:6a97487a0ab3 150 printf("HTS221: [temp] %7s C, [hum] %s%%\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
nikapov 0:6a97487a0ab3 151
nikapov 0:6a97487a0ab3 152 press_temp.get_temperature(&value1);
nikapov 0:6a97487a0ab3 153 press_temp.get_pressure(&value2);
nikapov 0:6a97487a0ab3 154 printf("LPS22HB: [temp] %7s C, [press] %s mbar\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
nikapov 0:6a97487a0ab3 155
nikapov 0:6a97487a0ab3 156 printf("---\r\n");
nikapov 0:6a97487a0ab3 157
nikapov 0:6a97487a0ab3 158 magnetometer.get_m_axes(axes);
nikapov 0:6a97487a0ab3 159 #ifdef TARGET_DISCO_L475VG_IOT01A
nikapov 0:6a97487a0ab3 160 printf("LIS3MDL [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
nikapov 0:6a97487a0ab3 161 #else // X-NUCLEO-IKS01A2
nikapov 0:6a97487a0ab3 162 printf("LSM303AGR [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
nikapov 0:6a97487a0ab3 163 #endif
nikapov 0:6a97487a0ab3 164
nikapov 0:6a97487a0ab3 165 acc_gyro.get_x_axes(axes);
nikapov 0:6a97487a0ab3 166 printf("LSM6DSL [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
nikapov 0:6a97487a0ab3 167
nikapov 0:6a97487a0ab3 168 acc_gyro.get_g_axes(axes);
nikapov 0:6a97487a0ab3 169 printf("LSM6DSL [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
nikapov 0:6a97487a0ab3 170
nikapov 0:6a97487a0ab3 171 wait(2);
nikapov 0:6a97487a0ab3 172 }
nikapov 0:6a97487a0ab3 173 }