I am using Mosquitto, an open-source, multi-platform, broker and client. I chose it because it is easy to use on my platform of choice (Ubuntu), is being actively developed, and has a wide range of security options including encrypted connections, access control lists, and isolation of clients by group. Of course, there are a number of choices for a MQTT broker and any of them will work with T&L Nodes
Installing
Mosquitto is easy, but configuration can be quite
complex. The most difficult task is not the configuration itself, rather
it is coming up with a plan to mange
access and security. Developing that plan, and setting up a
configuration to implement it, is more than I can cover here.
You should read the manual page for
mosquitto-conf
, the well commented example file
(/usr/share/doc/mosquitto/examples/mosquitto.conf.gz
on Debian
based systems), and probably seek out help from other sources.
In this post I cut through the complexity and present a
simple setup that will work for the
T&L Node and its
Console. Of course the MQTT settings in the
T&L Node Firmware must match these settings, so a
matching example of an include/mqtt_config.h
is also presented.
WARNING: This MQTT configuration is a totally unsecured configuration. Do not use it except as a test and learning exercise. Under no circumstances should you deploy this configuration.
###Installing mosquitto
While there is an official package available for Ubuntu (since Ubuntu
11.10), the version from the
development PPA
is more up-to-date.
Installation from the PPA is only slightly more difficult than the official package:
$ sudo add-apt-repository ppa:mosquitto-dev/mosquitto-ppa
$ sudo apd-get update
$ sudo apt-get install mosquitto mosquitto-clients
###Simple Mosquitto Configuration For this example I am going to use 192.168.148.2 for the MQTT broker. Most likely your MQTT broker will be running on a system at a different IP address. Simply replace 192.168.148.2 with that IP address anywhere you see it below.
Here is the whole file. The file name should be
/etc/mosquitto/mosquitto.conf
.
#
# Example mosquitto.conf file for T&L Nodes
#
# WARNING: This is a unsecured configuration. Do not use this for
# anything other than an experiment.
#
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_type all
log_dest file /var/log/mosquitto/mosquitto.log
# For esp8266+mqtt
listener 8266
mount_point raw/
# For HTML console
listener 10001
protocol websockets
The persistence
values improve the Console startup
experience. The gauges will display the last value received by the
broker rather than having to wait for the next measurement. The
down side is that the broker can report an initial value for a node that
is no long available (something to fix later).
Since this an experimental setup, we will log all message types.
Two listeners are required. One will be used by the T&L Nodes and the other by the Console.
8266
is a port number. The port number and the IP address of the MQTT
broker go in the include/mqtt_config.h
that is compiled into the
firmware. The mount_point
directive will organize all of the
topic messages from the T&L Nodes beneath the raw/
topic.
This makes it easy to find the T&L Node messages. The choice
for the mount_point
name is fairly arbitrary and may be multiple
levels. I chose raw/
to indicate that the values in the topic are
exactly as reported by the T&L Node (they will be scaled by the
updateGauge() function
of the Console).
The listener used by the Console is setup on port 10001
.
A separate listener is needed because the protocol is different:
Console uses websockets
. Note that any clients connecting
to this listener will be able to subscribe or post to any topic on
the MQTT broker. Be warned!
After your changes to the /etc/mosquitto/mosquitto.conf
file are
complete, restart the broker:
$ sudo service mosquitto restart
Check the log file, /var/log/mosquitto/mosquitto.log
, for errors.
###T&L Node Firmware Configuration In order to keep the firmware simple, network values are hard-coded. So we must modify one of the include files, rebuild, and flash the T&L Node.
Source for the T&L Node Firmware is on GitHub.
The network values go in the include/mqtt_config.h
file. That file
does not exist until you create it. The following will work with the
mosquitto
configuration above, but it is not complete. You will need
to add your own network credentials (you do run a secured network-
right?).
#ifndef _MQTT_SETTINGS_H
#define _MQTT_SETTINGS_H
/* Change this value to load default configurations */
/* Please don't change or if you know what you doing */
#define CFG_HOLDER 0x00FF55A4
#define CFG_LOCATION 0x3C
#define CLIENT_SSL_ENABLE
/*DEFAULT CONFIGURATIONS*/
#define MQTT_HOST "192.168.148.1"
#define MQTT_PORT 1880
#define MQTT_BUF_SIZE 1024
#define MQTT_KEEPALIVE 30 /*second*/
#define MQTT_CLIENT_ID "DVES_%08X"
#define MQTT_USER "DVES_USER"
#define MQTT_PASS "DVES_PASS"
#define STA_SSID "DVES_HOME"
#define STA_PASS "yourpassword"
#define STA_TYPE AUTH_WPA2_PSK
#define MQTT_RECONNECT_TIMEOUT 5 /*second*/
#define DEFAULT_SECURITY 0 // 0 = no security
#define QUEUE_BUFFER_SIZE 2048
#define PROTOCOL_NAMEv31 /*MQTT version 3.1 compatible with Mosquitto
v0.15*/
//PROTOCOL_NAMEv311 /*MQTT version 3.11 compatible with
https://eclipse.org/paho/clients/testing/*/
#endif
Copy the include/mqtt_config.tmpl
file to include/mqtt_config.h
and
make the following changes.
Set the IP address and MQTT port of the broker. The port is from the first listener section. So for this example the settings would be:
#define MQTT_HOST "192.168.148.2"
#define MQTT_PORT 8266
Set the credentials for your network:
#define STA_SSID "your_network_ssid"
#define STA_PASS "your_password"
#define STA_TYPE AUTH_WPA2_PSK
The options for STA_TYPE
are: AUTH_OPEN, AUTH_WEP,
AUTH_WPA_PSK, AUTH_WPA2_PSK, AUTH_WPA_WPA2_PSK, AUTH_MAX
.
The other setting you may want to change in mqtt_config.h
is
MQTT_CLIENT_ID
. The value has two parts, a prefix (DVES_
) and a
format string (%08X
). You can change the prefix if you like, but you
should not change the format string. The format
string will be replaced at run-time by a portion of the ESP-8266
MAC address to form a unique ID for MQTT. The MAC address is unique for
each ESP-8266, so it makes a good device ID.
Now rebuild the firmware and flash it to the T&L Node (more info in the firmware README file.):
$ make
$ make flash
The T&L Node Firmware will send a report when it is reset and every 5 minutes after that. You should see a gauge for the device on the Console. The date stamp on the gauge will be updated when a new report is received:
Subscribe to this blog via RSS.
Firmware 1
Hardware 5
Console 1
Tools 4
Background (2) Firmware (1) Hardware (5) Infrastructure (1) Console (1) T&l node firmware (1) Tools (4) Tlphnodev2 (2)
Comments