I recently setup a Raspberry PI as a DNS Sinkhole to block ads and Trackers on my home network and I love it. I paid about $120 for a Raspberry Pi LCD Screen and Case from Pakronics to go with a Raspberry Pi 3B+ I had laying around to Setup as a PiHole. This post will show how I made my Screen buttons work.
This post is not a sponsored advertisement, I like to log what I have done for documentation
Long story short, the Pi Hole is great and the LCD Screen and Case is working a treat and I needed to get the buttons on the LCD Panel and case working.
I love the stats that PiHole provides.
In only have Python 2.7 (and for a device on a private home network) and this is ok, given Python 2.7 is no longer supported.
How to check your Python version.
$ python --version
Python 2.7.16
I know Python 2.7 is a bit old, Python 2.7.16 was released in March 2019. I need to update my script to Python 3.
I created this Python Script to handle the button events
sudo nano ~/simonpihole_buttons.py
I added this code to the Python file that will handle the LCD button presses.
Tip: Make sure your commands work before you add them to the Python Script.
#!/usr/bin/python
import subprocess
import time
import RPi.GPIO as GPIO
import os
GPIO.setwarnings(False)
channel_list = [17, 22, 23, 27]
backlightOn = True
piHoleOn = True
def buttonPowerOff(channel):
os.system("sudo shutdown -h now")
def buttonToggleBacklight(channel):
global backlightOn
if backlightOn:
backlightOn = False
backlight.start(0)
else:
backlightOn = True
backlight.start(100)
def buttonDisablePiHole(channel):
global piHoleOn
if piHoleOn:
piHoleOn = False
os.system('pihole disable')
else:
piHoleOn = True
os.system('pihole enable')
def buttonRestartDNS(channel):
os.system('pihole restartdns')
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel_list, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(18, GPIO.OUT)
backlight = GPIO.PWM(18, 1023)
backlight.start(100)
GPIO.add_event_detect(27, GPIO.FALLING, callback=buttonPowerOff, bouncetime=200)
GPIO.add_event_detect(22, GPIO.FALLING, callback=buttonToggleBacklight, bouncetime=200)
GPIO.add_event_detect(23, GPIO.FALLING, callback=buttonDisablePiHole, bouncetime=200)
GPIO.add_event_detect(17, GPIO.FALLING, callback=buttonRestartDNS, bouncetime=200)
while True:
time.sleep(1)
GPIO.cleanup()
I tested the script and commands many many times and I can the python script to test the button functionality
cd ~
sudo python simonpihole_buttons.py
I decided to make the first button (from the top) power down the pi, then Disable the PiHiole Service then toggle the Raspberry Pi LCD (ON/OFF) and finally the last button restarts the DNS service.
I created this file to create the service for the Python script
sudo nano /etc/systemd/system/simonspibuttons.service
I added the following code to the service
[Unit]
Description=Simons pibuttons Script
After=multi-user.target
[Service]
User=pi
WorkingDirectory=/home/pi
ExecStart=/usr/bin/python -u /home/pi
/simonpihole_buttons.py
[Install]
WantedBy=multi-user.target
I ran this command to start the button service
sudo systemctl enable simonspibuttons.service
sudo systemctl start simonspibuttons.service
If this does not work you can reboot your pi.
{“type”:”block”,”srcIndex”:24,”srcClientId”:”00b90020-9b1d-4d70-a9f9-62282db13e7f”,”srcRootClientId”:””}After the service was enabled I checked it’s status.
sudo systemctl status simonspibuttons.service
● pibuttons.service - pibuttons Padd PiHole Script
Loaded: loaded (/etc/systemd/system/simonspibuttons.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2020-05-08 19:13:33 AEST; 21h ago
Main PID: 787 (python)
Tasks: 3 (limit: 2067)
Memory: 6.4M
CGroup: /system.slice/simonspibuttons.service
└─787 /usr/bin/python -u /scripts/simonpihole_buttons.py
May 08 19:13:33 raspberrypihole systemd[1]: Started Simons pibuttons Script.
Cool the service has enabled and is working
Result
After a few weeks I am happy with the button order and functionality.
Shutting down from a button press (before a storm or a the kids bedtime is priceless). I did add some of my wife’s nail polish to add a different colour to each button.
I intend to add a flat heat pipe and external heatsink to the PiHole so there will be heaps of room for printed labels for each button. The Pi CPU doe’s not get super hot but it does get above 55c from time to time.
I love my PiHole
Troubleshooting
If you having troubles making the service run the following to force the service creation (but enter your service name)
cd /etc/systemd/system
sudo systemctl --force --full edit simonpibuttons.service
The Python service won’t boot, try
systemctl journalctl --boot --unit=simonibuttons.service
The Python script won’t run, try
chmod +X /scripts/simonpihole_buttons.py
Install Python 2.7
sudo apt-get install python2.7
v1.1