Get started with Grafana and InfluxDB in you Raspberry PLC

Learn how to setup a Grafana monitoring solution with InfluxDB in the Raspberry PLC
July 31, 2024 by
Get started with Grafana and InfluxDB in you Raspberry PLC
Boot & Work Corp. S.L., Ricard Franch Argullol

Grafana is a powerful tool for creating interactive and insightful dashboards, while InfluxDB excels at handling time-series data. Together, they provide a strong open source solution for real-time data analysis.

In this guide, we’ll show you how to install and configure Grafana and InfluxDB, both open source projects, on your Industrial Shields Raspberry PLC. By the end, you’ll have a functional data visualization setup that lets you and analyze your data effectively.


Requirements

Remember! The Raspberry PLC must be powered by a 12-24Vdc power supply.


Installing the necessary software


1. Raspberry Pi OS and Industrial Shields libraries

First of all, we need to set up the proper environment for Grafana and InfluxDB in our PLC. By default, we provide a 32 bit operating system image with the Raspberry PLC. In this case, however, due to InfluxDB v2 compatibility, we will be using a 64 bit image. You can build one as described on the following tutorial but selecting a 64 bits image:

Afterwards, install our libraries to interact with the inputs and outputs of the PLC for C++ and Python, the procedure to do so is described on our GitHub:

Remember! The Raspberry Pi OS image needs to be the 64 bit version.


2. Grafana

Next, install Grafana following the steps on their website:


3. InfluxDB v2

Finally, install InfluxDB, the time-series database we will be using to store our data in this example. We will install it as a service, which you can do following the steps provided on the official InfluxDB v2 documentation:

Also install the Python module to interact with InfluxDB, with the following command:

pip3 install Influxdb-client


Setting up InfluxDB


1. Configuring the database

Now that we have all the necessary software installed we can start setting up our database. The initial setup process can be run with the InfluxDB UI, the influx CLI, or the HTTP API. In this case, we will use the UI. 

To access the UI when InfluxDB is running, simply use your browser to visit http://10.10.10.20:8086 (where the IP address is the address of the Raspberry PLC). Click "Get Started" and follow the steps to enter:

  • Username and password for the admin of the database.
  • Organization name. A workspace for a group of users. 
  • Bucket. A named location where time series data is stored. Buckets pertain to a organization and have a retention period, a duration of time that each data point persists in the database. 

Finally, we will create a token to interact with the database. InfluxDB API tokens ensure secure interaction between InfluxDB and external tools such as clients. In order to interact with the database, we need to create one for our user with the appropriate permissions. In this case, we will create one with reading and writing permissions for all buckets. The procedure to create one using the InfluxDB UI is the following:

  1. In the navigation menu on the left, select Load Data > API Tokens.
  2. Press the GENERATE API TOKEN button.
  3. Select Custom API Token.
  4. Set a description and check the Read and Write boxes for All buckets.
  5. Finally, save your token. This is the only time you will be able to see it, so make sure you store it in a secure place!

Generating a token in InfluxDB

With these simple steps we already have an admin user and a bucket to store some data, as well as a token to read/write from the buckets.

If you want to know more about the set up procedure or alternative ways of doing it check Influx documentation:


2. Storing data into InfluxDB with Python

In order to get some data to show afterwards in Grafana, we will make a simple Python script that reads an analog input and stores the value into the database. We will use the RPIPLC and the InfluxDB Python modules installed previously.

Create a file named as you prefer, e.g. "storeread.py", and copy the following code. This program reads the I0.12 input and stores the value into the database every seconds.  See how we are using our python-librpiplc library to perform the readings, and remember to adapt the rpiplc.init() function according to your PLC version (check the GitHub for more information).

Also, you need to adapt the variables according to the settings configured in the previous steps:

  • URL. The address of the database. In this example, the program is run on the same machine as the database, so we can simply use "http://localhost:8086".
  • Organization. The organization we created in the previous steps.
  • Bucket. The bucket we created in the previous steps, where the data will be stored.
  • Token. The token created in the previous steps, which will give our program permissions to write to the bucket.

