I’ve been working on a small project to automate my router restarts when the internet goes down, and I wanted to share my setup and experiences with the community. Here’s how I approached it:
The Problem:
I’ve noticed that my 4G router occasionally drops the connection, especially during peak hours or when there’s heavy usage. Manually restarting it every time was getting frustrating, so I decided to build a simple solution using an ESP32.
The Solution:
I designed a device that monitors the internet connection and automatically restarts the router if it detects an outage. Here’s how it works:
-
Monitoring the Connection:
- I used an ESP32 module to continuously ping a reliable server (like Google’s DNS at 8.8.8.8) to check the internet status.
- If the ping fails consistently over a set period, the device assumes there’s an outage.
-
Restart Mechanism:
- The ESP32 controls a relay that’s connected to the router’s power supply.
- When an outage is detected, the relay cuts the power to the router for a few seconds and then restores it, effectively restarting the device.
-
Status Indicators:
- I added two LEDs to provide visual feedback:
- A red LED lights up when the connection is lost.
- A yellow LED indicates when the router is being restarted.
- I added two LEDs to provide visual feedback:
The Code and Setup:
I used ESPHome for this project since it’s lightweight and integrates seamlessly with Home Assistant. Below is a simplified version of my code setup:
yaml
esphome:
name: router_monitor
platform: ESP32
board: esp32dev
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
output:
- platform: gpio
pin: 32
id: redLED - platform: gpio
pin: 33
id: yellowLED
sensor:
- platform: ping
ip_address: 8.8.8.8
id: internet_monitor
update_interval: 30s
script:
- id: restart_router
then:- output.turn_on: yellowLED
- delay: 20s
- output.turn_off: yellowLED
globals:
- id: connection_status
type: bool
restore_value: false
interval:
- interval: 30s
then:- if:
condition: !lambda ‘return id(connection_status) == false;’
then:
- sensor.template.publish:
id: connection_status_sensor
state: !lambda ‘id(connection_status) = true; return id(connection_status);’
- output.turn_on: redLED - else:
- output.turn_off: redLED
- if:
automation:
- trigger:
platform: sensor
sensor: internet_monitor
on_state: ‘unavailable’
action:- lambda: !lambda ‘id(connection_status) = false;’
- script.execute: restart_router
Challenges and Tips:
- Power Supply: Make sure the relay can handle the router’s power requirements. I used a 5V relay for mine.
- Placement: Keep the ESP32 and router in a dry, well-ventilated area to avoid any electrical issues.
- Firmware Updates: Regularly check for ESP32 firmware updates to ensure stability.
Alternative Approaches:
If you’re not comfortable with hardware hacking, you could:
- Use a Raspberry Pi to monitor the connection and send a restart command via SSH or a GPIO pin.
- Set up a cloud-based monitoring service that alerts you of outages.
Conclusion:
This project has been a great learning experience and has saved me a lot of frustration. It’s a simple yet effective solution for anyone dealing with frequent router issues. I’d love to hear about your experiences or alternative methods you’ve tried!
Cheers,
[Your Name]