Hi all,
I have been working on a little bit with humidity and Room temperature sensing with Reef-PI So and I want to share what I have so fare.
One of the key point with my selection was that this has to be cheap while strait forward to set up. The SHT20 is a logical choice as the price is about £2.5-3 with casing, works with DC 3.3 ~ 5.5V and communicates on I2C which makes easy to implement.
So Let's see how to add it to PI and than connect to Reef-PI software.
As of 29/03/2021 Reef OI has no Humidity Tab, only supports 1 Wire temperature sensors so we have to use the PH logic and File input to set it up. Not a biggie but not an elegant solution.
For file input I like to use virtual drives in the memory as it is fast, we don't need the files to be stored permanently and it saves your SD card and my experience the CPU usage goes down doing this.
So let's see the steps:
1. Wire up your device as per the above picture. (I won't give you pin numbers for SDA and SCL because you may have changed your configuration of pin-out. )
2. If not enabled i2C ye do it now. (I use the GUI you can do it in the hardcore way via command line too )
3. Install necessary packages such as I2C-Tools ( note: You may have some of these packages already installed. If you are not sure just run all it won't hurt)
Execute the following four commands respectively to install and restart.
[In terminal]
sudo apt-get install i2c-tools
sudo apt-get install python-smbus
sudo adduser pi i2c
sudo reboot
4. Test I2C and confirm the sensor address
[In terminal]
i2cdetect -y 1
To confirm whether the I2C link sensor is successful (0x40 in the picture is the address of my SHT20, everyone's sensor address may not be the same, this should be noted)
5. Create the directories you will store your code (You can change it but if you do fallow it trough in all the codes)
[In terminal]
sudo mkdir /Reef-Hardware
sudo mkdir /Reef-Hardware/SHT20
sudo chown pi -R /Reef-Hardware/
sudo chgrp pi -R /Reef-Hardware/
chmod 755 -R /Reef-Hardware/
6. Write Python temperature and humidity collection program ( Mine is a modified version of one I have found on-line )
Note: the code below is designed to be able to used manual testing also to be added to a scheduler (crontab)
Note 2: The script is writing temperature in C if you want to record in F you will have to change the line strSHT = repr(cTemp) to strSHT = repr(fTemp)
[In terminal]
nano /Reef-Hardware/SHT20/SHT20_Reader.py
import os
import smbus
import time
#Create directories and define output files
rootDir = "/dev/shm"
WorkDrir = "/ReeFHardwareComs/SHT20"
path = rootDir + WorkDrir
access_rights = 0o777
F_Hum_Name = "SHT20_Hum"
F_Temp_name = "SHT20_Temp"
if not os.path.isdir(path):
try:
os.makedirs(path, access_rights)
except OSError:
print ("Creation of the directory %s failed" % path)
else:
print ("Successfully created the directory %s " % path)
#Get I2C bus
bus = smbus.SMBus(1)
#SHT20 address,0x40(64)
addr =0x40
#Send Temperature measurement command
# 0xF3(243) NO HOLD master
bus.write_byte(addr,0xF3)
time.sleep(0.5)
#Read data back, 2byte
#Temp MSB, Temp LSB
data0 = bus.read_byte(0x40)
data1 = bus.read_byte(0x40)
#convert the data
temp =data0 *256 +data1
cTemp = -46.85 +((temp*175.72)/65536.0)
ftemp =cTemp*1.8+32
#Send humidity measurement command
bus.write_byte(0x40,0xF5)
time.sleep(0.5)
data0 = bus.read_byte(0x40)
data1 = bus.read_byte(0x40)
humidity=data0 *256 +data1
humidity=-6+((humidity*125.0)/65536.0)
print("Humidity is : ",'%.2f'% humidity,"%")
print("Temperature in C is: "'%.2f'%cTemp,"C")
F_Temp = open(path + "/" + F_Temp_name, "w")
strSHT = repr(cTemp)
F_Temp.write(strSHT)
F_Temp.close()
os.chmod(path + "/" + F_Temp_name, access_rights)
F_Hum = open(path + "/" +F_Hum_Name, "w")
print (F_Hum)
strSHT = repr(humidity)
F_Hum.write(strSHT)
F_Hum.close()
os.chmod(path + "/" + F_Hum_Name, access_rights)
7. run your code to test
[In terminal]
sudo python /Reef-Hardware/SHT20/SHT20_Reader.py
Output should be something like this :
('Humidity is : ', '51.04', '%')
('Temperature in C is: 23.57', 'C')
8. Add the job to crontab (scheduler) so it runs every 30 sec
Note crontab can't run jobs only every minute but the sleep allows you to delay the start
[In terminal]
sudo crontab -e
Add the below 2 lines at the end
* * * * * python /Reef-Hardware/SHT20/SHT20_Reader.py
* * * * * ( sleep 30 ; python /Reef-Hardware/SHT20/SHT20_Reader.py )
9. Add the sensor to Reef PI
- add the file-analog divers first (The script above creates 2 files you will use)
/dev/shm/ReeFHardwareComs/SHT20/SHT20_Hum
/dev/shm/ReeFHardwareComs/SHT20/SHT20_Temp
Now add the sensors as PH sensors like below:
10. Adjust your dashboard to show your charts
And you are done.
Product parameters:
Resolution: 12Bit
Repeatability: ±0.1%RH
Accuracy: 25 ° C ± 3% RH
Interchangeability: fully interchangeable
Response time: 1/e (63%) 25 ° C 8s
1m/s air 8s
Hysteresis: <±0.1%RH
Long-term stability: <±0.25% RH/yr
Temperature:
Resolution: 14Bit
Repeatability: ±0.1 °C
Accuracy: 25 ° C ± 0.3 ° C
Response time: 1/e (63%) 10S
Electrical characteristics:
Power supply:
Supply current: measuring 0.3mA standby 60μA
Sampling period: times greater than 2 seconds
Pin Description:
1, VDD power supply 3.3 ~ 5.5V DC
2, SDA bidirectional data line
3, GND ground, negative power supply
4, SCL clock line
I have been working on a little bit with humidity and Room temperature sensing with Reef-PI So and I want to share what I have so fare.
One of the key point with my selection was that this has to be cheap while strait forward to set up. The SHT20 is a logical choice as the price is about £2.5-3 with casing, works with DC 3.3 ~ 5.5V and communicates on I2C which makes easy to implement.
So Let's see how to add it to PI and than connect to Reef-PI software.
As of 29/03/2021 Reef OI has no Humidity Tab, only supports 1 Wire temperature sensors so we have to use the PH logic and File input to set it up. Not a biggie but not an elegant solution.
For file input I like to use virtual drives in the memory as it is fast, we don't need the files to be stored permanently and it saves your SD card and my experience the CPU usage goes down doing this.
So let's see the steps:
1. Wire up your device as per the above picture. (I won't give you pin numbers for SDA and SCL because you may have changed your configuration of pin-out. )
2. If not enabled i2C ye do it now. (I use the GUI you can do it in the hardcore way via command line too )
3. Install necessary packages such as I2C-Tools ( note: You may have some of these packages already installed. If you are not sure just run all it won't hurt)
Execute the following four commands respectively to install and restart.
[In terminal]
sudo apt-get install i2c-tools
sudo apt-get install python-smbus
sudo adduser pi i2c
sudo reboot
4. Test I2C and confirm the sensor address
[In terminal]
i2cdetect -y 1
To confirm whether the I2C link sensor is successful (0x40 in the picture is the address of my SHT20, everyone's sensor address may not be the same, this should be noted)
5. Create the directories you will store your code (You can change it but if you do fallow it trough in all the codes)
[In terminal]
sudo mkdir /Reef-Hardware
sudo mkdir /Reef-Hardware/SHT20
sudo chown pi -R /Reef-Hardware/
sudo chgrp pi -R /Reef-Hardware/
chmod 755 -R /Reef-Hardware/
6. Write Python temperature and humidity collection program ( Mine is a modified version of one I have found on-line )
Note: the code below is designed to be able to used manual testing also to be added to a scheduler (crontab)
Note 2: The script is writing temperature in C if you want to record in F you will have to change the line strSHT = repr(cTemp) to strSHT = repr(fTemp)
[In terminal]
nano /Reef-Hardware/SHT20/SHT20_Reader.py
import os
import smbus
import time
#Create directories and define output files
rootDir = "/dev/shm"
WorkDrir = "/ReeFHardwareComs/SHT20"
path = rootDir + WorkDrir
access_rights = 0o777
F_Hum_Name = "SHT20_Hum"
F_Temp_name = "SHT20_Temp"
if not os.path.isdir(path):
try:
os.makedirs(path, access_rights)
except OSError:
print ("Creation of the directory %s failed" % path)
else:
print ("Successfully created the directory %s " % path)
#Get I2C bus
bus = smbus.SMBus(1)
#SHT20 address,0x40(64)
addr =0x40
#Send Temperature measurement command
# 0xF3(243) NO HOLD master
bus.write_byte(addr,0xF3)
time.sleep(0.5)
#Read data back, 2byte
#Temp MSB, Temp LSB
data0 = bus.read_byte(0x40)
data1 = bus.read_byte(0x40)
#convert the data
temp =data0 *256 +data1
cTemp = -46.85 +((temp*175.72)/65536.0)
ftemp =cTemp*1.8+32
#Send humidity measurement command
bus.write_byte(0x40,0xF5)
time.sleep(0.5)
data0 = bus.read_byte(0x40)
data1 = bus.read_byte(0x40)
humidity=data0 *256 +data1
humidity=-6+((humidity*125.0)/65536.0)
print("Humidity is : ",'%.2f'% humidity,"%")
print("Temperature in C is: "'%.2f'%cTemp,"C")
F_Temp = open(path + "/" + F_Temp_name, "w")
strSHT = repr(cTemp)
F_Temp.write(strSHT)
F_Temp.close()
os.chmod(path + "/" + F_Temp_name, access_rights)
F_Hum = open(path + "/" +F_Hum_Name, "w")
print (F_Hum)
strSHT = repr(humidity)
F_Hum.write(strSHT)
F_Hum.close()
os.chmod(path + "/" + F_Hum_Name, access_rights)
7. run your code to test
[In terminal]
sudo python /Reef-Hardware/SHT20/SHT20_Reader.py
Output should be something like this :
('Humidity is : ', '51.04', '%')
('Temperature in C is: 23.57', 'C')
8. Add the job to crontab (scheduler) so it runs every 30 sec
Note crontab can't run jobs only every minute but the sleep allows you to delay the start
[In terminal]
sudo crontab -e
Add the below 2 lines at the end
* * * * * python /Reef-Hardware/SHT20/SHT20_Reader.py
* * * * * ( sleep 30 ; python /Reef-Hardware/SHT20/SHT20_Reader.py )
9. Add the sensor to Reef PI
- add the file-analog divers first (The script above creates 2 files you will use)
/dev/shm/ReeFHardwareComs/SHT20/SHT20_Hum
/dev/shm/ReeFHardwareComs/SHT20/SHT20_Temp
Now add the sensors as PH sensors like below:
10. Adjust your dashboard to show your charts
And you are done.
Product parameters:
Resolution: 12Bit
Repeatability: ±0.1%RH
Accuracy: 25 ° C ± 3% RH
Interchangeability: fully interchangeable
Response time: 1/e (63%) 25 ° C 8s
1m/s air 8s
Hysteresis: <±0.1%RH
Long-term stability: <±0.25% RH/yr
Temperature:
Resolution: 14Bit
Repeatability: ±0.1 °C
Accuracy: 25 ° C ± 0.3 ° C
Response time: 1/e (63%) 10S
Electrical characteristics:
Power supply:
Supply current: measuring 0.3mA standby 60μA
Sampling period: times greater than 2 seconds
Pin Description:
1, VDD power supply 3.3 ~ 5.5V DC
2, SDA bidirectional data line
3, GND ground, negative power supply
4, SCL clock line