Giter Site home page Giter Site logo

rsaikali / dht22-mqtt Goto Github PK

View Code? Open in Web Editor NEW
9.0 3.0 3.0 59 KB

Python script to get temperature and humidity measures from a DHT22 sensor published to a MQTT (message queue) broker.

License: Apache License 2.0

Dockerfile 31.24% Python 64.06% Shell 4.70%
raspberrypi dht22 mqtt docker

dht22-mqtt's Introduction

dht22-mqtt

PEP8 Docker

dht22-mqtt is a Python script to get temperature and humidity measures published to a MQTT (message queue) broker. Temperature and humidity are retrieved through a DHT22 sensor (connected to RaspberryPi GPIO in my case).

Measurements are retrieved using the given GPIO pin, and published into MQTT broker given the topic, host and port you have configured.

Hardware needed

This project needs a DHT22 temperature and humidity sensor connected to a RaspberryPi. Many examples are available on Google on how to plug the sensor to RaspberryPi GPIO pins.

Please note that I'll use the GPIO pin 4 in the following chapters.

How to use it?

dht22-mqtt can be used as a standalone Python script or as a Docker container.

Use as a standalone script

Install Linux requirements on RaspberryPi:

apt-get update
apt-get install --no-install-recommends -y libgpiod2 gcc build-essential

Git clone the project:

git clone https://github.com/rsaikali/dht22-mqtt.git
cd dht22-mqtt

Install Python requirements:

pip3 install -r requirements.txt

Configure through environment variables (those are default values if nothing given):

# Which Raspberry GPIO pin to use
export DHT22_PIN=4
# How many seconds between measures
export DHT22_CHECK_EVERY=10

# MQTT broker host
export MQTT_SERVICE_HOST=mosquitto.local
# MQTT broker port
export MQTT_SERVICE_PORT=1883
# MQTT broker user - optional
export MQTT_SERVICE_USER=mqtt_user
# MQTT broker password - optional
export MQTT_SERVICE_PASSWORD=very_strong_password
 
# MQTT broker topic to publish measures
export MQTT_SERVICE_TOPIC=home/livingroom
# MQTT client ID (default will be the hostname)
export MQTT_CLIENT_ID=dht22-mqtt-service

If you do not set user and password environment variables, auth is not used.

Launch application:

python ./dht22-mqtt.py

You should see output printed:

(...)
2020-02-16 10:57:50 [dht22-mqtt-service] [+] [home/livingroom/temperature] --- 20.7°C ---> [mosquitto.local:1883]
2020-02-16 10:57:50 [dht22-mqtt-service] [+] [home/livingroom/humidity] ------ 55.7% ----> [mosquitto.local:1883]
2020-02-16 10:58:00 [dht22-mqtt-service] [+] [home/livingroom/temperature] --- 20.9°C ---> [mosquitto.local:1883]
2020-02-16 10:58:00 [dht22-mqtt-service] [+] [home/livingroom/humidity] ------ 55.8% ----> [mosquitto.local:1883]
(...)

Use as Docker container

Use Docker hub image

An image is available on Docker Hub: rsaikali/dht22-mqtt

Needed environment is obviously the same as the standalone script mechanism, described in the Dockerfile:

Please note that you'll need to use --privileged when running Docker to have access to GPIO.

docker run --name dht22-mqtt \
           --privileged \
           --restart=always \
           --net=host \
           -tid \
           -e DHT22_PIN=4 \
           -e DHT22_CHECK_EVERY=10 \
           -e MQTT_SERVICE_HOST=mosquitto.local \
           -e MQTT_SERVICE_PORT=1883 \
           -e MQTT_SERVICE_USER=mqtt_user \
           -e MQTT_SERVICE_PASSWORD=very_strong_password \           
           -e MQTT_SERVICE_TOPIC=home/livingroom \
           -e MQTT_CLIENT_ID=dht22-mqtt-service \
           rsaikali/dht22-mqtt

Build your own Docker image

