DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. IoT
  4. Storing and Visualizing Time-Series Data From a Raspberry Pi

Storing and Visualizing Time-Series Data From a Raspberry Pi

Learn more about build, storing, and virtualizing time-series data from a Raspberry Pi.

Matthew Perry user avatar by
Matthew Perry
·
Jul. 01, 19 · Tutorial
Like (2)
Save
Tweet
Share
11.54K Views

Join the DZone community and get the full member experience.

Join For Free

When talking about IoT and edge with developers, the mention of Raspberry Pi quickly becomes part of that conversation. For those that aren’t familiar, Raspberry Pi is a minicomputer that packs a powerful punch in a small package. It can be used “for whatever you want” and there are tons of project examples showcasing its versatility. With the recent release of the Raspberry Pi 4 Model B, you can get faster CPU, 4K support, and up to 4GB of RAM.

In the context of IoT, developers can use a Raspberry Pi to serve as a prototype for larger-scale edge initiatives. Here, we are interested in exploring how to run edge hardware, push time-series data from that hardware into a TimescaleDB, and configure a visualization of that data in Grafana. To demonstrate this scenario, in this article, we will show you how to create an IoT system based on Raspberry Pi (the “edge”) and Timescale Cloud (for hosted TimescaleDB and hosted Grafana).

While this demo is small (aka uses a single Raspberry Pi device), you can actually scale this demonstration with thousands of devices using the same setup.

The Moving Parts

First, we will discuss the setup for our system. “The Edge” is a Raspberry Pi that has been wired to a photocell light sensor. Running on the device is a script (photoresister.py), which is recording the status of the light sensor at 5Hz. Each observation is sent to a TimescaleDB instance (hosted on Timescale Cloud) and recorded in a hypertable. A Grafana instance (also hosted on Timescale Cloud) is connected to the database to visualize the light values.

All the code and detailed instructions for the below can be found in this GitHub repository.

The Cloud

We need to create two things in Timescale Cloud: a TimescaleDB instance (to store the light sensor readings sent from the device) and a Grafana instance (to visualize the data in TimescaleDB).

If you don’t already have an account, sign up for Timescale Cloud (first-time users get $300 in trial credit). Login and create a new TimescaleDB instance.

After a few minutes, the database instance is powered-up and ready to use. Using psql (or similar SQL client), connect to the database and create the schema.

With the TimescaleDB instance running and the schema in place, it’s time to create a new Grafana instance.

Next, log into your Grafana instance and configure your TimescaleDB instance as a data source. With the data source in place, you can create a Grafana dashboard that connects to that data source and will visualize the data in TimescaleDB. You can create the dashboard by importing the Grafana dashboard JSON found here.

Note: The above Grafana dashboard JSON assumes you have created datasource “Timescale Cloud”, so please adjust as necessary.

The Edge

We are starting with a standard Raspberry Pi setup with Raspbian. For our light sensor, these resources will explain how to configure a photocell:

  • Photocell overview
  • Basic photocell reading

With the photocell in place, we will set up the photoresistor.py python script. This script runs on the device, which will write aggregate sensor readings and batch insert the data into the TimescaleDB instance in the cloud.

Note: As a prerequisite (in addition the photocell setup), you’ll need to install the PostgreSQL database adapter for Python.

Create a systemd startup script which will help to ensure the sensor comes back online during reboot. Be sure to enable the TIMESCALEDB_CONNECTION within the environment variable.

The Outcome

With all the pieces in place (and letting the system run for a bit), here’s a snapshot of how the Grafana dashboard fluctuates over the course of ~25 minutes. You can see the light sensor readings show when we are in the light and in darkness.

Added bonus: Since Grafana supports creating alert rules, if you are feeling adventurous, you can set up a rule to send an alert when we are in the dark.

Above is a small scale demonstration of what it looks like to get an IoT sensor system running with a Raspberry Pi and Timescale Cloud. We hope this gives you some ideas about what you can create on your own. If you are ready to get started, you can use this demonstration to get up and running today!

raspberry pi Data (computing) Time series

Published at DZone with permission of Matthew Perry. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The 12 Biggest Android App Development Trends in 2023
  • Remote Debugging Dangers and Pitfalls
  • Tech Layoffs [Comic]
  • Taming Cloud Costs With Infracost

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: