Temperature and humidity measurement with HTU21D chip

Get temperature and humidity readings, which will be used in an automated stand.
1. Structure. Connection diagram. Connection to the PC
For our future stand we don't need to have laboratory accuracy readings. A sensor with a I2C HTU21D interface can be used to monitor temperature and humidity. It shows humidity with an accuracy of about 2% and temperature with an accuracy of about 0.3%. HTU21D is a good and cheap sensor.

Next step: we take the fork and connect PC/ plug in via Ethernet or WiFi.
Connect the sensor as shown in the Figure.
To read the data, we need to know the address and the I2C read commands.

• Address I2C 0x40
• Temperature read command 0xE3
• Moisture read command 0xE5
2. Browser Search Bar Control
We need to set a GET HTTP request sequence to Fork with a specific query string. The commands in Fork are based on the principle «key = value» or just «key». Each query in the browser will receive a response from Fork. You can also combine queries using the & symbol.

1. Start with I2C
Provide the I_MODE command value I2C

http://192.168.0.205/control?I_MODE=I2C
In the end we'll get I2C

2. Read the temperature in ASCII_HEX format. For this purpose we will use two commands.
The first G_REQ_RES_FORM is to specify the format of the query and the response in value ascii_hex.
The second I_I2C_WRITE_READ command is to generate a sequence I2C to the restart.
Value - consists of a few bytes.
The result will be presented in the form of a sequence of bytes that the sensor has responded to.

Here 's the request we got

http://192.168.0.205/control?g_req_res_form=ascii_hex&i_i2c_write_read=40010003E3
In response to this request, we received ASCII_HEX,69FC69

3. Let's read humidity data. Specified the format of the data and created a sequence for using the I2C. As with temperature data, but only the command for the sensor has changed.

http://192.168.0.205/control?g_req_res_form=ascii_hex&i_i2c_write_read=40010003E5
Example of the answer of ASCII_HEX, 2ADE29We got respond from the sensor.

Now we need to single out the temperature and humidity data we need. For temperature and humidity, the package is arranged in the same way and does not depend on the data bytes.
We will also need formulas to translate data. Unfortunately, in datasheet in formulas there is an error with the power of two, so I will write here corrected formulas.
For temperature
Stemp - sensor data, n - number of bits of readings
For humidity
Overall as a result we received the following sensor readings

Temperature: 25.89 ℃
Relative humidity: 78%
3. HTML Javascript page Control
The actions in the previous paragraph are quite tiresome, and we don't have all day. Besides, I would like to have some interface in this case. Therefore we will make html page and automatic parsing on javascript. Here's what we've got.
We turn on the I2C on the interface output by hitting "init I2C" button. And we use "read temperature and RH" button to send a command so we can immediately get temperature and humidity readings.
We can also upload a page to Fork and ask directly from there. To do this, you will need to change one line in the code.

var url_path 'http://192.168.0.205/
to
var url_path '/';
And download the file using the file manager.

http://192.168.0.205/file_manager
Now to launch our interface we need go to

http://192.168.0.205/user_example_i2c.html
4. Control via Python script
Let's do the same now, but by using Python.
First, we connect/plug in the fork module and create the target's device

from forkapi.fork import Fork
dev1 = Fork('192.168.0.205')
Then we also need to initialize the I2C on the interface input

dev1.I2CEnable()
Then give the command to our sensor and get the respond

addr = 0x4
data = b'\xE3
res = dev1.I2CWriteRead(addr,3,data)
As before, having carried out a number of transformations we get temperature and humidity readings. The result is displayed on the console.
There you go!
5. References to the code examples

Engineers from these countries work with the Fork

Subscribe news
© All Rights Reserved, 2022 Privacy policy forktestlab@gmail.com