Sunspot Home

more notes for backup and a bad memory - -

Learning about MQTT and NodeRED - - - - - LESSON 1 _ WORK IN PROGRESS!!!

Objective - Control ESP8266 devices

on my home network by verbal commands to a
Amazon Alexa Dot

using MQTT and NodeRED

Notes for next time
There are many guides you can Google - I thank and used these and others.

I followed some of this-

I did - eventually -
cd /home/pi/node-RED
sudo wget
sudo apt-key add mosquitto-repo.gpg.key

Then make the repository available to apt:
cd /etc/apt/sources.list.d/

then a text file /etc/apt/sources.list.d/mosquitto-jessie.list
must contain
deb jessie main

(I got it with
sudo wget
not as listed in the link )

sudo apt-get update

Now install Mosquitto proper. There are three packages:

mosquitto is the MQTT broker (i.e. server)
mosquitto-clients are the command-line clients
python-mosquitto are the Python bindings

sudo apt-get install mosquitto mosquitto-clients python-mosquitto
sudo reboot

Testing Mosquitto

It starts on boot so -
sudo /etc/init.d/mosquitto stop
now open mosquitto in verbose mode
mosquitto -v

Open up two more terminal windows.
We are going to use mosquitto_pub and mosquitto_sub to send ourselves test messages.

Subscribing to MQTT Topic with Mosquitto
First we are going have our client subscribe to a topic called "debug". Use this command line in your "subscriber" window.
mosquitto_sub -h -i testSub -t debug
The host flag (-h) is the server running mosquitto, in this case, localhost.
The identity flag (-i) isn't required. When a client id is not provided, mosquitto_sub will create one.
The topic flag (-t) is the topic we want to subscribe to, in this case, "debug".
Notice on the server I didn't create the topic, the topic is created when the subscriber or publish first hooks into it.

Publish to MQTT Topic with Mosquitto
Now that we have a client listening to a topic, let's have a client publish to it, here's the command for that on the third window
mosquitto_pub -h -i testPublish -t debug -m 'Hello World'

Hello World appears in window 2

Also thanks to -

Using the same 3 terminal windows

stop the waiting mosquitto_sub window 2 with CTRL-C

In it start

mosquitto_sub -v -t 'topic/test'

In the pub window 3 do

mosquitto_pub -t 'topic/test' -m 'helloWorld'

So the main broker window is happy to support a different subject.

Talk and listen to an ESP8266-01

A first test using pubsubclient with this Sketch for the Arduino IDE

I used an ESP8266-01 with nothing connected.
NB! I powered it from a 3 terminal 1 amp 5 volt to 3.3 volt voltage dropper that has 470 microfarad capacitors across the input and output and was fed from a 5V 2Amp Macintosh wall power supply. The programming completes! - it used to stop after 85% of the dots. . . . . . . . . . . . . .

The only program modifications to mqtt_esp8266.ino were
const char* ssid = "BTHub6-xxxx";
const char* password = "mypassword";
const char* mqtt_server = "";

(I first put the LAN address of my Mosquitto server PLUS the port - but I now see that that is added later in the code.)

Basic ESP8266-01 MQTT example switch pin2 send text to broker
OK 7/12/16

This sketch demonstrates the capabilities of the pubsub library in combination
with the ESP8266 board/library.

It connects to an MQTT server then:
- publishes "hello world" to the topic "outTopic" every two seconds
- subscribes to the topic "inTopic", printing out any messages
it receives. NB - it assumes the received payloads are strings not binary
- If the first character of the topic "inTopic" is an 1, switch ON the ESP Led,
else switch it off

It will reconnect to the server if the connection is lost using a blocking
reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
achieve the same result without blocking the main loop.

To install the ESP8266 board, (using Arduino 1.6.4+):
- Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs":
- Open the "Tools -> Board -> Board Manager" and click install for the ESP8266"
- Select your ESP8266 in "Tools -> Board"


#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#define PIN 2 // Flash the LED connected between GPIO2 and 3.3 volts via a 1.5 Kohm resistor

// Update these with values suitable for your network.

