Real time DHT22 sensor data visualization with RabbitMQ, Flask and D3.js on Raspberry Pi Zero

In this post I am going to detail how to create a real time data pipeline for processing sensor data. First we will connect the sensor and create the code to read it. I will use the DHT22 temperature and humidity sensor on the Raspberry Pi Zero WH. Then we’ll setup the real time data flow with Python and RabbitMQ. Finally we will use Flask and D3.js to display the data in a live dashboard in a browser.

To begin let’s connect the DHT22 to the Raspberry Pi Zero. Using for example female to female jumper cables, connect:

  • DHT22 PIN 1 to RPI 3.3V
  • DHT22 PIN 2 to RPI GPIO4
  • DHT22 PIN 4 to RPI GND

To see the pin layout on your Raspberry Pi you can use the “pinout” command which comes with gpiozero. The gpiozero library is installed by default with the more recent versions of Raspbian (however not on Raspbian Lite).

To read the sensor, I am using the Adafruit DHT Python library. Simply install this and pika, another Python library needed for this project:

 pip install Adafruit_DHT pika 

On top of this we want to create a real time data flow, so for this reason I will be using RabbitMQ to push the data to a Flask web app. To install RabbitMQ on the Raspberry Pi use:

 apt-get install rabbitmq-server 

Then start the RabbitMQ server:

 rabbitmq-server 

I created 3 main pieces of code:

  1. a Python script reading the sensor and sending data to the RabbitMQ server
  2. a Python script running the Flask’s app that retrieves data from rabbit and renders the webpage
  3. a webpage template with D3.JS chart

You can find all of them on my Github repository:

https://github.com/ionutpi/rabbitmq-flask-sse

The real time data push is achieved with Server-Sent Events, which enables a one way data stream from server to the webpage, without needing to refresh the webpage.

First start the script running reading the sensor data, then run the Flask app. If you ssh to the machine you will need to use to terminals. In order to see the webpage on any computer connected to the same LAN where your Raspberry Pi is, don’t forget to add set the ‘0.0.0.0’ argument¬† in the app.run() function. Then you can type in your Pi’s IP address and port on which Flask is running on another computer on the same network and you will see the chart.

I configured the scripts to read the humidity reading of DHT-22, and push it to the webpage as a value between 0 and 1. In general the humidity readings should be pretty static, but you can play a bit with the sensor (e.g. hold it in your palm), so the humidity values change more. The chart should look like this:

You can of course add data persistence, like i showed in my previous post, by storing the data in SQLite.

ZeroCam Raspberry Pi Zero NoIR Camera

Leave a Reply

Your email address will not be published / Required fields are marked *