Display Arduino sensor data using Grafana

Collecting and display sensor data can be fun and helpful. Wouldn’t it be interesting to know the average temperature of last month based on sensor data collected using Arduino, MQTT and a time series database? Ever wanted to know the postman’s show-up times for making future predictions? At Maker Faire Hannover 2017 I presented a demo how to collect sensor data using Arduino and how to display it.

My Arduino solution

This article is derived from 2 years old notes and some parts were neccessary to change for Raspberry Pi 3.

Now focusing on the Raspberry Pi 3 to act as server, the guide has been adjusted. If there is any improvement, please leave a comment. This setup is actually configured for local use, therefore encryption is not enabled. For internet use, you will want to encrypt the communication between Arduino and mosquitto. So Grafana web interface should be TLS-enabled using letsencrypt.

There will be posts about extending this demo later.

Let’s begin.

Open-Source software used for this

The hardware

  • Rasbperry Pi 3 (1, 2 working aswell)
  • LCD touch shield (or HDMI display)
  • Wemos D1 mini (ESP8266)
  • BMP180 temperature and pressure sensor
  • BH1750 light sensor
  • HTU21 temperature and humidity sensor
  • 18650 batteries for power supply
  • Tiny pcb for quick assembly

Data flow

Sensor data is read from I2C capable sensors and delivered to a MQTT server running on raspberry pi.
It is like a chat server where every sensor has its own channel (topic). Communication to MQTT can be encrypted using TLS.

As a substitute for raspberry pi, a cheap linux box can be used aswell. Cheap Linux VPS with SSD are easy to rent for about 5 $ per month. A Debian installation on any old laptop does the trick aswell. Pro: a builtin display to view the graphs.

Telegraf running on the raspberry pi is looking for all MQTT channels and fetches messages. Messages matching a defined input format will be imported to influxdb.

Influxdb stores the measurements and preaggregate values. As a time-series database running on Go, influxdb will respond to queries sent by Grafana.

With Grafana, dashboards can be built displaying graphs with data queried from influxdb.

The setup

That’s how the data flows in general. Let’s continue with the setup now.

Setup Raspbian

A todo-list lacking instruction details. Basic setup can be looked up somewhere else.

  1. Raspberry Pi has Internet connection via Router running DHCP
  2. Install Raspbian stretch (RASPBIAN STRETCH WITH DESKTOP)
    Download image, write to SD card using Win32 Disk Imager (Windows) or dd (Mac & Linux)
    Run and use raspi-config to:

    1. Enable SSH
    2. Set timezone, language & keyboard layout
    3. Resize available disk storage
  3. Update system
  4. Install LCD driver
    Instructions can be found here: http://www.waveshare.com/wiki/3.5inch_RPi_LCD_(A)
    This has to be done after any kernel upgrade.
  5. Avoid screen going blank / dim

Install mosquitto

Install mosquitto MQTT from repository.

After installing, mosquitto will be enabled to use authentification by writing this configuration to /etc/mosquitto/conf.d/auth.conf.

Create user arduino and set password.

Reload service to apply changes.

For testing MQTT service, open up two shells. Run the first command to “subscribe” to topic test and the second one for messages to be published on the test topic.

Your message should appear in shell 1. If true: Success.

Install influxdb

To install and update influxdb time-series database automatically using the package manager, add the repository and fetch the key.

Afterwards install influxdb.

Manual way: The installation package can be downloaded here.

For telegraf, credentials can be configured. Grafana will access influxdb with its own credentials. So create a pair aswell.

Install telegraf

Now install the adapter that picks the values from MQTT channels and sends it to influx.

Since the influxdata repository was added during the previous step, install telegraf just by typing:

Again, package for manual installation can be found here.

To configure telegraf acting as MQTT consumer, open /etc/telegraf/telegraf.conf, scroll down to the section [[inputs.mqtt_consumer]] and uncomment it.

The import part looks like:

As for username and password, new credentials should be generated for use with telegraf (telegraf:importdatatoinfluxdb).

Sensor data will be written to influxdb. For authentification insert the credentials created in the previous step.

Database name is changed aswell.

Install grafana

Since grafana is not prebuilt for raspberry pi’s arm architecture, we would have to do it manually.
Luckily fg2it was kind enough to provide a package repository for raspberry pi. More to know is found in the wiki or on grafana itself.

It even works on a raspberry pi 1 which takes hours to install – I tested that just for the lulz.

After installing, the webinterface is accessible on port tcp/3000. Use admin:admin to login.

After login, click on the Grafana logo, go to Data Sources and click Add data source.

With the click on Add the connection is tested and established.

Next step will be to enable Arduino sensor to send some data to the system.

 

Configure the raspberry as WiFi hotspot

The raspberry pi 3 (with onboard wifi) is intended to act as access point for all the arduino sensor boards. These are delivering sensor data to mosquitto without any other access point in between.

Install dnsmasq

Add configuration to file end. This will configure DHCP – dynamic ip configuration – for the Arduino clients accessing the MQTT server.

Test configuration and restart service.

Add option in /etc/dhcpcd.conf

Install hostapd WiFi access point.

Configure the WiFi accesspoint. DHCP

Make file unreadable by any other system user.

Change the option to the proper configuration file to be used at startup by default.

Enable hostapd at system start.

Test the WiFi Hotspot. Output should contain “active”.

Info: Connected devices don’t have Internet access! If desired, routing directives will have to be added to iptables.

Arduino

The sensors do need some micro-assembly. For this purpose Wemos D1 mini is used and for quick demo a I2C sensor (BMP180) is added.

Arduino wiring

D1 – CLK BMP180
D2 – DATA BMP180
3V – V+ BMP180
GND – GND BMP180

RST + D0 = Deep Sleep enable

Arduino Sketch

My sensor sketch: https://gist.github.com/screenzone/7ddbb18b94b2acc2e8ad526dc503ad16

This sketch will work on Wemos D1 mini and can be easily extended and improved.
Necessary Arduino libraries:

Also have a look at Mongoose OS as alternative to this Arduino based solution.

Make data appear on display

Use the Arduino sketch to generate some sensor data. Afterwars a grafana dashboard can be built using influxdb as data source.

To launch the chrome as kiosk mode remotely via SSH use (change the url):

If the touch capability of the TFT needs to be disabled:

That’s it! Don’t hesitate to ask in the comments. A few detailed steps were skipped.
Writing this article took far more time than expected.

Mini how-to’s to operate this setup on a cheap Linux VPS is going to follow soon. Maybe there will be some youtube content 😉

Stay tuned.

Damian

Leave a Reply

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