WiFi timed on/off switch ✅
The Problem:
I currently live with my mom in an apartment, and we have our own WiFi router in the living room. My mom states she is "sensitive" to the WiFi being on, and gets headaches, sickness, anxiety, etc. when the WiFi is on, and can't sleep when it is on. I need the WiFi for my self-hosted cloud service, self-hosted web server, and any online work I want to do at home, so I came to an agreement with my mom that I can turn off the WiFi by midnight each night, and only turn it on when I need it. This corresponds to ON-time beginning at 5:30PM (when I get home from work) on weekdays & 9AM on weekends, and OFF-time beginning at 12:00AM (when my mom needs the WiFi off to sleep on both weekends and weekdays).
The Solution:
To solve this issue, I looked at my shelves of components and found a 2-channel relay module (very similar if not identical to the one in the image below) with relay output rated at above US mains voltage, and above the current that the WiFi router will draw.

I thought of using a microcontroller (MCU) powered by the other wall socket (using USB-C or mini-USB) to control the relay, with on and off-times modifiable through re-programming the MCU (since I likely will not change the on and off-times much).
The timing for the real-time clock (RTC) is the main issue in the implementation of this project, since MCU clocks aren't very accurate to real time and drift quite a bit. Since I don't have a RTC module or IC, I think I instead will attempt to use an ESP32 WiFi connection during the ON-time to sync the internal clock to a clock from the internet daily, resetting the clock and re-aligning it with the real world. As long as my ESP32 can handle the WiFi being turned on and off and still connecting to it when the request for the current time goes through, I believe that this method is straightforward and easy for the task at hand.
- In my own research, I found out that using a Network Time Protocol (NTP) for obtaining date and time is quite easy to do on ESP32. The following tutorial covers this on ESP32: https://randomnerdtutorials.com/esp32-date-time-ntp-client-server-arduino/. I think I will sync the timer with real-world time starting 30 minutes after the WiFi ON (to allow for the router to boot up, thus at 6PM, called UPDATE-time) and ending at WiFi OFF. This will consist of connecting to WiFi with a stored SSID and Password in the code, reading the date and time, and making sure the ON-time is changed to 9AM on Friday at 6PM, and then ON-time changed back to 5:30PM on Sunday at 6PM.
- The ESP32 time when not syncing with real-world time, i.e. during the period between WiFi OFF and WiFi ON, is the hardest part to implement in this configuration, since we have to use an internal timer and count time with the "millis()" command. The command's usage is explained here: https://mechatronicslab.net/courses/esp32-arduino-programming-handbook/lessons/non-blocking-delays-with-millis-for-esp32-arduino-programming/. The command just counts the number of milliseconds since the board was powered on or reset, and so I can compare this number when OFF-time begins and then continuously afterwards to see if the difference matches the time difference (in milliseconds) between ON-time and OFF-time to trigger the relay to turn the WiFi back on. As long as I cast the total millisecond count from OFF-time to current time to int32, the natural wrapping around 49 days of continuous counting should always still give the right difference.
The relay turning the normally closed (NC) connection to an open connection (i.e. requiring power; lighting the LED) when the OFF time is reached, with the present time given by the WiFi connection to the NTP server and compared to a pre-programmed value to determine when to turn WiFi OFF. In this case, I programmed the OFF-time to be 9:31PM.
The relay turning the open connection back to it's NC state when the internal millis() timer determines that the time between OFF-time and ON-time has been surpassed. In this case, I programmed the ON-time to be 9:32PM.
I used PlatformIO for the programming of the ESP32, utilizing the "arduino" framework since this is a fairly simple use case, and doing register-level bitmask operations would just be more hassle than it's worth.
The only libraries I had to import were "Arduino.h", "WiFi.h", and "time.h" which all are internal to the PlatformIO and ESP32 toolkits, so I didn't have to download anything.
New Update
Because I wanted this project to feel a little more complete, today I soldered the ESP32 to a perfboard, 3d-modeled and printed an enclosure to fit the relay and the ESP32 with a cable gland for the mains power cable and a hole for the USB-C port. I also mounted the perfboard and the relay board to the bottom of the printed PLA enclosure to keep it secure.
