Page 1 of 1

MQTT to Relay with Node-Red

Posted: 24 Apr 2016, 11:53
by JR01
Hi, I see a lot of discussions on using MQTT to actuate a relay. For instance this by @tozett, http://www.esp8266.nu/forum/viewtopic.p ... elay#p4271, that refers to the way Domoticz does it, and this one http://www.esp8266.nu/forum/viewtopic.p ... nHab#p5921 that controls MQTT for relay switching from OpenHab by @anantbhula, then I find very interesting MQTT import, http://www.esp8266.nu/forum/viewtopic.php?f=4&t=1148 by @namirda, which I would like use in future if it becomes part of the mainstream package, or easy to add.

I have originally planned to use OpenHab, so above topic on MQTT + OpenHab would work. But for now I am having so much fun with Node-Red, and as a controller it is simple, (for now). For now, I would like to send an MQTT message to the channel that the ESP have subscribed to. I am testing with matt-spy, where I publish:
- Topic: /RHZA/ESP12-02/GPIO/16/ (I also tried it without the last /)
- Data: '-n 1' and also tried with '1'

In order to switch on a relay on GPIO16, and by the way, the http command http://192.168.2.64/control?cmd=GPIO,16,1 works, it switches on the relay.

The ESP's name is ESP12-02, and it subscribed to /RHZA/%sysname%/#

Question 1: The actuation of the relay with http works without me defining anything in the devices section. To get this to work, should I add a Devices item, maybe with the switch? From the wiki http://www.esp8266.nu/index.php/Relais it does not seam necessary.
Question 2: Anybody has ideas on how to use MQTT pure, to actuate a relay?

Re: MQTT to Relay with Node-Red

Posted: 24 Apr 2016, 14:22
by tozett
SEND
if you define your relay-item in the devices section,
you can set in the config to openhab-mqtt,
than you can configure node-red to get the MQTT-messages.
(it assume, the relay/gpio can send his states via MQTT

and check them in the debug section (see picture here).


RECEIVE
than, (i did not do this yet), it think you can install the experimente MQTT receive plugin,
and use RULES to switch the Relay via MQTT.

??
may you go this way ... :roll:

Re: MQTT to Relay with Node-Red

Posted: 25 Apr 2016, 12:00
by JR01
tozett wrote:SEND
if you define your relay-item in the devices section,
you can set in the config to openhab-mqtt,
than you can configure node-red to get the MQTT-messages.
(it assume, the relay/gpio can send his states via MQTT
Thank you tozett, I am sending all my sensors successfully onto MQTT using Openhab-MQTT option on sensors. I do rain water, warm water flow from geyser, temp outside DS18b20, temp / hum inside, which the messages from MQTT is subscribed in Node-Red, which then processes data, and sends to Thingspeak, a MySQL on RasPi, as well as serve it to web pages using node-red nodes, which provides house participants to info, like cost of warm water used last hour / day / previous days / weeks.

As per your suggestion, which you say you have also not done yet, I now need to send MQTT messages to ESP to switch geyser on/off. I will try your suggestions.... Tx.

Re: MQTT to Relay with Node-Red

Posted: 25 Apr 2016, 13:23
by tozett
as node-red may can send http-request to fulfill your goal,
i am interested in your succesions in switching gpios with mqtt.
i would think, doing most things on the espeasy with mqtt will be a good and universal protocol option...

may you can show in short how you serve your webpage from node-red,
as i considered doing the same (when i find time..)
;-)

best luck,
tozett

Re: MQTT to Relay with Node-Red

Posted: 26 Apr 2016, 21:07
by JR01
@tozett, what an honour to help you, you have helped me in my darkest hours of trying to get the ESP from stopping to reboot! It is really really easy with Node-Red. See the screen below. the temp and humid comes straight from MQTT connectors, the warm water analytics (same with water with tipping bucket, both into an ESP12E, R78) comes from writing water flow meter, that writes via MQTT into an MySQL db on RasPi, then I do a query with visual programming with the Node-Red UI node, here. http://flows.nodered.org/node/node-red-contrib-ui. I can share more if you like.

an honour!

Re: MQTT to Relay with Node-Red

Posted: 26 Apr 2016, 21:10
by JR01
Here is a visual of the Node-Red nodes and flow to construct the UI. I also have more pages on it, to report the costs of warm water, then I ping the ESP's every 5min to check if they are alive, then write this into the MySQL if not alive, so when I do analytics, I know when not to trust the data.

What do you think?
Screen Shot 2016-04-26 at 9.08.46 PM.png
Screen Shot 2016-04-26 at 9.08.46 PM.png (232.27 KiB) Viewed 35090 times

Re: MQTT to Relay with Node-Red

Posted: 26 Apr 2016, 22:19
by tozett
Fantastic!

i will go your way, too!! :D
(my next sparetime for this)

thanks for the insight. i guess we will meet here someplace again...

:D

Re: MQTT to Relay with Node-Red

Posted: 27 Apr 2016, 08:21
by tozett
i am on debian 8,
installed the node-red-UI, but it dont show up...
anybody with a hint for me ?
(google has not that much, mostly working or raspberry...)
:o

Re: MQTT to Relay with Node-Red

Posted: 27 Apr 2016, 08:34
by JR01
Did you remember to first change directory to the node red dir before issuing the command to get and install?

cd ~/.node-red

Only then issue the command : npm install node-red-xxxx (applicable package name)

Re: MQTT to Relay with Node-Red

Posted: 27 Apr 2016, 09:00
by tozett
hey thanks,
now working.

it was annoying, that i am a noob at so many things,
so i dived deeper into testing/googling and found, that the

Code: Select all

npm install <module>
installs node.js modules in the local dir....
cannot remember how i installed node-red, so i cannot remember why my modules are installed at

/usr/lib/node_modules

but as i found the node-red-contrib-ui directory in the local /root/directory,
i copied it to /usr/lib/node_modules and it is working..

dont know if i have to do more about that, but its running... :D
------------------------------
edit2: i searched my brain, how i installed this, and now think i must have done it like the doku said:
https://nodejs.org/en/download/package- ... tributions
so that i am not aware about the path...
maybe i had to install the ui with something like -global or the like.. dont know...

Re: MQTT to Relay with Node-Red

Posted: 27 Apr 2016, 09:10
by fluppie
https://openenergymonitor.org/emon/node/12437 check this RaspberryPi image, emonCMS, Jessie Lite, Node-Red & openHAB. It's booting pretty quick.

Re: MQTT to Relay with Node-Red

Posted: 27 Apr 2016, 09:16
by tozett
that was it, install as global, -g

i investigated the install of node-red, and as the wiki/doku said, i installed it as -global

http://nodered.org/docs/getting-started/installation

so i had to intall the UI with the same global-flag "-g" to have it at the right place.. :)

Re: MQTT to Relay with Node-Red

Posted: 27 Apr 2016, 15:11
by tozett
i want to thank you for the inspiration with the web-ui in node-red.
i also install the 1-wire owfs module, and have all temp-values from my other server..
fantastic...

Re: MQTT to Relay with Node-Red

Posted: 27 Apr 2016, 18:42
by JR01
+1 ! Looks like you have done a lot today! Could you show us a screen shot of the UI?

Re: MQTT to Relay with Node-Red

Posted: 27 Apr 2016, 19:15
by tozett
node-red-ui component builds the ui itself,
cannot manually control html-elements. so it looks:

Re: MQTT to Relay with Node-Red

Posted: 27 Apr 2016, 20:08
by JR01
Very impressive, well done!

Re: MQTT to Relay with Node-Red

Posted: 28 Apr 2016, 20:09
by JR01
JR01 wrote:Hi, I see a lot of discussions on using MQTT to actuate a relay. .....
Question 2: Anybody has ideas on how to use MQTT pure, to actuate a relay?
For those wondering if actuation of relays works through MQTT-OpenHab - it does.
I have subscribed to: /RHZA/ESP12-01/# on an ESP12E-01 on R105.

Pumping into /RHZA/ESP12-01/GPIO/16 (I use Node-Red)
with payload 0 or 1
Switches the relay on or off. Mine is a 100A used to switch my geyser.
And no need to register GPIO16 under devices.
Thank you to dev's, this is great!

Re: MQTT to Relay with Node-Red

Posted: 28 Apr 2016, 20:23
by tozett
thanks for proof...
i saw this by accident, thought it would work the way you described, but was not sure.
in some discussions, the dev stated nothing special to develop this way,
but, hey: great! it works!

