Wednesday, February 22, 2023

IoT: Monitor temperature and humidity on a Grafana dashboard

 I wanted to set up an IoT project for a long time for learning purposes. When I started to learn for a AWS certificate I thought it was big time for some hands on practice.

The goal

Monitor temperature and humidity in the attic in order to get critical min and max temperatures during the course of a year.





The architecture

I had a rough idea about the components I wanted to use and I quickly realized that using AWS IoT core and Lambda was a common way to do the job. I was also curious about using a time series db, so Influxdb came into play. I didn't want to spend time on the UI, so Grafana was a natural choice.

Data handling at the edge

I use a Raspberry Pi 3.0 with a Debian OS at the edge. It's connected to a humidity and temperature sensor. I collect data with the dht.DHT22 driver from the adafruit lib.
Three Python scripts manage the data handling at the edge:
  1. Collect sensor data with the above mentioned lib. Since temperature and humidity don't change too fast, I collect data every 1 second though the sensor provides a higher sample rate. The data is stored in a log rotation (5 files switched on an hourly basis).
  2. Every hour a cron job triggers a script that fetches the data from the past hour from the log and aggregates temperature and humidity to mean, max and min values, which are totally sufficient for my purpose.
  3. Every hour a cron job triggers a script that publishes the aggregated data to AWS IoT core

Data handling in the cloud

I set up two triggers for the data I subscribe to with IoT Core:
  1. Persist the messages to S3. This helped me to replay ingestion when I messed up with the lambda code, the configuration or the Influxdb. On the long run I set up S3 so that they are deleted after a couple of days.
  2. Call a Lambda function that extracts the data from the message and stores them in the influxdb. If storing the data fails, I setup SNS to send an email.

Data visualization

I installed Influxdb and Grafana on an EC2 instance, which provides me an easy approach to visualize the data I collect from the sensor under my roof. 



Learnings

I spent a lot of time writing the javascript for the Lambda function. I was finally successful using Visual Studio Code with the AWS toolkit. So I created, built and tested the code locally, then deployed the result to the cloud.
Things I touched:
  • IAM
  • S3
  • Route 53
  • Security Groups
  • IoT Core
  • SNS
  • Lambda
  • Cloud Watch
  • EC2

Bottom line

With regards to my AWS learning this project created a huge progress. Components, services and configurations are much clearer to me now. I would say the result has the level of a proof of concept. I spent 2 to 4 hours per day over three weeks. 



No comments:

Post a Comment