Donsky's Tutorial Uses Python, WebSockets to Turn a Raspberry Pi Into an IoT Environmental Monitor

Using any Raspberry Pi and a DHT22 environmental sensor, this Python-powered project offers live updates to a swish graphical dashboard.

Pseudonymous maker "Donsky" has put together a guide for turning a Raspberry Pi into a simple environmental monitor — using Python, Flash, and WebSockets to offer up a constantly-updated dashboard accessible from any browser.

"Do you need to display real-time updates coming from your server into your HTML page," Donsky asks, rhetorically, by way of introduction to the tutorial. "Then using Websocket is a good option. WebSocket [is] a means of sending information in a bi-directional way with little overhead."

If you've ever wanted to try a simple WebSockets project, this may be the perfect place to start. (📹: Donsky)

The project builds on an earlier guide in which Donsky demonstrates how to use compact web framework Flask to provide a web server from a Raspberry Pi, initially using it to display the status of general-purpose input/output (GPIO) pins. To extend the project further, Donsky added WebSocket support for real-time updates — then Chart.js to automatically map readings into a graphical dashboard, rather than providing them as simple text.

"We only generated dummy data to serve as sensor readings," Donsky writes of his last project update. "[Now] we will take it further by creating an actual Internet of Things (IoT) project that involves a real sensor, which is the DHT22 temperature and humidity sensor. It will contain a dashboard page that will show the current DHT22 temperature and humidity readings. We will display it in both text and graphical chart format so as to show the real-time movement of the temperature readings."

The hardware requirements for Donsky's project are simple: any Raspberry Pi single-board computer (SBC) or compatible with an accessible general-purpose input/output (GPIO) header connected to the DHT22 temperature and humidity sensor. The Raspberry Pi acts as both sensor node and server, using Python to read the sensor module and Flask with the Flask-SocketIO library to host the dashboard and receive sensor reading updates over WebSockets.

The full tutorial is available on Donsky's website, with source code available on GitHub under the permissive Apache 2.0 license.

Gareth Halfacree
Freelance journalist, technical author, hacker, tinkerer, erstwhile sysadmin. For hire:
Latest articles
Sponsored articles
Related articles
Latest articles
Read more
Related articles