2020-01-15

How To Create Temperature Monitor with Email Alerts

I need to monitor the temperature in a room and make sure it doesn't get too cold in the winter, so I decided to setup a raspberry pi as my temperature sensor, and create a simple application/service that regularly checks the temperature and updates me via email.

Once you know what you're doing it's really quite easy.  I had to do a lot of research, so it took me longer than I'd like to admit, but it was fun setting everything up and learning.  I stuck with python throughout because it simplifies everything.

Reading the Temperature 

First, you need a temperature sensor.  There's a really good tutorial by circuitbasics.com.  All you need is a raspberry pi 3b (older and cheaper ones probably work too), DS18B20, and a 4.7K resistor (anything between 1K and 10K would probably work).  I also used a 2x4 .1" female header for soldering the temperature sensor circuit so that I can easily connect it to the raspberry pi.

After connecting the temperature sensor circuit to the raspberry pi as shown in the tutorial and enabling the 1-wire interface, you'll be able to read the temperature sensor.

  1. Add this to the end of /boot/config.txt: dtoverlay=w1-gpio
  2. Reboot the raspberry pi
  3. Execute: sudo modprobe w1-gpio;sudo modprobe w1-therm
  4. Execute: cat /sys/bus/w1/devices/28-*/w1_slave
  5. The temperature is at the end (t=x) in thousandths of degrees Celsius
The tutorial also includes a bit of python code to read and output the temperature.

Web Service

The web service was extremely simple to create.  I used flask, and followed the hello world tutorial from pythonspot.com.  Then I added the ip address for external access, customized the port number and merged the temperature reading python code from before.  The web service I created simply returns the temperature in degrees Celsius because I wasn't interested in any other information.

Raspberry Pi Startup

Next, I wanted the python based web service to run on startup so that it'll always be available.  Following the example by instructables, I configured cron to start the service on boot.  To verify, I restarted the raspberry pi, went to the URL in my browser and made sure the temperature showed up.

Independent Polling Temperature Service

My last step was to setup a separate service on another computer that polls the temperature, verifies it, and notifies on error.  I'm using a different computer because I want to get a notification if the room gets cold (below zero), and the chips on the raspberry pi are only commercial grade (0 to 70C).  It would probably still work until -10C, but I still decided to keep it separate.
So I wrote a python service that reads the temperature, compares it to thresholds and sends an email if it's too cold.  I check the temperature hourly, but that's easily changed, I also have a very high temperature threshold because the temperature sensor reads hot (it's kept warmer than ambient by the raspberry pi) and I track the temperatures and email trends daily.  If reading the temperature ever fails I also send an email, which I found to have a lot of false positives so I added a couple of retries when reading the temperature.  I also write messages to syslog (I'm polling from a Linux box) so that I can easily see recent temperature readings.
I did fool around with setting up my polling application as a service that would automatically restart, but didn't fully get that working.

Anyway, here's my temperature poller.  It has been running for a couple of weeks without a glitch.

Source Email Address

One last note, I created a separate email address to send the notification emails from because I had to reduce the security on it.  I don't use the email address for anything except sending these emails so if it does get hacked it's not a big deal.

Let me know if you're interested in other source and I'll post that too.