MQTT – a simple way to collect data in your home

MQTT(Message Queuing Telemetry Transport) is a lightweight publish-subscribe based messaging protocol. It is designed for connecting remote locations with very low bandwidth and consists of clients (publisher or subscriber) communicating with the server (broker) .

Broker

One example for a MQTT borker is Eclipse Mosquitto https://mosquitto.org/ – you can install it for example on your raspbian OS with the following command: apt-get install mosquitto

https://fhem.de/ has a built-in MQTT server which can be used very easily.

Publisher

mosquito has a publisher as well:

Syntax: mosquitto_pub -h IP_BROKER -p 1883 -u USERNAME -P PASSWORD -m $TEMP -t class/node/measurement

Example: mosquitto_pub -h 192.168.0.1 -p 1883 -u mqtt -P 1234 -m 22 -t sensor/bathroom/temperature

DHT22 & MQTT

Example bash script to read a DHT22 sensor on a raspberry PI and publish the data to a MQTT broker:

#! /bin/bash
 
# read temperature from DHT sensor
INPUT=$(/home/pi/smarthome/dht_scripts/AdafruitDHT.py 22 4 |tail -n1 |cut -d"=" -f2 |cut -d"*" -f1)
 
TEMP=$(echo "$INPUT" )
 
#push to mqtt broker
 
mosquitto_pub -h 192.168.0.1 -p 1883 -u mqtt -P 1234 -m $TEMP -t sensor/bathroom/temperature
#! /bin/bash
 
# read humidity from DHT sensor
INPUT=$(/home/pi/smarthome/dht_scripts/AdafruitDHT.py 22 4 |tail -n1 |cut -d"y" -f2 |cut -d"=" -f2 |cut -d"%" -f1)
 
TEMP=$(echo "$INPUT" )
 
#push to mqtt broker
 
mosquitto_pub -h 192.168.0.1 -p 1883 -u mqtt -P 1234 -m $TEMP -t sensor/bathroom/humidity
 

Adafruit DHT library