![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Updated version for LoraWan GEII IUT
Dependencies: Servo Cayenne-LPP
Revision 64:1e20d66960c3, committed 2022-03-04
- Comitter:
- alainpegatoquet
- Date:
- Fri Mar 04 09:50:35 2022 +0000
- Parent:
- 63:cd8ab5860303
- Commit message:
- Maj des drivers mbed-os et mbed-lora-radio-drv
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/driver_mbed_TH02.cpp Fri Mar 04 09:50:35 2022 +0000 @@ -0,0 +1,187 @@ +/* + * TH02_dev.cpp + * Driver for DIGITAL I2C HUMIDITY AND TEMPERATURE SENSOR + * + * Copyright (c) 2014 seeed technology inc. + * Website : www.seeed.cc + * Author : Oliver Wang + * Create Time: April 2014 + * Change Log : + * + * The MIT License (MIT) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/****************************************************************************/ +/*** Include files ***/ +/****************************************************************************/ +#include "driver_mbed_TH02.h" +// #include <Wire.h> +// #include <Arduino.h> + +/* Use Serial IIC */ +#ifdef SERIAL_IIC +#endif + +TH02_dev myTH02; +I2C i2cth02(PB_9, PB_8); +/****************************************************************************/ +/*** Local Variable ***/ +/****************************************************************************/ + + +/****************************************************************************/ +/*** Class member Functions ***/ +/****************************************************************************/ + +void TH02_dev::begin(void) +{ + /* Start IIC */ + // Wire.begin(); + /* TH02 don't need to software reset */ + printf("System Start !\r\n\r\n"); +} + +float TH02_dev::ReadTemperature(void) +{ + /* Start a new temperature conversion */ + TH02_IIC_WriteReg(REG_CONFIG, CMD_MEASURE_TEMP); + //delay(100); + /* Wait until conversion is done */ + while(!isAvailable()); + uint16_t value = TH02_IIC_ReadData(); + + value = value >> 2; + /* + Formula: + Temperature(C) = (Value/32) - 50 + */ + float temper = (value/32.0)-50.0; + + return temper; +} + +float TH02_dev::ReadHumidity(void) +{ + /* Start a new humility conversion */ + TH02_IIC_WriteReg(REG_CONFIG, CMD_MEASURE_HUMI); + + /* Wait until conversion is done */ + //delay(100); + while(!isAvailable()); + uint16_t value = TH02_IIC_ReadData(); + + value = value >> 4; + + /* + Formula: + Humidity(%) = (Value/16) - 24 + */ + + float humility = (value/16.0)-24.0; + + return humility; +} + +/****************************************************************************/ +/*** Local Functions ***/ +/****************************************************************************/ +uint8_t TH02_dev::isAvailable() +{ + uint8_t status = TH02_IIC_ReadReg(REG_STATUS); + if(status & STATUS_RDY_MASK) + { + return 0; //ready + } + else + { + return 1; //not ready yet + } +} + +void TH02_dev::TH02_IIC_WriteCmd(uint8_t u8Cmd) +{ + char cmd = u8Cmd; + /* Port to arduino */ + // Wire.beginTransmission(TH02_I2C_DEV_ID); + // Wire.write(u8Cmd); + // Wire.endTransmission(); + i2cth02.write(TH02_I2C_DEV_ID, &cmd, 1); + +} + +uint8_t TH02_dev::TH02_IIC_ReadReg(uint8_t u8Reg) +{ + /* Port to arduino */ + char Temp = 0; + + /* Send a register reading command */ + TH02_IIC_WriteCmd(u8Reg); + + // Wire.requestFrom(TH02_I2C_DEV_ID, 1); + // while(Wire.available()) + // { + // Temp = Wire.read(); + // } + + i2cth02.read(TH02_I2C_DEV_ID, &Temp, 1); + + return Temp; +} + +void TH02_dev::TH02_IIC_WriteReg(uint8_t u8Reg,uint8_t u8Data) +{ + // Wire.beginTransmission(TH02_I2C_DEV_ID); + // Wire.write(u8Reg); + // Wire.write(u8Data); + // Wire.endTransmission(); + char cmd[2]; + cmd[0] = u8Reg; + cmd[1] = u8Data; + i2cth02.write(TH02_I2C_DEV_ID, cmd, 2); +} + +uint16_t TH02_dev::TH02_IIC_ReadData(void) +{ + /* Port to arduino */ + uint16_t Temp = TH02_IIC_ReadData2byte(); + return Temp; +} + +uint16_t TH02_dev::TH02_IIC_ReadData2byte() +{ + uint16_t TempData = 0; + // uint16_t tmpArray[3]={0}; + char tmpArray[3]; + + TH02_IIC_WriteCmd(REG_DATA_H); + + // Wire.requestFrom(TH02_I2C_DEV_ID, 3); + // while(Wire.available()) + // { + // tmpArray[cnt] = (uint16_t)Wire.read(); + // cnt++; + // } + i2cth02.read(TH02_I2C_DEV_ID, tmpArray, 3); + + /* MSB */ + TempData = (tmpArray[1]<<8)|(tmpArray[2]); + return TempData; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/driver_mbed_TH02.h Fri Mar 04 09:50:35 2022 +0000 @@ -0,0 +1,82 @@ +/* + * TH02_dev.h + * Driver for DIGITAL I2C HUMIDITY AND TEMPERATURE SENSOR + * + * Copyright (c) 2014 seeed technology inc. + * Website : www.seeed.cc + * Author : Oliver Wang + * Create Time: April 2014 + * Change Log : + * + * The MIT License (MIT) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef _TH02_DEV_H +#define _TH02_DEV_H + +/****************************************************************************/ +/*** Including Files ***/ +/****************************************************************************/ +// #include <Wire.h> +// #include <Arduino.h> +# include "mbed.h" + +/****************************************************************************/ +/*** Macro Definitions ***/ +/****************************************************************************/ +// #define TH02_I2C_DEV_ID 0x40 +const int TH02_I2C_DEV_ID = 0x40<<1; +//Address for mbed + +#define REG_STATUS 0x00 +#define REG_DATA_H 0x01 +#define REG_DATA_L 0x02 +#define REG_CONFIG 0x03 +#define REG_ID 0x11 + +#define STATUS_RDY_MASK 0x01 //poll RDY,0 indicate the conversion is done + +#define CMD_MEASURE_HUMI 0x01 //perform a humility measurement +#define CMD_MEASURE_TEMP 0x11 //perform a temperature measurement + +#define TH02_WR_REG_MODE 0xC0 +#define TH02_RD_REG_MODE 0x80 +/****************************************************************************/ +/*** Class Definition ***/ +/****************************************************************************/ +class TH02_dev +{ +public: + void begin(); + uint8_t isAvailable(); + float ReadTemperature(void); + float ReadHumidity(void); +private: + void TH02_IIC_WriteCmd(uint8_t u8Cmd); + uint8_t TH02_IIC_ReadReg(uint8_t u8Reg); + void TH02_IIC_WriteReg(uint8_t u8Reg,uint8_t u8Data); + uint16_t TH02_IIC_ReadData(void); + uint16_t TH02_IIC_ReadData2byte(void); +}; + +extern TH02_dev myTH02; + +#endif // _TH02_DEV_H \ No newline at end of file
--- a/main.cpp Tue Oct 20 13:21:32 2020 +0000 +++ b/main.cpp Fri Mar 04 09:50:35 2022 +0000 @@ -15,7 +15,7 @@ * limitations under the License. */ #include <stdio.h> -#include "th02.h" +#include "driver_mbed_TH02.h" #include "CayenneLPP.h" #include "lorawan/LoRaWANInterface.h" #include "lorawan/system/lorawan_data_structures.h" @@ -39,6 +39,7 @@ CayenneLPP Payload(MAX_SIZE); +// Dummy values float celsius = -4.1; float accel[] = {1.234, -1.234, 0}; float rh = 30; @@ -50,20 +51,22 @@ int size = 0; -DigitalOut Alarme (PC_13);// alarme LED output -Servo Myservo(PA_7); //servomotor output -TH02 MyTH02 (I2C_SDA,I2C_SCL,TH02_I2C_ADDR<<1);// connect hsensor on RX2 TX2 +//DigitalOut Alarme (PC_13); // alarme LED output +DigitalOut Alarme (LED2); // alarme LED output +Servo Myservo(PA_7); // servomotor output +//TH02 MyTH02 (I2C_SDA,I2C_SCL,TH02_I2C_ADDR<<1);// connect hsensor on RX2 TX2 + /* * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing */ -#define TX_TIMER 20000 +#define TX_TIMER 10000 /** * Maximum number of events for the event queue. * 10 is the safe number for the stack events, however, if application * also uses the queue for whatever purposes, this number should be increased. */ -#define MAX_NUMBER_OF_EVENTS 30 +#define MAX_NUMBER_OF_EVENTS 10 /** * Maximum number of retries for CONFIRMED messages before giving up @@ -75,10 +78,6 @@ */ #define PC_9 0 -/** - * Dummy sensor class object - */ -DS1820 ds1820(PC_9); /** * This event queue is the global event queue for both the @@ -109,10 +108,8 @@ void servo(uint8_t uAngle) { - - } - +} /** * Entry point for application @@ -121,14 +118,6 @@ { // setup tracing setup_trace(); - // th02 temerature sensor section - int iTemp,iTime,iTempbrute,iRH,iRHbrute; - // Myservo.calibrate(0.0005, 45); - printf ("\n\r start reading TH02 for first time"); - MyTH02.startTempConv(true,true); - - - // stores the status of a call to LoRaWAN protocol lorawan_status_t retcode; @@ -184,43 +173,27 @@ * Sends a message to the Network Server *************************************************************************************************************/ static void send_message() - {int iTime,iTempbrute,iRHbrute; - float fTemp,fRH; - uint16_t packet_len; +{ + int iTime; + uint16_t packet_len; int16_t retcode; - int32_t sensor_value, rh_value; - MyTH02.startTempConv(true,true); - iTime= MyTH02.waitEndConversion();// wait until onversion is done - fTemp= (float)MyTH02.getConversionValue()/10; - - printf ("\n\r temp value=%2.1f",fTemp); - - MyTH02.startRHConv(true,true); - iTime= MyTH02.waitEndConversion();// wait until onversion is done - fRH= (float) MyTH02.getConversionValue()/10; - - printf ("\n\r humidity value= %2.1f",fRH ); + float fTemp,fHumid; -/* - if (ds1820.begin()) { - ds1820.startConversion(); - sensor_value = ds1820.read(); - printf("\r\n Dummy Sensor Value = %d \r\n", sensor_value); - ds1820.startConversion(); - } else { - printf("\r\n No sensor found \r\n"); - return; - } - */ - + // Read Sensor temp and humidity values + fTemp = myTH02.ReadTemperature(); + printf("Temp=%.2f\t",fTemp); + fHumid = myTH02.ReadHumidity(); + printf("Humidity=%.2f\n",fHumid); + + // Payload is in Cayenne format Payload.reset(); - size = Payload.addTemperature(1, (float) fTemp); - size =size+ Payload.addRelativeHumidity(2, fRH); + size = Payload.addTemperature(1, (float) fTemp); // Add Temp in payload + size = size+ Payload.addRelativeHumidity(2, fHumid); // Add Humidity in payload -// send complete message with cayenne format - retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, Payload.getBuffer(), Payload.getSize(), - MSG_UNCONFIRMED_FLAG); + // Send complete message with cayenne format + retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, Payload.getBuffer(), Payload.getSize(), + MSG_UNCONFIRMED_FLAG); if (retcode < 0) { retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n") @@ -243,7 +216,8 @@ * Receive a message from the Network Server */ static void receive_message() -{int num_port, iPosition=0,iIndex,iEtatAlarme; +{ + int num_port, iPosition=0,iIndex,iEtatAlarme; uint8_t port; int flags; int16_t retcode = lorawan.receive(rx_buffer, sizeof(rx_buffer), port, flags); @@ -257,36 +231,35 @@ for (uint8_t i = 0; i < retcode; i++) { printf("%02x", rx_buffer[i]); } - // printf("\n test value=%d", port); - // *****************************code todo here ******************************************** - switch (port){ + + // printf("\n test value=%d", port); + // *****************************code todo here ******************************************** + switch (port) { case 3: // control led - printf("\n led=%d", (int)rx_buffer[0]); - if ((rx_buffer[0]-0x30)==0) - - iEtatAlarme=0; - else iEtatAlarme=1; + printf("\n led=%d", (int)rx_buffer[0]); + //if ((rx_buffer[0]-0x30)==0) + if (rx_buffer[0]==0) + iEtatAlarme=0; + else + iEtatAlarme=1; Alarme.write(iEtatAlarme); - - printf("\n alarme=%d",iEtatAlarme); - break; - case 4:// control servomotor - for (iIndex=0;iIndex<retcode;iIndex++) - {iPosition=iPosition*10+(rx_buffer[iIndex]-0x30);// convert receive string to angular position - } - - - printf("\n servo position =%d",iPosition); - Myservo.position ( iPosition-45 ); // set servo motor position from 0 to 180 - break; - default: printf("\n port inconnu =%d",(int)port); - break; - } - - - // ***************************** end code todo here ***************************************** - - + + printf("\n alarme=%d",iEtatAlarme); + break; + case 4: // control servomotor + for (iIndex=0; iIndex<retcode; iIndex++) { + iPosition = iPosition*10 + (rx_buffer[iIndex]-0x30); // convert receive string to angular position + } + + printf("\n Servo position =%d",iPosition); + Myservo.position ( iPosition-45 ); // set servo motor position from 0 to 180 + break; + default: + printf("\n port inconnu =%d",(int)port); + break; + } + +// ***************************** end code todo here ***************************************** memset(rx_buffer, 0, sizeof(rx_buffer)); } @@ -295,6 +268,7 @@ */ static void lora_event_handler(lorawan_event_t event) { + printf("\r\nEventCode = %d \r\n", event); switch (event) { case CONNECTED: printf("\r\n Connection - Successful \r\n"); @@ -330,7 +304,7 @@ receive_message(); break; case RX_TIMEOUT: - printf("\r\n timeout in reception - Code = %d \r\n", event); + printf("\r\n timeout in reception - Code = %d \r\n", event); break; case RX_ERROR: printf("\r\n Error in reception - Code = %d \r\n", event);
--- a/mbed-lora-radio-drv.lib Tue Oct 20 13:21:32 2020 +0000 +++ b/mbed-lora-radio-drv.lib Fri Mar 04 09:50:35 2022 +0000 @@ -1,1 +1,1 @@ -https://github.com/ARMmbed/mbed-semtech-lora-rf-drivers/#16958f814d505cfbbedfa16d9bf8b9dff0e0442b +https://github.com/ARMmbed/mbed-semtech-lora-rf-drivers/#6012fa43cf9f2cae46fa9d424fe4051d00e157a2
--- a/mbed-os.lib Tue Oct 20 13:21:32 2020 +0000 +++ b/mbed-os.lib Fri Mar 04 09:50:35 2022 +0000 @@ -1,1 +1,1 @@ -https://github.com/ARMmbed/mbed-os/#0063e5de32fc575f061244c96ac60c41c07bd2e6 +https://github.com/ARMmbed/mbed-os/#b6e5a0a8afa34dec9dae8963778aebce0c82a54b
--- a/mbed_app.json Tue Oct 20 13:21:32 2020 +0000 +++ b/mbed_app.json Fri Mar 04 09:50:35 2022 +0000 @@ -35,12 +35,12 @@ "lora.duty-cycle-on": true, "lora.phy": "EU868", - "lora.device-eui": " { 0x00, 0x87, 0xCC, 0xEA, 0xD9, 0x44, 0xF5, 0x29 }", - "lora.application-eui": "{ 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x02, 0x16, 0x8E }", - "lora.application-key": "{ 0x77, 0x8A, 0x26, 0xDF, 0x9D, 0x70, 0xDB, 0xEF, 0x43, 0x03, 0x0F, 0xD2, 0x5F, 0x47, 0x1D, 0x00 }", - "lora.appskey": "{ 0x9A, 0x9D, 0xA0, 0x2C, 0xC3, 0xC4, 0xB2, 0xD6, 0xA0, 0x42, 0xEC, 0xE3, 0xBF, 0x4A, 0x53, 0xFF }", - "lora.nwkskey": "{ 0xDE, 0x09, 0x34, 0xE3, 0x1F, 0x60, 0xEB, 0x9E, 0x30, 0xB2, 0x13, 0x1F, 0xE6, 0x53, 0xBA, 0xBC }", - "lora.device-address": " 0x26013AC5" + "lora.device-eui": "{ 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x04, 0xD5, 0xA4 }", + "lora.application-eui": "{ 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x03, 0xD6, 0x48 }", + "lora.application-key": "{ 0x2E, 0xCE, 0x79, 0x1A, 0xA5, 0x3E, 0x87, 0xBC, 0xCE, 0xD4, 0x60, 0xAD, 0x98, 0x52, 0x35, 0x03 }", + "lora.appskey": "{ 0x47, 0x0C, 0xCE, 0x54, 0xDB, 0xD6, 0x35, 0x02, 0x4B, 0x83, 0x73, 0xF2, 0x38, 0x52, 0xBA, 0x76 }", + "lora.nwkskey": "{ 0x69, 0x65, 0xA9, 0x25, 0x23, 0xC3, 0x7D, 0x78, 0x9D, 0xBB, 0x93, 0x06, 0xF0, 0xAA, 0x70, 0xCB }", + "lora.device-address": " 0x260B89AE" },
--- a/th02.cpp Tue Oct 20 13:21:32 2020 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,479 +0,0 @@ -// ********************************************************************************** -// Driver definition for HopeRF TH02 temperature and humidity sensor -// ********************************************************************************** -// Creative Commons Attrib Share-Alike License -// You are free to use/extend this library but please abide with the CC-BY-SA license: -// http://creativecommons.org/licenses/by-sa/4.0/ -// -// For any explanation see TH02 sensor information at -// http://www.hoperf.com/sensor/app/TH02.htm -// -// Code based on following datasheet -// http://www.hoperf.com/upload/sensor/TH02_V1.1.pdf -// -// Written by Charles-Henri Hallard (http://hallard.me) -// -// History : V1.00 2014-07-14 - First release -// V1.10 2015-04-13 - changed to Wire library instead of m_I2C -// -// All text above must be included in any redistribution. -// -// ********************************************************************************** -#include "th02.h" -#include "mbed.h" -#include "math.h" - -// Class Constructor - -TH02::TH02(PinName sda,PinName scl,uint8_t address): m_I2C(sda, scl) -{ - _address = address; // m_I2C Module Address - _last_temp = TH02_UNINITIALIZED_TEMP; // Last measured temperature (for linearization) - _last_rh = TH02_UNINITIALIZED_RH; // Last measured RH -//m_I2C.frequency(10000); //set 10khz i2c frequency - -} - -TH02::~TH02() -{ - -} - - - -/* ====================================================================== -Function: writeCommand -Purpose : write the "register address" value on m_I2C bus -Input : register address - true if we need to release the bus after (default yes) -Output : Arduino Wire library return code (0 if ok) -Comments: -====================================================================== */ -uint8_t TH02::writeCommand(uint8_t command, bool release) -{ - int iError; - (void) m_I2C.start(); - //Wire.beginTransmission(_address); - iError=m_I2C.write(_address);// send adress of i2c slave - - if (iError==1) { // ack received -// Wire.write(command) ; - iError= m_I2C.write(command); - - - if (release==true) { - m_I2C.stop();// return stop error code - } - } - - if (iError==1) iError=0;// ack received - else iError=1;// no ack - return iError; -} - -/* ====================================================================== -Function: writeRegister -Purpose : write a value on the designed register address on m_I2C bus -Input : register address - value to write -Output : Arduino Wire library return code (0 if ok) -Comments: -====================================================================== */ -uint8_t TH02::writeRegister(uint8_t reg, uint8_t value) -{ - int iError; - - bool ret = false; - - //Wire.beginTransmission(_address); - (void) m_I2C.start(); - iError=m_I2C.write(_address);// send adress of i2c slave -// Wire.write(reg); - if (iError==1) { - - iError= m_I2C.write(reg); - -// Wire.write(value); - (void) m_I2C.write(value); - } -// return Wire.endTransmission(); - m_I2C.stop();// return stop error code - if (iError==1) iError=0;// ack received - else iError=1;// no ack - wait_ms(1); - return iError; -} - -/* ====================================================================== -Function: readRegister -Purpose : read a register address value on m_I2C bus -Input : register address - pointer where the return value will be filled -Output : Arduino Wire library return code (0 if ok) -Comments: -====================================================================== */ -uint8_t TH02::readRegister(uint8_t reg, uint8_t * value) -{ - uint8_t ret ; - int iAck,iRedVal,iError; - // Send a register reading command - // but DO NOT release the m_I2C bus -// (void) m_I2C.start(); - //iError=m_I2C.write(_address);// send adress of i2c slave - - //if (iError==1) // ack received - //{ - ret = writeCommand(reg, false);// no stop - - if ( ret == 0) { //if command ok - // Wire.requestFrom( (uint8_t) _address, (uint8_t) 1); - (void) m_I2C.start(); - iError=m_I2C.write(_address+0x01);// send adress of i2c slave in read mode - *value =m_I2C.read(0);//send non ack - // if (Wire.available() != 1) - /*if (iAck != 1) - - // Other error as Wire library - ret = 4; - else - // grab the value*/ - // *value = iRedVal; // return Red value by ref - - //} - - // Ok now we have finished -// Wire.endTransmission(); - - } - (void) m_I2C.stop();// return stop error code - wait_ms(1); - return ret; -} - -/* ====================================================================== -Function: getId -Purpose : Get device ID register -Input : pointer where the return value will be filled -Output : Arduino Wire library return code (0 if ok) -Comments: - -====================================================================== */ -uint8_t TH02::getId(uint8_t * pvalue) -{ - return (readRegister(TH02_ID, pvalue)); -} - -/* ====================================================================== -Function: getStatus -Purpose : Get device status register -Input : pointer where the return value will be filled -Output : Arduino Wire library return code (0 if ok) -Comments: -====================================================================== */ -uint8_t TH02::getStatus(uint8_t * pvalue) -{ - return (readRegister(TH02_STATUS, pvalue)); -} - -/* ====================================================================== -Function: isConverting -Purpose : Indicate if a temperature or humidity conversion is in progress -Input : - -Output : true if conversion in progress false otherwise -Comments: -====================================================================== */ -bool TH02::isConverting(void) -{ - uint8_t status; - // Get status and check RDY bit - if ( getStatus(&status) == 0) - - { - // printf("\n lecture status %x",status); - if ( (status & TH02_STATUS_RDY) ==1 ) - return true; - } - return false; -} - -/* ====================================================================== -Function: getConfig -Purpose : Get device configuration register -Input : pointer where the return value will be filled -Output : Arduino Wire library return code (0 if ok) -Comments: -====================================================================== */ -uint8_t TH02::getConfig(uint8_t * pvalue) -{ - return (readRegister(TH02_CONFIG, pvalue)); -} - -/* ====================================================================== -Function: setConfig -Purpose : Set device configuration register -Input : value to set -Output : true if succeded, false otherwise -Comments: -====================================================================== */ -uint8_t TH02::setConfig(uint8_t config) -{ - return (writeRegister(TH02_CONFIG, config)); -} - -/* ====================================================================== -Function: startTempConv -Purpose : Start a temperature conversion -Input : - fastmode true to enable fast conversion - - heater true to enable heater -Output : true if succeded, false otherwise -Comments: if heater enabled, it will not be auto disabled -====================================================================== */ -uint8_t TH02::startTempConv(bool fastmode, bool heater) -{ - // init configuration register to start and temperature - uint8_t config = TH02_CONFIG_START | TH02_CONFIG_TEMP; - - // set fast mode and heater if asked - if (fastmode) config |= TH02_CONFIG_FAST; - if (heater) config |= TH02_CONFIG_HEAT; - - // write to configuration register - return ( setConfig( config ) ); -} - -/* ====================================================================== -Function: startRHConv -Purpose : Start a relative humidity conversion -Input : - fastmode true to enable fast conversion - - heater true to enable heater -Output : true if succeded, false otherwise -Comments: if heater enabled, it will not be auto disabled -====================================================================== */ -uint8_t TH02::startRHConv(bool fastmode, bool heater) -{ - // init configuration register to start and no temperature (so RH) - uint8_t config = TH02_CONFIG_START; - - // set fast mode and heater if asked - if (fastmode) config |= TH02_CONFIG_FAST; - if (heater) config |= TH02_CONFIG_HEAT; - - // write to configuration register - return ( setConfig( config ) ); -} - -/* ====================================================================== -Function: waitEndConversion -Purpose : wait for a temperature or RH conversion is done -Input : -Output : delay in ms the process took. -Comments: if return >= TH02_CONVERSION_TIME_OUT, time out occured -====================================================================== */ -uint8_t TH02::waitEndConversion(void) -{ - // okay this is basic approach not so accurate - // but avoid using long and millis() - uint8_t time_out = 0; - - // loop until conversion done or duration >= time out - while (isConverting() && (time_out <= TH02_CONVERSION_TIME_OUT) ) { - ++time_out; - wait_ms(2); - } - - // return approx time of conversion - return (time_out); -} - -/* ====================================================================== -Function: roundInt -Purpose : round a float value to int -Input : float value -Output : int value rounded -Comments: -====================================================================== */ -int16_t TH02::roundInt(float value) -{ - - // check positive number and do round - if (value >= 0.0f) - value = floor(value + 0.5f); - else - value = ceil(value - 0.5f); - - // return int value - return (static_cast<int16_t>(value)); -} - -/* to avoid math library may I need to test something - like that -float TH02::showDecimals(float x, int numDecimals) -{ - int y=x; - double z=x-y; - double m=pow(10,numDecimals); - double q=z*m; - double r=round(q); - return static_cast<double>(y)+(1.0/m)*r; -} -*/ - - -/* ====================================================================== -Function: getConversionValue -Purpose : return the last converted value to int * 10 to have 1 digit prec. -Input : float value -Output : value rounded but multiplied per 10 or TH02_UNDEFINED_VALUE on err -Comments: - temperature and rh raw values (*100) are stored for raw purpose - - the configuration register is checked to see if last conv was - a temperature or humidity conversion -====================================================================== */ -int16_t TH02::getConversionValue(void) -{ - char cMaChaine[4]; - int iError; - int32_t result=0 ; - uint8_t config; - int16_t ret = TH02_UNDEFINED_VALUE; - - // Prepare reading address of ADC data result - /*if ( writeCommand(TH02_DATAh, false) == 0 ) // no stop - {*/ - // Read 2 bytes adc data result MSB and LSB from TH02 - //Wire.requestFrom( (uint8_t) _address, (uint8_t) 2); - writeCommand(TH02_DATAh, false); - - // read of two register - (void) m_I2C.start(); - iError=m_I2C.write(_address+1);// send adress of i2c slave read mode - if (iError==1) { - cMaChaine[0]= m_I2C.read(1);// read first byte with ack - cMaChaine[1]=m_I2C.read(0);// read first byte without ack - - m_I2C.stop();// rperform stop - - - - //iError= m_I2C.read (_address,cMaChaine,4,false);// send stop at end - // printf (" \n\r lecture I2C: %02x %02x",cMaChaine[0],cMaChaine[1]); - //} - result=(cMaChaine[0]<<8 )|cMaChaine[1]; - // Get configuration to know what was asked last time - - if (getConfig(&config)==0) { - // last conversion was temperature ? - if( config & TH02_CONFIG_TEMP) { - result >>= 2; // remove 2 unused LSB bits - result *= 100; // multiply per 100 to have int value with 2 decimal - result /= 32; // now apply datasheet formula - if(result >= 5000) { - result -= 5000; - } else { - result -= 5000; - result = -result; - } - - // now result contain temperature * 100 - // so 2134 is 21.34 C - - // Save raw value - _last_temp = result; - } - // it was RH conversion - else { - result >>= 4; // remove 4 unused LSB bits - result *= 100; // multiply per 100 to have int value with 2 decimal - result /= 16; // now apply datasheet formula - result -= 2400; - - // now result contain humidity * 100 - // so 4567 is 45.67 % RH - _last_rh = result; - } - - // remember result value is multiplied by 10 to avoid float calculation later - // so humidity of 45.6% is 456 and temp of 21.3 C is 213 - ret = roundInt(result/10.0f); - } - } - - else{ - - m_I2C.stop();// rperform stop - } - return ret; -} - - - - - -/* ====================================================================== -Function: getConpensatedRH -Purpose : return the compensated calulated humidity -Input : true if we want to round value to 1 digit precision, else 2 -Output : the compensed RH value (rounded or not) -Comments: -====================================================================== */ -int16_t TH02::getConpensatedRH(bool round) -{ - float rhvalue ; - float rhlinear ; - int16_t ret = TH02_UNDEFINED_VALUE; - - // did we had a previous measure RH - if (_last_rh != TH02_UNINITIALIZED_RH) { - // now we're float restore real value RH value - rhvalue = (float) _last_rh / 100.0 ; - - // apply linear compensation - rhlinear = rhvalue - ((rhvalue*rhvalue) * TH02_A2 + rhvalue * TH02_A1 + TH02_A0); - - // correct value - rhvalue = rhlinear; - - // do we have a initialized temperature value ? - if (_last_temp != TH02_UNINITIALIZED_TEMP ) { - // Apply Temperature compensation - // remember last temp was stored * 100 - rhvalue += ((_last_temp/100.0) - 30.0) * (rhlinear * TH02_Q1 + TH02_Q0); - } - - // now get back * 100 to have int with 2 digit precision - rhvalue *= 100; - - // do we need to round to 1 digit ? - if (round) { - // remember result value is multiplied by 10 to avoid float calculation later - // so humidity of 45.6% is 456 - ret = roundInt(rhvalue/10.0f); - } else { - ret = (int16_t) rhvalue; - } - } - - return ret; -} - -/* ====================================================================== -Function: getLastRawRH -Purpose : return the raw humidity * 100 -Input : -Output : int value (ie 4123 for 41.23%) -Comments: -====================================================================== */ -int32_t TH02::getLastRawRH(void) -{ - return _last_rh; -} - -/* ====================================================================== -Function: getLastRawTemp -Purpose : return the raw temperature value * 100 -Input : -Output : int value (ie 2124 for 21.24 C) -Comments: -====================================================================== */ -int32_t TH02::getLastRawTemp(void) -{ - return _last_temp; -}
--- a/th02.h Tue Oct 20 13:21:32 2020 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,123 +0,0 @@ -// ********************************************************************************** -// Driver definition for HopeRF TH02 temperature and humidity sensor -// ********************************************************************************** -// Creative Commons Attrib Share-Alike License -// You are free to use/extend this library but please abide with the CC-BY-SA license: -// http://creativecommons.org/licenses/by-sa/4.0/ -// -// For any explanation see TH02 sensor information at -// http://www.hoperf.com/sensor/app/TH02.htm -// -// Code based on following datasheet -// http://www.hoperf.com/upload/sensor/TH02_V1.1.pdf -// -// Written by Charles-Henri Hallard (http://hallard.me) -//ported to mbed env by Philippe LAURENT (IUT de NICE France) -// -// History : V1.00 2014-07-14 - First release -// V1.10 2015-04-13 - changed to Wire library instead of I2C -// -// All text above must be included in any redistribution. -// -// ********************************************************************************** -#ifndef TH02_H -#define TH02_H - -#include <mbed.h> // - -// TH02 I2C Device address -#define TH02_I2C_ADDR 0x40 - -// TH02 Registers addresses -#define TH02_STATUS 0 -#define TH02_DATAh 1 -#define TH02_DATAl 2 -#define TH02_CONFIG 3 -#define TH02_ID 17 - -// TH02 custom error code return function -#define TH02_I2C_ERR 0xFF - -// Unititialized values (arbitrary) -#define TH02_UNINITIALIZED_TEMP 55555 // int32_t internal value -#define TH02_UNINITIALIZED_RH 1111 // int32_t internal value -#define TH02_UNDEFINED_VALUE 12345 // int16_t returned value - -// we decide error if conversion is >= 50ms -#define TH02_CONVERSION_TIME_OUT 50 - -// Bit definition of TH02 registers values -#define TH02_STATUS_RDY 0x01 - -#define TH02_CONFIG_START 0x01 -#define TH02_CONFIG_HEAT 0x02 -#define TH02_CONFIG_TEMP 0x10 -#define TH02_CONFIG_HUMI 0x00 -#define TH02_CONFIG_FAST 0x20 - -// THO2 Linearization Coefficients -#define TH02_A0 -4.7844 -#define TH02_A1 0.4008 -#define TH02_A2 -0.00393 - -// TH02 Temperature compensation Linearization Coefficients -#define TH02_Q0 0.1973 -#define TH02_Q1 0.00237 - - - - - -class TH02 { - public: - TH02(uint8_t address); - uint8_t getId(uint8_t * pvalue); - uint8_t getId(void); - uint8_t getStatus(uint8_t * pvalue); - bool isConverting(void); - uint8_t waitEndConversion(void); - uint8_t getConfig(uint8_t * pvalue); - uint8_t setConfig(uint8_t config); - uint8_t startTempConv(bool fastmode = false, bool heater = false); - uint8_t startRHConv(bool fastmode = false, bool heater = false); - int16_t roundInt(float value); - int16_t getConversionValue(void); - int16_t getConpensatedRH(bool round); - int32_t getLastRawRH(void); - int32_t getLastRawTemp(void); - // int16_t getConversionValue_Raw(void); - -/** - * TH02 constructor - * - * @param sda I2C data pin - * @param scl I2C clock pin - * @param address I2C slave sensor address - - */ - TH02(PinName sda, PinName scl, uint8_t address); - - /** - * MFRC522 destructor - */ - ~TH02(); - - - - private: -I2C m_I2C; - uint8_t writeCommand(uint8_t command, bool release=true); - uint8_t writeRegister(uint8_t reg, uint8_t value); - uint8_t readRegister(uint8_t reg, uint8_t * value); - - int32_t _last_temp; // Last measured temperature (for linearization) - int32_t _last_rh; // Last measured RH - uint8_t _address; // I2C Module Address - - - -}; - - - -#endif \ No newline at end of file