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.)
/* This sketch demonstrates the capabilities of the pubsub library in combination It connects to an MQTT server then: It will reconnect to the server if the connection is lost using a blocking To install the ESP8266 board, (using Arduino 1.6.4+): */ #include <ESP8266WiFi.h> // Update these with values suitable for your network. const char* ssid = "BTHub6-65TZ"; WiFiClient espClient; void setup_wifi() { delay(10); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { randomSeed(micros()); Serial.println(""); void callback(char* topic, byte* payload, unsigned int length) { // Switch on the LED if an 1 was received as first character } void reconnect() { void setup() { void loop() { if (!client.connected()) { long now = millis();
|
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 import paho.mqtt.publish as publish
|
To receive the strings from the Pi
#!/usr/bin/env python import paho.mqtt.client as mqtt #import the client1 def on_connect(client, userdata, flags, rc): def on_message(client1, userdata, message): broker_address="192.168.1.23" client1 = mqtt.Client("outTopic") #create new instance
|
Please email me if you want to swap notes