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).
- Download the latest build of ESPEasy here (currently revision R120).
- Unzip the archive.
- Plug in your NodeMCU to a free USB port on your computer.
- Determine which COM port your NodeMCU connected on (you can look in the device manager, or pull up an Arduino IDE to find out).
- Double-click on the “flash.cmd” file in the unzipped archive.
- It will ask you which COM port, just type the number and hit enter.
- Then it will ask you which Flash Size you want to use, type 4096 and press enter.
- 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.
- If you selected the correct COM port, you’ll see the status as the tool flashes the firmware to your NodeMCU.
- Once it is complete, you may need to reset your NodeMCU.
- Now follow the instructions here: http://www.letscontrolit.com/wiki/index.php/EasySetup to configure your NodeMCU on your local network.
- If everyone worked properly, you should now have an IP address on your local network.
- 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:
- Congrats! You’re NodeMCU is flashed! Now we can do some fun stuff!
- 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.
- 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.
- 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.
- Plug your lamp into your first switched outlet.
- 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.
- 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.
- 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”.
- In your ESPEasy admin page, navigate to the “Tools” page.
- In the command box, enter GPIO,16,0 and click Submit.
- Voila! If everything is hooked up right, your lamp should turn on!
- Send the same command, but swap the 0 to a 1, and when you click Submit your light should turn off.
- Repeat steps 3-8 to verify the connections for the remaining relays on your board.
- If everything is good, now’s the time to put the covers on your relay buildout. Your end result might look something like this:
- 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.
- Flash your NodeMCU with the above instructions and give it a unique name/number.
- 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.
- Connect the VCC and GND to available corresponding pins.
- Connect the data pin to any free GPIO pin on the NodeMCU.
- Now go to the Device page in the ESPEasy config website.
- Click Edit next to any free task.
- 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 1 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.
- 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.
- 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.