Flip it Off: How to Build a Website Blocker Switch with Raspberry Pi

Building Website Off Switches with a Raspberry Pi
(Image credit: Tom's Hardware)

If you’re like me, you spend a little too much time on a few websites, particularly social media sites. Try as you might to avoid them, you find yourself unconsciously typing in Facebook, Reddit, or something else into your search bar. There are a ton of browser extensions, desktop apps, and router rules you can configure to help, but I wanted something more physical.

This project uses light switches and an at-home DNS server running on a Raspberry Pi Zero to turn on or off access to specific websites. If I want access to a site I have blocked, I need to physically get up and turn off the switch -- something that’s slightly more difficult than turning off a browser extension.

What You’ll Need To Make A Website Off Switch

  • Raspberry Pi Zero W, SD Card, and Power Supply
  • Two single-pole light switches
  • A PVC double-gang switch box
  • A plastic 2-toggle switch plate
  • Four male-female jumper wires
  • Phillips and flathead screwdrivers

How to Build a Website Off Switch With a Raspberry Pi

1. Set up your Raspberry Pi Zero W and connect to it remotely. If you don’t know how, see our tutorial on how to set up a headless Raspberry Pi.

2. Update and Install git if not already installed.

sudo apt-get update && sudo apt-get install -y git

3. Clone the code from this repository. It will handle the management of the dnsmasq tool, which will be our new DNS server.

git clone https://github.com/rydercalmdown/internet_kill_switch

4. Run the installation command from within the newly cloned directory. This will take care of installing all base dependencies, installing the dnsmasq tool, and ensuring it’s running.

cd internet_kill_switch
make install

5. Ensure dnsmasq is running by entering the following command. To exit, use Ctrl + C. You should see status “Running” in green.

sudo systemctl status dnsmasq
# Use Ctrl + C to exit

6. Try running the software to ensure everything works accordingly. You should see a few log messages with no errors. Exit the software with Ctrl + C.

make run
# Use Ctrl + C to exit

7. Wire the male end of each jumper cable to the switches. You should be able to tuck the pin under the screw and tighten it until it’s snug. These switches are designed for high AC voltage, but will work just the same for our project.

(Image credit: Tom's Hardware)

8. Attach the female end of the jumper cables to the Raspberry Pi. Pin order doesn’t matter, as long as one end of the switch goes to a GPIO pin, and the other goes to a ground pin. The default code expects GPIO (BCM) pins 18 and 24, though this is configurable.

(Image credit: Tom's Hardware)

9. Tuck the raspberry pi into the PVC double-gang switch box, and affix it to the back with hot glue.

(Image credit: Tom's Hardware)

10. Run the power cable through the switch box inlet, and connect it to the Raspberry Pi.

(Image credit: Tom's Hardware)

11. Attach both switches to the switch box using the screws they came with.

(Image credit: Tom's Hardware)

12. Place the switch plate on top of the switches, and attach it with the screws it came with.

(Image credit: Tom's Hardware)

13. Label which switch belongs to which site. Keep track of which switch is attached to which pin.

(Image credit: Tom's Hardware)

14. Open the src/toggle_switches.py file for editing.

cd src/
nano toggle_switches.py

15. Modify toggle_switches.py to list the websites you want to block for each switch. You can add or remove switches by modifying the SWITCHES list, and adding new objects of the class SwitchConfig. The accepted parameters are the BCM pin number, followed by a list of sites to block.

# Example: The switch connected to BCM pin 18 blocks social media, and the switch connected to BCM pin 24 blocks video websites
SWITCHES = [
   SwitchConfig(18, ['facebook.com', 'instagram.com']),
   SwitchConfig(24, ['netflix.com', 'youtube.com', 'vimeo.com']),
]

16. Run the application to test it, and make note of the IP address it prints out when starting - that’s the IP address of your Pi.

cd ~/internet_kill_switch
make run
# IP Address is 10.0.0.25

17. Configure your DNS servers on your computer with that IP address. I'm configuring mine in System Preferences > Network of my MacBook, but you can also configure it at the router level.

18. Test the switches on the machine that now uses your Pi as a DNS server - in my case, my MacBook. With the switches in the off position, you should be able to access websites freely. Once you flip the switches to the on position, after a short delay, you should receive a network error when trying to visit that website.

(Image credit: Tom's Hardware)

19. To confirm, use the following command on your computer, substituting google.com with the site you’ve chosen to block:

# When the switch is off
dig +short google.com
172.217.164.238
# When the switch is on
dig +short google.com
127.0.0.1

20. Edit your /etc/rc.local file to include the following line, so this software runs when your Pi reboots.

sudo nano /etc/rc.local
# Add this line before the very last line
cd /home/pi/internet_kill_switch/src && sudo ../env/bin/python app.py &

21. Mount your switches. If you would like to put this switchbox on the wall, you’ll have to find a place to mount it that’s close to an outlet so you can power the Pi. Alternatively, you can just leave the switchbox on a table or shelf somewhere.

Building Website Off Switches with a Raspberry Pi

(Image credit: Tom's Hardware)

Enjoy, and if ever you’re not able to load websites, try removing the Pi’s IP address from your DNS servers - there’s likely a problem.

Ryder Damen
Freelance Writer

Ryder Damer is a Freelance Writer for Tom's Hardware US covering Raspberry Pi projects and tutorials.

  • randomizer
    Admin said:
    If I want access to a site I have blocked, I need to physically get up and turn off the switch...

    You need to physically get up to block the site too though. Maybe you'll find that too hard. :)
    Reply