import influxdb_client, os, time
from influxdb_client import InfluxDBClient, Point, WritePrecision
from influxdb_client.client.write_api import SYNCHRONOUS
from librpiplc import rpiplc

url = "http://localhost:8086"
org = "YOUR ORG"
bucket= "YOUR BUCKET"
token = "YOUR TOKEN"

write_client = influxdb_client.InfluxDBClient(url=url, token=token, org=org)
write_api = write_client.write_api(write_options=SYNCHRONOUS)


if __name__ == "__main__":
    rpiplc.init("RPIPLC_V4", "RPIPLC_21")

    while True:
        reading = rpiplc.analog_read("I0.12")
        print("%d" % reading)
        point = (
            Point("Reading")
            .tag("Input", "I0.12")
            .field("Value", reading)
        )
        write_api.write(bucket=bucket, org="Industrial Shields", record=point)
        time.sleep(1)


Finally, we can run this simple program with the command python storeread.py to store data into InfluxDB. If we go to the InfluxDB UI, we can check that our data is being saved correctly by making a query. If you want to know more about using queries with the InfluxDB UI, check their official documentation:


InfluxDB data explorer


Grafana

Now that we have our data available we can build our Dashboard in Grafana. First of all, open Grafana in your browser: http://10.10.10.20:3000/ (if you are not using the default IP of Ethernet 0, remember to change it accordingly to the one you are using). The first time you enter you will need to input a username and password, the default are both "admin" (for security reasons, you should change the credentials, which you can do throug the webpage).


1. Configuring the connection with InfluxDB

Once inside Grafana, the first thing we need to do is establish a connection with our InfluxDB to get the data. This can be done by following these steps:

  1. Go to Connections > Add new connection.
  2. Select InfluxDB.
  3. Select Add new data source.
  4. Set the configuration accordingly:
    1. Query language. In this case we will be using Flux, but you can choose the one you prefer.
    2. URL: http://localhost:8086
    3. Select "Basic auth" and set the user and password.
    4. Organization. The organization created earlier.
    5. Token. The token we created to give access to the database.
    6. Default bucket. Set it to the bucket previously used to store the data.
  5. Press Save & Test.


Influx connection with Grafana configuration



2. Showing the data

Now that the connection between Grafana and InfluxDB is set up we can create our dashboard and start querying data. To create one:

  1. Click Dashboards in the left-side menu.
  2. On the Dashboards page, click New and select New Dashboard from the drop-down menu.
  3. On the dashboard, click + Add visualization.
  4. Select the InfluxDB source we created in the previous steps:

    Data source selection in Grafana
  5. A new window will appear where we can configure all the aspects of the visualization, as well as the query to obtain the data. In this case we are using a time series visualization, one of the multiple options Grafana offers. On the right side menu we can configure all its aspects: graph styles, legends, etc. In the query field we need to input a Flux (or another language) query to get our data. In this case we are only reading the I0.12 readings that we introduced in the database, with the according organization and tags:
    from(bucket: "Example")
      |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
      |> filter(fn: (r) => r["_measurement"] == "Reading")
      |> filter(fn: (r) => r["Input"] == "I0.12")
      |> filter(fn: (r) => r["_field"] == "Value")
      |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
      |> yield(name: "mean")

    Grafana visualization configuration page

  6.  Click Apply, then on the dashboard page press the Save button on the top bar.

With this we already have a simple dashboard with one visualization. Now we can add more of different types to show more data. For example, here is one with readings of more inputs and different types of visualizations, such as tables and gauges:

Grafana dashboard


Now you know the basic steps to have a functional Grafana + InfluxDB application on your Raspberry PLC. You can use this example as a stepping stone to build your application. If you want to know more about Grafana and InfluxDB check out their respective documentations:

If you want to know more

check out the following link and do not forget to read the other posts in this blog!

​Search in our Blog

Get started with Grafana and InfluxDB in you Raspberry PLC
Boot & Work Corp. S.L., Ricard Franch Argullol July 31, 2024
Share this post
Tags

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC comparison >>>