Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
130 lines (74 sloc) 5.89 KB
layout title description date sidebar comments sharing footer
page
MQTT
emonPi / emonBase MQTT technical guide
2014-12-18 21:49
true
false
true
true
emonPi Architecture Overview Fig.1 - emonPi Architecture Overview

MQTT is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport.

The emonPi with emonSD pre-built SD card by default runs a local Mosquitto MQTT server. This server is accessible (via authentication on port 1883. See MQTT Service Credentials.

{% linkable_title MQTT Publishers %}

{% linkable_title emonHub %}

EmonHub python service decodes the data received from the emonPi + RF nodes and publishes to the emonPi's Mosquitto MQTT server using the following two topic formats (both formats are published to parallel):

{% linkable_title 1. New MQTT 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 using another service e.g. openHAB.

[basetopic]/[node]/[keyname]

Note: the default base topic is emon/ this is set in ~/data/emonhub.conf and /var/www/emoncms/settings.php.

Example:

emon/emonpi/power1

{% linkable_title 2. Legacy CSV MQTT Topic Format %}

Used by older version of emonPi and emonPiLCD service. All data from a single node is published in CSV format to a single topic. More compact but does not allow naming of keys and difficult to subscribe to a single key.

emonhub/rx/[nodeID]/values format

Example:

emonhub/rx/10/values format

emonHub servie can be restarted with $ sudo service emonhub restart. Latest log file entries can be viewed via the Emoncms web interface admin or with: $ tail /var/log/emonhub/emonhub.log. All the data currently being published to MQTT topic can be viewed in real-time in the EmonHub log.

{% linkable_title Emoncms Publisher %}

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: house/power/solar.


{% linkable_title MQTT Subscribers %}

{% linkable_title Emoncms MQTT Service %}

Emoncms MQTT Input 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.

Example:

A power value published to emon/emonpi/power1 would result in an Emoncms Input from Node: emonpi with power1=XX.

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. See related blog posts below for example posting temperature data to Emoncms from Weather Underground using NodeRED running locally on the emonPi.

Emoncms MQTT Service is running by default on the emonSD software stack

The MQTT input service can be restarted using $ sudo service mqtt_input restart.The MQTT input service runs a phpmqtt_input.php script. Latest log file entries can be viewed via Emoncms web inerface admin or with: $ tail /var/log/emoncms.log

{% linkable_title EmonPiLCD Service %}

The emonPi's python LCD Service Script subscribes to the 'Legacy MQTT Topic' structure to obtain the real-time data to display on the emonPi LCD.

The emonPiLCD service can be restarted with: $ sudo service emonPiLCD restart. Latest log file entries can be viewed with: $ tail /var/log/emonpilcd/emonpilcd.log.


{% linkable_title Testing MQTT %}

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/#'

Note: # 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 helloWord :-)

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: emonpimqtt2016.


{% linkable_title Related Blog Posts %}

{% linkable_title Resources %}