Skip to content

A Python script that manages an LED bar graph and two activity LED on a raspberry pi

License

Notifications You must be signed in to change notification settings

fcusson/pihole-activity

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

76 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pihole-Activity

A Python project that manages an LED bar graph and two activity LED on a Raspberry Pi

Getting Started

This project’s goal is to enclose a pi-hole server in a project box with added activity lights in the front panel. This project requires a raspberry pi with pi-hole already setup on it.

Preparation

Cloning the repository to your Pi-Hole

Access your pi-hole terminal via your prefered way. In the case of my setup, I access it through SSH

git clone https://github.com/Darkfull-Dante/pihole-activity.git

Installing Dependencies

The following dependencies are required to run the program. Make sure all dependencies are installed with the lines of code bellow before continuing on.

Wiring Pi

WiringPi is a utility for Raspberry Pi to control GPIO. This project is built around the use of this utility. For more details on the project see wiringPi.

Python 3

Python 3 is the most up to date version of Python (at the time of writing the code) and is the version used for the code. Everything could work with Python 2 but this tutorial will not delve into the change required to make it work.

Python 3 RPi.GPIO

Python 3 does not include RPi.GPIO out of the box. It is included in the python3-dev package, but, in order to keep this install lightweight, the dev version was not installed. Required for the correct access of GPIO pins with the python code.

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install wiringpi python3 python3-rpi.gpio

Bill of material

For the final build

Description Link Quantity Price
10 segments red LED Bar Graph AliExpress 1 $1.96
Red LED AliExpress 1 $2.10
Green LED See Red LED link 1 ($2.10)
220Ω resistor AliExpress 12 $1.54
Single Row Right Angle Pin Header AliExpress 11 $1.91
Single Row Pin Header AliExpress 6 $1.15
Female to Female Jumper wires1 AliExpress 15 $1.79
PerfBoard AliExpress 1 $4.34
Nylon 4mm M3 spacer AliExpress 6 $3.75
125x80x32 Project Box2 AliExpress 1 $2.74
Total $21.28

1Ideally look for jumper cable in ribbon style so you can keep bundles together for each circuit. 2The size provided is based on a Raspberry Pi Zero W with an ethernet to micro USB adapter. I suggest you draw everything to size and make sure everything fits before commiting to a project box size.

All prices include shipping and most of them comes in far greater quantity than needed.

For prototyping

If it's not your first Raspberry Pi project, you should have most of these item. They will not be permanent to the build and will be reusable afterward.

Description Link Quantity Price
BreadBoard AliExpress 1 $3.17
Male-to-Male Jumper wires See BreadBoard link 16 ($3.17)
Raspberry Pi GPIO Extender Shield1 AliExpress 1 $1.80
BreadBoard power supply1 See BreadBoard link 1 ($3.17)
Total $4.97

1These two elements are optional, but helps a lot in the prototyping phase.

General

This list of item are purely equipment that will be need to do the build like I did. You can use any other methods of your likng to get to the same result.

  • Soldering Iron
  • Solde
  • Flux
  • Helping hand
  • Electric drill
  • Exacto
  • Hot Glue Gun
  • Multimeter

Raspberry Pi equipment

This is the Raspberry Pi that was used for my build. Any other Raspberry Pi could work for the build, but the size and way the wiring was made is based on this form factor.

  • Raspberry Pi Zero W
  • Ethernet to micro USB adapter
  • 5v 2.5a micro USB power supply
  • 32 Gb micro SD card
  • 20x2 Pin Header (optionnal, but will simplify the connection to the Raspberry Pi Zero)1

1This part is not required but will make any upgrade or changes to the build much easier down the line, as you will not be soldering the jumper wires directly to the board itself.

Circuit Board

This section will review the electronic components of the build. It will explain how the system works, how to prototype, test and make the permanent modules for the build.

Electric Diagram

Electric Diagram

The circuit is made of two distinct elements. First there is the LED Bar graph and second, the activity light module. The two modules receive power from the GPIO and connect to ground.

If you were running V1.0.2 or earlier of the code, the LED Bar Graph was setup for LED on when GPIO was set to LOW. A rewiring of your setup will be required moving foward.