https://github.com/ESP8266nu/ESPEasy/bl ... sy.ino#L94 (or does it has nothing to do with your findings.. :? 8-) )

Re: MQTT to Relay with Node-Red

Posted: 29 Apr 2016, 14:08
by tozett
i dont know, if i am on the right track ( :roll: ),
but if you really can switch the GPIO with MQTT, than the other commands "could" work also?
i am a computerlanguage i-literate, but it this works, maybe it should be documented in the wiki?
maybe new with version r1xx?
:oops:

Re: MQTT to Relay with Node-Red

Posted: 20 Jun 2016, 15:01
by crsherman
JR01 wrote:Here is a visual of the Node-Red nodes and flow to construct the UI. I also have more pages on it, to report the costs of warm water, then I ping the ESP's every 5min to check if they are alive, then write this into the MySQL if not alive, so when I do analytics, I know when not to trust the data.

What do you think?

Screen Shot 2016-04-26 at 9.08.46 PM.png
JR01,
Would you mind sharing your node red json file?

I am in the process of setting up feeds from temperature sensors using Linkit and MQTT. I am receiving the data within node-red but am having trouble with the function node to feed into a mysql database.

Any help would be appreciated.

Re: MQTT to Relay with Node-Red

Posted: 21 Jun 2016, 19:15
by JR01
.@crsherman, yeah, would not mind sharing my .jsons. My RPi is down currently, busy with refurbishment of the breadboards for ESP8266 to sensors - building into vera board solutions to take out instability, need to fire RPi up again next 3 days, then I will upgrade to the latest cool version 0.14.3 of NodeRed, then will post for you. I have lots going on on my .json file, point me to what you want to achieve so I can lift that out to start off with? And are you also interested in creating the phone html 'app' I use to control things with?

Re: MQTT to Relay with Node-Red

Posted: 21 Jun 2016, 23:04
by crsherman
JR01 wrote:.@crsherman, yeah, would not mind sharing my .jsons. My RPi is down currently, busy with refurbishment of the breadboards for ESP8266 to sensors - building into vera board solutions to take out instability, need to fire RPi up again next 3 days, then I will upgrade to the latest cool version 0.14.3 of NodeRed, then will post for you. I have lots going on on my .json file, point me to what you want to achieve so I can lift that out to start off with? And are you also interested in creating the phone html 'app' I use to control things with?

Thanks JR01!

Yes - definitely interested in the phone html app to control things.

My setup as it stands now:
1.) numerous sensors for temperature, humidity and weather using Linkit Duo and Grove sensors (soil moisture, temperatue, humidity at various locations). I wish to integrate these fully into NR for graphical display to a webpage for weather information for the family use.
2.) several other rasperry pi's and arduino's around the house setup for control of garage, outdoor lighting, misting system, and pool pumps. No rhyme or reason to why the mix - just developed that way
3.) MQTT Broker - mosquitto setup on macmini
3.) Node red server setup on same macmini
4.) Mysql database within MAMP setup on same macmini

I currently have most of my sensors publishing topics to my broker using Pubsubclient. I have most of these integrated into my NR flow but still having issues with the topic being saved to a mysql database. I currently have the datA but not the datE or timestamp being saved (still trying to work through a timestamp node injection - looking at thethingbox-node-timestamp ).

Looking at the next step of converting my arduino's and rpi's over to being controlled by my node-red flow. I have started this effort but am trying to get up to speed on tying NR to an html page. I have been studying and reading Peter Scargill's site extensively and was looking at Imperihome when i came across your blog.

All of this being done with the thought that whatever i come up with - the wife and kids can use easily (maybe the hardest part of all of this).

Any guidance/help or examples would be much appreciated.

Re: MQTT to Relay with Node-Red

Posted: 21 Jun 2016, 23:46
by JR01
In the mean time, some help to get the MySQL going: below is what I have in a function node, receiving for instance humidity from MQTT, and prior to a MySQL node.

ValA = msg.payload ;
msg.topic = "INSERT INTO `SysTaskVal2day` (`idSystem`, `idTask`, `tmstamp`, `val`) VALUES (1,1,now(),"+ValA+");" ;
return msg;

Check out my DB schema of MySQL here:

http://iotplay.blogspot.co.za/2016/05/a ... ation.html

Re: MQTT to Relay with Node-Red

Posted: 22 Jun 2016, 05:29
by crsherman
JR01 wrote:In the mean time, some help to get the MySQL going: below is what I have in a function node, receiving for instance humidity from MQTT, and prior to a MySQL node.

ValA = msg.payload ;
msg.topic = "INSERT INTO `SysTaskVal2day` (`idSystem`, `idTask`, `tmstamp`, `val`) VALUES (1,1,now(),"+ValA+");" ;
return msg;

Check out my DB schema of MySQL here:

http://iotplay.blogspot.co.za/2016/05/a ... ation.html

Never thought to just put the timestamp into the sql query - duh!

I was taking the following approach.

Function timestamp Node with the following code:

msg.payload.data.date = new Date();
msg.payload = msg.payload.data;
return msg;

Function query node with the following code:

msg.topic = "insert into tempreadings (tmstamp, humidity, temp1) values (" + msg.payload.date + "," + msg.payload.temperature + "," + msg.payload.humidity + ")";
return msg;

Both of these sequentially feeding into a mysql node with the appropriate database information. The date piece was still giving me fits. I like your approach better and can easily modify my node flow - THANKS!

I took a look at your DB schema and was impressed. Logical way of approaching the setup of the databases. I was looking at the necessary setup to feed Imperihome also (still not sure if I want to go this route or the route of just html pages - quite frankly do not know enough to even know what i am getting into in regards to programming the flow for imperihome). Alot of information on Peter's blog on this. Any comment on whether or not your schema would still support their requirements.

Again - thanks for the help!

Re: MQTT to Relay with Node-Red

Posted: 22 Jun 2016, 06:33
by JR01
I want to still look at Imperihome sometime, looked a bit after Pete blogged about it some weeks ago. But I am a bit of a purist, and the reason why I do home automation is not for the home automation, but to prove IoT concepts and to create parallels people can understand easily. So the database, and ability to do analytics on it, is key for me at this stage. I also tried that node with time stamp, was not able to get it going, but probably could have tried harder.

Re: MQTT to Relay with Node-Red

Posted: 22 Jun 2016, 07:57
by JR01
Since I posted the info on my blog regarding the MySQL db design, and the issues blogged on Pete's blog and I also found prior to that discussions on OpenHab forums that RPi writes to sd card crashes at some point, as sd cards only has a limited amnt of writes, I implemented the concept proposal made by one of the users on Pete's site, with suggestion to mount a table with type Memory in the db, do the day's mqtt sensor writes to that table, which runs in memory, thus saving sd card writes, and that night at 24:00 write the temp in memory table to sd card once. This is working very well. Will share more details if you are interested. Still need to upd my blog with that outcome.

Re: MQTT to Relay with Node-Red

Posted: 22 Jun 2016, 22:17
by pppp33
YOU ARE TERRIFIC ! ;)
great work !
Thanx for sharing.....
paolo

Re: MQTT to Relay with Node-Red

Posted: 22 Jun 2016, 23:23
by JR01
crsherman, I see you say you are also interested in the UI tool I used on NodeRed, not much to it, check out this node:
http://flows.nodered.org/node/node-red-contrib-ui. Once loaded & wired-up, you fire it up on mobile browser on your home wireless with: http://localhost:1880/ui

On my blog I briefly show the interface I have built, will write more what I have learnt of the family using it in future, and progress on it, see image of an earlier version here:
Image

Re: MQTT to Relay with Node-Red

Posted: 28 Jun 2016, 02:44
by crsherman
JR01 wrote:crsherman, I see you say you are also interested in the UI tool I used on NodeRed, not much to it, check out this node:
http://flows.nodered.org/node/node-red-contrib-ui. Once loaded & wired-up, you fire it up on mobile browser on your home wireless with: http://localhost:1880/ui

On my blog I briefly show the interface I have built, will write more what I have learnt of the family using it in future, and progress on it, see image of an earlier version here:
Image


JR01,
I installed the node-red-contrib-ui and have been able to setup temperature feeds from mqtt but do not understand how I would go about controlling a pin on my remote Linkit Duo. Are you using mqtt to control the geyser switch? Any help would be appreciated.

Re: MQTT to Relay with Node-Red

