led python

Earlier in the tutorial, SEO Guide to Creating a Website Uptime Monitor Using Python I showed you how to create a simple uptime monitor and store that information in MySQL. This next phase is a direct extension of the previous tutorial and assumes you have that all set up. The code in the tutorial will fit into the previous code. Now we can extend this uptime monitor for real-time visual notifications with LEDs and a small 16×2 LCD screen. There is a decent amount of setup involved if you don’t already have a Raspberry Pi and a breadboard kit. Don’t let that discourage you though.

If I can get this to work, anyone can believe me. This originated as an April 2020 quarantine project and it was so much fun. It’s nerdy, creative and so satisfying when those lights go on! Let’s get started!

Requirements and Assumptions

Importing Necessary Modules and Components

Python modules are like libraries in other coding languages. They are collections of pre-made functions that you can use to save time by not reinventing the wheel. Most of the Python modules we’re going to use should be preinstalled, but one that isn’t is gpiozero. GPIO stands for General Purpose Input/Output and is the circuitry behind how the Raspberry Pi communicates with the breadboard.  To install these, go to your command terminal and type in this command:

pip3 install gpiozero

To communicate with the 16×2 LCD screen install this driver

Breadboard and circuit setup is beyond the scope of this tutorial. It’s not overly complicated. It’s simply connecting the breadboard to the Raspberry Pi. Inserting LEDs and resistors and connecting with pins. I barely passed high school and figured this out, don’t let it scare you. Here is a quick tutorial.

LCD screen install is also not complicated, here is the tutorial.

Getting the Script Ready

Fire up your favorite code editor or IDE. I recommend PyCharm for more experienced coders or Thonny for beginners.

Let’s start by adding these imports. Don’t forget this script is an add-on to this. gpiozero is used for turning the LEDs on and off on the breadboard. lcddriver is for sending text to the LCD screen. Remember all this code from here on out should be merged in with the previous tutorial.

from gpiozero import LED
import lcddriver

Setup the LED Objects

Now let’s define LED variables as LED() objects. We are going to blink the green LED if the site is up, red if the site is down and blue in addition if the site is responsibly slowly. The number in LED() function will correspond to which GPIO pin you connect the LED to.

greenLED = LED(12)
redLED = LED(13)
blueLED = LED(14)

Send Text to the LCD

It’s time we send text to the LCD screen. We first register the LCD driver with lcddriver.lcd(). The screen holds about 16 characters for each of the two rows. The number parameters indicate which line should display each string. In the first line, we print out the name of the website and the HTML download speed time. The second line will print out the status code of the request to the website. Usually either a 200 is up or a 404 if the URL is down.

display = lcddriver.lcd()
display.lcd_display_string(name + " Speed:" + str(gettime), 1)
display.lcd_display_string(status_code, 2)

Turn on LEDs

Let’s start using those LEDs! If the response code is 200 we’ll start a loop that runs 5 times. This helps save having to manually turn the LEDs on and off for blinking. To turn the green LED on we simply called on(). To create a blink we need a pause, so we’ll delay the script a tenth of a second and turn the LED off using off(). We can only determine speed if the site is up, so here we will check if gettime which stores the HTML download speed, is greater than 1.5s. If it is, turn the blue LED on and off.

if response.status_code == 200:
    for x in range(0,5):
        if gettime > 1.5:

If the website is down, or not returning a status code of 200 it’s time to print out that message on the LCD and activate the red LED. This code is similar to the above with the exception that the LCD message is different and the blue LED code is exuded.

    display.lcd_display_string(name + " is Down", 1)
    display.lcd_display_string(status_code, 2)
    gettime = 0
    for x in range(0,5):

Lastly, we do some clean-up and clear the LCD of any messages and we make sure all LEDs are turned off.


Working Example

Greg Bernhardt
Follow me

Leave a Reply