Random Nerd Tutorials
Shares

Control Sonoff Basic Switch with ESP Easy Firmware and Node-RED

Shares

In this project you’ll learn how to use the ESP Easy firmware on a Sonoff basic smart switch and control it with Node-RED using Node-RED dashboard. The Node-RED software is running on a Raspberry Pi. The ESP Easy firmware can also be integrated with other home automation platforms like Home Assistant, Domoticz, openHAB, etc…

Watch the Project Video Demonstration


We recommend using the following tutorials as a reference:

Project Overview

By the end of this project you’ll have a Sonoff running the ESP Easy firmware that can be controlled with HTTP GET requests using a web browser or another Wi-Fi enabled device.  After completing the configuration on the ESPEasy web interface, you can integrate it with most home automation platforms. For this example, I’ll be using Node-RED that is running on a Raspberry Pi. After connecting a couple of nodes, you can control the Sonoff using a button in your Node-RED dashboard.

The figure below shows a high-level overview on how the project works:

Prerequisites:

If you like home automation and you want to learn more about Node-RED, Raspberry Pi, ESP8266 and Arduino. I recommend that you download my course: Build a Home Automation System for $100.

Parts Required

Here’s a complete list of the parts required for this project:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

About the Sonoff

The Sonoff is a device that you put in series with your power lines allowing you to remotely turn any device on and off.

In simple terms, a Sonoff device contains an ESP8266 chip connected to a relay. This way you can connect to the Sonoff via Wi-Fi to control the relay.  The sonoff comes with a firmware that allows you to control it using the eWeLink app. However, most people prefer to flash the Sonoff device with custom firmware as we’ll do in this project.

Safety Warning

Make sure your Sonoff is disconnected from mains voltage while you are uploading a new firmware. Never touch any Sonoff component while it’s connected to mains voltage. After everything is disconnected and unplugged, open the Sonoff’s plastic box enclosure.

warning-m

Sonoff Pinout

The Sonoff is meant to be hacked, and you can see clearly that some connections were left out, so that you can solder some pins and upload a custom firmware. The figure below shown the pinout.

sonoff_gpio-r

The Sonoff comes with an ESP8266 Wi-Fi chip built-in that we can re-program with a custom firmware.

I’ve soldered 4 header pins, so that I can easily connect and disconnect wire cables to my Sonoff device.

pins-soldered

Preparing 3.3V FTDI Programmer

You need an FTDI programmer to upload a new firmware to your Sonoff. Use the schematics provided as a reference.

Warning: uploading a custom firmware is irreversible and you’ll no longer be able to use the app eWeLink.

I’ve added a toggle switch in the power line, so that I can easily turn the Sonoff on and off to flash a new firmware without having to unplug the FTDI programmer.

I used hot glue to glue the ends of the wires together. This prevents you from making wrong connections between the FTDI programmer and the Sonoff in the future.

hot-glue-ftdi

Boot your Sonoff in Flashing Mode

To flash a new firmware to your Sonoff, you have to boot your Sonoff in flashing mode. To do that, follow these next 4 steps:

1. Connect your 3.3V FTDI programmer to your computer

2. Hold down the Sonoff button

hold-down-sonoff-button

3. Toggle the switch to apply power to the Sonoff circuit

apply-power-to-sonoff

4. Then, you can release the Sonoff button

Now, your Sonoff should be in flashing mode and you can upload a new firmware.

ESP Easy Firmware

The ESP Easy is a firmware that can be used in any ESP module, whether it is an ESP-01, ESP8266-12E, ESP32, and so on – read their Wiki page. This firmware provides an easy way to configure your ESP module – you don’t have to write any code.

After loading the firmware, all the configurations, like setting GPIOs as inputs, outputs, etc are done in your browser using a user-friendly graphical interface.

Flashing the Sonoff with ESP Easy Firmware on a Windows PC

Go to the ESP Easy firmware GitHub releases page and click the link ESPEasy_v2.0-xxxxxxxx.zip to download it:

After downloading, unzip the folder and run the FlashESP8266.exe file:

Select COM-Port (your FTDI’s programmer port) and choose the firmware that ends with 1024_DOUT.bin: ESP_Easy_v2.0-xxxxxxxx_normal_ESP8266_1024_DOUT.bin

Note: to flash the firmware on other operating systems, you can use the ESPTool.py to flash the firmware to your ESP8266. You can read more information about that subject here.

Troubleshooting

If you try to upload the sketch and it fails to flash, it means that your Sonoff is not in flashing mode. You’ll need to repeat the process described in the earlier section “Boot your Sonoff in flashing mode” in this guide and try to flash again.

Final circuit

After uploading the code, re-assemble your Sonoff. Be very careful with the mains voltage connections. It’s the exact same procedure as shown in the introductory guide.

sonoff_circuit

If your house has a Earth connection, you should wire your Sonoff as follows:

Connecting to Your Wi-Fi Network

