Catie fork
Dependencies: SDFileSystem X_NUCLEO_IKS01A1
Fork of ATT_AWS_IoT_demo by
Revision 29:537df716ba0b, committed 2017-04-05
- Comitter:
- peyo
- Date:
- Wed Apr 05 16:05:41 2017 +0000
- Parent:
- 28:a31312c9756d
- Commit message:
- Use IKS01A1 sensors
Changed in this revision
diff -r a31312c9756d -r 537df716ba0b Sensors/Temperature/HTS221.h --- a/Sensors/Temperature/HTS221.h Wed Apr 05 08:32:05 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,93 +0,0 @@ - -#ifndef HTS221_H_ -#define HTS221_H_ - -class HTS221 { -public: - HTS221(void); - int begin(void); - int activate(void); - int deactivate(void); - - int bduActivate(void); - int bduDeactivate(void); - - int readHumidity(void); - double readTemperature(void); -private: - int storeCalibration(void); - unsigned char _h0_rH, _h1_rH; - unsigned int _T0_degC, _T1_degC; - unsigned int _H0_T0, _H1_T0; - unsigned int _T0_OUT, _T1_OUT; - double _temperature; - int _humidity; - unsigned char _address; - - unsigned char readRegister(unsigned char slaveAddress, unsigned char regToRead); - int writeRegister(unsigned char slaveAddress, unsigned char regToWrite, unsigned char dataToWrite); -}; - -#define HTS221_ADDRESS 0xBF - -//Define a few of the registers that we will be accessing on the HTS221 -#define WHO_AM_I 0x0F -#define WHO_AM_I_RETURN 0xBC //This read-only register contains the device identifier, set to BCh - -#define AVERAGE_REG 0x10 // To configure humidity/temperature average. -#define AVERAGE_DEFAULT 0x1B - -/* - * [7] PD: power down control - * (0: power-down mode; 1: active mode) - * - * [6:3] Reserved - * - * [2] BDU: block data update - * (0: continuous update; 1: output registers not updated until MSB and LSB reading) -The BDU bit is used to inhibit the output register update between the reading of the upper -and lower register parts. In default mode (BDU = ?0?), the lower and upper register parts are -updated continuously. If it is not certain whether the read will be faster than output data rate, -it is recommended to set the BDU bit to ?1?. In this way, after the reading of the lower (upper) -register part, the content of that output register is not updated until the upper (lower) part is -read also. - * - * [1:0] ODR1, ODR0: output data rate selection (see table 17) - */ -#define CTRL_REG1 0x20 -#define POWER_UP 0x80 -#define BDU_SET 0x4 -#define ODR0_SET 0x1 // setting sensor reading period 1Hz - -#define CTRL_REG2 0x21 -#define CTRL_REG3 0x22 -#define REG_DEFAULT 0x00 - -#define STATUS_REG 0x27 -#define TEMPERATURE_READY 0x1 -#define HUMIDITY_READY 0x2 - -#define HUMIDITY_L_REG 0x28 -#define HUMIDITY_H_REG 0x29 -#define TEMP_L_REG 0x2A -#define TEMP_H_REG 0x2B -/* - * calibration registry should be read for temperature and humidity calculation. - * Before the first calculation of temperature and humidity, - * the master reads out the calibration coefficients. - * will do at init phase - */ -#define CALIB_START 0x30 -#define CALIB_END 0x3F -/* -#define CALIB_T0_DEGC_X8 0x32 -#define CALIB_T1_DEGC_X8 0x33 -#define CALIB_T1_T0_MSB 0x35 -#define CALIB_T0_OUT_L 0x3C -#define CALIB_T0_OUT_H 0x3D -#define CALIB_T1_OUT_L 0x3E -#define CALIB_T1_OUT_H 0x3F - */ - -#endif -
diff -r a31312c9756d -r 537df716ba0b Sensors/Temperature/hts221_driver.cpp --- a/Sensors/Temperature/hts221_driver.cpp Wed Apr 05 08:32:05 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,250 +0,0 @@ - -#include "HTS221.h" - - -// ------------------------------------------------------------------------------ -//jmf -- define I2C pins and functions to read & write to I2C device - -#include <string> -#include "mbed.h" - -#include "hardware.h" -//I2C i2c(PTC11, PTC10); //SDA, SCL -- define the I2C pins being used. Defined in a -//common locatioin since sensors also use I2C - -// Read a single unsigned char from addressToRead and return it as a unsigned char -unsigned char HTS221::readRegister(unsigned char slaveAddress, unsigned char ToRead) -{ - char data = ToRead; - - i2c.write(slaveAddress, &data, 1, 1); - i2c.read(slaveAddress, &data, 1, 0); - return data; -} - -// Writes a single unsigned char (dataToWrite) into regToWrite -int HTS221::writeRegister(unsigned char slaveAddress, unsigned char regToWrite, unsigned char dataToWrite) -{ - const char data[] = {regToWrite, dataToWrite}; - - return i2c.write(slaveAddress,data,2,0); -} - - -//jmf end -// ------------------------------------------------------------------------------ - -//static inline int humidityReady(uint8_t data) { -// return (data & 0x02); -//} -//static inline int temperatureReady(uint8_t data) { -// return (data & 0x01); -//} - - -HTS221::HTS221(void) : _address(HTS221_ADDRESS) -{ - _temperature = 0; - _humidity = 0; -} - - -int HTS221::begin(void) -{ - uint8_t data; - - data = readRegister(_address, WHO_AM_I); - if (data == WHO_AM_I_RETURN){ - if (activate()){ - storeCalibration(); - return data; - } - } - - return 0; -} - -int -HTS221::storeCalibration(void) -{ - uint8_t data; - uint16_t tmp; - - for (int reg=CALIB_START; reg<=CALIB_END; reg++) { - if ((reg!=CALIB_START+8) && (reg!=CALIB_START+9) && (reg!=CALIB_START+4)) { - - data = readRegister(HTS221_ADDRESS, reg); - - switch (reg) { - case CALIB_START: - _h0_rH = data; - break; - case CALIB_START+1: - _h1_rH = data; - break; - case CALIB_START+2: - _T0_degC = data; - break; - case CALIB_START+3: - _T1_degC = data; - break; - - case CALIB_START+5: - tmp = _T0_degC; - _T0_degC = (data&0x3)<<8; - _T0_degC |= tmp; - - tmp = _T1_degC; - _T1_degC = ((data&0xC)>>2)<<8; - _T1_degC |= tmp; - break; - case CALIB_START+6: - _H0_T0 = data; - break; - case CALIB_START+7: - _H0_T0 |= data<<8; - break; - case CALIB_START+0xA: - _H1_T0 = data; - break; - case CALIB_START+0xB: - _H1_T0 |= data <<8; - break; - case CALIB_START+0xC: - _T0_OUT = data; - break; - case CALIB_START+0xD: - _T0_OUT |= data << 8; - break; - case CALIB_START+0xE: - _T1_OUT = data; - break; - case CALIB_START+0xF: - _T1_OUT |= data << 8; - break; - - - case CALIB_START+8: - case CALIB_START+9: - case CALIB_START+4: - //DO NOTHING - break; - - // to cover any possible error - default: - return false; - } /* switch */ - } /* if */ - } /* for */ - return true; -} - - -int -HTS221::activate(void) -{ - uint8_t data; - - data = readRegister(_address, CTRL_REG1); - data |= POWER_UP; - data |= ODR0_SET; - writeRegister(_address, CTRL_REG1, data); - - return true; -} - - -int HTS221::deactivate(void) -{ - uint8_t data; - - data = readRegister(_address, CTRL_REG1); - data &= ~POWER_UP; - writeRegister(_address, CTRL_REG1, data); - return true; -} - - -int -HTS221::bduActivate(void) -{ - uint8_t data; - - data = readRegister(_address, CTRL_REG1); - data |= BDU_SET; - writeRegister(_address, CTRL_REG1, data); - - return true; -} - - -int -HTS221::bduDeactivate(void) -{ - uint8_t data; - - data = readRegister(_address, CTRL_REG1); - data &= ~BDU_SET; - writeRegister(_address, CTRL_REG1, data); - return true; -} - - -int -HTS221::readHumidity(void) -{ - uint8_t data = 0; - uint16_t h_out = 0; - double h_temp = 0.0; - double hum = 0.0; - - data = readRegister(_address, STATUS_REG); - - if (data & HUMIDITY_READY) { - data = readRegister(_address, HUMIDITY_H_REG); - h_out = data << 8; // MSB - data = readRegister(_address, HUMIDITY_L_REG); - h_out |= data; // LSB - - // Decode Humidity - hum = ((int16_t)(_h1_rH) - (int16_t)(_h0_rH))/2.0; // remove x2 multiple - - // Calculate humidity in decimal of grade centigrades i.e. 15.0 = 150. - h_temp = (((int16_t)h_out - (int16_t)_H0_T0) * hum) / ((int16_t)_H1_T0 - (int16_t)_H0_T0); - hum = ((int16_t)_h0_rH) / 2.0; // remove x2 multiple - _humidity = (int16_t)((hum + h_temp)); // provide signed % measurement unit - } - return _humidity; -} - - - -double -HTS221::readTemperature(void) -{ - uint8_t data = 0; - uint16_t t_out = 0; - double t_temp = 0.0; - double deg = 0.0; - - data = readRegister(_address, STATUS_REG); - - if (data & TEMPERATURE_READY) { - - data= readRegister(_address, TEMP_H_REG); - t_out = data << 8; // MSB - data = readRegister(_address, TEMP_L_REG); - t_out |= data; // LSB - - // Decode Temperature - deg = ((int16_t)(_T1_degC) - (int16_t)(_T0_degC))/8.0; // remove x8 multiple - - // Calculate Temperature in decimal of grade centigrades i.e. 15.0 = 150. - t_temp = (((int16_t)t_out - (int16_t)_T0_OUT) * deg) / ((int16_t)_T1_OUT - (int16_t)_T0_OUT); - deg = ((int16_t)_T0_degC) / 8.0; // remove x8 multiple - _temperature = deg + t_temp; // provide signed celsius measurement unit - } - - return _temperature; -} -
diff -r a31312c9756d -r 537df716ba0b Sensors/X_NUCLEO_IKS01A1.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Sensors/X_NUCLEO_IKS01A1.lib Wed Apr 05 16:05:41 2017 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/teams/ST/code/X_NUCLEO_IKS01A1/#d1c67d482bad
diff -r a31312c9756d -r 537df716ba0b Sensors/hardware.h --- a/Sensors/hardware.h Wed Apr 05 08:32:05 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,6 +0,0 @@ -#ifndef Hardware_H_ -#define Hardware_H_ -extern I2C i2c; //SDA, SCL -- define the I2C pins being used - -#endif -
diff -r a31312c9756d -r 537df716ba0b main.cpp --- a/main.cpp Wed Apr 05 08:32:05 2017 +0000 +++ b/main.cpp Wed Apr 05 16:05:41 2017 +0000 @@ -22,7 +22,7 @@ #include "aws_iot_mqtt_interface.h" // Sensors -#include "HTS221.h" +#include "x_nucleo_iks01a1.h" #if DEBUG_LEVEL > 0 #include "mbedtls/debug.h" @@ -43,7 +43,7 @@ // AWS defines #define PATH_MAX 1024 -#define MAX_LENGTH_OF_UPDATE_JSON_BUFFER 200 // NOTE: Be wary of this if your JSON doc grows +#define MAX_LENGTH_OF_UPDATE_JSON_BUFFER 300 // NOTE: Be wary of this if your JSON doc grows #define SHADOW_SYNC_INTERVAL 3.0 // How often we sync with AWS Shadow (in seconds) // Comment out the following line if color is not supported on the terminal @@ -96,12 +96,19 @@ uint32_t port = AWS_IOT_MQTT_PORT; char iccidName[21] = "12345678901234567890"; +/* Instantiate the expansion board */ +static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(I2C_SDA, I2C_SCL); + // Sensor data -float temperature = 0.0; -int humidity = 0; +static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor; +static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor; +static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor; +static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor; +float iks01a1_temperature = 0.0; +float iks01a1_pressure = 0.0; +float iks01a1_humidity = 0.0; +uint8_t id; -// Temp/humidity object -HTS221 hts221; //===================================================================================================================== // @@ -119,9 +126,6 @@ // SD card access (MOSI, MISO, SCK, CS) SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); -// I2C bus (SDA, SCL) -I2C i2c(PTC11, PTC10); - //===================================================================================================================== // // Functions @@ -175,8 +179,10 @@ //********************************************************************************************************************* void printData() { - INFO("Temperature is: %0.2f F", temperature); - INFO("Humidity is: %02d", humidity); + INFO("Temperature is: %0.2f C", iks01a1_temperature); + INFO("Humidity is: %0.2f", iks01a1_humidity); + INFO("Pressure is: %0.2f mbar", iks01a1_pressure); + switch (ledColor) { case COLOR_OFF: INFO("LED: Off"); @@ -382,11 +388,12 @@ buttonOverride = false; // Get temp/humidity values - temperature = CTOF(hts221.readTemperature()); - humidity = hts221.readHumidity(); + temp_sensor1->get_temperature(&iks01a1_temperature); + humidity_sensor->get_humidity(&iks01a1_humidity); + pressure_sensor->get_pressure(&iks01a1_pressure); // Loading data into JSON format - sprintf(cPayload, "{\"color\":\"%s\",\"temperature\":%f,\"humidity\":%d}", colorStrings[ledColor], temperature, humidity); + sprintf(cPayload, "{\"color\":\"%s\",\"temperature\":%f,\"humidity\":%f,\"pressure\":%f}", colorStrings[ledColor], iks01a1_temperature, iks01a1_humidity, iks01a1_pressure); Msg.PayloadLen = strlen(cPayload) + 1; Params.MessageParams = Msg; @@ -437,18 +444,24 @@ ledController.pKey = "ledColor"; ledController.type = SHADOW_JSON_UINT8; - // JSON struct for temperature\humidity readings + // JSON struct for temperature\humidity\pressure readings jsonStruct_t temperatureHandler; temperatureHandler.cb = NULL; temperatureHandler.pKey = "temperature"; - temperatureHandler.pData = &temperature; + temperatureHandler.pData = &iks01a1_temperature; temperatureHandler.type = SHADOW_JSON_FLOAT; jsonStruct_t humidityHandler; humidityHandler.cb = NULL; humidityHandler.pKey = "humidity"; - humidityHandler.pData = &humidity; - humidityHandler.type = SHADOW_JSON_INT16; + humidityHandler.pData = &iks01a1_humidity; + humidityHandler.type = SHADOW_JSON_FLOAT; + + jsonStruct_t pressureHandler; + pressureHandler.cb = NULL; + pressureHandler.pKey = "pressure"; + pressureHandler.pData = &iks01a1_pressure; + pressureHandler.type = SHADOW_JSON_FLOAT; INFO("AWS IoT SDK Version(dev) %d.%d.%d-%s", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG); @@ -476,11 +489,10 @@ // Initialize sensors INFO("Init sensors..."); - //void hts221_init(void); - //i = hts221.begin(); - //if(!i) { - // WARN(RED "HTS221 NOT DETECTED!!\n\r"); - //} + humidity_sensor->read_id(&id); + printf("HTS221 humidity & temperature = 0x%X\r\n", id); + pressure_sensor->read_id(&id); + printf("LPS25H pressure & temperature = 0x%X\r\n", id); // Setup SW3 button to falling edge interrupt INFO("Init interrupts..."); @@ -570,8 +582,9 @@ } // Read sensor data - temperature = 24.5; - humidity = 60; + temp_sensor1->get_temperature(&iks01a1_temperature); + humidity_sensor->get_humidity(&iks01a1_humidity); + pressure_sensor->get_pressure(&iks01a1_pressure); INFO("\n=======================================================================================\n"); // Initialize JSON shadow document @@ -584,10 +597,11 @@ buttonOverride = false; } - // Updates the 'reported' color/temp/humidity - rc = aws_iot_shadow_add_reported(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 3, &ledController, + // Updates the 'reported' color/temp/humidity/pressure + rc = aws_iot_shadow_add_reported(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 4, &ledController, &temperatureHandler, - &humidityHandler); + &humidityHandler, + &pressureHandler); if (rc == NONE_ERROR) { rc = aws_iot_finalize_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer);