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

In part I of the tutorial, we got our relay hardware all set up and ready to go. Now it’s time to get the our NodeMCU firmware squared away. Optionally, at the end of this tutorial, I’ll show you how to set up another NodeMCU with some sensors.

What you’ll need:

  • Your completed relay system from part I of the tutorial.
  • A wifi router (and knowledge of it’s credentials)
  • A separate wireless device you can use to connect to the NodeMCU (I used my phone)

Flash Your NodeMCU with ESPEasy

The NodeMCU is a fun piece of hardware, you can program it using LUA, or just like an Arduino from the the Arduino IDE, or flash custom firmware to do something else. After a TON of hunting and trying different things, I stumbled across ESPEasy – a custom firmware that makes managing the NodeMCU painless. Note: this assumes you are using Windows. If you’re on a Mac or Linux, you’ll need to flash with a different tool. You can find instructions on the ESP8266 wiki. If you purchased the same NodeMCU that I linked in the first part of the tutorial, you’ll want to use the 4096 version of the firmware (because you have a 4MB flash).

  1. Download the latest build of ESPEasy here (currently revision R120).
  2. Unzip the archive.
  3. Plug in your NodeMCU to a free USB port on your computer.
  4. Determine which COM port your NodeMCU connected on (you can look in the device manager, or pull up an Arduino IDE to find out).
  5. Double-click on the “flash.cmd” file in the unzipped archive.
  6. It will ask you which COM port, just type the number and hit enter.
  7. Then it will ask you which Flash Size you want to use, type 4096 and press enter.
  8. Then it will ask which build of the firmware you want to use. Type in the number that corresponds to the build number you downloaded. In my case, it was build 120, so I typed 120 and pressed enter.
  9. If you selected the correct COM port, you’ll see the status as the tool flashes the firmware to your NodeMCU.
  10. Once it is complete, you may need to reset your NodeMCU.
  11. Now follow the instructions here: to configure your NodeMCU on your local network.
  12. If everyone worked properly, you should now have an IP address on your local network.
  13. Connected back to your regular wifi, try opening a web browser and navigating to the IP address of your NodeMCU. You should get a screen that looks like this:
  14. Congrats! You’re NodeMCU is flashed! Now we can do some fun stuff!
  15. Before you get too much further, go to the Config page and give your device a unique name. Also fill in the “Unit nr” with a unique number. You won’t have to use this in the future, it’s just an identifier. If you have multiple NodeMCU’s, be sure they each have a unique name and number.
  16. You can also set up a static IP address for this node, I find it much eaiser than hunting for the right IP every time it gets a new one from DHCP.
  17. Click Submit to save your settings.

Test Our Relay Setup

Now that we’ve got a functional firmware, we can do some testing. Grab a lamp or something that you can plug into the outlets and obviously see if the outlet is working. Right now your “always on” outlet should work, and none of the other outlets should.

  1. Plug your lamp into your first switched outlet.
  2. Earlier we connect each of our relays to a different pin on our NodeMCU. Determine which pin on  your NodeMCU controls the relay that is connected to this outlet. For the top-left outlets, those are connected to the first relay on my relay board, which is connected to D0 on my NodeMCU.
  3. Now for a bit of trickiness. The ESPEasy firmware does not reference PIN numbers, it references GPIO numbers. You’ll need to find a pinout reference for your particular NodeMCU to determine your GPIO numbers. This is the pinout of my board. Using that reference, my first relay is connected to pin D0, which corresponds to GPIO16.
  4. We are now going to send a command through our ESPEasy page to tell the NodeMCU to set the pin corresponding to our first relay as “HIGH”.
  5. In your ESPEasy admin page, navigate to the “Tools” page.
  6. In the command box, enter GPIO,16,0 and click Submit.
  7. Voila! If everything is hooked up right, your lamp should turn on!
  8. Send the same command, but swap the 0 to a 1, and when you click Submit your light should turn off.
  9. Repeat steps 3-8 to verify the connections for the remaining relays on your board.
  10. If everything is good, now’s the time to put the covers on your relay buildout. Your end result might look something like this:
  11. Note the USB cable comes runs from my NodeMCU to a USB wall adapter for it’s power. That’s why we included some “Always On” outlets in our configuration. Now make some pretty labels so you don’t forget which outlet is which.

