ESP32 Based Remote Power-Cycler Switch for Router

by gokux in Circuits > Wireless

3976 Views, 63 Favorites, 0 Comments

ESP32 Based Remote Power-Cycler Switch for Router

image1-6.png

If you have a Wi-Fi connection, you may encounter some problems with your Wi-Fi router, such as disconnecting from the internet. Most of the time, you can fix this issue by power cycling your router.

To make this process easier, I've created a wireless switch that lets you turn the router off and on without needing to be near it. Press a button at your desk, and the router will be turned off for two seconds, and then it will turn back on automatically.

Both the sender and receiver are made using the Seed Studio Xiao ESP32C3. Right now, we use the ESP-Now protocol to communicate with the sender and receiver. One of the main advantages of using ESP-Now is that no extra wireless communication circuit is needed; the ESP32 devices talk directly to each other, which makes the project less complicated overall.

Supplies

Timeline 1_01_00_38;50.jpg

TX Parts

  1. Seeedstudio XIAO ESP32C3
  2. OMRON 12x12x7.3mm Tactile 4 Pin Push Button Switch
  3. USB C cable
  4. Super glue

RX Parts 

  1. Seeedstudio XIAO ESP32C3
  2. Mini 360 Step-Down Buck Converter Power Module
  3. IRLZ44N Mosfet
  4. 10K Resister 
  5. 2*2.1 x 5.5mm DC Power Jack Socket Female - Panel Mount
  6. DC to DC cable
  7. B-7000 Glue

Tools Used 

  1. Soldering kit
  2. Wire cutter
  3. Third-Hand Soldering Tool
  4. Screwdriver 
  5. Tweezer 
  6. Nose Plier
  7. 3d printer (grey,orange PLA)

Designing in Fusion 360

Screenshot 2024-09-30 115311.png

Design is broken down into several levels for easy 3d printing

3D Printing

Timeline 1_01_01_47;23.jpg

Due to its complex nature, I designed the object in parts, so we will need to assemble it after 3D printing. I used orange PLA and grey PLA. 3D printed all models in my cubic printer. In 0.2mm layer height and 30% infill

Finding the MAC Address of Your XIAO ESP32C3




We are using the ESP-NOW protocol for communication between the transmitter (TX) and receiver (RX). For this, the transmitter (TX) needs to know the unique MAC address of the RX ESP32. You will need to run the following code on your ESP32C3 receiver (XIAO) to find its MAC address. After running this program, the MAC address will be displayed on the serial monitor.

My MAC address is 64:E8:33:8A:22:54

Code for finding the MAC address


#include <WiFi.h>
#include <esp_wifi.h>

void readMacAddress(){
uint8_t baseMac[6];
esp_err_t ret = esp_wifi_get_mac(WIFI_IF_STA, baseMac);
if (ret == ESP_OK) {
Serial.printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
baseMac[0], baseMac[1], baseMac[2],
baseMac[3], baseMac[4], baseMac[5]);
} else {
Serial.println("Failed to read MAC address");
}
}

void setup(){
Serial.begin(115200);

WiFi.mode(WIFI_STA);
WiFi.STA.begin();

Serial.print("[DEFAULT] ESP32 Board MAC Address: ");
readMacAddress();
}

void loop(){

}

TX Code

Before uploading the code we need to enter the MAC address of the our receiver which is 64:E8:33:8A:22:54 for me we need to add a 0x before it ,

Then it will be 0x64,0xE8,0x33,0x8A,0x22,0x54

Put that in this line of the code 

// MAC address of the receiver

uint8_t receiverAddress[] = {0x64, 0xE8, 0x33, 0x8A, 0x22, 0x54};

TX code


#include <WiFi.h>
#include <esp_now.h>

// MAC address of the receiver (RX) ESP32
uint8_t rx_mac[] = {0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX}; // Replace with actual MAC address

void setup() {
Serial.begin(115200);

// Initialize WiFi in STA mode
WiFi.mode(WIFI_STA);

// Initialize ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("ESP-NOW initialization failed");
while (true);
}

// Register peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, rx_mac, 6);
peerInfo.channel = 0; // use the current channel
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
while (true);
}

// Register callback function
esp_now_register_send_cb(onDataSent);

// Setup button pin
pinMode(D0, INPUT_PULLUP);
}

void loop() {
if (digitalRead(D0) == LOW) { // Button pressed
// Send signal to power cycle
uint8_t data[] = {0x01}; // Signal to RX
esp_now_send(rx_mac, data, sizeof(data));
delay(1000); // Debounce delay
}
}

void onDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
if (status == ESP_NOW_SEND_SUCCESS) {
Serial.println("Data sent successfully");
} else {
Serial.println("Data send failed");
}
}

Rx Code

There is change need for the RX code


#include <WiFi.h>
#include <esp_now.h>

#define RELAY_PIN D0

void setup() {
Serial.begin(115200);

// Initialize WiFi in STA mode
WiFi.mode(WIFI_STA);

// Initialize ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("ESP-NOW initialization failed");
while (true);
}

// Register callback function (new format)
esp_now_register_recv_cb(onDataReceived);

// Setup relay pin
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH); // Ensure relay is HIGH (closed) initially
}

void loop() {
// Nothing to do here
}

// Updated callback function with esp_now_recv_info
void onDataReceived(const esp_now_recv_info *recvInfo, const uint8_t *data, int len) {
Serial.print("Message from: ");
for (int i = 0; i < 6; i++) {
Serial.printf("%02X", recvInfo->src_addr[i]);
if (i < 5) Serial.print(":");
}
Serial.println();

if (len == 1 && data[0] == 0x01) {
Serial.println("Power cycle signal received");
powerCycle();
}
}

void powerCycle() {
digitalWrite(RELAY_PIN, LOW); // Open relay to cut power
delay(2000); // Wait 2 seconds
digitalWrite(RELAY_PIN, HIGH); // Close relay to restore power
}

Wiring Diagram of TX and RX

Wiring diagram of TX

Wiring diagram of RX


Voltage Setting on Step-down Converter

Before assembling we need to preset the voltage on the step-down converter Connect your 12v source into IN+ and IN- and set the voltage to 5v. measure it from the OUT+ and OUT-- using a multimeter. it is used to provide the necessary 5v to XIAO ESP32C3

RX Assembly

8.1

We can start the assembly by soldering a 10k resistor between G and S of the MOSFET 

8.2

Now place the MOSFET into the 3D printed slot and melt the tab using a soldering iron to fix the MOSFET in place.

8.3

Glue both the step-down converter and XIAO ESP32C3 into the 3d print 

8.4

Connect the antenna to Xiao and attack it on the side wall

8.5

Install both of the DC jack 

8.6

Now complete all of the wiring according to the wiring diagram 

8.7

Glue the front cap and close it 

So we just finished the RX build 

TX Assembly

9.1

Put the push button in the button holder and bend the 4 terminals into the 3d print using a nose player 

9.2

stick the antenna into the body of the 3d print and connect the antenna port to XIAO

9.3

Glue the button holder into the main body using superglue 

9.4

Now push in the switch cap on top of the push button 

So we're done with the build of both devices. Now, let's look at how to set them up.

How to Set Them Up

It is so simple to set them up with your router just connect the 12v power from the adapter to the input DC jack of the receiver, then using a DC to DC male cable, connect the output of the receiver to the power input of the wifi router also power the TX push button using a USB power.