Posted: 28 Jun 2016, 22:20
by JR01
Controlling geyser with MQTT ? = yes.

In an 'MQTT out' node, make the topic the below, and pass 1 into it via a flow:
/ESP03/GPIO/2

Means the following: Send to ESP03, to the GPIO, for pin 2, and switch it on.

Remember to make your MQTT subscribe template on the ESP:
/%sysname%/#

And the name of the ESP - in this case, is ESP03.

voila !

And the flow for the UI to switch it? I have a button, where the family can override the geyser on/off, which is on a schedule, but the override counts down 25min, then it switch the geyser off again.

Image

Re: MQTT to Relay with Node-Red

Posted: 29 Jun 2016, 04:54
by crsherman
JR01 wrote:Controlling geyser with MQTT ? = yes.

In an 'MQTT out' node, make the topic the below, and pass 1 into it via a flow:
/ESP03/GPIO/2

Means the following: Send to ESP03, to the GPIO, for pin 2, and switch it on.

Remember to make your MQTT subscribe template on the ESP:
/%sysname%/#

And the name of the ESP - in this case, is ESP03.

voila !

And the flow for the UI to switch it? I have a button, where the family can override the geyser on/off, which is on a schedule, but the override counts down 25min, then it switch the geyser off again.

Image

Ok - I think I understand better but have a few questions.

I am using pubsubclient for arduino. So I believe my template code for subscribing to this topic would be something similar to this:

client.subscribe("ESP03/#"); --- I believe that this is what you are referring to when you mention the subscribe template???

I take it I also have to setup the appropriate pin modes on my duo also???

Re: MQTT to Relay with Node-Red

Posted: 29 Jun 2016, 06:08
by JR01
No, the substibe template setting is on Advanced tab on EspEasy, I will oost a pic tonight if you are not finding it

Re: MQTT to Relay with Node-Red

Posted: 29 Jun 2016, 08:48
by tozett
sub2.png
sub2.png (50.04 KiB) Viewed 31791 times

Re: MQTT to Relay with Node-Red

Posted: 02 Jul 2016, 11:29
by jram
This is a great thread. I've been doing node-red for a while now but have only recently discovered ESPeasy.

One of the challenges that we face is learning to install and configure all this stuff. I found a great script on Peter Scargills site that almost completely automates the installation of Node Red, SQL Lite and all the support you need to get going from a 'bare' pi. Its here... http://tech.scargill.net/installing-the-works/

Re: MQTT to Relay with Node-Red

Posted: 18 Jul 2016, 00:57
by crsherman
JR01 wrote:.@crsherman, yeah, would not mind sharing my .jsons. My RPi is down currently, busy with refurbishment of the breadboards for ESP8266 to sensors - building into vera board solutions to take out instability, need to fire RPi up again next 3 days, then I will upgrade to the latest cool version 0.14.3 of NodeRed, then will post for you. I have lots going on on my .json file, point me to what you want to achieve so I can lift that out to start off with? And are you also interested in creating the phone html 'app' I use to control things with?

JR01,
Would you mind lifting the geyser json first and sharing?

Thanks,
crsherman

Re: MQTT to Relay with Node-Red

Posted: 18 Jul 2016, 20:40
by JR01
Sure, I did a write-up for you, with the json on my blog:

http://iotplay.blogspot.com/2016/07/swi ... es-on.html

Re: MQTT to Relay with Node-Red

Posted: 24 Jul 2016, 12:32
by JR01
the boys over at node-red are about to release a new DASHBOARD on npm. I got it working on the pre-release, and IT IS AWESOME.

http://iotplay.blogspot.co.za/2016/07/u ... d.html?m=1

Image

Re: MQTT to Relay with Node-Red

Posted: 24 Jul 2016, 15:23
by crsherman
JR01 wrote:Sure, I did a write-up for you, with the json on my blog:

http://iotplay.blogspot.com/2016/07/swi ... es-on.html
JR01,
Just got a chance to read and study your blog - it was just what I needed to understand the flow - thank you for sharing.

I am in the process of using espeasy and I have my project moving in the right direction thanks to you - much appreciated.

CRS

Re: MQTT to Relay with Node-Red

Posted: 24 Jul 2016, 22:59
by JR01
8-) Your welcome !