To build an linux/arm/v7 docker image from another architecture, you'll need a special (experimental) Docker multi-architecture build functionality detailled here: Building Multi-Arch Images for Arm and x86 with Docker Desktop

You'll basically need to activate experimental features and use buildx.

export DOCKER_CLI_EXPERIMENTAL=enabled
docker buildx create --use --name build --node build --driver-opt network=host
docker buildx build --platform linux/arm/v7 -t <your-repo>/dht22-mqtt --push .

Known issues

DHT22 sensor is not extremely reliable, you'll sometimes find errors in log, those are not a big deal, as it will retry by itself.

2020-02-16 11:05:00 [dht22-mqtt-service] [+] [home/livingroom/temperature] --- 22.7°C ---> [mosquitto.local:1883]
2020-02-16 11:05:00 [dht22-mqtt-service] [+] [home/livingroom/humidity] ------ 56.6% ----> [mosquitto.local:1883]
2020-02-16 11:05:10 [dht22-mqtt-service] [-] An error occured while getting DHT22 measure
2020-02-16 11:05:10 [dht22-mqtt-service] [-] Checksum did not validate. Try again.
2020-02-16 11:05:20 [dht22-mqtt-service] [+] [home/livingroom/temperature] --- 22.9°C ---> [mosquitto.local:1883]
2020-02-16 11:05:20 [dht22-mqtt-service] [+] [home/livingroom/humidity] ------ 56.8% ----> [mosquitto.local:1883]

When running, a required library may take 100% CPU or return bad reading, those are known bugs with issues in progress:

top - 11:10:00 up 12:27,  1 user,  load average: 2.00, 2.01, 2.03
Tasks: 130 total,   3 running, 127 sleeping,   0 stopped,   0 zombie
%Cpu(s):  9.0 us, 18.0 sy,  0.0 ni, 72.9 id,  0.0 wa,  0.0 hi,  0.1 si,  0.0 st
MiB Mem :   3854.5 total,   3134.0 free,    222.5 used,    498.0 buff/cache
MiB Swap:      0.0 total,      0.0 free,      0.0 used.   3521.4 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
11407 root      rt   0    1496    340    284 R 100.0   0.0  43:56.08 libgpiod_pulsei
    9 root      20   0       0      0      0 S   0.4   0.0   0:02.95 ksoftirqd/0

dht22-mqtt's People

Contributors

cyberdoc avatar rsaikali avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

dht22-mqtt's Issues

ModuleNotFoundError: No module named 'adafruit_dht'

Hi,
I´ve followed the instructions to start to use this script and send Temp. and Humidity towards my HA server...
But I´ve got this error when I try to run it. This is the log with the steps followed:

