Coder Social home page Coder Social logo

dht-mqtt's Introduction

DHT-MQTT

Read humidity and temperature measurements from a DHT11/DHT22 sensor connected to a Raspberry Pi and publish them to an MQTT broker.

It also supports the AM2302 sensor, which is the wired version of DHT22.

Run as a service

Requirement: Python 3, pip

Install

Install the CircuitPython-DHT library:

pip3 install adafruit-circuitpython-dht paho-mqtt
sudo apt-get install libgpiod2

Configure and run as a service

  1. Clone and enter the directory
git clone xxx
cd dht-mqtt
  1. Create config.env

Copy from config.env.example.

Set the right values.

  • BROKER (string) is the MQTT broker's host name or IP address
  • BROKER_PORT (integer) is the MQTT broker's port
  • PIN (integer) is the GPIO pin number that the sensor data wire port is connected to. The python board module can be used to list available pins on the board. Learn more here.
  • SENSOR (string) is the sensor type. It should be either of: DHT11, DHT22, DHT21
  1. Run to test:
./run.sh
  1. Turn into a service

We use the dht-mqtt.service file to create the service. In the service file, the path for the run.sh script is set to /home/pi/dht-mqtt/run.sh. Modify the file to have the correct path and user.

Make a symbolic link to the service file:

sudo ln -s /home/pi/dht-mqtt/dht-mqtt.service /lib/systemd/system/dht-mqtt.service

Load the service file, enable (start on boot), and start now:

sudo systemctl daemon-reload
sudo systemctl enable --now dht-mqtt

Check the logs:

journalctl -u dht-mqtt -n 100 -f

Use systemctl restart dht-mqtt to load new configurations.

Run in a Docker container

WIP

docker run --privileged farshidtz/adafruit_dht [11|22|2302] GPIOpin#

For example to read from a DHT22 attached to GPIO4:

docker run --privileged farshidtz/adafruit_dht 22 4

Build Locally

docker build -t my_dht .

dht-mqtt's People

Contributors

farshidtz avatar

Stargazers

 avatar  avatar

Watchers

 avatar

dht-mqtt's Issues

Add snap packaging

The current systemd service is useful on Raspberry Pi Zero W (armv6) on which snaps aren't supported. Adding a snap is useful for running the service on armv7 and armv8 versions of the Raspberry Pi, to allow automatic updates and additional snap features.

The run.sh script currently sources an env file. It can be modified to read snap options when executed by a snap, e.g. by checking if SNAP is set, and export the equivalent environment variables.

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.