Optional: Set Up Another NodeMCU with Sensors

The ESPEasy firmware makes setting up sensors a breeze. With this NodeMCU, we’ll hook up a DHT22 Temperature/Humidity sensor to send frequent Temp/Humidity readings.

  1. Flash your NodeMCU with the above instructions and give it a unique name/number.
  2. Connect your DHT22 to the NodeMCU. You will only use three of the four pins. With the grill of the unit facing you and the pins facing down, the pins are numbered 1-4 left-to-right.
    Pin1: VCC
    Pin2: Data
    Pin3: Unused
    Pin4: GND
  3. Connect the VCC and GND to available corresponding pins.
  4. Connect the data pin to any free GPIO pin on the NodeMCU.
  5. Now go to the Device page in the ESPEasy config website.
  6. Click Edit next to any free task.
  7. Configure the device screen for the DHT22 as shown below:
    You’ll notice in the formula for temperature, that I am converting the Centigrade temp reading that is given by the DHT22 into Fahrenheit. If you don’t want to do that, just leave the formula field blank like it is for the humidity. Also note that I had trouble getting reading from my DHT22, and it turns out you have to enter a number  in the “IDX/Var” field. Once I did that, the readings started coming through. Also be sure to change the GPIO dropdown to the GPIO number that matches the data pin for your DHT22.
  8. If you hooked everything up correctly, once you click Submit you should see readings on the device page like so:
    You’ll notice I also have a Luminosity sensor hooked up to this NodeMCU. You can ignore the DHT on line 3 of the above image – that was a test and you don’t need that in your config.
  9. Congrats! You now have a sensor capturing data!

Next up in Part III we will get the server side set up to do something with our devices and data.

9 Replies to “IoT Remote Relay with NodeMCU/ESP8266, MQTT/Mosquitto, and Node Red – Part II”

  1. Andy, first of all I want to thank you. You’ve created the best manual for ESP8266 quick & practical usage I ever seen in Internet.
    My question: I set Admin password in Config. The password is asked when I connect from my desktop computer (Firefox). But next I tried to enter from another browser and even from another computer and password is not asked allowing anyone access my ESP’s control panel.
    May you check this issue with your NodeMCU device?

    1. Hi Vitaly – thanks for the kudos! I appreciate it! As far as the login issue, you’ve run into one of the limitations of the ESPEasy firmware. The web server they built into the firmware is quite remedial, it does not have a lot of functionality built into it, one of those things being browser sessions. I did some testing and I can confirm the password protection in ESPEasy is “global” (I can’t think of a better word). Basically since there is no session support, the password is used to lock the NodeMCU from any access. Once the password has been used, access to the config is completely open an unprotected until you reboot the NodeMCU and the password goes back into force. TL;DR: You need to ‘logout’ of your NodeMCU when you’re done, and the way you do that is by rebooting it. Hope that helps!

  2. Andy, now I successfully reproduced your temperature and pressure technique for displaying in web browser (PART II). Now I understood, I should reboot NodeMCU each time after I read new data. I am going to go ahead with your PART III. I have not enough time yet to learn PART III. But I have main question – if ESP is under password, how about security in this case? Each time when server gets new data, should be ESP rebooted to force password again?

    1. Hi Vitaly – the password tools are pretty remedial in ESPEasy. Yes, once you have entered a password into ESPEasy the only way to re-enable the password (at least as far as I can tell) is to reboot the device. However keep in mind the password is only for getting into the administrative page of the ESP. You can send/receive data, commands, etc., through MQTT and such without using a password, so there is no reason to reboot the ESP after reading data. There are ways you can also lock down the MQTT but I have not experimented with that at all.

      1. Hi Andy. Thank you again for reply. I had no experience with MQTT so far. What is the easiest way to get sensors data from ESPEasy by MQTT?. Is there some special client software or what?

        1. Check out tutorial part 3 – it shows you how to set up a Mosquitto MQTT server. Remember that MQTT in inherently temporary. Also in that tutorial you’ll see how to set up a node-red server which will act on the MQTT messages as they are received. If you want to store the data for future reference (to product time-based data graphs, for example) you need to store the MQTT messages in a database of some sort. I’ve covered this in this tutorial using the “TIG” stack (Telegraf, InfluxDB, and Grafana).

Leave a Reply

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