There are multiple advantages to the change in the setup. Mostly, a simpler way to eventually use PWM in the LED Bar Graph. Also, the new circuit does not use the 3.3V rail which releases the PIN for other use.

BreadBoard Prototype

Testing the prototype

BreadBoard Diagram

During the prototyping phase, make sure that everything is connected according to the exemple above and then connect to the Raspberry Pi. Make sure to use an appropriate resistor in the circuit. Make sure that every LED as its own resistor. An LED can draw to much current which could damage the Raspberry Pi GPIO. LEDs are a kind of diodes, which means that current can only go through a certain direction. For the circuit to work, you must make sure that the positive is connected on the side receiving current to work. The positive side of an LED is always the longer leg. In the case of the LED bar graph, there is no visual way to know the positive side other than testing. If at first the bar graph doesn’t light, simply rotate 180 degree and retest.

quick tip: mark the positive side of the LED bar graph for future reference.

When everything is connected and the Raspberry Pi is turned on, run the LEDCheck.py python code. This code will cycle through the 12 LED of the modules. The LED should light up in the following order: LED 1 to 10 of the LED bar graph (in order), green LED, red LED.

When everything is connected to your liking, move to the test folder of the repository

cd ~/pihole-activity/test

then run the python script called LEDCycle

python3 LEDCycle.py

you should get the following result without errors

Bar Graph LED 1 on
Bar Graph LED 2 on
Bar Graph LED 3 on
Bar Graph LED 4 on
Bar Graph LED 5 on
Bar Graph LED 6 on
Bar Graph LED 7 on
Bar Graph LED 8 on
Bar Graph LED 9 on
Bar Graph LED 10 on
Status LED on
ad Blocked LED on

While the code run, make sure that all the LED turn on correctly and in the right order. Make the necessary changes if need be.

Troubleshooting

  • No lights from the LED bar graph: The LED bar graph is probably mounted in reverse. Rotate 180 degree and rerun the code.
  • The Green or Red LED do not light up: make sure that both lights connect to ground. Only one ground is used in the pins, but both lights should join on the negative side to eventually connect to the ground.

Running the scripts

percentCheck.py

Now it is time to test code in action. The first code we will test is percentCheck.py. That code is responsible for Turning on the LED Bar Graph in accordance to the percentage of ads blocked today and also activating the green LED if the status of the Pi-Hole is "enabled". You can run the test code with the following command.

Make sure you are still in the test folder of the repo

python3 percentCheck.py

You should get a similar result as this:

enabled
58
pin 1 on, pin no : 11
pin 2 on, pin no : 12
pin 3 on, pin no : 13
pin 4 on, pin no : 15
pin 5 on, pin no : 16
pin 6 on, pin no : 18
pin 8 off, pin no : 3
pin 9 off, pin no : 5
pin 10 off, pin no : 24
Pi-Hole is enabled
enabled
58
enabled
58
...

Make sure the result and the LED lights are consistent. Try disabling the Pi-Hole to see if the light goes off. When you are done testing, press Ctrl+C to deactivate the code. All LED should go off.

adBlocked.py

The second test code is adBlocked.py. This script is responsible for flashin the red LED every (ish) time an ad is blocked. Due t concern for epyleptics, the refresh rate is currently capted at 2 seconds (the script check if new ads have been blocked every 2 seonds). To test the code, run the following command:

python3 adBlocked.py

Force your pi-hole to block ads. My personnal test is speedtest.net. You should get result like that:

ad Blocked
ad Blocked
ad Blocked
ad Blocked
ad Blocked
...

When you are done testing this element, press Ctrl+C to stop the script.

Permanent

The following are the permenant modules that we will install in the build. Be carefull at this point as the module are made to be permanent. Make sure the design fits for your particular case and that the wiring replicate the prototype we made.

If you know how to use a multimeter, I recommend you test the circuit, making sure there is no unwanted bridge in your module. It simplifies a lot trouble shooting afterward.

LED Bar Graph Module

Front Back
LED Bar Graph Module - Front LED Bar Graph Module - Back

For the LED Bar Graph, make sure that you know which side is the positive. Remember that an LED is a diode and diode will only let current pass from the positive to the negative and not the other way around. I suggest you place all the complonent in the perfBoard without solder first to verify if you are confortable with the design. After you feel ready to start the soldering, go at it one piece at a time. Always start by securing the corners of parts that have more than two pins.

