Send DHT11 to MQTT

How It Works

Publish to MQTT

Python Code

  • PIN 3.3v (#1): Green LED (Power Indicator)
  • PIN 4 (#7): DHT11 Sensor Data
  • PIN 21 (#40): Red LED (MQTT Indicator)
#!/usr/bin/env python

import Adafruit_DHT  
import gpiozero  
import json  
import paho.mqtt.publish as mqtt_pub  
import time

dht_pin = 4  
led_pin = 21

dht = Adafruit_DHT.DHT11  
led = gpiozero.LED(led_pin)

broker = 'iot.eclipse.org'  
topic = 'easypi/dht11'  
interval = 10

while True:  
  h, t = Adafruit_DHT.read_retry(dht, dht_pin)
  obj = {
    'timestamp': int(time.time()),
    'humidity': int(h),
    'temperature': int(t)
  }
  txt = json.dumps(obj)
  print txt
  led.on()
  mqtt_pub.single(topic, txt, hostname=broker)
  led.off()
  time.sleep(interval)

Systemd Unit

# /etc/systemd/system/dht11.service
[Unit]
Description=DHT11 Sensor Daemon  
After=network.target

[Service]
ExecStart=/usr/local/bin/dht11.py  
#StandardOutput=null
#StandardError=null
Restart=always  
RestartSec=10

[Install]
WantedBy=multi-user.target  

Install Packages

$ wget https://github.com/adafruit/Adafruit_Python_DHT/archive/master.zip
$ unzip master.zip
$ cd Adafruit_Python_DHT-master
$ sudo apt install build-essential python-dev
$ sudo python setup.py install
$ python -c 'import Adafruit_DHT'

$ curl https://bootstrap.pypa.io/get-pip.py | sudo python
$ sudo pip install gpiozero paho-mqtt
$ python -c 'import gpiozero, paho.mqtt'

Start Service

$ sudo systemctl daemon-reload
$ sudo systemctl start dht11
$ sudo journalctl -f -u dht11
Sep 21 17:25:00 raspberrypi dht11.py[5036]: {"timestamp": 1474478690, "temperature": 25, "humidity": 56}  
Sep 21 17:25:00 raspberrypi dht11.py[5036]: {"timestamp": 1474478692, "temperature": 25, "humidity": 56}  
Sep 21 17:25:00 raspberrypi dht11.py[5036]: {"timestamp": 1474478694, "temperature": 25, "humidity": 56}  
Sep 21 17:25:00 raspberrypi dht11.py[5036]: {"timestamp": 1474478696, "temperature": 25, "humidity": 56}  
Sep 21 17:25:00 raspberrypi dht11.py[5036]: {"timestamp": 1474478698, "temperature": 25, "humidity": 56}  
$ sudo systemctl enable dht11

Subscribe to MQTT

$ sudo apt install mosquitto
$ mosquitto_sub -h iot.eclipse.org -t 'easypi/#' | jq .
{
  "timestamp": 1474515263,
  "temperature": 26,
  "humidity": 54
}
{
  "timestamp": 1474515271,
  "temperature": 26,
  "humidity": 54
}
{
  "timestamp": 1474515273,
  "temperature": 26,
  "humidity": 56
}

For subscriptions, two wildcard characters are supported:

  • A # character represents a complete sub-tree of the hierarchy and thus must be the last character in a subscription topic string, such as SENSOR/#. This will match any topic starting with SENSOR/, such as SENSOR/1/TEMP and SENSOR/2/HUMIDITY.
  • A + character represents a single level of the hierarchy and is used between delimiters. For example, SENSOR/+/TEMP will match SENSOR/1/TEMP and SENSOR/2/TEMP.

Tweet with Node-RED

DHT11

Server: iot.eclipse.org:1883  
Topic: easypi/dht11  

trigger 1hr

Send the existing msg.payload  
then wait for 1 hour  
then send nothing  

json

NONE  

transform

msg.payload.datetime = new Date(msg.payload.timestamp*1000).toISOString();  
msg.media = context.global.fs.readFileSync('/root/.node-red/icon.png');  
return msg;  
  • Download image to /root/.node-red/
  • Edit settings.js(add fs to functionGlobalContext)
  • Restart Node-RED service

TODO: Generate dynamic image to post!

template

EasyPi DHT11 Sensor  
===================
时间: {{payload.datetime}}
湿度: {{payload.humidity}}%
温度: {{payload.temperature}}°C

Tweet

@vimagick

Node-RED Dashboard

LED Bar Graph

#!/usr/bin/env python

import json  
import paho.mqtt.client as mqtt  
from gpiozero import LEDBarGraph

graph = LEDBarGraph(21, 20, 16, pwm=True)

def on_connect(client, userdata, flags, rc):  
    client.subscribe("easypi/dht11")

def on_message(client, userdata, msg):  
    print msg.payload
    obj = json.loads(msg.payload)
    hum = obj["humidity"]
    graph.value = hum / 100.0

client = mqtt.Client()  
client.on_connect = on_connect  
client.on_message = on_message

client.connect("iot.eclipse.org", 1883, 60)

client.loop_forever()