Simple python script which reads temperature and humidity sensors and send it to a MQTT broker.
- DHT sensor on D4
- build with
sudo docker compose build
For the container, challenge are:
- access to the GPIO
- pretend to be a raspberry board
According to this SO answer,
Running the container in privileged mode works.
I didn't succeed by mapping --device /dev/gpiomem
.
Adafruit_dht lib depends on adafruit_platformdetect lib and RPi.GPIO. These try to determine the board model through config files which are usually present with Raspbian.
It is up to you to find the right infos for your board thanks to lib examples below.
Lib example:
from adafruit_platformdetect import Detector
detector = Detector()
print("Chip id: ", detector.chip.id) # Chip id: BCM2XXX
print("Board id: ", detector.board.id) # Board id: RASPBERRY_PI_3A_PLUS
It tries to read env vars before analyzing config files. So simply add this to the compose file:
environment:
- BLINKA_FORCECHIP=BCM2XXX
- BLINKA_FORCEBOARD=RASPBERRY_PI_3A_PLUS
Lib example:
import RPi.GPIO as GPIO
print(GPIO.RPI_INFO)
It reads the following config files:
- /proc/device-tree/system/linux,revision
- /proc/cpuinfo
Focus on /proc/cpuinfo
only.
The lib tries to find a line beginning with Hardware : BCMxxxx
.
On my raspbian installation, the line doesn't exist. So I copy-paste and edit the file.
cp /proc/cpuinfo proc-cpuinfo #copy paste
chmod 644 proc-cpuinfo #change permission for writing
nano proc-cpuinfo #edit the file and add , for example, `Hardware : BCM2837`
Then bind the file to the container. So in compose file:
volumes:
- ./proc-cpuinfo:/proc/cpuinfo:ro