After powering your Sonoff device, you need to use your computer/laptop to connect to the ESP_Easy_0 Access Point (AP). In this case, I’m using my smartphone, so tap to connect to the ESP_Easy_0.

Type the password configesp and press the “CONNECT” button.

Then, open your browser and type the following IP address: 192.168.4.1 or simply tap “SIGN IN” button.

Configuring the WiFi page

You’ll be redirected to a web page at 192.168.4.1 that allows you to configure your ESP’s/Sonoff’s WiFi credentials. Select your network name, type the password and press the “Connect” button.

After a few seconds, it should print a message with your ESP’s IP address. In my case, it’s 192.168.1.113. Save that number, because you’ll need it in the next step.

Preparing the ESP Easy firmware

When you first access your the ESP Easy firmware web interface at your IP address (in my case: 192.168.1.113), you’ll be asked to enter a password (the default password is configesp).

After login, you’ll be redirected to the main page that contains different details about the device, as shown below.

Config

I strongly recommend going to the Config tab and type a new admin password (this will overwrite the configesp password). So, make sure you put down the new password, otherwise you’ll no longer be able to access the ESP Easy configurations web page.

Controllers

By default, it comes with the protocol Domoticz HTTP enabled. Remove it by clicking the “Edit” button.

Set the Protocol to “Standalone” and click the “Submit” button.

The figure below shows how the Controllers tab should look like after that step.

Hardware

The ESP Easy firmware allows you to set the Sonoff’s built-in LED as a Wi-Fi status LED. To do that, in the “Hardware” tab, go to “GPIO->LED” and set it to GPIO-13 (D7), which is the Sonoff’s built-in LED.

Devices

In the Devices tab, you should create a switch button that is assigned to the GPIO 12. You use GPIO 12 because that’s the ESP8266 GPIO the relay inside the Sonoff device is connected to. Press the “Edit” button.

Select as your Device “Switch input – Switch”.

Follow these configurations:

  • Name your device
  • Tick the Enabled box
  • 1st GPIO: GPIO-12 (D6)
  • Switch Type: Switch
  • Switch Button Type: Normal Switch

After completing all the configurations, press the “Submit” button:

The figure below shows how your Devices tab should look like after this previous configuration.

Rebooting your Sonoff

For all the configuration to take effect, you should reboot your Sonoff. Go to the Tools tab and press the “Reboot” button.

Testing Your Sonoff Installation

Follow the next steps to test your Sonoff installation.

Open your browser and go to this URL to turn on your Sonoff: http://[your_sonoff_ip_address]/control?cmd=GPIO,12,1

Then, go to the next URL to turn off your Sonoff: http://[your_sonoff_ip_address]/control?cmd=GPIO,12,0

Creating the Node-RED Flow

Before creating the flow, make sure you have the Dashboard nodes installed. Then, you can either import the Node-RED flow or create it by yourself.

Importing the Node-RED flow

If you don’t want to create the Node-RED flow, you can import it. For that, go to the GitHub repository or click the figure below to see the raw file, and copy the code provided.

Next, in the Node-RED window, at the top right corner, select the menu, and go to Import  > Clipboard.

Then, paste the code provided and click Import.

Then, you should change the HTTP request nodes to include your Sonoff’s IP address.

Creating the Node-RED Flow

If you prefer to create the Node-RED flow yourself, instead of importing the code, follow these next steps.

Drag the following four nodes:

  1. Dashboard switch
  2. Function switch
  3. HTTP request – on command
  4. HTTP request – off command

Then, edit the nodes as shown in the figures below.

1. Dashboard switch node:

2. Function switch node:

3. HTTP request – on command (you must replace with your Sonoff’s IP address).

4. HTTP request – off command (you must replace with your Sonoff’s IP address).

After editing all the nodes, wire your flow as shown below.

Finally, deploy your application

 

Demonstration

Congratulations! You project is now completed. Go to http://your-pi-ip-address/ui to control the Sonoff with the Node-RED Dashboard.

You can access your application in any browser in your local network to control your devices on and off.

Wrapping Up

In this project we’ve shown how to control your Sonoff with the ESP Easy firmware. As an example, we’ve used Node-RED, but you can integrate it with most home automation platforms.

We hope you’ve found this project useful. If you liked this post, you may also like:

Thanks for reading.


Learn ESP32 with Arduino IDE

This our complete guide to program the ESP32 with Arduino IDE, including projects, tips, and tricks! The registration closes soon, so SIGN UP NOW »

Recommended Resources

Home Automation using ESP8266 »
Build IoT projects and home automation gadgets with the ESP8266 Wi-Fi module.

Build a Home Automation System »
Learn how to build a automation system using open-source hardware and software from scratch.

Arduino Step-by-Step Projects »
Build 25 cool Arduino projects with our couse even with no prior experience!

Leave a Comment:

Add Your Reply

⏰ Registrations are open ⏰
Sign up for "Learn ESP32 with Arduino IDE" Course