Visualize Data With Grafana
Once you have configured data query for your organization’s data store, you can visualize your data from a variety of third-party tools, including Grafana.
You can choose to visualize data from a component on one machine, from multiple components together, or from many components across a fleet of machines, all from a single pane of glass.
Only components that capture sensor readings or other time-series data support data visualization.
You can visualize both the captured data itself as well as its metadata, including machine ID, organization ID, and tags.
Follow the steps in this tutorial to learn how to collect data from your machine, sync that data to the Viam app, enable third-party access to that data, and present that data visually and flexibly in Grafana.
Info
This tutorial focuses on using Grafana to visualize your captured data. For general guidance appropriate for any third-party visualization tool, see Visualize data.
Prerequisites
Before following this tutorial, ensure you have:
Added a new machine in the Viam app and follow the setup instructions to install
viam-server
on the computer you’re using for your project and connect to the Viam app.A configured sensor component, such as the
bme280
sensor, that reports data. If it’s a physical sensor, make sure to connect it to your machine’s computer.This tutorial uses a dataset of plant moisture measurements, originally captured for our Plant watering tutorial using an analog resistive soil moisture sensor connected to an analog-to-digital-converter (ADC). The ADC functionality was provided by the
mcp300x-adc-sensor
module from the Viam Registry.If you already have data synced to the Viam app that you want to use, you can skip this requirement, and can skip directly to the Configure data query portion of this tutorial.
A Grafana instance.
- You can use either a local instance of Grafana Enterprise, or Grafana Cloud. This tutorial will use Grafana Cloud.
- You can use the free trial version of Grafana Cloud.
The data management service
You can manage how your machine captures and syncs data to the cloud using the data management service.
Add the data management service
First, add the data management service to your machine to be able capture and sync data:
Navigate to your machine’s CONFIGURE tab in the Viam app.
Click the + button in the left-hand menu and select Service from the dropdown.
Select data management.
Give the service a name, like
viam-data-manager
, then click Create.On the panel that appears, you can manage the capturing and syncing functions individually. By default, the data management service captures data locally to the
~/.viam/capture directory, and syncs captured data files to the Viam app every 6 seconds (0.1
minutes in the configuration). Leave the default settings as they are, and click Save in the upper-right corner of the screen to save your changes.
For more information, see data management service configuration.
Configure data capture for a component
Once you have added the data management service, you can configure data capture for specific components on your machine. For this tutorial, you will configure data capture for a sensor component, gathering sensor readings to later visualize in Grafana. Only sensor readings or other time-series data can be visualized in this manner.
To enable data capture for a sensor component:
Navigate to your machine’s CONFIGURE tab in the Viam app.
In the configuration pane for your configured sensor component, find the Data capture section, and click the Add method button to enable data capture for this camera.
Set the Method to
Readings
and the Frequency to0.333
. This will capture readings from the sensor device roughly once every 3 seconds. You can adjust the capture frequency if you want the sensor to capture more or less data, but avoid configuring data capture to higher rates than your hardware can handle, as this could lead to performance degradation.
Click Save Config at the bottom of the window to save your changes.
After a short while, your sensor will begin capturing live readings, and syncing those readings to the Viam app. You can check that data is being captured and synced by clicking on the menu icon on the sensor configuration pane. and selecting View captured data.
For more information see data management service configuration.
Configure data query
Next, enable the ability to query your synced data.
When you sync captured data to Viam, that data is stored in the Viam organization’s MongoDB Atlas Data Federation instance.
Configuring data query allows you to directly query your data using the Viam app or a compatible client (such as mongosh
), but also allows Grafana to access that data and visualize it.
To enable data query:
Follow the steps to configure data query.
Note the username and hostname returned from these steps, in addition to the password you chose for that user. You will use this information in the next section.
Configure Grafana
With your machine capturing data and syncing it to Viam, and direct query of that data configured, you can now configure Grafana to access and visualize that data:
Navigate to your Grafana web UI, and add the Grafana MongoDB data source plugin to your Grafana instance.
For more information, see Install Grafana Plugins.
Navigate to the Grafana data source management page, and select the Grafana MongoDB data source that you just added. For more information, see Grafana data source management.
Enter the following information in the configuration UI for that plugin:
Connection string: Enter the following connection string, substituting the hostname returned from the previous section for
<MONGODB-ATLAS-DF-HOSTNAME>
and the desired database name to query for<DATABASE-NAME>
:mongodb://<MONGODB-ATLAS-DF-HOSTNAME>/<DATABASE-NAME>?directConnection=true&authSource=admin&tls=true
For example, to use the
sensorData
database, the default name for uploaded sensor data, your connection string would resemble:mongodb://data-federation-abcdef12-abcd-abcd-abcd-abcdef123456-e4irv.a.query.mongodb.net/sensorData?directConnection=true&authSource=admin&tls=true
The connection string is specific to your organization ID and configured user. You must have followed the steps under configure data query previously in order for this URL to be valid.
Credentials: User: Enter the following username, substituting your organization ID as determined earlier, for
<YOUR-ORG-ID>
:db-user-<YOUR-ORG-ID>
For example, using the organization ID from the previous example, your connection string would resemble:
db-user-abcdef12-abcd-abcd-abcd-abcdef123456
Credentials: Password: Enter the password you provided when you configured data query previously.
Click the Save & test button to save your settings. Grafana will perform a health check on your configuration settings to verify that everything looks good.
This connection allows Grafana to access all synced sensor data under your organization, from any machine.
Visualize your data
With the data connection configured, Grafana now has access to your data and you are ready to create a new dashboard to visualize that data.
- If you are using a local Grafana instance, see Build your first dashboard
- If you are using Grafana Cloud, see Create a dashboard in Grafana Cloud
When prompted to select a data source, select the MongoDB data source that you added earlier.
Note
Be sure to install the MongoDB data source, not the integration.
Select the visualization type that matches your data, which is most likely Time series.
When done, click Save to save your dashboard.
The example below displays readings from a fleet of deployed machines, each equipped with a temperature sensor, reporting the ambient temperature of the office over the course of the day.
Tip
If you have a lot of data, or are exploring an existing data set that you are not familiar with, you may find it useful to browse your data first, before creating a visualization dashboard based on it.
You can use MongoDB Compass to connect to your Viam data store using the same connection string and credentials as above, and browse your data using Compass’ Schema Analyzer, which offers fast insights into what types of data you have available.
Query your data in Grafana
You can also use query language directly in Grafana using the MongoDB Query Editor, which enables data query functionality similar to that of the MongoDB shell, mongosh
.
For example, to limit the visualization of the plant watering data set to just the moisture-sensor
component, within a certain time range, and limiting returned results to 1000 records, you could use the following query in the query editor:
sensorData.readings.aggregate([
{$match: {
component_name: "moisture-sensor",
time_received: {$gte: ISODate(${__from})}
}},
{$limit: 1000}
]
)
This query uses the Grafana global variable $__from
, which is populated by the value set from the From
dropdown menu on your dashboard, allowing for visualizations based on this query to be dynamically updated when you change your desired time range from that dropdown menu.
See Grafana’s Global variables documentation for more information.
Next steps
In this tutorial, you learned:
- how to use the data management service to capture data from your machine and sync it to the Viam app
- how to enable data query access to your synced data
- how to connect Grafana to your data
- how to build a dashboard visualizing that data
- how to use query language to dynamically update the visualization based on UI selections
From here you could:
- include data from additional sensors, or from more machines in your fleet, to be able to view everything together from a single pane of glass
- experiment with your query language syntax to give more flexibility to the operator, by allowing for more UI-based customization of the visualization using Grafana global variables
For more ideas, check out our other tutorials.
You can also ask questions in the Community Discord and we will be happy to help.
Was this page helpful?
Glad to hear it! If you have any other feedback please let us know:
We're sorry about that. To help us improve, please tell us what we can do better:
Thank you!