Don't be affraid to bend the metal legs, they will help keep the parts in place when soldering. When your done soldering the parts, cut the excess from the metal legs of the parts.

When you are ready to do the back tracing, make sure to reference where you are soldering as to not make mistake in the soldering lines you'll make. All the resistors should be connected on the same line connecting to one single right angle pin header. That will be were we connect the 3.3v rail.

After that make sure to connect the resistor>LED and then LED>Pin Header in distinct line. Each LED should have its one unique line of solder directing the current to the pins header.

Activity LED Module

Front Back
Activity LED Module - Front Activity LED Module - Back

For this module, a slight modification is required to the straight pin header. On a flat surface, press the short pin side until it slip right up to the plastic part. This mod will let us mount the pin header under the perfBoard leting us connect fro the back instead of the front of the module.

Making the module is very similar to the LED Bar Graph. Make sure the module makes sense to you and that you are confortable soldering the parts. This one might be more difficult as it is more compact. A helping hand comes in very handy.

Ground Converger Module

Front Back
Ground Converger Module Front Ground Converger Module Back

This is the simplest of the module. This one is completely optionnal and is only to reduce the footprint on the Raspberry Pi. Basically, we want a module that will simply take all our ground connection into one main ground that will connect to one ground GPIO.

If you do not want that soluttion you can simply use two ground pin on your Raspberry Pi

For this build, the minimum is 3 pins, 2 inputs, 1 output. If you think of any possible expension in the future tu your build, I suggest you already add more ground pins. My build is setup with 5 inputs and 1 output.

Installation

Change boot option

Change boot option to make sure that the user "pi" is autologged-in at boot to force scrypt to run:

sudo raspi-config

From there go in System 1 Options/S2 Boot & Auto Login/B2 Console AutoLogin

Run script at startup

We will be using the etc/profile to run the 2 scripts at boot. To do so, first open the profile file

sudo nano /etc/profile

then add the 2 following line of code at the end of the file

sudo python /home/pi/pihole-activity/src/percentCheck.py &
sudo python /home/pi/pihole-activity/src/adBlocked.py &

When ready, reboot your pi-hole

sudo reboot

Let the Pi-Hole boot, it can take sometime, but the LED will eventually light up.

Config.ini

This section will go through the different settings possible in the config.ini file. (I know there is just one right now, I wrote the paragraph for future settings to come)

LEDBarGraph

option description
reverseLEDBarGraph Lets you reverse the order of the pins for the LED bar graph. If you were to mount the bar graph in reverse by accident or solder backward the connection to your raspberry pi, this setting lets you reverse the order (pin one becomes 10, 2 becomes 9, etc.). Saves you some resoldering. (I'm obviously not talking by experience)
highModeBarGraph Lets you change the power mode of the LED bar graph. High Mode means that an input most be high to turn On the LED. Set to true for this behavior. If your circuit expect a low signal to turn on, set to false.

StatusLED

option description
reverseStatus Lets you reverse the order of the pins for the status module. If you were to mount the leds in reverse by accident or solder backward the connection to your raspberry pi, this setting lets you reverse the order (pin 37 becomes 35). Saves you some resoldering. (I'm obviously not talking by experience)
highModeStatus Lets you change the power mode of the status module. High Mode means that an input most be high to turn On the LED. Set to true for this behavior. If your circuit expect a low signal to turn on, set to false.

Enclosing the project

This part will guide you though the steps of enclosing your project inside a project box. If you are using a different Raspberry Pi thant the Zero W, the placement of the parts, the cable management and holes required may differ.

The parts you have might be different than mine, always measure and carefully cut. You can always cut more in a small hole, it's a lot harder to add material to a bigger hole

1. Trace the project on paper

HandDrawn Tracing

Take time to measure everything as much as you can. A suggestion, make sure you have evrything appart from the project box first so you can have precise measurements. Make sure the design and cable management make sense.

2. Place without glueing or screwing

3. Mark the places where holes are needed

4. Remove the material

5. Fix the components

Inside the case

Contribution

Author

About

A Python script that manages an LED bar graph and two activity LED on a raspberry pi

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages