Library for the DHT11 & DHT22 digital-output relative humidity & temperature sensors connected to a Raspberry Pi.
The library
- is based upon this project (recommend to read the blog).
- if a sensor value can not be read, NaN is returned.
- tested with the sensors DHT11 and DHT22 (see sample code) connected to a Raspberry Pi 3.
Install
Copy jdht.jar, jdht.xml and dht.jar to the B4J additional libraries folder. Copy libdht.so to the dirapp folder.
When testing on the Raspberry Pi using the B4J-Bridge, ensure libdht.so is in the tempjars folder.
Wiring
DHT11 = Raspberry Pi 3
5v = 5v
Data = Physical Pin #16 [BCM23]
GND = GND
DHT22 = Raspberry Pi 3
5v = 5v
Data = Physical Pin #15 [BCM22]
GND = GND
Example B4J Non-UI Program
Sub Process_Globals
Private dht11 As DHT
Private dht22 As DHT
Private timer1 As Timer
End Sub
Sub AppStart (Args() As String)
Log("AppStart")
' Init the sensor with sensortype and pin (BCM)
dht11.Initialize(11, 23)
dht22.Initialize(22, 22)
timer1.Initialize("timer1", 2000)
timer1.Enabled = True
StartMessageLoop
End Sub
Sub timer1_tick
Log($"DHT11 ${DateTime.Time(DateTime.Now)}: ${dht11.Temperature}*, ${dht11.Humidity}%"$)
Log($"DHT22 ${DateTime.Time(DateTime.Now)}: ${dht22.Temperature}*, ${dht22.Humidity}%"$)
End Sub
Output
dht11 09:51:01: 19.9*, 41.7%
dht22 09:51:01: 19.9*, 41.7%
dht11 09:51:03: 18.0*, 44.6%
dht22 09:51:03: 18.0*, 44.6%
dht11 09:51:11: 18.0*, 56.4%
dht22 09:51:12: 18.0*, 56.4%
dht11 09:51:23: 18.7*, 71.7%
dht22 09:51:23: 18.7*, 71.7%
ChangeLog
20170306 v1.0