I got the Am2302 sensor working on my Pi 4 running OpenWrt.
Thanks to @eduperez and @AndrewZ for help and advice.
My Router details:
OpenWrt SNAPSHOT r15090-52a5d0d27f / LuCI Master git-20.332.74200-03c77da
Kernel: 5.4.80
Raspberry Pi 4
Here are the steps I followed:
I connected the sensor to GPIO pins on the Pi as explained here but obtained readings not via python but using the sysfs method reading values from a device tree file set up like this..
opkg update
opkg install kmod-iio-dht11
modprobe dht11
This loads the needed dht11 module.. industrialio is also loaded:
lsmod | grep dht
dht11 16384 0
industrialio 65536 1 dht11
As mentioned in the information about the dht11 module..
root@OpenWrt:/sys/bus/iio/devices# opkg list | grep -e kmod-iio-dht11
kmod-iio-dht11 - 5.4.80-1 - support for DHT11 and DHT22 digitial humidity and temperature sensors attached at GPIO lines. You will need a custom device tree file to specify the GPIO line to use.
It requires a device tree file to specify the GPIO line to use and then where readings can be read from.
For this, there is already a device tree overlay in: /boot/overlays/dht11.dtbo
To make use of it, to /boot/config.txt
you add this:
dtoverlay=dht11,gpiopin=4
Now reboot the Pi.
You you will see that a "device tree" exists at /sys/bus/iio/devices/iio:device0
for your attached sensor and you can "cat" two files for temperature and humidity readings.
cat /sys/bus/iio/devices/iio:device0/in_temp_input
cat /sys/bus/iio/devices/iio:device0/in_humidityrelative_input
Sometimes the readings don't give values. This is expected and is to do with the features of the sensor. A workaround created by adam van gaalen in this really helpful Raspberry Pi forum thread uses a tcsh script that takes five consecutive readings and reports that which occurred most often in the sample.
I had to install bc and tsch and make the script executable.
opkg update
opkg bc
opkg tsch
chmod +x scriptname
Here is the script:
#!/bin/tcsh
#
@ NTRIES = 0
echo '' >& TEMP_readings
echo '' >& RH_readings
while ( ${NTRIES} < 5 )
/bin/cat /sys/devices/platform/dht11@4/iio:device0/in_temp_input >>& TEMP_readings
/bin/cat /sys/devices/platform/dht11@4/iio:device0/in_humidityrelative_input >>& RH_readings
@ NTRIES = ${NTRIES} + 1
end
#
set TEMP = "`/bin/cat TEMP_readings |& /bin/grep -v dht11@0 | /bin/grep -v -- '-' | /usr/bin/uniq -c | /usr/bin/sort -n | /usr/bin/tail -1 | /usr/bin/tr -s ' ' ' ' | /bin/sed -e 's/^ //' | /usr/bin/cut -f2 -d' '`"
set RH = "`/bin/cat RH_readings |& /bin/grep -v dht11@0 | /bin/grep -v -- '-' | /usr/bin/uniq -c | /usr/bin/sort -n | /usr/bin/tail -1 | /usr/bin/tr -s ' ' ' ' | /bin/sed -e 's/^ //' | /usr/bin/cut -f2 -d' '`"
#
# Format the readings to one decimal place
# Credit: https://unix.stackexchange.com/a/264283/400373
set T = "`echo 'scale=1; $TEMP' / 1000 | bc -l`"
set H = "`echo 'scale=1; $RH' / 1000 | bc -l`"
echo "TEMP = ${T}, RH = ${H}"
#
# This was the original way of printing out the readings
#echo "TEMP = ${TEMP}, RH = ${RH}"
#
exit 0
Then run the script to get sensor values from AM2302 attached to Pi 4 running OpenWrt.
./scriptname
I have two AM2302 sensors at home now. One uses the Adafruit_DHT library in which Python is used to get the readings. And now this other attached to OpenWrt using the sysfs approach. I positioned them both close together to compare their readings. The temperature readings are about 1C apart however for the humidity readings one sensor is saying 65%RH but the other is saying 88%RH.
I guess the difference in RH is down to cheap sensors rather than any difference using the sysfs approach to reading sensor values instead of python..
Cheers,
Flex