Wireless Switch with NodeMCU (ESP8266) and a Relay

Alejandro González
Trabe
Published in
3 min readMay 27, 2021
https://wallpaperaccess.com/full/1472995.jpg

Today, there is a wide choice of IoT products that allow us to make our day-to-day lives easier. Switch on an electrical device or even change the colour of our lights are some features we can do with these products. In this post, we will show how to build a Wi-Fi switch to turn on and off any electrical device spending very little money.

What we need?

  • NodeMCU board: It’s an open-source development kit that allow us to prototype our IOT products using its own development language (Lua) or MicroPython. NodeMCU board contains an ESP8266 Wi-Fi enabled chip.
  • A relay: An electromagnetic switch operated by a relatively small electric current that can turn on or off a much larger electric current. There are relay modules whose electromagnet can be powered by 5V and with 3.3V. Both can be used with the ESP8266.
  • Arduino IDE: To upload our code to the NodeMCU board we need an IDE. We use Arduino IDE, but there are others that could be used as well.

Components… Assemble!

The following picture shows a scheme of how the components must be connected. In this case, we use a lamp holder, but it could be an extension cable, being able to switch on and off any device connected to it.

https://www.geekering.com/wp-content/uploads/2019/03/Fritzing_bb-768x604.png

Software

Arduino IDE can be installed using the guide they provide. Once installed, it is necessary to set up the NodeMCU drivers in order to be able to use our board. These drivers can be found in this link.

To put our code into the board, we must config our IDE by selecting the port where the board it’s connected (Tools -> Port) and the board type that we are using (Tools -> Select Board Type).

Once we have installed and configured the Arduino IDE, it’s time to load our code on the board. The following code implements a REST API with a light embedded UI (User Interface) using aREST and aREST_UI libraries.

By using Arduino IDE, we can write our code in C++ or C, instead of use Lua or MicroPython. This is because the IDE compiles C or C++ code into a binary executable that is directly loaded on the board.

What the code above does is basically to connect to our home Wi-Fi, prints its IP, sets an On/Off button, configures the REST API and waits on a loop for client requests.

To load this code to our board we must put it on a new file and then load it as we can see in the image below.

Once the code its loaded, we can check the board IP on the Serial Monitor (Tools -> Serial Monitor).

Extended Version

As we are using aREST, it would be interesting to expose an endpoint that allow us to turn on and off our devices remotely, without using the UI.

By loading the code above in our board, we can be able to switch on and off our device by accessing the following addresses: http://<ip of the board>/led?params=0 to turn off andhttp://<ip of the board>/led?params=1 to turn on.

Let’s Test On Real Life

One we have our components assembled, and the code loaded, it’s time to test our IoT home-made device going into the IP provided by the Serial Monitor. In the GIF below we can see how it works.

Using simple, low-cost components, we have built an IoT device that can be very useful in our day-to-day lives.

--

--