Setting up InfluxDB and Grafana for storing and viewing Arduino measured sensor values

InfluxDB Line Protocol on HTTP

The Scenario

Let’s assume that you have 4 Arduinos in 4 different corners of your house and you want to monitor and log the temperature, humidity and light intensity in these corners. Then for using InfluxDB for your great home environment monitoring you need only one database in InfluxDB. Let’s call the database ‘embedonix‘.

Each entry in a database is called a “Measurement” in InfluxDB terms and each “Measurement” consists of “Tag(s)” and “Value(s)”. The tags, usually being a string or an integer, are signifying a spatial concept e.g. what is the source of values and where does it come from. The values associated with tags are just the values!

So in our scenario, we need a few tags for better extraction and filtering of data from the database. These tags can be ‘SOURCE=arduino_n‘ where n is the ID you assign to your Arduinos, and another tag might be ‘PLACE=kitchen‘ or ‘PLACE=garage‘…you get the idea. Then comes the values, which for our case would be something like ‘TEMP=20‘, ‘HUMIDITY=85.7‘, ‘LIGHT=780.98‘.

The format for sending the above data would be:

First, is the name of measurement then comes the tag(s) separated by a comma. After the last tag, there is a blank space, anything after this space will be treated as a value (comma separated if there is more than one value). So fixing for out scenario, an insertion query over HTTP will have to look like this:

And of course, the tags will be likely to be hard coded in each of your Arduinos (or any other microcontroller) but the values should be queried from the sensors dynamically. If you are asking what is the significant of measurement name, then my answer is (I can not get a clear answer from InfluxDB’s documentation) perhaps you would like to separate your data by month name, so the measurement for month June would be my_house_data_june, and when next month comes, you will update that code 😉

How to use create a database inInfluxDB

InfluxDB is in some senses similar to your average database system such as MySQL, as it has a kind of database->table->column structure. So in order to insert some data in InfluxDB, you need to have a database created before attempting to insert something into it. This can be done in many ways, but the easiest is to use the administration panel in the browser as below:

Query for creating a database in influxdb

Query for creating a database in influxdb

The used query in this case is

Now you can start inserting data in the newly created database (in this tutorial we assume the name is embedonix).

How to use INSERT queries over HTTP in InfluxDB

The Line Protocol for HTTP uses POST request to the write script on the server which is hosting InfluxDB, followed by the name of the database that data should be inserted in. A correctly formed HTTP header then will look like this:

As you can see above, the POST is requested to the write script with the db argument set to the name of the database we want to put data in (here, ‘/write?db=embedonix’). After the Content-Length there is a blank line that tells the HTTP parser that the actual content starts here. The Content-Length is important to be exactly as the number of characters in the data, otherwise, the HTTP parser will refuse to handle the request!

In the next section, we will see how we can write data in InfluxDB using Arduino.

Saeid Yazdani

An Electronics Engineer with passion in Embedded Systems, Wireless Sensor Networks, Assembly, Desktop and Mobile Development, Web Development and generally anything that has to do with hardware and software of any kind!

Leave a Reply

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