Node Red and MQTT setup

996 views
Skip to first unread message

Paul F Prinsloo

unread,
Mar 26, 2017, 3:22:59 PM3/26/17
to SonoffUsers
Hello Guys 

I have battling now for three hours with this and for the life of it I am not able to get Node Red - MQTT to connect to the SonOff Basic. Is there somewhere that I can look at tutorial of sorts to see how the configuration of node red and the server is done?

Best Regards

Paul

Ps. The rest is working VERY well thank you for all the hardwork done here

Julian Knight

unread,
Mar 26, 2017, 4:02:44 PM3/26/17
to SonoffUsers
How are you trying to connect. Normally, you'd just connect the SONOFF to MQTT and similarly connect Node-RED. That works fine and many of us Node-RED fans are using both.

Paul F Prinsloo

unread,
Mar 26, 2017, 4:21:56 PM3/26/17
to SonoffUsers
I am trying to connect directly to the SonOff through node red , or should I use something else to facilitate the connection , I saw a tread on a MQTT Broker but I am not sure if that should be used as well?

Julian Knight

unread,
Mar 26, 2017, 5:19:49 PM3/26/17
to SonoffUsers
You'd need to give more information on how you are connecting. Node-RED is a general tool and there are lots of ways to connect things.

MQTT is a pretty standard way of connecting messages from IoT systems & ESP8266 based system such as the SONOFF make it easy because there is a nice standard library for Arduino IDE programming - written, as it happens, by one of the key devs for Node-RED as well.

So rather than trying to connect a SONOFF to Node-RED directly, set up an MQTT broker (mosquitto is very light on resources and ideal even on SBC's like a Raspberry Pi. All the hard work is done for you.

Paul F Prinsloo

unread,
Mar 26, 2017, 10:38:36 PM3/26/17
to SonoffUsers
Hello Julian 

I think that was what I missed , I have not set up a MQTT broker so that would be the first to do. This is the first time I play with Mqtt and Node Red. Is there a "tutorial" somewhere that I could use for the setup after mosquitto is installed?

Best Regards

Paul

Tobias

unread,
Mar 27, 2017, 10:01:45 AM3/27/17
to SonoffUsers
Hello Paul.

As Julian stated, you need to use a MQTT Broker, I as well would recommend Mosquitto (open source). 
I currently have two installations of Mosquitto, one installed on my OpenWrt router, the other on a Raspberry Pi. 

What kind of device / OS will you install your MQTT Broker at? 
This link should provide you some information of how to work with MQTT using Node-RED, http://noderedguide.com/tag/mqtt/

I recommend having the installation as simple as possible to start with (no TLS, username or password), make some tests and add the additional security later on.

Paul F Prinsloo

unread,
Mar 27, 2017, 10:12:01 AM3/27/17
to SonoffUsers
Hi Tobias 

I am currently working on a Win - Config with node Red running but I am going to change to a Pi , setting it up as we speak :). The thing is I would like to control the SonOff Basic so after fiddling a bit more I have found that you can control the device via a Http request. I basicly need On , Off and status. Would there be any reason to still push with the MQTT ?

Regards

Paul 

Julian Knight

unread,
Mar 27, 2017, 12:57:18 PM3/27/17
to SonoffUsers
Hi again Tobias.

Firstly, Mosquitto works just fine on Windows, though a Pi is generally more convenient for an always-on device - just make sure you get a really good MicroSD card such as the Samsung EVO because Linux as an OS takes rather a toll on poor quality SD cards. Alternatively, use an external SSD/HDD drive or try to minimise OS writes (mainly be redirecting logging to RAM).

Would you still NEED MQTT? No, not an absolute need. But it will most likely be a lot more reliable and will certainly save you effort in the long run.

For example, if you use an http connection from NR and your SONOFF crashes and restarts, it might no longer have the setting NR expected it too. With retained messages on MQTT, this is easily resolved as your restarting SONOFF only needs to subscribe to the one topic and it will get the last setting anyway for free.

More complex scenarios also become possible with MQTT. For example, you could set up a resilient cluster. Or you could have a second MQTT broker running outside your network (on a VPS for example) and replicate just some (the less sensitive) topics to that device around which you could build an Internet facing service. You probably don't want anything like that now, but you never know in the future.

Then there is security of course, if you use HTTP connections, you'd need to set this up yourself. With a broker, you just configure the broker and the client to work over https and have a uid/pw login. Just configuration, no coding, job done.

Paul F Prinsloo

unread,
Mar 28, 2017, 4:46:42 PM3/28/17
to SonoffUsers
Hello Julian

Ok I have now got Raspberry Pi with Mosquitto and node red running. Now for the setup:

Am I correct to assume that the SonOff Basic needs to connect to Mosquitto on the Pi ?  

Currently I have the Sonoff setup as follow 




And in Node Red the following settings



Then I attempt to send the following command , "cmnd/sonoff/power toggle" , and then wait for the response but the MQTT does not connect 



What am I missing or is it everything :)


Best regards


Paul






