Wireless Master/Slave Light Switch Using ESP-NOW With ESP8266

by ArtSuzhou in Circuits > Microcontrollers

2032 Views, 7 Favorites, 0 Comments

Wireless Master/Slave Light Switch Using ESP-NOW With ESP8266

Wireless Mater/Slave Switch Demo

If I turn on the main light switch in the room, I would like to turn on some additional lights as well. I don't want to use expensive home automation products or install any new wiring in the house. Instead, I'm looking for a wireless system that can control the power to the additional lights through small receivers. These receivers will monitor the status of the main light switch and activate the power relay for the additional lights accordingly. Ideally, I want a cost-effective solution.

Espressif Systems, the company that created the ESP8266 and ESP32 microcontrollers, also developed ESP-NOW. This communication protocol is a simple and efficient wireless solution that is well-suited for a variety of applications. It boasts several advantages, including low power consumption, easy setup, and the ability to support broadcasting.

One of the key benefits of ESP-NOW is its support for broadcasting, which means that a single device can send data to multiple devices simultaneously. This is particularly useful in this case where we need to control multiple lights in a room at the same time.

Another advantage of ESP-NOW is its cost-effectiveness. The ESP8266 is widely popular, and there are many resources available to help developers integrate it into their projects. Additionally, it can be programmed using the popular Arduino development environment.

Supplies

IMG_9292.jpg

The following are the parts and tools required to make this project. All the parts are available from online stores. A 3D printer and a fine tip soldering iron and some soldering skills are required to make this project. You also need to know how to use Arduino IDE to upload a sketch.

Master controller:

Slave receiver:

Additionally, you'll need:

Software tools:

  • Arduino IDE

How It Works

network.png
IMG_9279_cover.jpg

In the ESP-NOW communication model, there is a master device and one or more slave devices. The master device is responsible for initiating the communication and sending messages to the slave devices. The slave devices receive the messages and can respond to the master device if necessary.

A MAC address (Media Access Control address) is a unique identifier assigned to each Wemos D1 board for use as a network address in communications within a network segment. In the case of ESP-NOW, the master device uses its MAC address to identify itself to the slave devices. When the master device sends a message to a slave device, it includes its MAC address as part of the message header. The slave device can then use this MAC address to identify the source of the message and respond accordingly.

When the Master lamp is turned on, the Master control unit will start transmitting its MAC address and data (incremental integer numbers). The Slave unit continuously monitors the data from the Master unit and pairs with the Master unit upon receiving its MAC address. The Slave unit then send a response to the Master unit, confirming that it has paired successfully. The Master unit will continue to send data, and the Slave unit will keep receiving and responding to it. When the Master lamp is turned off, the Slave unit will no longer receive data changes and will turn off the Slave lights.

Make 3D Printed Parts

IMG_9273.jpg
lamp control v1.png
complete.PNG
IMG_9304.jpg

The 3D printed case was designed using Autodesk Fusion 360 and is printed using PLA material with a 30% infill. Support material is not required except for the cover. For the cover, it is recommended to use a white or light-colored material to allow the LED light to shine through. The STL files for the complete case are attached.

Electronics and Wiring

Lamp control.jpg
Finished master.jpg
Finished slave.jpg
d93ce1fe-ba8f-4a45-ae27-2296c8c6eab7.__CR0,0,300,300_PT0_SX300_V1___.jpg

Wiring the rest of the circuit according to the wiring schematic as well as the comments in the Arduino sketch. Use caution when working with mains voltage. Make sure that the "hot" wire on the extension cord goes to the relay between "COM" and "NO" terminals.

Programming

serial MAC.PNG
slave MAC.PNG

This code uses the ESP8266WiFi library and the espnow.h library for ESP8266. To use this code, please make sure that the relevant libraries are installed in your Arduino IDE. To install the ESP8266WiFi library and board package for the Wemos D1 mini, follow these steps:

  1. Open the Arduino IDE.
  2. Go to the "File" menu, and select "Preferences".
  3. In the "Additional Boards Manager URLs" field, enter the following URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json
  4. Click "OK" to close the preferences dialog.
  5. Go to the "Tools" menu, and select "Board" > "Boards Manager".
  6. In the "Boards Manager" dialog, search for "esp8266".
  7. Click on "esp8266" and click the "Install" button.
  8. Wait for the installation to complete.
  9. Once the installation is complete, you can select the Wemos D1 mini board from the "Tools" menu.
  10. To install the espnow.h library, go to the "Sketch" menu, and select "Include Library" > "Manage Libraries".
  11. In the "Library Manager" dialog, search for "esp-now".
  12. Click on "esp-now" and click the "Install" button.
  13. Wait for the installation to complete.

After installing the required libraries, you can proceed to upload the Master and Slave code to the Wemos D1 mini board. However, before uploading the Slave code, you will need to modify it with the MAC address of your own Wemos D1 board in the Master unit.

To do this, upload the sketch to the Master unit first. The MAC address will be printed out in the serial monitor (you may need to press the reset button on the Master unit) as shown in the attached picture. Then, copy the MAC address and paste it into the Slave unit sketch where indicated.

You may notice in the code that the ESP-NOW roles of Master and Slave nodes are reversed. This has been intentionally done to address the issue where the slave node would be stuck on when the communication with the master node is lost due to a power failure (main lamp is switched off). By reversing the roles, the Slave unit is now able to detect the data loss and turn off the light accordingly in the event of a power loss at the Master unit, ensuring a more reliable and resilient system.

Once the code is uploaded and the devices are powered on, each Master can control multiple Slaves, and each Slave will respond only to the Master's MAC address as programmed. This means that you can have multiple Masters in the vicinity, and they will work independently.