`pi@rasppi:/etc/rpimonitor $ sudo apt-get update
Des:1 http://raspbian.raspberrypi.org/raspbian bullseye InRelease [15,0 kB]
Obj:2 http://archive.raspberrypi.org/debian bullseye InRelease
Obj:3 http://giteduberger.fr rpimonitor/ InRelease
Des:4 http://raspbian.raspberrypi.org/raspbian bullseye/main armhf Packages [13,2 MB]
Descargados 13,2 MB en 40s (330 kB/s)
Update rpimonitor Packages Status
Leyendo lista de paquetes... Hecho

pi@rasppi:/etc/rpimonitor $ sudo apt-get install --no-install-recommends -y libgpiod2 gcc build-essential
Leyendo lista de paquetes... Hecho
Creando árbol de dependencias... Hecho
Leyendo la información de estado... Hecho
build-essential ya está en su versión más reciente (12.9).
gcc ya está en su versión más reciente (4:10.2.1-1+rpi1).
fijado gcc como instalado manualmente.
libgpiod2 ya está en su versión más reciente (1.6.2-1+rpi1).
El paquete indicado a continuación se instaló de forma automática y ya no es necesario.
libfuse2
Utilice «sudo apt autoremove» para eliminarlo.
0 actualizados, 0 nuevos se instalarán, 0 para eliminar y 1 no actualizados.
pi@rasppi:/etc/rpimonitor $ git clone https://github.com/rsaikali/dht22-mqtt.git
Clonando en 'dht22-mqtt'...
remote: Enumerating objects: 218, done.
remote: Counting objects: 100% (218/218), done.
remote: Compressing objects: 100% (136/136), done.
remote: Total 218 (delta 96), reused 131 (delta 42), pack-reused 0
Recibiendo objetos: 100% (218/218), 34.83 KiB | 178.00 KiB/s, listo.
Resolviendo deltas: 100% (96/96), listo.
pi@rasppi:/etc/rpimonitor $ cd dht22-mqtt

pi@rasppi:/etc/rpimonitor/dht22-mqtt $ sudo pip3 install -r requirements.txt
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting Adafruit-Blinka==4.1.0
Downloading https://www.piwheels.org/simple/adafruit-blinka/Adafruit_Blinka-4.1.0-py3-none-any.whl (90 kB)
|████████████████████████████████| 90 kB 619 kB/s
Collecting adafruit-circuitpython-dht==3.2.6
Downloading https://www.piwheels.org/simple/adafruit-circuitpython-dht/adafruit_circuitpython_dht-3.2.6-py3-none-any.whl (7.0 kB)
Collecting Adafruit-PlatformDetect==2.4.0
Downloading https://www.piwheels.org/simple/adafruit-platformdetect/Adafruit_PlatformDetect-2.4.0-py3-none-any.whl (13 kB)
Collecting Adafruit-PureIO==1.0.4
Downloading https://www.piwheels.org/simple/adafruit-pureio/Adafruit_PureIO-1.0.4-py3-none-any.whl (7.5 kB)
Collecting paho-mqtt==1.5.0
Downloading https://www.piwheels.org/simple/paho-mqtt/paho_mqtt-1.5.0-py3-none-any.whl (61 kB)
|████████████████████████████████| 61 kB 574 kB/s
Collecting psutil==5.7.0
Downloading https://www.piwheels.org/simple/psutil/psutil-5.7.0-cp39-cp39-linux_armv6l.whl (265 kB)
|████████████████████████████████| 265 kB 599 kB/s
Collecting pyftdi==0.44.2
Downloading https://www.piwheels.org/simple/pyftdi/pyftdi-0.44.2-py35-none-any.whl (113 kB)
|████████████████████████████████| 113 kB 948 kB/s
Collecting pyserial==3.4
Downloading https://www.piwheels.org/simple/pyserial/pyserial-3.4-py2.py3-none-any.whl (193 kB)
|████████████████████████████████| 193 kB 897 kB/s
Collecting pyusb==1.0.2
Downloading https://www.piwheels.org/simple/pyusb/pyusb-1.0.2-py3-none-any.whl (57 kB)
|████████████████████████████████| 57 kB 506 kB/s
Requirement already satisfied: RPi.GPIO==0.7.0 in /usr/lib/python3/dist-packages (from -r requirements.txt (line 10)) (0.7.0)
Collecting spidev==3.4
Downloading https://www.piwheels.org/simple/spidev/spidev-3.4-cp39-cp39-linux_armv6l.whl (38 kB)
Collecting sysv-ipc==1.0.1
Downloading https://www.piwheels.org/simple/sysv-ipc/sysv_ipc-1.0.1-cp39-cp39-linux_armv6l.whl (63 kB)
|████████████████████████████████| 63 kB 265 kB/s
Requirement already satisfied: rpi-ws281x>=4.0.0 in /usr/local/lib/python3.9/dist-packages (from Adafruit-Blinka==4.1.0->-r requirements.txt (line 1)) (4.3.2)
Installing collected packages: pyusb, pyserial, sysv-ipc, spidev, pyftdi, Adafruit-PureIO, Adafruit-PlatformDetect, Adafruit-Blinka, psutil, paho-mqtt, adafruit-circuitpython-dht
Attempting uninstall: pyusb
Found existing installation: pyusb 1.2.1
Uninstalling pyusb-1.2.1:
Successfully uninstalled pyusb-1.2.1
Attempting uninstall: pyserial
Found existing installation: pyserial 3.5b0
Not uninstalling pyserial at /usr/lib/python3/dist-packages, outside environment /usr
Can't uninstall 'pyserial'. No files were found to uninstall.
Attempting uninstall: sysv-ipc
Found existing installation: sysv-ipc 1.1.0
Uninstalling sysv-ipc-1.1.0:
Successfully uninstalled sysv-ipc-1.1.0
Attempting uninstall: spidev
Found existing installation: spidev 3.5
Not uninstalling spidev at /usr/lib/python3/dist-packages, outside environment /usr
Can't uninstall 'spidev'. No files were found to uninstall.
Attempting uninstall: pyftdi
Found existing installation: pyftdi 0.53.3
Uninstalling pyftdi-0.53.3:
Successfully uninstalled pyftdi-0.53.3
Attempting uninstall: Adafruit-PureIO
Found existing installation: Adafruit-PureIO 1.1.9
Uninstalling Adafruit-PureIO-1.1.9:
Successfully uninstalled Adafruit-PureIO-1.1.9
Attempting uninstall: Adafruit-PlatformDetect
Found existing installation: Adafruit-PlatformDetect 3.19.5
Uninstalling Adafruit-PlatformDetect-3.19.5:
Successfully uninstalled Adafruit-PlatformDetect-3.19.5
Attempting uninstall: Adafruit-Blinka
Found existing installation: Adafruit-Blinka 6.20.4
Uninstalling Adafruit-Blinka-6.20.4:
Successfully uninstalled Adafruit-Blinka-6.20.4
Attempting uninstall: psutil
Found existing installation: psutil 5.8.0
Not uninstalling psutil at /usr/lib/python3/dist-packages, outside environment /usr
Can't uninstall 'psutil'. No files were found to uninstall.
Attempting uninstall: paho-mqtt
Found existing installation: paho-mqtt 1.6.1
Uninstalling paho-mqtt-1.6.1:
Successfully uninstalled paho-mqtt-1.6.1
Attempting uninstall: adafruit-circuitpython-dht
Found existing installation: adafruit-circuitpython-dht 3.7.1
Uninstalling adafruit-circuitpython-dht-3.7.1:
Successfully uninstalled adafruit-circuitpython-dht-3.7.1
Successfully installed Adafruit-Blinka-4.1.0 Adafruit-PlatformDetect-2.4.0 Adafruit-PureIO-1.0.4 adafruit-circuitpython-dht-3.2.6 paho-mqtt-1.5.0 psutil-5.7.0 pyftdi-0.44.2 pyserial-3.4 pyusb-1.0.2 spidev-3.4 sysv-ipc-1.0.1
pi@rasppi:/etc/rpimonitor/dht22-mqtt $ ls -la
`

