Telegram

Someone is Working on an Awesome Pi-hole Dashboard, Complete with an OLED Screen

Here at Magisk Modules, we’re constantly exploring innovative ways to enhance our digital lives, optimize network performance, and bolster online privacy. That’s why we’re incredibly excited to share a deep dive into a truly captivating project: an enhanced Pi-hole dashboard, beautifully complemented by an OLED screen for real-time monitoring. While many have implemented standard Pi-hole setups, this project takes the concept to a whole new level of elegance and functionality. Prepare to be inspired as we dissect the intricacies of this impressive endeavor.

Elevating Pi-hole: Beyond the Standard Web Interface

Pi-hole, the network-level advertisement and tracking blocker, is already a powerful tool. Its default web interface provides essential insights into network activity, blocked domains, and query logs. However, for users craving a more visually engaging and immediately accessible overview, a dedicated dashboard is a game-changer. This project transcends the limitations of the standard interface by offering:

  • Real-time Data Visualization: Instead of navigating through web pages, crucial Pi-hole statistics are displayed dynamically on the OLED screen. This allows for instant monitoring of network activity without the need to log in to the web interface.

  • Customizable Metrics: The displayed information is not fixed. Users can tailor the dashboard to showcase the most relevant metrics, such as total queries, queries blocked, percentage blocked, unique clients, and even real-time query logs scrolling across the screen.

  • Enhanced Aesthetics: The OLED screen provides a sleek and modern aesthetic, transforming the Pi-hole setup from a purely functional device into a visually appealing component of any tech enthusiast’s workspace.

The Allure of OLED: Why This Display Technology Matters

The choice of an OLED (Organic Light Emitting Diode) screen is no accident. OLED technology offers several advantages over traditional LCD displays, making it the ideal choice for this project:

  • Superior Contrast Ratio: OLED displays boast virtually infinite contrast ratios, meaning that blacks are truly black. This results in vibrant colors and sharp text, even in low-light conditions. This is particularly important for a dashboard that may be viewed in a variety of lighting environments.

  • Wide Viewing Angles: OLED screens maintain consistent image quality even when viewed from extreme angles. This ensures that the dashboard remains readable regardless of the user’s position.

  • Fast Response Time: OLEDs have incredibly fast response times, eliminating blurring and ghosting. This is crucial for displaying real-time data updates smoothly and seamlessly.

  • Energy Efficiency: While OLEDs can consume more power when displaying bright images, they are generally more energy-efficient than LCDs when displaying predominantly dark content, which is often the case with dashboards.

  • Compact Size: OLED screens are available in small form factors, making them ideal for embedding within a Pi-hole enclosure or mounting discreetly on a desk.

Deep Dive: Components and Software Architecture

Building such a sophisticated dashboard requires a combination of hardware and software expertise. Let’s break down the key components and their roles:

Hardware Components

  • Raspberry Pi (or Equivalent): This serves as the brains of the operation, running the Pi-hole software and handling the data processing required to generate the dashboard display. A Raspberry Pi Zero W is often sufficient for this task due to its low power consumption and small size, but a more powerful model like the Raspberry Pi 4 may be preferred for more demanding visualizations or additional functionalities.

  • OLED Screen: As previously discussed, the OLED screen is the primary display for the dashboard. Common sizes range from 0.96 inches to 1.3 inches, and connectivity is typically achieved via I2C or SPI.

  • Enclosure (Optional): A custom-designed enclosure can provide a professional and aesthetically pleasing housing for the Raspberry Pi and OLED screen. This can be 3D-printed or constructed from other materials like wood or acrylic.

  • Power Supply: A reliable power supply is essential to ensure stable operation of the Raspberry Pi and OLED screen. A standard USB power adapter is usually sufficient.

  • Connecting Wires: Jumper wires are needed to connect the OLED screen to the Raspberry Pi’s GPIO pins.

Software Architecture

The software side of the project involves several key components working in concert:

  • Pi-hole: The core of the system, responsible for blocking advertisements and tracking domains.

  • Data Collection Script: A custom script (typically written in Python) is responsible for collecting data from the Pi-hole API. This script retrieves metrics such as total queries, queries blocked, percentage blocked, and client statistics.

  • Data Processing and Formatting: The collected data is then processed and formatted into a format suitable for display on the OLED screen. This may involve calculating percentages, rounding numbers, and formatting text strings.

  • OLED Display Driver: A driver library (such as Adafruit’s SSD1306 library) is used to communicate with the OLED screen and render the data. This library provides functions for drawing text, shapes, and images on the display.

  • Display Logic: This component is responsible for defining what data is displayed on the OLED screen and how it is arranged. This may involve creating a custom layout with different sections for different metrics.

  • Scheduling: A scheduler (such as cron) is used to run the data collection script periodically, ensuring that the dashboard is updated with real-time data.

Implementation Details: From Code to Completion

Let’s delve into the practical aspects of building this enhanced Pi-hole dashboard.

Setting Up the Raspberry Pi

  1. Install Raspberry Pi OS: Start by installing the latest version of Raspberry Pi OS (formerly Raspbian) on an SD card. We recommend using the Raspberry Pi Imager tool for this process.

  2. Enable SSH: Enable SSH access to the Raspberry Pi so that you can connect to it remotely. This can be done during the initial setup process or later through the raspi-config tool.

  3. Connect to Wi-Fi: Configure the Raspberry Pi to connect to your Wi-Fi network.

  4. Update and Upgrade Packages: Run the following commands to update and upgrade the installed packages:

    sudo apt update
    sudo apt upgrade
    
  5. Install Pi-hole: Follow the official Pi-hole installation guide to install Pi-hole on your Raspberry Pi.

