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-

http://www.switchdoc.com/2016/02/tutorial-installing-and-testing-mosquitto-mqtt-on-raspberry-pi/

I did - eventually -
cd /home/pi/node-RED
sudo wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
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 http://repo.mosquitto.org/debian jessie main

(I got it with
sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
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

https://www.baldengineer.com/mqtt-tutorial.html

WINDOW 1
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.

WINDOW 2
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 127.0.0.1 -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.

WINDOW 3
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 127.0.0.1 -i testPublish -t debug -m 'Hello World'

Hello World appears in window 2

Also thanks to -

https://www.baldengineer.com/mqtt-tutorial.html
http://jpmens.net/2013/09/01/installing-mosquitto-on-a-raspberry-pi/

 

https://www.rs-online.com/designspark/building-distributed-node-red-applications-with-mqtt

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
https://github.com/knolleary/pubsubclient/blob/master/examples/mqtt_esp8266/mqtt_esp8266.ino

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 = "192.168.1.23";

(I first put the LAN address of my Mosquitto server 192.168.1.23 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":
http://arduino.esp8266.com/stable/package_esp8266com_index.json
- 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 = "192.168.1.23";

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

void setup_wifi() {

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

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

randomSeed(micros());

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

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

// 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())) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic", "hello world");
// ... and resubscribe
client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}

void setup() {
pinMode(PIN, OUTPUT); // Initialize the PIN pin as an output
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}

void loop() {

if (!client.connected()) {
reconnect();
}
client.loop();

long now = millis();
if (now - lastMsg > 5000) {
lastMsg = now;
++value;
snprintf (msg, 75, "hello world #%ld", value);
Serial.print("Publish message: ");
Serial.println(msg);
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 192.168.1.23
I did sudo /etc/init.d/mosquitto stop then mosquitto -v

A second 192.168.1.23 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 192.168.1.22
Also on the Mac I opened a terminal window to 192.168.1.22
and sent from Pi3 to the Mosquitto server with inTopic on 192.168.1.23
mosquitto_pub -h 192.168.1.23 -t 'inTopic' -m 'from Pi3' - s
So the default is the localhost but -h lets you connect from a second Mosquito machine


NodeRED

My First NodeRED test

NodeRED and Mosquitto are loaded onto 192.168.0.23
http://192.168.1.23:1880/# contains this screen shot

In the /home/pi/.node-red folder I did
npm install node-red-contrib-wemo-emulator
from https://www.npmjs.com/package/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 192.168.1.23:1883 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 192.168.1.23
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 -
https://xperimentia.com/2015/08/20/installing-mosquitto-mqtt-broker-on-raspberry-pi-with-websockets/
and now it all runs on boot

Python !
From https://www.baldengineer.com/mqtt-tutorial.html - 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 192.168.1.23

Create and run -

#!/usr/bin/env python
# /home/pi/Python_mqtt/flash_ESP_inTopic.py

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

 

To receive the strings from the Pi

#!/usr/bin/env python
# /home/pi/Python_mqtt/ESP8266_client2.py

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)
print(m)

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

broker_address="192.168.1.23"

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
time.sleep(1)
client1.connect(broker_address) #connect to broker
client1.loop_start() #start the loop
client1.subscribe("outTopic")
time.sleep(6)
client1.disconnect()
client1.loop_stop()

 

 

 

 


Please email me if you want to swap notes

SUNSPOT HOME