BLE_HTS_Demo

This BLE_HTS_Demo program enables you to collect Temperature and Humidity data reading from sensor and transmit to collector device such as smartphone.

Below documents teach you how to install app that can connect and read data from our DELTA-DFCM-NNN40 development board. There are two versions, Android and iPhone.

/media/uploads/Marcomissyou/ios_app_for_environment_sensor_0518.pdf

/media/uploads/Marcomissyou/android_app_for_environment_sensor.pdf

Committer:
silviaChen
Date:
Tue Jul 18 09:34:06 2017 +0000
Revision:
9:2ff66a3d164a
Parent:
0:ef0f188a6fdd
Child:
10:5220f45c8ec1
update mbed-os so both NNN50 and NQ620 (as well as NQ624 module) platform are supported; Add config in mbed_app.json to fix NQ620 use internal RC issue ; remove the unused shield folder;update BLE_API;Modify SCL/SDA pin

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Marcomissyou 0:ef0f188a6fdd 1 /* Copyright (c) 2009 Nordic Semiconductor. All Rights Reserved.
Marcomissyou 0:ef0f188a6fdd 2 *
Marcomissyou 0:ef0f188a6fdd 3 * The information contained herein is property of Nordic Semiconductor ASA.
Marcomissyou 0:ef0f188a6fdd 4 * Terms and conditions of usage are described in detail in NORDIC
Marcomissyou 0:ef0f188a6fdd 5 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
Marcomissyou 0:ef0f188a6fdd 6 *
Marcomissyou 0:ef0f188a6fdd 7 * Licensees are granted free, non-transferable use of the information. NO
Marcomissyou 0:ef0f188a6fdd 8 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
Marcomissyou 0:ef0f188a6fdd 9 * the file.
Marcomissyou 0:ef0f188a6fdd 10 *df
Marcomissyou 0:ef0f188a6fdd 11 */
Marcomissyou 0:ef0f188a6fdd 12
Marcomissyou 0:ef0f188a6fdd 13 #include <stdbool.h>
Marcomissyou 0:ef0f188a6fdd 14 #include <stdint.h>
Marcomissyou 0:ef0f188a6fdd 15
Marcomissyou 0:ef0f188a6fdd 16 #include <mbed.h>
Marcomissyou 0:ef0f188a6fdd 17 #include "hts221.h"
Marcomissyou 0:ef0f188a6fdd 18
silviaChen 9:2ff66a3d164a 19 I2C i2c(p26, p27); //SDA, SCL
Marcomissyou 0:ef0f188a6fdd 20
Marcomissyou 0:ef0f188a6fdd 21 /*lint ++flb "Enter library region" */
Marcomissyou 0:ef0f188a6fdd 22
Marcomissyou 0:ef0f188a6fdd 23 static const char expected_who_am_i = 0xBCU; //!< Expected value to get from WHO_AM_I register.
Marcomissyou 0:ef0f188a6fdd 24
Marcomissyou 0:ef0f188a6fdd 25 uint8_t H0_rH_x2;
Marcomissyou 0:ef0f188a6fdd 26 uint8_t H1_rH_x2;
Marcomissyou 0:ef0f188a6fdd 27 uint16_t T0_degC_x8;
Marcomissyou 0:ef0f188a6fdd 28 uint16_t T1_degC_x8;
Marcomissyou 0:ef0f188a6fdd 29
Marcomissyou 0:ef0f188a6fdd 30 int16_t H0_T0_OUT;
Marcomissyou 0:ef0f188a6fdd 31 int16_t H1_T0_OUT;
Marcomissyou 0:ef0f188a6fdd 32 int16_t T0_OUT;
Marcomissyou 0:ef0f188a6fdd 33 int16_t T1_OUT;
Marcomissyou 0:ef0f188a6fdd 34
Marcomissyou 0:ef0f188a6fdd 35 float T0_DegC_cal;
Marcomissyou 0:ef0f188a6fdd 36 float T1_DegC_cal;
Marcomissyou 0:ef0f188a6fdd 37 float H0_RH_cal;
Marcomissyou 0:ef0f188a6fdd 38 float H1_RH_cal;
Marcomissyou 0:ef0f188a6fdd 39
Marcomissyou 0:ef0f188a6fdd 40 bool hts221_init(void)
Marcomissyou 0:ef0f188a6fdd 41 {
Marcomissyou 0:ef0f188a6fdd 42 bool transfer_succeeded = true;
Marcomissyou 0:ef0f188a6fdd 43
Marcomissyou 0:ef0f188a6fdd 44 i2c.frequency(400000);
Marcomissyou 0:ef0f188a6fdd 45 hts221_register_write(0x10 , TRes_4 << 3 | HRes_5);
Marcomissyou 0:ef0f188a6fdd 46 hts221_register_write(0x20 , PD_On | BDU_Off | ODR_1Hz); // Control register 1
Marcomissyou 0:ef0f188a6fdd 47 hts221_register_write(0x21 , NoBoot | HeaterOff | No_OS); // Control register 2
Marcomissyou 0:ef0f188a6fdd 48 hts221_register_write(0x22 , DRDY_H | PP_OD_PP | DRDY_NON); // Control register 3
Marcomissyou 0:ef0f188a6fdd 49
Marcomissyou 0:ef0f188a6fdd 50 // Read and verify product ID
Marcomissyou 0:ef0f188a6fdd 51 transfer_succeeded &= hts221_verify_product_id();
Marcomissyou 0:ef0f188a6fdd 52
Marcomissyou 0:ef0f188a6fdd 53 return transfer_succeeded;
Marcomissyou 0:ef0f188a6fdd 54 }
Marcomissyou 0:ef0f188a6fdd 55
Marcomissyou 0:ef0f188a6fdd 56 bool hts221_verify_product_id(void)
Marcomissyou 0:ef0f188a6fdd 57 {
Marcomissyou 0:ef0f188a6fdd 58 char who_am_i[1];
Marcomissyou 0:ef0f188a6fdd 59 hts221_register_read(ADDRESS_WHO_AM_I, &who_am_i[0], 1);
Marcomissyou 0:ef0f188a6fdd 60 if (who_am_i[0] != expected_who_am_i) return false;
Marcomissyou 0:ef0f188a6fdd 61 else return true;
Marcomissyou 0:ef0f188a6fdd 62 }
Marcomissyou 0:ef0f188a6fdd 63
Marcomissyou 0:ef0f188a6fdd 64 void hts221_register_write(uint8_t register_address, uint8_t value)
Marcomissyou 0:ef0f188a6fdd 65 {
Marcomissyou 0:ef0f188a6fdd 66 char w2_data[2];
Marcomissyou 0:ef0f188a6fdd 67
Marcomissyou 0:ef0f188a6fdd 68 w2_data[0] = register_address;
Marcomissyou 0:ef0f188a6fdd 69 w2_data[1] = value;
Marcomissyou 0:ef0f188a6fdd 70 i2c.write(HTS221_WriteADDE, w2_data, 2);
Marcomissyou 0:ef0f188a6fdd 71
Marcomissyou 0:ef0f188a6fdd 72 }
Marcomissyou 0:ef0f188a6fdd 73
Marcomissyou 0:ef0f188a6fdd 74 void hts221_register_read(char register_address, char *destination, uint8_t number_of_bytes)
Marcomissyou 0:ef0f188a6fdd 75 {
Marcomissyou 0:ef0f188a6fdd 76 i2c.write(HTS221_WriteADDE, &register_address, 1, 1);
Marcomissyou 0:ef0f188a6fdd 77 i2c.read(HTS221_WriteADDE, destination, number_of_bytes); //Note by Tsungta, API may have a bug
Marcomissyou 0:ef0f188a6fdd 78
Marcomissyou 0:ef0f188a6fdd 79 //runaboud read function added by Tsungta
Marcomissyou 0:ef0f188a6fdd 80 /* if (number_of_bytes == 1) {
Marcomissyou 0:ef0f188a6fdd 81 i2c.write(HTS221_WriteADDE, &register_address, 1, 1);
Marcomissyou 0:ef0f188a6fdd 82 i2c.write(HTS221_ReadADDE);
Marcomissyou 0:ef0f188a6fdd 83 *destination = i2c.read(0);
Marcomissyou 0:ef0f188a6fdd 84 i2c.stop();
Marcomissyou 0:ef0f188a6fdd 85 } else {
Marcomissyou 0:ef0f188a6fdd 86 register_address |= 0x80;
Marcomissyou 0:ef0f188a6fdd 87 i2c.write(HTS221_WriteADDE, &register_address, 1, 1);
Marcomissyou 0:ef0f188a6fdd 88 i2c.write(HTS221_ReadADDE);
Marcomissyou 0:ef0f188a6fdd 89 while (number_of_bytes-- > 0)
Marcomissyou 0:ef0f188a6fdd 90 *destination++ = i2c.read(0);
Marcomissyou 0:ef0f188a6fdd 91 i2c.stop();
Marcomissyou 0:ef0f188a6fdd 92 }*/
Marcomissyou 0:ef0f188a6fdd 93 }
Marcomissyou 0:ef0f188a6fdd 94
Marcomissyou 0:ef0f188a6fdd 95 void HTS221_Calib(void)
Marcomissyou 0:ef0f188a6fdd 96 {
Marcomissyou 0:ef0f188a6fdd 97 char cal_data[16];
Marcomissyou 0:ef0f188a6fdd 98
Marcomissyou 0:ef0f188a6fdd 99 hts221_register_read(0xB0, cal_data, 16);
Marcomissyou 0:ef0f188a6fdd 100
Marcomissyou 0:ef0f188a6fdd 101 H0_rH_x2 = cal_data[0];
Marcomissyou 0:ef0f188a6fdd 102 H1_rH_x2 = cal_data[1];
Marcomissyou 0:ef0f188a6fdd 103 T0_degC_x8 = ((cal_data[5] & 0x03) << 8) + cal_data[2]; //MSB + LSB in
Marcomissyou 0:ef0f188a6fdd 104 T1_degC_x8 = ((cal_data[5] & 0x0C) << 6) + cal_data[3]; // MSB
Marcomissyou 0:ef0f188a6fdd 105
Marcomissyou 0:ef0f188a6fdd 106 H0_T0_OUT = (cal_data[7] << 8) + cal_data[6];
Marcomissyou 0:ef0f188a6fdd 107 H1_T0_OUT = (cal_data[11] << 8) + cal_data[10];
Marcomissyou 0:ef0f188a6fdd 108 T0_OUT = (cal_data[13] << 8) + cal_data[12];
Marcomissyou 0:ef0f188a6fdd 109 T1_OUT = (cal_data[15] << 8) + cal_data[14];
Marcomissyou 0:ef0f188a6fdd 110
Marcomissyou 0:ef0f188a6fdd 111 // convert negative 2's complement values to native negative value
Marcomissyou 0:ef0f188a6fdd 112 if (H0_T0_OUT&0x8000) H0_T0_OUT = -(0x8000-(0x7fff&H0_T0_OUT));
Marcomissyou 0:ef0f188a6fdd 113 if (H1_T0_OUT&0x8000) H1_T0_OUT = -(0x8000-(0x7fff&H1_T0_OUT)); //((~H1_T0_OUT)+1);//
Marcomissyou 0:ef0f188a6fdd 114 if (T0_OUT&0x8000) T0_OUT = -(0x8000-(0x7fff&T0_OUT));
Marcomissyou 0:ef0f188a6fdd 115 if (T1_OUT&0x8000) T1_OUT = -(0x8000-(0x7fff&T1_OUT));
Marcomissyou 0:ef0f188a6fdd 116
Marcomissyou 0:ef0f188a6fdd 117 T0_DegC_cal = (float) T0_degC_x8/8;
Marcomissyou 0:ef0f188a6fdd 118 T1_DegC_cal = (float) T1_degC_x8/8;
Marcomissyou 0:ef0f188a6fdd 119 H0_RH_cal = (float) H0_rH_x2/2;
Marcomissyou 0:ef0f188a6fdd 120 H1_RH_cal = (float) H1_rH_x2/2;
Marcomissyou 0:ef0f188a6fdd 121
Marcomissyou 0:ef0f188a6fdd 122 }
Marcomissyou 0:ef0f188a6fdd 123
Marcomissyou 0:ef0f188a6fdd 124 void HTS221_ReadTempHumi( float *pTemp , float *pHumi)
Marcomissyou 0:ef0f188a6fdd 125 {
Marcomissyou 0:ef0f188a6fdd 126 char sensor_data[4];
Marcomissyou 0:ef0f188a6fdd 127 int16_t H_OUT;
Marcomissyou 0:ef0f188a6fdd 128 int16_t T_OUT;
Marcomissyou 0:ef0f188a6fdd 129
Marcomissyou 0:ef0f188a6fdd 130 hts221_register_read(0xA8, sensor_data, 4);
Marcomissyou 0:ef0f188a6fdd 131
Marcomissyou 0:ef0f188a6fdd 132 H_OUT = (sensor_data[1] << 8) + sensor_data[0];
Marcomissyou 0:ef0f188a6fdd 133 T_OUT = (sensor_data[3] << 8) + sensor_data[2];
Marcomissyou 0:ef0f188a6fdd 134
Marcomissyou 0:ef0f188a6fdd 135 // convert negative 2's complement values to native negative value
Marcomissyou 0:ef0f188a6fdd 136 if (H_OUT&0x8000) H_OUT = -(0x8000-(0x7fff&H_OUT)); //((~H_OUT)+1);;
Marcomissyou 0:ef0f188a6fdd 137 if (T_OUT&0x8000) T_OUT = -(0x8000-(0x7fff&T_OUT));
Marcomissyou 0:ef0f188a6fdd 138
Marcomissyou 0:ef0f188a6fdd 139 *pTemp = linear_interpolation(T0_OUT, T0_DegC_cal, T1_OUT, T1_DegC_cal, T_OUT);
Marcomissyou 0:ef0f188a6fdd 140 *pHumi = linear_interpolation(H0_T0_OUT, H0_RH_cal, H1_T0_OUT, H1_RH_cal, H_OUT);
Marcomissyou 0:ef0f188a6fdd 141 // Constraint for measurement after calibration
Marcomissyou 0:ef0f188a6fdd 142 if ((int)*pHumi>MaxHumi-1 | (int)*pHumi==-72) *pHumi = MaxHumi;
Marcomissyou 0:ef0f188a6fdd 143 if ((int)*pHumi<MinHumi ) *pHumi = MinHumi;
Marcomissyou 0:ef0f188a6fdd 144 if ((int)*pTemp>MaxTemp-1) *pHumi = MaxTemp;
Marcomissyou 0:ef0f188a6fdd 145 if ((int)*pHumi<MinTemp ) *pHumi = MinTemp ;
Marcomissyou 0:ef0f188a6fdd 146 }
Marcomissyou 0:ef0f188a6fdd 147
Marcomissyou 0:ef0f188a6fdd 148 float linear_interpolation(int16_t x0, float y0, int16_t x1, float y1, float mes)
Marcomissyou 0:ef0f188a6fdd 149 {
Marcomissyou 0:ef0f188a6fdd 150 float a = (float) ((y1 - y0) / (x1 - x0));
Marcomissyou 0:ef0f188a6fdd 151 float b = (float) -a*x0 + y0;
Marcomissyou 0:ef0f188a6fdd 152 float cal = (float) a * mes + b;
Marcomissyou 0:ef0f188a6fdd 153 return cal;
Marcomissyou 0:ef0f188a6fdd 154 }
Marcomissyou 0:ef0f188a6fdd 155
Marcomissyou 0:ef0f188a6fdd 156 /*lint --flb "Leave library region" */
Marcomissyou 0:ef0f188a6fdd 157