Julian Knight

unread,
Mar 28, 2017, 5:57:35 PM3/28/17
to SonoffUsers
The fact that Node-RED is not connecting is a totally different issue to whatever you are doing with the SONOFF. Don't forget, that's one of the reasons you are using MQTT, to disintermediate between systems.

So you need to find out what is causing the Pi to fail to connect. That is most likely because you are using the IP address of the Pi as the IP address to connect to the mosquitto server. As Node-RED and mosquitto are running on the same device, the correct IP address is either "localhost" or "127.0.0.1".

Incidentally, probably not the best idea to set the client ID on the SONOFF to a fixed value unless you remember to make every SONOFF device you run have different ID's. MQTT brokers generally get cranky if you have two devices with the same ID. That's why the default in the firmware you are using has part of the devices MAC address appended.

Finally, to make sure you know everything that is happening on the broker, it is well worth grabbing a copy of MQTT-Spy. It is Java based so will run on most devices with a GUI. Windows, Linux desktop and Mac are all supported. It will show you all the messages going through the broker and will let you format the messages for easy viewing too and you can send messages for testing.

MQTT-Spy will let you see exactly what topics the SONOFF is sending along with the payloads.

Ilkka Tengvall

unread,
Mar 29, 2017, 4:31:54 PM3/29/17
to SonoffUsers
Hi,

don't give up :) I have exactly your setup at home, and I'm now connecting more sonoffs to it. Like said earlier, you need to figure out one thing at the time. First, use mosquitto_sub and mosquitto_pub to debug your server. They are clients for sending stuff, and receiving stuff from mosquitto. First verify manually that you can send and receive stuff from mosquitto:

If you have not set username and password to your mqtt, this is how you listen to everything in one terminal:

mosquitto_sub -h server_name_or_ip -p 1883  -t +/# -v

then from the other terminal you can try to send sonoff messages, and see if the _sub sees it:

mosquitto_pub -h server_name_or_ip -p 1883  -t cmnd/sonoff/power1  -m TOGGLE

this would give stuff like this on the subscriber terminal (_sub):

cmnd/sonoff/power1 TOGGLE
stat/sonoff/RESULT {"POWER1":"ON"}
stat/sonoff/POWER1 ON
cmnd/sonoff/power1 TOGGLE
stat/sonoff/RESULT {"POWER1":"OFF"}
stat/sonoff/POWER1 OFF

See, as I actually have a sonoff switch under that channel name, it actually responds too with the status.

Once you get that workin, it's time to debug node-red. For example I listen to status messages like this from the server in NR: "stat/sonoff/POWER", and send messages like this: "cmnd/sonoff/power" and message is either 1 or 0. I can send you screenshots once you get mqtt working. Have faith :)


Ilkka Tengvall

unread,
Mar 29, 2017, 4:33:56 PM3/29/17
to SonoffUsers
ooops, don't send "power1" but "power". I have one sonoff dual, that actually has the power1 and power2 instead of power, thus such messages in my post.

Paul F Prinsloo

unread,
Mar 29, 2017, 6:34:39 PM3/29/17
to SonoffUsers
Hello IIkka 

I must admit this version is giving me a run for my money. I updated my other SonOff switch with the ESP firmware and that worked fairly easily (Also I cheated there is a very good tutorial on YouTube :) ) 
But I do like this ones web interface as you can also control via the web page. Ill be most grateful if you can send me some screen shots of your setup so that I could compare.

Best regards

Paul

Julian Knight

unread,
Mar 30, 2017, 4:34:32 AM3/30/17
to SonoffUsers
If you are having problems with Tasmota, you might want to back up a bit. Try resetting the device back to the defaults and only change the Wi-Fi login and MQTT broker IP address, nothing else. Then check Mosquitto either with MQTT-Spy or mosquitto_sub as previously suggested so that you have the confidence that your SONOFF is actually sending stuff to MQTT. Press the button a few times so that you can see exactly what topic is being output.

Then setup Node-RED so it listens for that topic and outputs to debug.

Only once you have all that working should you attempt to change anything else.

Honestly, this stuff works out of the box. 

Paul F Prinsloo

unread,
Mar 30, 2017, 4:39:24 AM3/30/17
to SonoffUsers
Good point , it is easy enough to reload the firmware and start fresh , will have a look and check if I can get it going. Half the problem is that I am not sure if the issue is me or the relay but I am 99% sure its me... :) 

A steep learning curve but Ill get there!

Paul F Prinsloo

unread,
Mar 30, 2017, 12:03:12 PM3/30/17
to SonoffUsers
Hello Julian , again ,
I am very happy to report that I have managed to connect to the sonoff / mqtt feed.  So fi I oust the button I can see in MQTTlens as well node red that the relay is on or off. 

Thank you everyone that gave some advise so that I could make this work!!

Now onto the next challange but I will start a new thread.



On Thursday, March 30, 2017 at 10:34:32 AM UTC+2, Julian Knight wrote:
Reply all
Reply to author
Forward
0 new messages