Once finished all the installation steps... I´ve edited the script dht22-mqtt.py and run it and get the error:

pi@rasppi:/etc/rpimonitor/dht22-mqtt $ python ./dht22-mqtt.py Traceback (most recent call last): File "/etc/rpimonitor/dht22-mqtt/./dht22-mqtt.py", line 6, in <module> import adafruit_dht ModuleNotFoundError: No module named 'adafruit_dht'

I´ve making some investigations regarding that module;

pi@rasppi:/etc/rpimonitor/dht22-mqtt $ sudo find / -name Adafruit* /etc/Adafruit_Python_DHT /etc/Adafruit_Python_DHT/dist/Adafruit_DHT-1.4.0-py3.9-linux-armv6l.egg /etc/Adafruit_Python_DHT/Adafruit_DHT.egg-info /etc/Adafruit_Python_DHT/build/lib.linux-armv6l-3.9/Adafruit_DHT /etc/Adafruit_Python_DHT/examples/AdafruitDHT.py /etc/Adafruit_Python_DHT/Adafruit_DHT /etc/mqtt-io/lib/python3.9/site-packages/Adafruit_DHT-1.4.0.egg-info /etc/mqtt-io/lib/python3.9/site-packages/Adafruit_DHT /etc/rpimonitor/Adafruit_Python_DHT-1.4.0 /etc/rpimonitor/Adafruit_Python_DHT-1.4.0/dist/Adafruit_DHT-1.4.0-py2.7-linux-armv6l.egg /etc/rpimonitor/Adafruit_Python_DHT-1.4.0/Adafruit_DHT.egg-info /etc/rpimonitor/Adafruit_Python_DHT-1.4.0/build/lib.linux-armv6l-2.7/Adafruit_DHT /etc/rpimonitor/Adafruit_Python_DHT-1.4.0/examples/AdafruitDHT.py /etc/rpimonitor/Adafruit_Python_DHT-1.4.0/Adafruit_DHT find: ‘/run/user/1000/gvfs’: Permiso denegado /root/.cache/pip/wheels/72/3e/03/21f854577a4a1e0456fa4e3112173c891e72ea5b2d19405526/Adafruit_Python_DHT-1.4.0-cp39-cp39-linux_armv6l.whl /usr/local/lib/python3.9/dist-packages/Adafruit_Blinka-4.1.0.dist-info /usr/local/lib/python3.9/dist-packages/Adafruit_Python_DHT-1.4.0.dist-info /usr/local/lib/python3.9/dist-packages/Adafruit_PureIO-1.0.4.dist-info /usr/local/lib/python3.9/dist-packages/Adafruit_PureIO /usr/local/lib/python3.9/dist-packages/Adafruit_PlatformDetect-2.4.0.dist-info /usr/local/lib/python3.9/dist-packages/Adafruit_DHT-1.4.0-py3.9-linux-armv6l.egg /usr/local/lib/python3.9/dist-packages/Adafruit_DHT-1.4.0-py3.9-linux-armv6l.egg/Adafruit_DHT /usr/local/lib/python3.9/dist-packages/Adafruit_DHT
Using this one: Adafruit_DHT for the import module I don´t see errors related to that:
import Adafruit_DHT

But I get another error that I can´t know what is talking about...

`pi@rasppi:/etc/rpimonitor/dht22-mqtt $ python ./dht22-mqtt.py
2022-02-19 14:03:58,968 [root] DEBUG ################################################################################
2022-02-19 14:03:58,972 [root] DEBUG # DHT22_PIN=4
2022-02-19 14:03:58,977 [root] DEBUG # DHT22_CHECK_EVERY=1
2022-02-19 14:03:58,981 [root] DEBUG # MQTT_SERVICE_HOST='192.168.10.15'
2022-02-19 14:03:58,990 [root] DEBUG # MQTT_SERVICE_PORT=1883
2022-02-19 14:03:58,999 [root] DEBUG # MQTT_SERVICE_USER=None
2022-02-19 14:03:59,003 [root] DEBUG # MQTT_SERVICE_PASSWORD=None
2022-02-19 14:03:59,008 [root] DEBUG # MQTT_SERVICE_TOPIC='blueroom/sensor'
2022-02-19 14:03:59,019 [root] DEBUG # MQTT_CLIENT_ID=None
2022-02-19 14:03:59,023 [root] DEBUG ################################################################################
Traceback (most recent call last):
File "/etc/rpimonitor/dht22-mqtt/./dht22-mqtt.py", line 46, in
dht22_sensor = Adafruit_DHT.DHT22(DHT22_PIN)
TypeError: 'int' object is not callable

Find attached the script in case you want to review it.

dht22-mqtt.txt
`
I´m using a Raspberry Pi (first version) with this OS version:
Linux rasppi 5.10.92+ #1514 Mon Jan 17 17:35:21 GMT 2022 armv6l GNU/Linux

Keep waiting your comments.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.