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
- Raspberry Pi (3 or 4 recommended) using Rasbperry Pi OS
- Python 3 is installed and basic Python syntax understood
- Breadboard Starter Kit
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
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): greenLED.on() time.sleep(0.1) greenLED.off() time.sleep(0.1) if gettime > 1.5: blueLED.on() time.sleep(0.1) blueLED.off() time.sleep(0.1)
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.
else: display.lcd_display_string(name + " is Down", 1) display.lcd_display_string(status_code, 2) time.sleep(1) gettime = 0 for x in range(0,5): redLED.on() time.sleep(0.1) redLED.off() time.sleep(0.1)
Lastly, we do some clean-up and clear the LCD of any messages and we make sure all LEDs are turned off.
display.lcd_clear() greenLED.off() redLED.off() blueLED.off()