Connecting the OLED Screen

  1. Enable I2C (or SPI): Depending on the OLED screen’s connectivity, enable I2C or SPI in the raspi-config tool.

  2. Connect the Wires: Connect the OLED screen to the Raspberry Pi’s GPIO pins according to the screen’s documentation. For I2C, the typical connections are:

    • SDA (Serial Data) to GPIO2 (pin 3)
    • SCL (Serial Clock) to GPIO3 (pin 5)
    • VCC to 3.3V (pin 1)
    • GND to GND (pin 6)
  3. Install Required Libraries: Install the necessary libraries for communicating with the OLED screen. For example, if using the Adafruit SSD1306 library, run:

    sudo pip3 install adafruit-circuitpython-ssd1306
    sudo apt-get install python3-pil
    

Writing the Data Collection and Display Script (Python Example)

import time
import Adafruit_SSD1306
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import requests

# Pi-hole API endpoint (replace with your Pi-hole's IP address)
PIHOLE_API_URL = "http://pi.hole/admin/api.php"

# OLED screen dimensions
WIDTH = 128
HEIGHT = 64

# Initialize OLED display (I2C)
RST = None  # Reset pin (can be set to None if not used)
DC = 23
SPI_PORT   = 0
SPI_DEVICE = 0
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)

# Initialize library.
disp.begin()

# Clear display.
disp.clear()
disp.display()

# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
image = Image.new('1', (WIDTH, HEIGHT))

# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)

# Load default font.
font = ImageFont.load_default()

def get_pihole_data():
    try:
        response = requests.get(PIHOLE_API_URL)
        response.raise_for_status()  # Raise HTTPError for bad responses (4xx or 5xx)
        data = response.json()
        return data
    except requests.exceptions.RequestException as e:
        print(f"Error fetching data from Pi-hole API: {e}")
        return None

while True:
    # Get Pi-hole data
    pihole_data = get_pihole_data()

    # Clear the image
    draw.rectangle((0, 0, WIDTH, HEIGHT), outline=0, fill=0)

    if pihole_data:
        # Extract data from the API response
        domains_blocked = pihole_data.get('domains_being_blocked', 'N/A')
        dns_queries_today = pihole_data.get('dns_queries_today', 'N/A')
        ads_blocked_today = pihole_data.get('ads_blocked_today', 'N/A')
        percentage_blocked = pihole_data.get('ads_percentage_today', 'N/A')

        # Format the data for display
        line1 = f"Total Queries: {dns_queries_today}"
        line2 = f"Blocked: {ads_blocked_today}"
        line3 = f"Percentage: {percentage_blocked}%"
        line4 = f"Domains Blocked: {domains_blocked}"

        # Draw the text on the image
        draw.text((0, 0), line1, font=font, fill=255)
        draw.text((0, 16), line2, font=font, fill=255)
        draw.text((0, 32), line3, font=font, fill=255)
        draw.text((0, 48), line4, font=font, fill=255)
    else:
        draw.text((0, 0), "Error fetching data", font=font, fill=255)

    # Display image.
    disp.image(image)
    disp.display()

    # Wait for a specified time (in seconds)
    time.sleep(10)

Running the Script Automatically

To ensure the dashboard is always running, you can add the script to cron:

  1. Open the crontab editor: crontab -e

  2. Add the following line to run the script every minute:

    * * * * * python3 /path/to/your/script.py
    

    Replace /path/to/your/script.py with the actual path to your Python script.

Advanced Customization and Enhancements

  • Custom Metrics: Expand the data collection script to retrieve and display other Pi-hole metrics, such as the number of unique clients, the top blocked domains, or the real-time query log.

  • Dynamic Layout: Implement a dynamic layout that adjusts the displayed information based on the available screen space or user preferences.

  • Web Interface Integration: Create a web interface that allows users to configure the dashboard settings and customize the displayed metrics.

  • Alerts and Notifications: Add alerts that trigger when certain thresholds are met, such as a spike in network activity or a significant increase in blocked domains.

  • Integration with Home Automation Systems: Integrate the dashboard with home automation systems like Home Assistant to provide a centralized view of network activity and security.

Troubleshooting Common Issues

  • OLED Screen Not Displaying Anything: Double-check the wiring connections, ensure that I2C or SPI is enabled, and verify that the correct driver library is installed.
  • Data Not Updating: Verify that the Pi-hole API is accessible, that the data collection script is running correctly, and that the cron job is configured properly.
  • Script Errors: Carefully examine the script’s output for any error messages and consult the documentation for the relevant libraries.
  • Incorrect Data Display: Double-check the data extraction and formatting logic in the script to ensure that the correct values are being displayed.

Conclusion: A Powerful and Elegant Solution

Building an enhanced Pi-hole dashboard with an OLED screen is a rewarding project that combines functionality, aesthetics, and technical expertise. It provides a visually appealing and immediately accessible overview of network activity, empowering users to monitor their network security and optimize their online privacy. By following the steps outlined in this guide, you can create your own custom Pi-hole dashboard and elevate your network monitoring experience to a whole new level. We at Magisk Modules encourage exploration and innovation in the pursuit of a more secure and efficient digital world. For further Magisk Modules exploration, please visit Magisk Module Repository.

    Redirecting in 20 seconds...