IOT: Pushing data to OVHcloud metrics timeseries from Arduino

From Arduino to OVHcloud Metrics

Last spring, I built a wood oven in my garden. I’ve wanted to have one for years, and I finally decided to make it. To use it, I make a big fire inside for two hours, remove all the embers, and then it’s ready for cooking. The oven accumulates the heat during the fire and then releases it.

Once the embers are removed, I have to prioritise the dishes I want to cook as the temperature drops:

  • Pizza: 280°C
  • Bread: 250°C
  • Rice pudding: 180°C
  • Meringues: 100°C

I built a first version of a thermometer with an Arduino, to be able to check the temperature. This thermometer, made of a thermocouple (i.e. a sensor that measures high temperatures), displays the inside temperature on a little LCD screen.

The next step was to anticipate when to stuff dishes into the oven. Watching the temperature dropping down for hours was not a good idea. I needed the heat diagram of my oven! A heat diagram is just the chart of the temperature over a given period of time. But writing down temperature on a paper every ten minutes… wait… it will last more than 30 hours.

Please, let me sleep !

This needs some automation. Fortunately, OVHcloud has the solution: Metrics Data Platform: https://www.ovh.com/fr/data-platforms/metrics/

The Hardware

The aim of the project is to plug a sensor onto an Arduino that will send data to OVHcloud Metrics Data Platform (https://www.ovh.com/fr/data-platforms/metrics/) via the network. Basically, the Arduino will use the local wifi network to push temperature data to OVHcloud servers.

Do you know ESP8266? It’s a low-cost (less than 2€!) wifi microchip with full TCP/IP stack and microcontroller capability.

ESP8266 functional diagram

Implementation: Wemos

ESP8266 is not quite so easy to use on its own:

  • Must be powered at 3.3V (not too much, or it will burn)
  • No USB

That’s why it is better to use a solution that implements ESP8266 for us. Here is the Wemos!

  • Powered at 5V (6V is still ok)
  • USB for serial communication (for debugging)
  • Can be programmed via USB
  • Can be programmed with Arduino IDE
  • Costs less than 3€

Prepare your Arduino IDE

Install the integrated development environment

First of all you need to install Arduino IDE. It’s free, and available for any platform (Mac, Windows, Linux). Go to https://www.arduino.cc/en/main/software and download the version corresponding to your platform. At the time of writing, the current version is 1.8.10.

Additional configuration for ESP8266

When you install the Arduino IDE, it will only be capable of programming official Arduinos. Let’s add the firmware and libraries for ESP8266… 

Start Arduino and open the “Preferences” window (File > Preferences).

Enter https://arduino.esp8266.com/stable/package_esp8266com_index.json into the “Additional Board Manager URLs” field. You can add multiple URLs, separating them with commas.

Now open “Boards Manager” from the Tools > Board menu and install the esp8266 platform (don’t forget to select your ESP8266 board from the Tools > Board menu after installation).

You are now ready!

Order a Metrics Data Platform

Go to the OVHcloud Metrics Data Platform website: https://www.ovh.com/fr/data-platforms/metrics/. Click on the free trial, and finalise your order. If you don’t have an account, just create one. With this trial you will have 12 metrics (i.e. 12 sets of records). In this example, we will only use one.

Retrieve your token

Go to the OVH Control Panel: https://www.ovh.com/manager/cloud/#/. On the left-hand panel, you should have Metrics and a new service inside.

In the “Tokens” tab, you can copy the write token. Keep it, as we will need it later.

Note that to configure Grafana, you will need the read token.

Retrieve the host of the Metrics Data Platform

The host of your Metrics Data Platform is given in your service description. In the “Platforms” tab, copy the opentsdb host. Keep it, as we will need it later.

Deeper into the program

Now let’s have a look at an example. Here is a code that will push static data to OVHcloud Metrics Data Platform. You can use it with your sensor. You just have to code the sensor measure. When running, the Wemos will:

  • Try to connect to you wifi network
  • If successful, push data to OVHcloud Metrics Data Platform

The whole source code is available on my github:  https://github.com/landru29/ovh_metrics_wemos.

There are six main files:

  • ovh_metrics_wemos.ino: the main file
  • wifi.cpp: class that implements the process to connect to wifi via WPS (Wifi Protected Setup)
  • wifi.h: header file for the wifi
  • metrics.cpp: class that sends the metric data to OVHcloud Metrics Data Platform via HTTPS
  • metrics.h: header file for metrics
  • config.h.sample: model to create your configuration file (see below)

Create your configuration file

If you try to compile the program, you will get errors, as some definitions are missing. We need to declare them in a file: config.h.

  1. Copy config.h.sample into config.h
  2. Copy the write token you got in paragraph 5.1 (#define TOKEN “xxxxxx”)
  3. Copy the host you got in paragraph 5.2 (#define HOST “xxxxxx”)

Get the fingerprint of the certificate

As the Wemos will request through HTTPS, we need the certificate fingerprint. You will need the host you just grabbed from the “Platforms” tab and then:

Linux users

Just run this little script:

HOST=opentsdb.gra1.metrics.ovh.net; echo | openssl s_client -showcerts -servername ${HOST} -connect ${HOST}:443 2>/dev/null | openssl x509 -noout -fingerprint -sha1 -inform pem | sed -e "s/.*=//g" | sed -e "s/\:/ /g"

Copy the result in your config.h (#define FINGERPRINT "xx xx ..").

MAC users

Just run this little script:

HOST=opentsdb.gra1.metrics.ovh.net; echo | openssl s_client -showcerts -servername ${HOST} -connect ${HOST}:443 2>/dev/null | openssl x509 -noout -fingerprint -sha1 -inform pem | sed -e "s/.*=//g" | sed -e "s/\:/ /g"

Copy the result in your config.h (#define FINGERPRINT "xx xx ..").

Windows users

In your browser, go to https://opentsdb.gra1.metrics.ovh.net. Click on the lock next to the URL to display the fingerprint of the certificate. Replace all ‘:’ with one space.

Compile the project and upload it to the Wemos

  1. Open the .ino file in the Arduino IDE (you should have six tabs in the project)
  2. Plug the Wemos into you computer
  3. Select the port from Tools > Port
  4. On the top-left side, click on the arrow to upload the program
  5. Once uploaded, you can open the serial monitor: Tools > Serial Monitor

Right now, the program should fail, as the Wemos will not be able to connect to your wifi network.

Run the program

As we’ve already seen, the first run crashes. It’s because you need to launch a WPS connection, so depending on your internet modem, you will need to launch a WPS transaction. This could be a physical button on the modem, or a software action to trigger on the console (https://en.wikipedia.org/wiki/Wi-Fi_Protected_Setup).

When the process is launched on the modem side, you have something like 30 seconds to power the Wemos.

  1. Plug in your Wemos via USB => the program is running
  2. Select the port from Tools > Port (it may have changed)
  3. Open the serial monitor: Tools > Serial Monitor

Now you can follow the process.

Wifi connection

In the serial monitor (adjust the bit rate to 9600), you should get:

Try to connect
 
WPS config start
Trying to connect to <your modem> with saved config ...|SUCCESS
IP address: 192.168.xx.xx

If the wifi connection was successful, the serial console should display a local IP address (192.168.xx.xx), otherwise, it failed. Try again by triggering WPS on your modem and restarting the Wemos (unplug it and plug it back in).

Sending data to OVHcloud Metrics Data Platform

Now the Wemos is POSTing a request on the OVHcloud server. The serial console shows you the JSON it will send:

------------------------------------------------
POST opentsdb.gra1.metrics.ovh.net/api/put
[{"metric": "universe","value":42,"tags":{}}]
------------------------------------------------
beginResult: 0
http: 204
response: xxxx

If beginResult is negative, connection to the OVHcloud server failed. It could mean that the FINGERPRINT is wrong.

If http is not 2xx (it should be 204), the server could not process your request. It may mean that the TOKEN is wrong.

You got a 204? Great! It’s a success. Let’s check that on Grafana…

Configure Grafana

Go to OVHcloud Grafana: https://grafana.metrics.ovh.net/login. Log in with your OVHcloud account.

Configure a data source

Click on “Add data source”.

  • Name: choose one
  • Type: OpenTSDB
  • URL: https://<host you got from your manager (see below)>
  • Access: direct
  • Check “Basic Auth”
  • User: metrics
  • Password: <Read token from your manager (see below)>

Click on the “Add” button…

… and save it.

Create your first chart

Go back to https://grafana.metrics.ovh.net/ and click on “New Dashboard”.

Click on “Graph”.

Click on “Panel title”, then “Edit”.

Select your metric in the “metric name” field. The software must suggest the name universe (the name specified in the Arduino program). If it doesn’t, this means the metrics were not correctly sent by the Wemos. Close the “edit” panel (click the cross on the right) and save your configuration (top-left of the window).

Result analysis

Temperature rise

The first result to analyse is the temperature rise. The sensor was lying on the bricks of the oven. The yellow chart is the oven temperature, and the green chart is the ambient temperature.

  1. Between 11:05 and 11:10, there is a step at about 85°C. It seems to be the moisture of the oven that was drying.
  2. Then there’s a temperature drop, so I added some more wood to the oven (i.e. introduced cold stuff).
  3. At about 11:20, the slope is lighter, and I have no idea why. Fire not strong enough? Moisture deeper in the bricks?

Temperature dropdown

At this point, I moved all the embers at the back of the oven and put the sensor where the fire was burning. That’s why the chart begins at 400°C.

  1. The temperature dropdown seems to be something like F(t) = A/t
  2. At about 15:40, I changed the power supply from a phone power supply plugged in at 230V to a car battery with a voltage regulator (which seemed to be shitty)
  3. The ambient temperature is quite high between 15:00 and 17:00. It was a sunny day, so the sun was directly heating the circuit.
Website | + posts

Cyrille is a software developer in network teams in OVH. He is developing front applications and API for network automation