const char* ssid = "BTHub6-65TZ";
const char* password = "rdtbNya9d4Gu";
const char* mqtt_server = "";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void setup_wifi() {

// We start by connecting to a WiFi network
Serial.print("Connecting to ");

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {


Serial.println("WiFi connected");
Serial.println("IP address: ");

void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print("] ");
for (int i = 0; i < length; i++) {

// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '1') {
digitalWrite(PIN, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is acive low on the ESP-01)
} else {
digitalWrite(PIN, HIGH); // Turn the LED off by making the voltage HIGH


void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
// Once connected, publish an announcement...
client.publish("outTopic", "hello world");
// ... and resubscribe
} else {
Serial.print("failed, rc=");
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying

void setup() {
pinMode(PIN, OUTPUT); // Initialize the PIN pin as an output
client.setServer(mqtt_server, 1883);

void loop() {

if (!client.connected()) {

long now = millis();
if (now - lastMsg > 5000) {
lastMsg = now;
snprintf (msg, 75, "hello world #%ld", value);
Serial.print("Publish message: ");
client.publish("outTopic", msg);


The Arduino IDE Serial Monitor shows a long list with #numbers
Publish message: hello world #795

On my Mac there is a terminal window connected to
I did sudo /etc/init.d/mosquitto stop then mosquitto -v

A second terminal window runs mosquitto_sub -v -t 'outTopic'
- it displays the repeating messages from the ESP8266 like outTopic hello world #17

In a third192.168.1.23 terminal window I typed
mosquitto_pub -t 'inTopic' -m 'hello Graham'
-and the Arduino IDE Serial Monitor shows
Message arrived [inTopic] hello Graham

I have a second Raspberry Pi3 running Mosquitto at
Also on the Mac I opened a terminal window to
and sent from Pi3 to the Mosquitto server with inTopic on
mosquitto_pub -h -t 'inTopic' -m 'from Pi3' - s
So the default is the localhost but -h lets you connect from a second Mosquito machine


My First NodeRED test

NodeRED and Mosquitto are loaded onto contains this screen shot

In the /home/pi/.node-red folder I did
npm install node-red-contrib-wemo-emulator

I double clicked the wemo emu node to edit the contents - these are -
Node name Node test inTopic 0or1 - this is the name on the chart
Friendly Name Node Test this is name Alexa responds to "Alexa turn Node Test on"- see 0 or 1 in the messages.
Unique ID 1234 - not sure what this is - my test of FAUXMOS (not with NodeRED) did not ask for these - 1234 was OK
Port 58312 - - - set to the largest FAUXMOS port number +1
On Topic inTopic
On Payload 1
Off Topic inTopic
Off Payload 2

I was amazed to find that "Alexa discover my devices" worked - these things seldom happen!

The hello world strings are coming from the ESP8266 running the Basic ESP8266 MQTT example above via outTopic
The outTopic mqtt input box contains Server and Topic outTopic

The inject boxes contain either
any string - LED OFF - or -
1 is first character
The 1 causes an LED connected between GPIO 2 and +3.3 volts (via 1.5 K ohm) to go on.
( as does "Alexa turn Node Test on")

In order to watch the data flow I also opened 2 terminal boxes to the NodRED and Mosquitto box at
one with -
mosquitto_sub -v -t 'outTopic'
and the other with
mosquitto_sub -v -t 'inTopic'

I did have some trouble with the default install of Mosquitto and only -
sudo /etc/init.d/mosquitto stop then mosquitto -v got the system working - no good for a "run from boot" system
- some sort of permission problem (yet again)?
I loaded the conf files from here -
and now it all runs on boot

Python !
From - thanks !
first -

pip install paho-mqtt
To flash the LED on GPIO 2 of the ESP8266 we just, as above, talk to the same topic inTopic on the Mosquitto MQTT server on

Create and run -

#!/usr/bin/env python
# /home/pi/Python_mqtt/

import paho.mqtt.publish as publish
import time
print("Sending 0...")
publish.single("inTopic", "0", hostname="")
print("Sending 1...")
publish.single("inTopic", "1", hostname="")
print("Sending 0...")
publish.single("inTopic", "0", hostname="")


To receive the strings from the Pi

#!/usr/bin/env python
# /home/pi/Python_mqtt/

import paho.mqtt.client as mqtt #import the client1
import time

def on_connect(client, userdata, flags, rc):
m="Connected flags"+str(flags)+"result code "\
+str(rc)+"client1_id "+str(client)

def on_message(client1, userdata, message):
print("message received " ,str(message.payload.decode("utf-8")))


client1 = mqtt.Client("outTopic") #create new instance
client1.on_connect= on_connect #attach function to callback
client1.on_message=on_message #attach function to callback
client1.connect(broker_address) #connect to broker
client1.loop_start() #start the loop





Please email me if you want to swap notes