Add GPIO pins for LED on blocked ad alerts

I would like to wire up a simple 1 color LED to some gpio pins to light up whenever an ad is blocked, do you think this would be a pretty simple process?

I have installed syslog-ng and have it dumping my logs both locally and to a remote server, but have no sed/awk chops to speak of, any chance for some help or idea's with a project like this?

Someone has done this already (there are probably other ways, but this seems to work).

Actually what I'm looking to do is different then that post, while a nice idea, that project just shows the status of if pi-hole is active or not via led.

I would like a realtime led indicator light that would blink whenever an ad was blackholed.

I've already determined that the /var/log/pihole.log has an entry with the string "/etc/pihole/gravity.list" in the log's everytime it returns a blocked ad.

I have also installed wiringpi (being I'm actually on an odroid-c1+) to ease the gpio interface.

now I just need to figure out how to trigger the led and parse the pihole.log via a shell or python script of sorts.

Ok, Answered this and have it working!

I have the code in github here: GitHub - dstinebaugh/pihole-led: Realtime LED alerts when an ad is blocked when using pi-hole

Please feel free to use it or include it as you see fit!

full description on my blog here.

But the code needed to get this work requries wiringpi installed, and then either run this script manually or put an entry in /etc/rc.local to have it start on boot, or a cron job etc.

2 Likes

Yes, resurrecting an old post, but I felt like sharing and giving credit to the post that inspired me. :smiley:

I was very bored this past weekend and wanted pi-hole to blink and led when a block happened (as I don't have a fancy screen and I wanted to see if I could :stuck_out_tongue: ).

I was having too much difficulty trying to get the library mentioned earlier in the thread to work, so I started googling for alternatives. Apparently, gpio pins can be controlled through bash directly, and following Daniel's github magic script, was able to incorporate his method into a stand-alone bash script not dependent on other libraries. Unfortunately, it appears his blog link isn't working any more :frowning:

Link to the script in pastebin found here

#!/bin/bash
# Monitors Pi-Hole log and turns an LED connected to physical pin 12 (gpio18)
# when the gravity.list block string is detected in the pi-hole log. Just change gpio[n] to the
# corresponding pin you want to use if you don't want to use physical pin 12
# This script indicates its startup with 5 blinks, but can be freely modified to suit what you want to do.

# Enable control of pin physical pin 12 (gpio18) as an output
echo "18" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio18/direction

# Function section to keep things organized.
# Essentially, these turn the pin on or off
# with a varying wait time in between.
longBlink ()
{
echo "1" > /sys/class/gpio/gpio18/value
sleep 0.75
echo "0" > /sys/class/gpio/gpio18/value
sleep 0.25
}
quickBlink ()
{
echo "1" > /sys/class/gpio/gpio18/value
sleep 0.25
echo "0" > /sys/class/gpio/gpio18/value
sleep 0.125
}

#Script startup visual indicator: Blink 5 times before script runs
for i in {0..4..1}
do
longBlink
done

# Watch the pihole.log file for gravity string and quick blink led to indicate a block
tailf /var/log/pihole.log | while read INPUT
do
if [[ "$INPUT" == *"/etc/pihole/gravity.list"* ]]; then
quickBlink
fi
done

This script will run indefinitely until the tailf task is killed or the pi is rebooted. There is no escape within the script directly!

Edit: script after posting had important characters removed, put them back in and added pastebin

Nicely Done!

Yea my blog is down, I decided paying for all the services I was using (aws, ec2, rds, s3, elb's, cloudfront, etc for a blog with like 6 hit's a day) was a waste so I've taken it offline, but the github live's on!

I have a few other pi specific scripts that I just kinda throw in there if you ever get bored and want the challenge of deciphering my horrible smashed together scripts lol!

For the less Linux savvy of us, how would we go about adding this to pihole, and having it startup with the pi?

Got a few LEDs and stuff I can use to set this up physically, could do with a few pointers on how to do it on the pi code side too

Thanks

I guess it really doesn't tie directly to the pi-hole/dnsmasq service. It just monitors the pihole.log file when new lines are entered. I'm not really linux savvy at all, just learning; so after a bunch of googling ;), the easiest way I've found to get this working is to setup a cron job that calls the script when the pi starts up.

Save the above script in /home/pi/ directory and call the file what ever you'd like, in the following, I called it "ledPiHoleBlinker.sh"

I'm not sure if root was required to talk to the gpio or not, so I ran the crontab utility (manages a set of automated tasks) as root and entered a new line that tells it to start at boot:

As root (or add sudo before the command), enter the following into your terminal:

crontab -e

It may ask what editor you want to use first, I chose 'nano' because it was installed, and I'm no where near comfortable using vim editor. Crontab isn't too complicated, but takes a lot of room to explain in detail, this guide is what I referenced. :slight_smile:

The shorthand to get this script to work at startup is to enter the following on one line at the bottom of the file, then save and exit:

@reboot bash /home/pi/ledPiHoleBlinker.sh

This tells the rapsberry pi to run the script located at /home/pi/ledPiHoleBlinker.sh as a bash script when the pi is booted. Since the crontab editor (-e) was launched as root, editing root's list of jobs, the script will run with root access.

Reboot your pi, and if you followed the script, and didn't make any changes, you should see your LED blink 5 times, then resume blinking when a block is detected.

There are probably better ways to do this, but I haven't really messed with it beyond this tutorial.

2 Likes

Thanks for that, OK so I have the script copied over, added the startup line, got the LED wired to physical pin 12 (gpio 18) & ground, rebooted the Pi, but am not getting the 5 blinks, nor am I getting any blinks for blocked ads

Just ran chmod +x on the script to make it executable (Not sure if necessary) but that didn't work either

I copied your naming and locations exactly to avoid problems

Probably a dumb question, but just figured I would ask. Does the led light up correctly when you plug it into the basic powered pins and the same ground? Like either of the 3.3v or 5v pins? I'll be of more help when I get back home in a few hours and can have my hands my own pi to troubleshoot.

It does yes, tested 5v and that same ground, and also 3.3v and another ground, both worked fine

I'm going to guess that my pihole is not calling / executing the script correctly, I assume that as we added a startup line to execute it at boot, that we could also run it manually while the pi is online

I tried running the script using the startup command but starting from 'bash', "bash /home/pi/ledPiHoleBlinker.sh" without quotes, and it threw up a load of errors

Not sure if that means anything, or if the script can even be run manually, just taking a guess it should work the same as auto at boot

I have some good news and some bad news.

Good news is, I found an issue with the script I posted and have rectified it. Apparently, discourse pulls out # charcters if you don't put a \ in front of them when you start a post. That was an obvious issue when I was looking at the preview pane. But not only does it rip out those, but it also rips out " * " as well :frowning:
Not so obvious in the preview pane, and I missed them in the following line:

if [[ "$INPUT" == *"/etc/pihole/gravity.list"* ]]; then

Live and learn, I'll post things into pastebin from now on x.x
Link to the script in pastebin found here

Also, found that it really does need to be run as root, or else the lines referencing the gpio pins throw and access denied errors. So there's that.

The not so good news;
I don't know why your pi isn't blinking for you :frowning:
Even before I caught the text errors in the script, I was able to at least get the led to blink 5 times at boot when I had setup the "crontab -e" line as root and placed the @reboot line at the bottom of the file.

Just to be sure, I imaged a new sd card with the latest Jessie with Pixel image and installed pi-hole with all defaults (and static ip), and it blunk with no issue :confused:

The issue with being able to re-run it manually by launching the script is if you it starting with the crontab launching it at boot, it will still be in the background and will need to be killed to free up the gpio18 pin for receiving more commands from another task as the script does not escape on its own.

I used the "top" command in the terminal, and scrolled down until I could find an 'tailf' task next to a 'bash' task, and made note of the PID for them both and used a 'sudo kill [pid number]' command to stop them both. (You'll need to press "q" to get out of top). After that, then you can run the script manually.

Edit: phrasing

Well, odd, I tried running the script manually after a reboot, and without killing the task, and it still does the 5 blinks, but just gives a resource is busy error message about line 8, which just enables the pin, so it probably isn't a big issue if it's invoked manually (other than having multiple instances in the background).

Thanks, OK so I've updated the script with your pastebin version - still not getting any blinks at boot

When you say it needs to run as root, how do we achieve that when the Pi automatically logs in as user 'pi' at boot? Or is that something the script can request?

Running it manually results in these errors for me now:


root@raspberrypi:~# bash /home/pi/ledPiHoleBlinker.sh
/home/pi/ledPiHoleBlinker.sh: line 6: $'\r': command not found
: Permission deniedlinker.sh: line 8: /sys/class/gpio/export
: No such file or directoryh: line 9: /sys/class/gpio/gpio18/direction
/home/pi/ledPiHoleBlinker.sh: line 10: $'\r': command not found
/home/pi/ledPiHoleBlinker.sh: line 14: syntax error near unexpected token $'\r'' 'home/pi/ledPiHoleBlinker.sh: line 14: longBlink ()
root@raspberrypi:~#

From what I can see, the errors of command not found '\r' that code is not even in the script....

UPDATE: I accidentally pasted the entire code into PuTTY thinking I still only had the startup line code on clip, and the LED blinked 5 times

Rebooting does not cause the LED to blink at all

So it seems the code is correct, the Pi can run it, but it can not run the .sh script itself

EDIT - It is now also blinking when ads are logged / blocked so long as PuTTY is left running - so the only issue I seem to have, is getting the Pi to run that script from the .sh file, which from my errors above, and your post about needing to run it as root, that is the issue

What is the best way to have the Pi run the script as root at boot?

Sweet, making progress; linux can be a pain. :stuck_out_tongue_closed_eyes:

In the terminal, when you're logged in as 'pi', enter

sudo su

Then press enter. You are now using the terminal as root. Please note that terminal colors are off by default for root. (Blue/green folder and file indicators)

Now you can run the 'crontab -e ' command, and enter that @reboot line mentioned in earlier at the bottom of the file. Save with ctrl+x and press enter. This should add the job to root's cron job list and it will execute your script at boot :slight_smile:

edit: words

1 Like

Ah right, so just adding the startup line as root is enough, I had already done that anyway :slight_smile:

And running the script manually as root doesn't work either, so seems something else is at fault...

I have it flashing for blocked ads, but the only way to achieve it is to run the entire script code from PuTTY and then leave PuTTY open

If I close PuTTY, it stops working

What permissions do you have on the ledPiHoleBlinker.sh script?

Fixed! :smiley:

I'm guessing you used Linux based OS to write the code and copy it to the Pi?

Apparently Windows breaks line endings (Which I am using), I found a thread talking about the same problem, and this code fixed it

sed -i 's/\r//' ledPiHoleBlinker.sh

Now the LED blinks 5 times at boot, and works for each blocked ad / logged

Thanks for your patience and time, good to learn a few things along the way too :slight_smile:

EDIT - Thread in question

Fantastic! :stuck_out_tongue: I had typed the script using nano into the pi terminal directly. I didn't know about the line ending stuff. Even more things to look up and be aware of lol!