Monitoring the temperature of your room with prometheus (and timescaledb)

Nabil Servais
Apr 29 · 3 min read

Nothing original here, just an another method to use your raspberry pi 3 and your temperature sensor. You can find a plenty of methods to do it on the web. ith Cacti, influxdb, mqtt, python script and other stuff. This time, I explain my methodology for choosing and implementing the solution.

Before choosing a technical solution, I have some constraints. It must cheap (in money and time), easy to maintain (for me), compatible with everything and opensource (I don’t want a sensor stop working because I don’t pay the abonnement or the company stop the support).

It excludes de facto “IoT” stuff found on aliexpress and amazon and big domotic solutions (remember I just want the temperature of a room).

I have a lot of solutions to exclude A LOT… I will take more time to exclude solutions than to implement and get fun.

So in software industry, what we choose today when we need storing time based data and alerting ? We used Prometheus or the TICK stack, right?

I’m more a Prometheus guy but I love TICK they are doing a great job, next time I will give a try.

Prometheus is time series database, a metrics scaper and a monitoring system. It scrape a http url with metrics. It is not 100% accurate but I didn’t need it for now.

Let’s go for Prometheus, let’s buy a cheap sensor. The raspberry pi 3 is a gift from my employer (think.fr) and see what will happen.

And I want to test timescaledb from long time storage and special analytics (I will develop in a later post).

Of course, Grafana is the best viewer for time series data.

For hosting, I choose Digital Ocean because VM is fast and cheap (if you want to you click on this link to give more credits).

The program will push data on a pushgateway because prometheus can’t scrape an exporter in my network. It is easy compare to MQTT but not scalable, not a problem there is only one sensor for now.

The pushgateway is only a pass-through to prometheus, it permits to expose metrics to prometheus when it not possible (batch, multi-processus program…) .

The final architecture.

The harder part of this solution was to find a sensor easily manageable with a raspberry Pi and golang (I love golang since the first public version).

My first idea was to use a TMP125 but people on forum don’t recommend it and prefer the DS18B20.

I’m not a specialist of electronics, so I trust people who tried it before me, maybe it is stupid but I just want the temperature of a room.

The hardest part was to branch to the raspberry pi. Don’t laugh I destroyed one of sensor because I branch the wrong wire at the wrong GPIO.

Me doing electronics

After getting the sensor working (follow this link ), now just write the golang program to get the temperature from the sensor.

Finally, it’s working!

Unfortunately, the sensor is not well supported by the great golang library periph (https://periph.io/) but I found a library doing the job and working well https://github.com/yryz/ds18b20.

The code.

Nothing complicated in this golang code, then use the classic go get and go build (you have to build the golang app on raspiberry Pi).

note :I will do a correct repo soon.

Next article will cover the deployment of the prometheus stack with grafana and timescaleDB.

Nabil Servais

Written by

Dev & Ops @ Think.fr (Lille) and Roller Derby Player