IoT Remote Relay with NodeMCU/ESP8266, MQTT/Mosquitto, and Node Red – Part III

In part II of the tutorial, we got our NodeMCU flashed, and optionally set up some sensors on another NodeMCU. In this tutorial, we’ll set up our MQTT and Node-Red servers, which will give us the ability to relay and take action on data that we receive.

What you’ll need:

  • Your completed Relay system
  • A server – in this tutorial I will be using a small linux VPS running Ubuntu 16. You can accomplish this with a different linux distro or even Windows – the concepts are the same, but you’ll be on your own to figure out the differences.
  • An MQTT client. I really like mqtt-spy.
  • To get the full benefit of the tutorial, you’ll want your second NodeMCU from part II configured with the DHT22 sensor.

Set Up Mosquitto Server and Connect Your NodeMCU’s

Mosquitto is an MQTT server. MQTT is a publish/subscribe based protocol, specifically built for small devices and small amounts of data. We’ll configure our NodeMCU’s to publish their data to our MQTT server, and to subscribe to data that is pertinent to them. This gives us remote data collection and remote control using our NodeMCU’s.

  1. Digital Ocean has a nice tutorial on setting up Mosquitto on Ubuntu 16.
  2. Once your Mosquitto server is running, it’s time to configure our NodeMCU to talk to it – let’s configure the NodeMCU that has the DHT22 sensor connected to it first.
  3. Go back to your ESPEasy configuration page and navigate to the Config page.
  4. Configure the MQTT settings as shown below, substituting the IP address shown for the IP address/URL of your Mosquitto server.
  5. Click “Submit” to enable MQTT. If your configuration is set up properly, the NodeMCU should now be publishing your DHT22 Temperature and Humidity data to the MQTT server.

Use mqtt-spy to Watch our MQTT Traffic

  1. Install mqtt-spy and open it.
  2. Create a new connection, and open it, replace the information shown with your name and the IP address/hostname of your Mosquitto server:
  3. Now we need to subscribe to the data that is being published by our NodeMCU. Under “Subscriptions and received messages” click “New”
  4. In the popup menu, enter the path /testnode/DHT/Temperature – replacing “testnode” with the name you gave your NodeMCU that has the DHT22 connected to it, and click “Subscribe”
  5. Click the tab that is created for your new subscription, in the table below, you should see messages starting to come in from your NodeMCU!
  6. Now follow the same configuration procedure with your relay board NodeMCU. Since we don’t have any sensors hooked up to that NodeMCU, it won’t be publishing anything. However, getting it connected to MQTT will allow us to publish messages to the board that will tell it to turn it’s relays on or off.
  7. To test if your board is correctly speaking with the MQTT server, we’ll send a test message through MQTT to try and turn on one of the relays.
  8. Plug a light into one of the outlets in your relay setup.
  9. In mqtt-spy, create a new message by filling in the “Publish Message” box at the top of the screen as shown below, changing the name of the device and GPIO number to match your device and the correct pin for the relay you are testing. In the data field, enter 0 to set the pin high and enable the relay.
  10. Click “Publish” and if you did everything correctly, the relay should be activated and your light should turn on!
  11. To turn off the relay, send 1 instead of 0 as the data.

Install node-red and Add Some Logic

