Skip to content

Latest commit

 

History

History
203 lines (122 loc) · 10.8 KB

README.md

File metadata and controls

203 lines (122 loc) · 10.8 KB

IoT with Arduino and Grafana

This repository contains information on how to start with IoT using Arduino, Grafana, Prometheus and Grafana Loki. In this repository, we will use Grafana Cloud that includes hosted Grafana, Prometheus and Loki to remove the overhead of installing and maintaining these systems. All of these projects are open source and therefore if you want, you can run them by yourself.

Contents

  1. Setting up and using Arduino IDE
  2. Setting up and using Grafana Cloud
  3. Sending Metrics
  4. Sending Logs
  5. Using Loki and Prometheus libraries
  6. Examples of projects built with Arduino and Grafana Cloud

Setting up and using Arduino IDE

The open source Arduino IDE makes it easy to write code and upload it to the board. The Arduino software is easy to use for beginners, yet flexible enough for advanced users.

Installing

To start, we are going to install Arduino IDE, a platform that’s used to write and upload programs to Arduino compatible boards. Follow the installing instruction for your OS.

Set up Arduino IDE to support ESP32 board

This step is not necessary if Arduino hardware is used. If you decide to use ESP32 development boards (we are using ESP32 boards in most of the projects listed below), we will need to do couple additional steps:

  1. If your OS won’t recognize the USB serial automatically, you’ll probably need to install CP210x USB to UART Bridge VCP Driver that lets your computer communicate with your development board.

  2. Add ESP32 boards definition that adds support for the EP32 board. Go to Arduino > Preferences and add url https://dl.espressif.com/dl/package_esp32_index.json to the Additional Boards Manager URLs input. This open-source board definition adds support for programming ESP32 boards.

    arduino

    Then go to Tools > Boards > Boards Manager and add ESP32 board manager.

    arduino

    Now, in Tools > Boards, you are able to select your ESP32 board. This tells the Arduino IDE which profile and base libraries to use when compiling the firmware image, and how to flash it to the board.

    arduino

Using Arduino IDE and uploading program

In the Arduino environment, the programs that we are writing are called sketches. Before uploading the sketches to your development board, make sure that:

  • Sketch can be compiled. Click on the Verify button on top of IDE. Verifying goes through your sketch, checks for errors and compiles it.
  • In the Tools > Board submenu, you’ve selected the correct type of development port
  • In the Tools > Port submenu, you’ve selected the new correct COM port
  • In the Tools > Board submenu, you’ve selected the correct type of development port

Sketches are then uploaded to development board using Upload button on top of IDE.

Setting up and using Grafana Cloud

As mentioned, we are going to be using Grafana Cloud — which comes with hosted Grafana, Grafana Loki, and Graphite — for our data storage and data visualization. The free tier comes with 10,000 series for Prometheus metrics and 50GB for logs in Loki, which is definitely more than enough for simple monitoring solutions.

Creating account

To start, we'll visit Grafana Cloud signup and create a new account. As soon as the account is all set up, we can see the portal with hosted Grafana, Loki, and Prometheus instances.

grafana cloud

Sending Metrics

To setup your project for sennding metrics, click on the Send Metrics button

send metrics

From the next page, first you will want to copy the Remote Write Endpoint address

send metrics

And put the value in your config.h

#define GC_URL "prometheus-blocks-prod-us-central1.grafana.net"

Next copy the Username / Instance ID

send metrics

#define GC_USER "137822"

Then create an API key by clicking Generate now

send metrics

Give the key a meaingful name and choose MetricsPublisher

send metrics

Then click Create API Key

Copy the value into the GC_PASS field

#define GC_PASS "eyJrIjoiMTkzNDFkMzM2YTNhZTRlNmE4ZDkyMjgzSTBhNGFiYTcwY2VjMzVjNiIsIm4iOiJlc3AzMi10ZXN0LTMiLCJpZCI6NDIwMDY1fQ=="

The final result should look something like:

#define WIFI_SSID     "your_ssid"
#define WIFI_PASSWORD "your_wifi_password"

#define GC_URL "prometheus-blocks-prod-us-central1.grafana.net"
#define GC_PATH "/api/prom/push"
#define GC_PORT 443
#define GC_USER "137822"
#define GC_PASS "eyJrIjoiMTkzNDFkMzM2YTNhZTRlNmE4ZDkyMjgzSTBhNGFiYTcwY2VjMzVjNiIsIm4iOiJlc3AzMi10ZXN0LTMiLCJpZCI6NDIwMDY1fQ=="

Sending Logs

Similar to metrics, click the Sending Logs button

send logs

We will take some values from the Data Source Settings section starting with the URL

send logs

#define GC_URL "logs-prod-us-central1.grafana.net"

Next grab the User

send logs

#define GC_USER "8435"

And now generate an API key by clicking Generate now

send logs

Create a meaninful name and choose MetricsPublisher

send logs

The final config.h should look similar to this

#define WIFI_SSID     "your_ssid"
#define WIFI_PASSWORD "your_wifi_pass"

#define GC_URL "logs-prod-us-central1.grafana.net"
#define GC_PORT 443
#define GC_PATH "/loki/api/v1/push"
#define GC_USER "8435"
#define GC_PASS "eyJrIjoiMTkzNDFkMzM2YTNhZTRlNmE4ZDkyMjgzSTBhNGFiYTcwY2VjMzVjNiIsIm4iOiJlc3AzMi10ZXN0LTMiLCJpZCI6NDIwMDY1fQ=="

Creating more API Keys

At this point, or anytime in the future, we can create the API keys for Loki and Prometheus, to publish metrics from the monitoring system to these databases. The API key can be created by clicking on API Keys in the navigation on the left side. Then we click on + Add API Key and create API keys.

API keys

Visualised data in Grafana

Hosted Loki & Prometheus instances are automatically added as data sources in your hosted Grafana. You can find them under the name grafanacloud-NAME-logs and grafanacloud-NAME-prom. You can use these data sources with data from monitoring solutions to create dahsboards or use them in Explore.

Using Loki and Prometheus libraries

We have created following Arduino libraries to make it easier for you to send data to Prometheus and Loki:

All libraries can be download and used directly trough your Arduino IDE Library manager.

Start with opening Arduino IDE. Then go to Tools > Manage Libraries...

libraries

Search for "Prometheus" and install PrometheusArduino, PromLokiTransport, SnappyProto (You may be prompted to install additional libraries, saying yes will be the easiest path but you can also install the necessary libraries manually, see below)

libraries

Search for "Loki" and install GrafanaLoki

libraries

For list of additional libraries that you might need to install, refer to PrometheusArduino dependencies and GrafanaLoki dependencies

For how to use PrometheusArduino and GrafanaLoki, refer to their READMEs and examples folder

Examples of projects built with Arduino and Grafana Cloud

We have created couple of DIY IoT projects that you can easily create by yourself. These project have README that contains all information needed for you to re-create them.

  1. Room comfort monitoring
  2. Sourdough monitoring
  3. Avocado plant monitoring
  4. Candle monitoring
  5. Dog tracking
  6. Time tracking