Graph temperature and humidity using Raspberry Pi and AWS Cloudwatch

Martin Koníček
Analytics Vidhya
Published in
4 min readMar 26, 2020

--

I have done many projects on Raspberry Pi and AWS but the majority of them have one common pitfall. They are difficult to manage long-term furthermore if your time for the project is limited.

Doing temperature and humidity monitoring with Raspberry Pi is very easy

When I was deciding about the next project my main criteria was that it must be very easy to develop and manage. So I decided to work with the DHT11 sensor for Raspberry Pi which can help you to measure humidity and temperature.

It is not difficult to connect the sensor to the RPI, you do not even need a PCB (Printed Circuit Board) neither any other parts. Just connect three cables to the RPI board and that’s it.

The main problem is how to store sensor data. Of course, you can use InfluxDB with Grafana or any other time-series database. I would not say that it is somehow difficult to set-it up but after a year or so you will see that it might be difficult to manage it. Trust me I have a lot of experience with system administration and I know that it is always a pity to do all upgrades and maintenance after a time when you practically forgot everything about a project.

You can easily connect DHT11 sensor to RPi, no circuit board is needed

So I started considering some managed solutions. Trust me I considered AWS a lot even more because I work with it daily. Unfortunately, I was unable to find some universal time-series database with a graphic facility in the whole AWS stack. The only possibility was using Cloudwatch for a graph with pushing metrics.

I did not find that offensive. Furthermore, I discovered more people on the Internet had the same idea. I found exactly this solution written in NodeJS by HumbleCode (site is currently offline during writing this article so I am not attaching a link). I am very glad that I was able to test my solution idea so quickly just because someone was solving the same problem before me.

The trouble with HumbleCode was that the quality of code was pretty low at least to my standards. It was expected that you run the daemon in the console as frontend process in utility called “screen” which allows you to run console in the background — that means you will have to manually start the daemon on every reboot of Raspberry Pi.

Monitoring temperature and humidity in AWS Cloudwatch

Furthermore, all variables (e.g. AWS credentials) were hard-coded in code and logging of the application was pretty weak. This was a big issue because when the sensor got accidentally disconnected I did not just know.

These were the main reasons why I decided to rewrite the sensor agent code from the beginning. Because I am not familiar with NodeJS I decided to use Python as the programming language.

I solved all the issues I had with the original solution. For example, my agent does not only put metrics into Cloudwatch but also logs the status of measurement into Cloudwatch logs. Why is that? If your RPI gets disconnected from the network or there is a problem with the sensor you can easily set-up an alert in Cloudwatch which will notify you by e-mail or SMS.

It is also a good idea to watch status of your Raspberry Pi and sensor

My code also works as SystemD daemon so it can start automatically on every boot of the device — no manual intervention necessary.

I am pretty happy with the current solution. It does not require any maintenance and just works. If you would like to set-up this project too you can check my code on GitHub — https://github.com/koss822/misc/tree/master/Aws/dht11

All necessary code is already written for you in Python

--

--