A lightweight DNS server with ad-blocking capabilities running on ESP32 Rpi Pico W microcontrollers using MicroPython. Block unwanted domains at the network level with a simple web interface for management.
demo.mp4
- DNS Server: Custom DNS server with domain blocking capabilities
- Web Interface: Manage your blocklist through a simple web UI (port 8080)
- DNS Caching: Improves performance by caching DNS responses with automatic cleanup.
- Multi-threaded: Handles DNS and web requests concurrently
- Persistent Storage: Blocklist is saved to flash memory
- Upstream DNS Forwarding: Uses Cloudflare DNS (1.1.1.1) by default
- ESP32 or Pico W development board
- USB cable for programming
- WiFi network connection
- Thonny IDE
- MicroPython firmware
- Download Thonny from https://thonny.org
- Install it on your computer (Windows, macOS, or Linux)
- Launch Thonny
In Thonny:
- Click Tools β Options
- Select the Interpreter tab
- Choose MicroPython (ESP32) from the dropdown
- Click Install or update MicroPython
- Select your USB port (In mac it is usually
USB to UART Bridge, please see the below guide) - Click Install or update MicroPython
- Wait for the process to complete
See this for full guide
- In Thonny, click the Stop/Restart button (red circle with X)
- You should see the MicroPython REPL prompt (
>>>) in the Shell window at the bottom - Try typing:
print("Hello from ESP32!")
- In Thonny, open the file you want to upload
- Click File β Save As...
- Select MicroPython device
- Save the file to the root directory of the device
Upload these files in order:
wifi_manager.pyblocklist_manager.pydns_socket.pyweb_socket.pyblocklist.txtmain.py
- Open
main.pyon your ESP device (from Thonny's file browser) - Modify lines 9-10:
SSID = "YourWiFiName" PASSWORD = "YourWiFiPassword"
- Save the file (Ctrl+S or Cmd+S)
- Make sure
main.pyis open in Thonny - Click the Run button (green arrow) or press F5
- Watch the Shell output for connection status
- Rename
main.pytoboot.pyon the device- Or create a simple
boot.pythat imports main:
import main
- Or create a simple
- Press the RST button on your ESP board
- The application will start automatically
Once the ESP is running, the Shell output will show:
==================================================
DNS Blocker Server Started!
==================================================
Web interface: http://192.168.1.XXX:8080
DNS server: 192.168.1.XXX:53
==================================================
- Open your web browser
- Navigate to
http://[ESP_IP_ADDRESS]:8080 - You'll see the blocklist management page
Add a domain:
- Enter the domain name (e.g.,
ads.example.com) - Click Add
- The domain is immediately blocked and saved to flash
Remove a domain:
- Click Remove next to the domain
- The domain is unblocked and removed from flash
To use the ESP DNS blocker on your devices:
Option 1: Per Device
- Go to WiFi settings on your phone/computer
- Manually set DNS to your ESP's IP address
Option 2: Router-wide
- Log into your router admin panel
- Set the primary DNS to your ESP's IP address
- All devices on the network will use the blocker
Edit dns_socket.py, line 5:
def __init__(self, blocklist_manager, upstream=("1.1.1.1", 53)):Popular alternatives:
- Google DNS:
("8.8.8.8", 53) - OpenDNS:
("208.67.222.222", 53)
Edit web_socket.py, line 4:
def __init__(self, blocklist_manager, wifi_manager, port=8080):Edit dns_socket.py, line 12:
self.cache_ttl = 60 # secondsI don't know, Please test and let me know.
To add many domains at once:
-
Edit
blocklist.txton your computer with one domain per line:ads.example.com tracker.example.net analytics.example.org -
Upload the updated
blocklist.txtto the ESP using Thonny -
Restart the ESP (press RST button)
Popular blocklist sources:
Note: Convert host file format (0.0.0.0 domain.com) to just domain.com
- This is a basic DNS blocker, not a firewall
- It only blocks DNS resolution, not IP-based connections
- Devices can bypass it if they use hardcoded DNS servers
- The web interface has no authentication
- Consider using HTTPS for the web interface in production
Feel free to enhance this project! Some ideas:
- Add authentication to web interface
- Implement HTTPS support
- Add DNS-over-HTTPS upstream support
- Statistics dashboard
- Whitelist functionality
- Regex pattern matching (ESP probably not that powerful to handle this)
MIT License. Feel free to use and modify!
Built with MicroPython.