HOWTO: Build a Temperature Alert System using a Raspberry Pi

I recently had my furnace fail while I was away on business.  Fortunately there were no lasting consequences but it could have been a very bad day.  To try and minimize the risk of such issues in the future, I decided I wanted a temperature monitoring solution that would alert me if the temperature in the house dropped below a pre-defined threshold.  I looked at things like a Nest and the TempStick among others but all of them were hundreds of dollars and or relied on third party cloud services.  Before I invested in something like that, I wanted to see if I could build my own solution for cheaper and that was designed to meet my exact needs.  I recently received a Raspberry Pi from a friend and decided this would be a great use for such a project. 

Specifically I set out to have the following goals:

  • The Raspberry Pi would record the temperature from a dedicated external temperature sensor every 20 minutes and record that value into a SQL database along with the timestamp
  • The Raspberry Pi would host a webserver that would allow me to review the current and historical temperatures from anywhere using my mobile phone
  • The webpage would also include a generated graphical chart showing the historical temperatures so I can review for any odd behavior at a glance
  • The Raspberry PI would automatically email me if the temperature dropped below a defined threshold so I could make phone calls to take immediate action
  • The Raspberry PI would upload a file to my off-site website host during each scanning interval via FTP
  • A cron job would run on my off-site webhost that would look for that file and if that file was not updated for more than one hour would alert me that either a power or Internet failure occurred at my house

I’m happy to report I achieved all of the goals above and figured I’d should share how I did it below.  Keep in mind I am not a software developer and so this solution is the result of grabbing things from the Internet and cobbling them together and tweaking until it does what I want.   With that out of the way, let’s begin with what you need to create something like this for yourself.

Hardware (Note: The Amazon Links are for reference only.  I purchased similar components from my local electronics shop)

Wiring

Once you have all the components above, you’ll need to wire them together.  I don’t have any experience with this kind of thing so I blindly followed this excellent Youtube video and it worked on the first try:

Software

You’ll need an operating system onto your SD card.  I used Rasbian which came from the official NOOBS installer available here: https://downloads.raspberrypi.org/NOOBS_latest.torrent

From here, I relied heavily on the following articles from others to complete the remainder of the code:

https://www.instructables.com/id/From-Data-to-Graph-a-Web-Jorney-With-Flask-and-SQL/
https://www.dexterindustries.com/howto/run-a-program-on-your-raspberry-pi-at-startup (Used the systemd method)
https://www.keanei.com/2017/02/09/uploading-a-file-automatically-with-ftp-on-the-raspberry-pi/
https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-14-04

Things I wish I knew when I started

This has been a surprisingly difficult project for me as there was a lot of new concepts I had to learn.  If I could go back in time and give myself some advice to save some heartache, here is what I would tell myself:

  1. Python does not use brackets to define where functions begin and end.  Instead it relies on the indentation of the code to determine this
  2. Flask, the web server that the main documentation followed uses for this project is intended as a development web server.  Specifically it by design only supports one connection at a time.  Therefore you need to add in some other stuff to make the web server “production ready”
  3. That other stuff involves something called “WSGI” or Web Server Gateway Interface.  I don’t fully understand it but this appears to be a framework you can insert in front of Flask to handle more of the requirements of a production web server
  4. The WSGI I used to sit in front of Flask is called Gunicorn
  5. There are many, many options to make a Python script run on boot.  I ended up using systemd and it worked as I wanted it to
  6. Specifically, once you make your script a service, you can manage it using the command sudo systemctl restart servicename
  7. You have to restart the service each time you make a change to the code
  8. To review any error messages the service/script might have generated, use sudo journalctl –u servicename
  9. The command sudo python scriptname is not the same as sudo python3 scriptname.  Both will work potentially but the former may behave in unexpected ways.  Make sure you use python3

10.   Flask hard codes the folders it expects to find its content in, specifically it has a /templates folder where your index.html must reside and /static for everything else

Script Files Overview

image

You can download all of the files above from here: https://pleasework.robbievance.net/downloads/pi-temperature-monitor.zip

Disclaimer: This code is provided as-is and is cobbled together from other sources.  There is very limited comments or other documentation at this time. 

Once this code is up and running, you should be able to use a web browser to connect to port 80 on your Raspberry PI and see the screen below:

image

In addition, if the Raspberry pi does not “check in” to the off-site web server for more than an hour, you should receive an email like the one below:

image

Finally, if the temperature drops below 15 degrees Celsius, you should receive an email indicating that.  (Note for testing purposes I set the threshold to 25 degrees for the example below)

image

This is about all the patience I have for this project today.  I may come back and provide a more detailed review of the actual code.  But if not and you have questions, I encourage you to leave them in the comments below and I’ll do my best to respond. 

1 comment

    • D. Gray on October 26, 2020 at 10:18 am
    • Reply

    There is a dedicated monitoring package you might be interested in that I use for monitoring the temperature for our server room.
    It can send out alerts if a threshold is gone below/above. It keeps historical date and has a nice web interface: https://xavierberger.github.io/RPi-Monitor-docs/01_features.html

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.