What is a relay?

A relay is a switch that open and close circuits when actuated with an electric signal (or manually by a person or an object). The main part of a relay is an electromagnet – a coil of wire that becomes a temporary magnet when electricity flows through it. The actuation of the relay will change the state of a contact from open to close or vice-versa  depending on the contact configuration.

The relay we are using today has an open state and a close state and it will alternate between states when actuated.

As you see in the image above, when a small current flows in the circuit on the left it activates the electromagnet which produces a magnetic field around it. The energized electromagnet pulls the metal bar (B) on the right circuit, closing the switch and allowing a much bigger current to flow.

The idea of a smart relay is that you can control the A switch by sending a signal through  the internet/network.


Step 1: Soldering nodemcu and relay shield

As stated above, soldering the nodemcu d1 mini and the relay shield it’s very easy. Inside the nodemcu package you will find a socket with short pins.

Soldering should be done like in the image below. Try mounting them without solder to be sure that the pins align – 5V from the nodemcu to 5V on the relay, ground to ground and so on


Step 2: Uploading Tasmota to NodeMCU

As we’ve done in the past, we will be using nodemcu-flasher (64 or 32) and Tasmota (release page)

Connect the microusb cable to WeMos/NodeMCU and open nodeMCU-flasher. In the first tab (Operation) select the COM port for your device. In the second tab (Config) select the Tasmota .bin file.

On the advanced tab, please make sure the settings are matching:

Click on “Flash” and get your phone ready .

When the upload is done, the board will create an access point for you to connect to. Search for a “sonoff-…” open wifi network and connect to it. Open your favorite browser and type

You will get a wifi setup page as shown in the picture below. Enter your wifi network details and click save.

For further configuration you will have to find your device on your network. You can scan the entire network or take a look at the dhcp list on your router.

Step 3: Module Configuration

After you have successful connected the board to your wifi network open a browser and navigate to its IP.

Click on the “Configuration” button and then “Configure Module”. Select “18 Generic” as your module type and hit save. The device will restart.

Again open the “Configuration Module” and set “D1 GPIO5” to “21 Relay1” then save.

You should have something like this:


You have now successfully created a smart relay. Anytime you want to activate the relay you can navigate to its IP and hit “Toggle”. You can also set timers and control it via HTTP.

In the next few steps I will show you how to connect it to Node-RED via MQTT.

Step 4: Connecting the board to MQTT

Click on “Configuration” and then “Configure MQTT”.

For this tutorial we will use our free MQTT server. You can use it too, no need to have your own!

The settings for MQTT are:

  • Host: mqtt.iotwithus.com
  • Port: 1883
  • Client – leave it on default
  • User – leave it on default
  • Password – leave it on default
  • Topic – here is a little tricky – enter a unique id for your bridge, some random 20-30 characters should do the job. Find a random string here, use it without quotes.
  • Full Topic: %topic%/%prefix%/

Here is my configuration, I use “rfbridge” for the topic just for demonstration.

Step 5: Connecting Node-RED to MQTT

Add a mqtt input node to your flow, click on it and select “Add new mqtt-broker” and configure it like in the image below:


Step 6: Installing Node-RED web interface

Click on the menu icon on the right. You will find there “Manage Palette”.

In the Install tab search for “node-red-dashboard” and click install. New nodes will appear on the left of your screen.

Step 7: Make it all work

To your flow add a mqtt input node, a switch node and a mqtt output node. It should look like this:

1st node (mqtt input) configuration:

  • Server: iotwithus.com (configured above)
  • Topic: (the topic you chose at Step 4)/stat/POWER
  • QoS: 2

2nd node(switch) configuration:

This node will appear on the web user interface. When you double click on it you will have to create a new group and tab.

Click on the pencil on the right and give it a name:

Let’s name it “Relays”

Also, you will need to create a new ui_tab. We will name it “Home”


Click on add until you get back to the switch node properties.

My configuration is in the image below, be sure you match it:

3rd node (mqtt output) configuration

  • Server: iotwithus.com (configured above)
  • Topic: (the topic you chose at Step 4)/cmnd/POWER
  • Retain: true


Click “Deploy” on the top right corner. The flow in this point should look like this:



The result:

Navigate to http://your-node-red-ip/ui

How it works: when the relay is OFF, poles 1 and 2 are disconnected and 2 and 3 are connected. That means if you have a lamp with 2 wires, you cut one of them and connect it to 1 and 2, the lamp will turn on when the relay is on. Obviously if want to do that, be sure that the lamp is not connected to a power source before you cut the wire. You can add some tape over the connectors to protect yourself from the vicious current.

If you liked this tutorial click on the bottom left notification bell.