Photo by @jamesponddotco / Unsplash.com

Building a Raspberry Pi-based temperature monitoring solution with Sense HAT LED matrix display and Azure Logic Apps (and some LEGO!)

I wrote about building a Raspberry Pi-based weather monitoring systems back in July, 2019. The solution utilizes Ruuvitag bluetooth beacons to measure temperatures and other data, and then submits those readings to Azure using Azure IoT Hub and Azure Functions. That setup is still working well today.

I also wrote about building a new housing cabinet for my network hardware using an IKEA cabinet. As part of this small hobby project I evolved my previous Raspberry Pi-based solution to provide me data back from Azure.

The end result, which I guess is the most interesting bit, is this LEGO case around the Raspberry Pi:

My kids used to love playing with LEGO. Then, Minecraft and Fortnite came along and nobody plays with LEGO anymore. Except me. I still have a few LEGO Technics projects to complete, such as the amazing Land Rover Defender:

The solution

The setup is still much the same as previously:

  1. Raspberry Pi pings the Ruuvitag beacon to measure temperature
  2. These are then submitted to Azure IoT Hub, which uses Azure Functions to parse the values and stores them in Azure SQL Database
  3. Build a Logic App to retrieve the latest value from the database
  4. The Raspberry Pi calls this Logic App, and prints the value using the Sense HAT LED matrix display locally
  5. Also build a bit of a logic to detect if temperatures are rising too high, and send an alert

You can view how steps 1) and 2) were built in my previous article on this.

Steps 3), 4) and 5) is more interesting. The Azure SQL database only has one table:

Notice that I’m capturing battery state also, so I can easily detect when I need to replace batteries on the beacons. A peek at the contents of the table shows relevant values:

Building the Logic App to retrieve latest temperature value

I wanted to use Logic Apps, as it gives a nice graphical approach and is somewhat easier to manipulate later compared to an Azure Function.

The resulting Logic App is simple:

The SQL query is also simple:

SELECT TOP 1 * FROM RuuviTagData ORDER BY Timestamp DESC

I’m then initializing a variable (of datatype Float) to store the temperature value:

And finally returning the value with a HTTP 200 OK status code.

Running this manually, or calling it via the custom URL, produces the necessary results:

Execution time for this simple procedure takes about 800 milliseconds up to 1.2 seconds, so very efficient and lightweight.

Building the calling logic for the Logic App from Raspberry Pi

The final bit is to configure Raspberry Pi to call our Logic App, and display the results using the Sense HAT matrix display. I whipped together a quick Python script, based on my previous implementation:

import datetime, requests
from sense_hat import SenseHat
from time import sleep

sense = SenseHat()
white = (255, 255, 255)

sense.set_rotation(180)

URL = "https://<URL-TO-LOGIC-APP>"

r = requests.get(url = URL)
data = r.json()
temp = str(data)
print(temp)
sense.show_message(temp + " C", text_colour=white)

I needed to rotate the Sense HAT display virtually to align the characters correctly during output. I also needed to convert the returned data, and perhaps there is a fancier way of doing it, as mine is admittedly a quick hack, and clumsy at the same time.

I set this script to execute every 30 minutes.

Building the temperature limit detection using a Logic App

This was an afterthought, but as it was so easy to implement, I created a separate Logic App for this. I’m pinging the latest temperature value from the database frequently, and in a For..Each loop I’ll check if it’s over a desired limit. For me, that limit is 30 Celsius.

And the email holds all data I need:

I could use a similar logic to detect low battery levels, also!

Building the LEGO case for Raspberry Pi

That’s the backside of the case. The cable is the USB power cable for the Raspberry Pi. I left a few strategic airholes for ventilation.

I wanted something that wouldn’t gather too much dust. It’s a very 0.1 prototype, and I definitely need to build a better base for it. I’m also a bit hesitant what to display on the LED matrix, as the scrolling text is visible only twice each hour. I might go for a graph, that shows historical temperature growth, as the Sense HAT is capable of accepting an array of pixels to display.

In summary

What a fun way to spend an evening! The LEGO case took the longest, about an hour to build. I used a few strategically placed blocks inside the case to lock the Pi in place. It doesn’t wiggle or jiggle at all.

For cost, the Logic Apps cost me about 16 cents/month: