Example application how to use some of the sensors based on Hani-IoT board. Using TMP06 sensor for temperature, FXPQ3115 for air pressure and ADXL345 for acceleration.

Dependencies:   TMP06 FXPQ3115 ADXL345_I2C

Committer:
Pawel Zarembski
Date:
Tue Mar 17 10:27:46 2020 +0100
Revision:
2:a08feb9db81b
Parent:
0:01fccb06cd7c
change to mbed-os-5.15.1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pawel Zarembski 0:01fccb06cd7c 1 #include "mbed.h"
Pawel Zarembski 0:01fccb06cd7c 2 #include <ADXL345_I2C.h>
Pawel Zarembski 0:01fccb06cd7c 3 #include <TMP06.h>
Pawel Zarembski 0:01fccb06cd7c 4 #include <FXPQ3115.h>
Pawel Zarembski 0:01fccb06cd7c 5
Pawel Zarembski 0:01fccb06cd7c 6 #define LOW 0
Pawel Zarembski 0:01fccb06cd7c 7 #define HIGH 1
Pawel Zarembski 0:01fccb06cd7c 8
Pawel Zarembski 0:01fccb06cd7c 9 #define SUCCESS 0
Pawel Zarembski 0:01fccb06cd7c 10 #define ERROR 1
Pawel Zarembski 0:01fccb06cd7c 11
Pawel Zarembski 0:01fccb06cd7c 12 // Accelerometer
Pawel Zarembski 0:01fccb06cd7c 13 ADXL345_I2C acc_sensor(P0_13, P0_14); // I2C_0 pins
Pawel Zarembski 0:01fccb06cd7c 14 DigitalOut cs_pin(P0_21); // Need to be high during communication
Pawel Zarembski 0:01fccb06cd7c 15
Pawel Zarembski 0:01fccb06cd7c 16 // Temperature
Pawel Zarembski 0:01fccb06cd7c 17 TMP06 temp_sensor(P0_1);
Pawel Zarembski 0:01fccb06cd7c 18 float temperature;
Pawel Zarembski 0:01fccb06cd7c 19
Pawel Zarembski 0:01fccb06cd7c 20 // Pressure
Pawel Zarembski 0:01fccb06cd7c 21 FXPQ3115 pres_sensor(P0_13, P0_14);
Pawel Zarembski 0:01fccb06cd7c 22
Pawel Zarembski 0:01fccb06cd7c 23 /* Accelerometer methods */
Pawel Zarembski 0:01fccb06cd7c 24 int setup_i2c_acc()
Pawel Zarembski 0:01fccb06cd7c 25 {
Pawel Zarembski 0:01fccb06cd7c 26 cs_pin = HIGH;
Pawel Zarembski 0:01fccb06cd7c 27 printf("Device ID is: 0x%02x\n", acc_sensor.getDeviceID());
Pawel Zarembski 0:01fccb06cd7c 28 thread_sleep_for(10);
Pawel Zarembski 0:01fccb06cd7c 29
Pawel Zarembski 0:01fccb06cd7c 30 if (acc_sensor.setPowerControl(0x00)) {
Pawel Zarembski 0:01fccb06cd7c 31 printf("didn't intitialize power control\n");
Pawel Zarembski 0:01fccb06cd7c 32 return ERROR;
Pawel Zarembski 0:01fccb06cd7c 33 }
Pawel Zarembski 0:01fccb06cd7c 34 thread_sleep_for(10);
Pawel Zarembski 0:01fccb06cd7c 35
Pawel Zarembski 0:01fccb06cd7c 36 // Full resolution, +/-16g, 4mg/LSB
Pawel Zarembski 0:01fccb06cd7c 37 if(acc_sensor.setDataFormatControl(0x0B)){
Pawel Zarembski 0:01fccb06cd7c 38 printf("didn't set data format\n");
Pawel Zarembski 0:01fccb06cd7c 39 return ERROR;
Pawel Zarembski 0:01fccb06cd7c 40 }
Pawel Zarembski 0:01fccb06cd7c 41 thread_sleep_for(10);
Pawel Zarembski 0:01fccb06cd7c 42
Pawel Zarembski 0:01fccb06cd7c 43 // 3.2kHz data rate
Pawel Zarembski 0:01fccb06cd7c 44 if(acc_sensor.setDataRate(ADXL345_3200HZ)){
Pawel Zarembski 0:01fccb06cd7c 45 printf("didn't set data rate\n");
Pawel Zarembski 0:01fccb06cd7c 46 return ERROR;
Pawel Zarembski 0:01fccb06cd7c 47 }
Pawel Zarembski 0:01fccb06cd7c 48 thread_sleep_for(10);
Pawel Zarembski 0:01fccb06cd7c 49
Pawel Zarembski 0:01fccb06cd7c 50 // Measurement mode
Pawel Zarembski 0:01fccb06cd7c 51 if(acc_sensor.setPowerControl(MeasurementMode)) {
Pawel Zarembski 0:01fccb06cd7c 52 printf("didn't set the power control to measurement\n");
Pawel Zarembski 0:01fccb06cd7c 53 return ERROR;
Pawel Zarembski 0:01fccb06cd7c 54 }
Pawel Zarembski 0:01fccb06cd7c 55 thread_sleep_for(10);
Pawel Zarembski 0:01fccb06cd7c 56 cs_pin = LOW;
Pawel Zarembski 0:01fccb06cd7c 57
Pawel Zarembski 0:01fccb06cd7c 58 return SUCCESS;
Pawel Zarembski 0:01fccb06cd7c 59 }
Pawel Zarembski 0:01fccb06cd7c 60
Pawel Zarembski 0:01fccb06cd7c 61 void i2c_acc_print_xyz(void)
Pawel Zarembski 0:01fccb06cd7c 62 {
Pawel Zarembski 0:01fccb06cd7c 63 int readings[3] = {0, 0, 0};
Pawel Zarembski 0:01fccb06cd7c 64 cs_pin = HIGH;
Pawel Zarembski 0:01fccb06cd7c 65 acc_sensor.getOutput(readings);
Pawel Zarembski 0:01fccb06cd7c 66 cs_pin = LOW;
Pawel Zarembski 0:01fccb06cd7c 67 printf("Accelerometer: x:%.02f y:%.02f z:%.02f\r\n", (int16_t)readings[0]*0.004, (int16_t)readings[1]*0.004,
Pawel Zarembski 0:01fccb06cd7c 68 (int16_t)readings[2]*0.004);
Pawel Zarembski 0:01fccb06cd7c 69 }
Pawel Zarembski 0:01fccb06cd7c 70
Pawel Zarembski 0:01fccb06cd7c 71 int main()
Pawel Zarembski 0:01fccb06cd7c 72 {
Pawel Zarembski 0:01fccb06cd7c 73 if (setup_i2c_acc() != SUCCESS) {
Pawel Zarembski 0:01fccb06cd7c 74 printf("Error while initializing accelerometer\r\n");
Pawel Zarembski 0:01fccb06cd7c 75 return ERROR;
Pawel Zarembski 0:01fccb06cd7c 76 }
Pawel Zarembski 0:01fccb06cd7c 77
Pawel Zarembski 0:01fccb06cd7c 78 if (pres_sensor.sensor_init() == SUCCESS) {
Pawel Zarembski 0:01fccb06cd7c 79 printf("Pressure sensor is up!\r\n");
Pawel Zarembski 0:01fccb06cd7c 80 }
Pawel Zarembski 0:01fccb06cd7c 81 else {
Pawel Zarembski 0:01fccb06cd7c 82 printf("Error while initializing barometer\r\n");
Pawel Zarembski 0:01fccb06cd7c 83 return ERROR;
Pawel Zarembski 0:01fccb06cd7c 84 }
Pawel Zarembski 0:01fccb06cd7c 85
Pawel Zarembski 0:01fccb06cd7c 86 while (true) {
Pawel Zarembski 0:01fccb06cd7c 87 /* Read Accelerometer */
Pawel Zarembski 0:01fccb06cd7c 88 i2c_acc_print_xyz();
Pawel Zarembski 0:01fccb06cd7c 89 /* Read Temperature */
Pawel Zarembski 0:01fccb06cd7c 90 if (temp_sensor.read(&temperature) == SUCCESS) {
Pawel Zarembski 0:01fccb06cd7c 91 printf("Temperature: %.01f C\n", temperature);
Pawel Zarembski 0:01fccb06cd7c 92 }
Pawel Zarembski 0:01fccb06cd7c 93 /* Read Pressure */
Pawel Zarembski 0:01fccb06cd7c 94 if (pres_sensor.read_oneshotMode_bar() == SUCCESS) {
Pawel Zarembski 0:01fccb06cd7c 95 printf("Pressure in Pascals: %d hPa\n", pres_sensor.print_pressure()/100);
Pawel Zarembski 0:01fccb06cd7c 96 }
Pawel Zarembski 0:01fccb06cd7c 97 printf("\n");
Pawel Zarembski 0:01fccb06cd7c 98 thread_sleep_for(5000);
Pawel Zarembski 0:01fccb06cd7c 99 }
Pawel Zarembski 0:01fccb06cd7c 100 }
Pawel Zarembski 0:01fccb06cd7c 101