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, 2021 Privacy policy forktestlab@gmail.com
Privacy policy
This privacy policy describes how and when we collect, use, and share information when you provide your personal information(name, phone number, email address) on the site forktestlab.com or related sites and services.
This privacy policy does not apply to the actions of third parties that we do not own or control. You can refer to Artes Electronics privacy policy to learn more about its privacy practices.
__________

Information we collect

In order to complete your order or contact you to discuss a partnership, you must provide us with certain information, such as your name, email address, phone number(in some cases, postal address, payment information, and details of the product you are ordering). You can also provide us with additional personal information (for example, for an individual order) if you contact us directly.
__________

Why we need your information and how we use it

We rely on a number of legal grounds to collect, use, and share your information, including:
as necessary to provide our services, such as when we use your information to fulfill your order, resolve disputes, or provide customer support;
when you have given your affirmative consent, which you can revoke at any time, for example by subscribing to our mailing list.
__________

Information sharing and disclosure

Information about our customers is important to our business, so we do not share this information with third parties. Otherwise, it is possible only after receiving your personal consent.
__________

Data storage

We only store your personal information for as long as it is necessary to provide our services and as described in this privacy policy. However, we may also be required to store this information in accordance with our legal and regulatory obligations, resolve disputes, and enforce our agreements. Maximum period of storage of your data: 5 years.
__________

Transfer of personal information outside the EU

We may store and process your information through third-party hosting services in the United States and other jurisdictions. As a result, we may transfer your personal information to a jurisdiction with different data protection and government oversight laws than your jurisdiction. If we are deemed to be transferring information about you outside the EU, we rely on Privacy Shield as the legal basis for the transfer.
__________

Your right

If you live in certain territories, including the EU, you have a number of rights with respect to your personal information. While some of these rights apply in General, some rights only apply in certain limited cases. We describe these rights below:
Access. You may have the right to access and obtain a copy of the personal information we hold about you by contacting us using the contact information below.
Edit, restrict, delete. You also have the right to change, restrict or delete your personal information. No exceptional circumstances (for example, when we are required to store data for legal reasons) We will generally delete your personal information upon request.
An object. You may object to the processing of some of your data based on our legitimate interests and the receipt of marketing messages from us after you have provided your explicit consent to receive them(providing mail is such consent). In such cases, we will delete your personal information if we do not have a valid and legitimate reason to continue using this information or if it is necessary for legal reasons.
__________

How to contact us

For the purposes of EU data protection law, we, Artes Electronics, are the data controller of your personal information. If you have any questions or concerns, you can contact us at info@arteselectronics.com. In addition, you can send us an mail at: office 17, 50A Tambovskaya str., Saint Petersburg, 192007, Russia