We use MQTT (Message Queuing Telemetry Transport) as one way of passing data between different hardware devices and software components within the OpenEnergyMonitor ecosystem.
The emonPi and emonBase running our emonSD software stack includes a local Mosquitto MQTT server. A device can connect to this server and publish data to a MQTT topic. A script on the emonPi/emonBase then subscribes and receives the data sent by the device.
This Mosquitto server is accessible on port 1883, the default username is
emonpi and password
The emonHub python service decodes the data received from the emonPi/emonBase + RF nodes and publishes to the emonPi/emonBase’s Mosquitto MQTT server using the following topic format:
Each data key (power) has its own MQTT topic as a sub-topic of the NodeID or NodeName. This MQTT topic structure makes it far easier to subscribe to a particular node/key of interest e.g.
emontx/power1 from another service.
Note: the default base topic is
emon/ this is set in
The emonHub service can be restarted with
$ sudo systemctl restart emonhub.
Latest log file entries can be viewed via the Emoncms web interface admin or with:
$ tail -f /var/log/emonhub/emonhub.log. All the data currently being published to MQTT topic can be viewed in real-time in the EmonHub log.
Data can be published to an MQTT topic using the
Publish to MQTT Emoncms Input Process. In the Input process ‘Text’ box add the topic, for example:
Emoncms MQTT Service
The Emoncms MQTT service subscribes to the MQTT base topic (default
emon/#) and posts any data on this topic to Emoncms Inputs with the NodeName and KeyName taken from the MQTT topic and sub-topic name.
A power value published to
emon/emonpi/power1 would result in an Emoncms Input from
Node: emonpi with
Data from any service (internal or external) that connect to the MQTT server (assuming authentication) and publishes to the base topic
emon/ will appear in Emoncms.
Emoncms MQTT Service is running by default on the emonSD software stack
The MQTT input service can be restarted using
$ sudo systemctl restart emoncms_mqtt. The Emoncms MQTT service runs the
Latest log file entries can be viewed via Emoncms web interface admin or with:
$ tail /var/log/emoncms/emoncms.log
The emonPi’s python LCD Service Script subscribes to the MQTT messages published by emonHub in order to obtain the real-time data to display on the emonPiLCD.
The emonPiLCD service can be restarted with:
$ sudo systemctl restart emonPiLCD.
Latest log file entries can be viewed with:
$ tail /var/log/emonpilcd/emonpilcd.log.
Install the mosquitto-clients package
To test from the command line you first need to install the
sudo apt install -y mosquitto-clients
Testing MQTT From the command line
Note all example commands presume you are using the MQTT Broker on the emonPi/emonBase/emonSD. Change the username/password/host if not.
To view all MQTT messages subscribe to
emon/# base topic :
$ mosquitto_sub -v -u 'emonpi' -P 'emonpimqtt2016' -t 'emon/#'
To view all MQTT messages for a particular node subscribe to sub-topic:
$ mosquitto_sub -v -u 'emonpi' -P 'emonpimqtt2016' -t 'emon/emonpi/#'
# denotes a wild-card
Test publishing and subscribing on a test topic
Subscribe to test topic:
$ mosquitto_sub -v -u 'emonpi' -P 'emonpimqtt2016' -t 'test'
Open another shell window to publish to the test topic :
$mosquitto_pub -u 'emonpi' -P 'emonpimqtt2016' -t 'test' -m 'helloWorld'
If all is working we should see
View the data from a browser or other device
To avoid connecting via SSH alternately you could use MQTTlens Chrome Extension or any other MQTT client connected to the emonPi IP address on port 1883 with user name:
emonpi and password:
There are also MQTT clients for your phone or tablet.