node-red is a fun tool that will allow you to add some logic to MQTT events in a nice drag-and-drop interface. It’s very easy to use, and very powerful.

  1. Install node-red on your server by following the instructions on their site.
  2. Once node-red is running, access it by going to your node-red URL in a browser, usually host on your domain at port 1880, such as https://mydomain.name:1880/
  3. What we want to do in node-red is create a data flow. We first have to retrieve some data, which we will do by subscribing to an mqtt message.
  4. From left menu, drag an mqtt event into your flow on the right:
  5. The triangle on the mqtt event shows us that it needs some configuration. Double-click the mqtt item to configure it:
  6. Click the pencil next to the server dropdown, so that we may add our Mosquitto server as a data source. Fill in your server details and click “Done”
  7. Now with the server added, we want to subscribe to an mqtt data feed. In this example, we’ll subscribe to the humidity data coming from our DHT22. Fill in the subscription field, swapping our “newdevice” for the name of your NodeMCU that has the DHT22 connected to it, and click “Done”
  8. Once you have added the subscription info, you can click “Deploy” at the top-right of the node-red screen to see if the connection to your mqtt server is successful. If the connection works, you’ll see the word “Connected” below your mqtt item, which is now labeled as “Humidity”:
  9. Now let’s take some action on our humidity level. Drag a “Switch” node (under the “Functions” left category) into your flow, and connect it to your Humidity node by dragging a line from the gray box on the right of the humidity node , to the gray box on the left of the change node :
  10. Double-click the switch node so we can add some rules. Click the “Add” button at the bottom of the configuration box to add a second rule. We will set this up to give us two outputs from the function, one for when the humidity is at or above 75%, and one for when the humidity is below 75%. Click “Done” to save the node configuration.
  11. Now that we have two outputs, we need to actions. The goal here is to set a flag for whether we want to turn our switch on or off. Drag two “change” nodes from the left menu into your flow, and connect them to the two outputs of the switch node we just created:
  12. Double-click the top change node to configure it. We’ll name this one “Turn Outlet On” – this will get activated when our humidity reaches 75%. Set the msg.payload value to 0. This is what we will send to our relay board, which will set the corresponding pin HIGH and turn on our outlet. Click “Done” to save the node .
  13. Double-click the bottom change node , name it “Turn Outlet Off” and set the msg.payload value to 1. Click “Done” to save it.
  14. Now that we have the correct value to send back to our relay board, we need to actually send it. Drag an mqtt output node to your flow, and connect both outputs of your change nodes, to the input of the new mqtt node :
  15. Now double-click the mqtt output node to configure it. We’ll use this to send the same mqtt command we tested in mqtt-spy to turn our relay on or off. Add the correct publish command for your relay GPIO.

    Notice we just give the topic in this configuration, not the data. When the mqtt message comes in with our humidity data, the switch node will route the message to either the “Turn Outlet On” or “Turn Outlet Off” change nodes, which will set the data payload to either 0 or 1, respectively. The mqtt output node will now send this back to our relay board.
  16. The entire flow is now complete. If everything works, when the humidity reaches or exceeds 75%, a message should be sent to your relay board, turning your outlet on. Once the humidity drops down below 75%, another command will tell the relay to turn the outlet off.
  17. To test it, we first need to deploy our new flow by clicking “Deploy” on the top right of our node-red screen.
  18. Now, the fun part! Take a breath, and exhale onto the DHT22 sensor. Your breath should put out enough humidity to raise the humidity level of the sensor to over 75% – which should turn on your outlet. Cross your fingers and try it! After a minute when the humidity dissipates, the outlet should turn off.

Extra Credit – Send a Pushover Notification to Your Phone

Now that you’ve learned how to control your relay system and create some rules, let’s add some notifications to the scheme. If the humidity exceeds 75%, aside from turning on the outlet, I want node-red to send a notification to my phone. I’ll use pushover.net to send me the notification. Pushover is an easy service to use, and fairly cheap for a license on your phone. The following procedure assumes you’ve set up a Pushover account, installed and connected it to your phone, and set up an application within pushover.

  1. Add an http request output node to your flow, and connect it to the top output of the Check Humidity Level node:
  2. Double-click the http request node to configure it. For the URL, I followed the instructions in the Pushover API for constructing an HTTP POST message, formatted like this:
    https://api.pushover.net/1/messages.json?token=myapptoken&user=myusertoken&message=Humidity+is+{{{payload}}}
    You’ll notice I have included {{{payload}}} in the message portion of my URL. The http request node will change that to the actual value that was sent in the humidity mqtt message. Click “Done” to save, and then “Publish” on the top-right to save.
  3. Now try the breath test again on  your DHT22. If you have Pushover configured correctly, you’ll get a notification on your phone. And then you’re get another one. And another one.
  4. We have now encountered a problem. Because we have not implemented any amount of rate limited, you’ll get a pushover message for every single humidity reading that exceeds 75%. To keep the notifications somewhat sane, we’ll limit this so node-red only asks Pushover to send a notification to my phone no more than once every 5 minutes.
  5. To accomplish this, drag a Delay node to your flow, and insert it between the “Check Humidity Level” node and the “Send Pushover Notification” node – you’ll have to delete the existing link between them and create new links:
  6. Double-click the delay node to configure it. From the “Action” dropdown, you want to select Limit rate to – which will allow you to configure how many messages are allowed over a specific amount of time. Here it is set to limit to one message per five minutes.
    An important consideration – if you don’t also check “drop intermediate messages” – node-red will keep all of the messages it receives in a queue and will continue sending them, even if the situation that generated the message has long passed. By checking that box, if node-red receives another message within a 5-minute window after it has sent a message, it will just discard it completely.
  7. Click “Done” and click “Publish” to complete your flow, which should look similar to this:

Congratulations! Now have fun with your new toys!

One Reply to “IoT Remote Relay with NodeMCU/ESP8266, MQTT/Mosquitto, and Node Red – Part III”

Leave a Reply

Your email address will not be published. Required fields are marked *