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

Sending data to InfluxDB using Arduino over HTTP

We are going to use an Arduino (any model) with an Ethernet shield. You can also perform the same task with WiFi shield with some minor changes in the code.

Arduino Mega with Ethernet shield

Arduino Mega with Ethernet shield

The code

Since I only have 1 Arduino at hand and no actual sensors, I am going to emulate the test scenario in the code. But You can of course change to code according to your needs. The important part of the code is how to write into the buffer and how to calculate exact length of the content so we can make the HTTP parser of InfluxDB happy! This can be done through the magic of sprintf function which can write into a buffer char array and return number of written bytes.

Here is the code, I have comment it throughly so it should be obvious what it does. If you have any questions, feel free to ask in the comment section or in the embedonix’s forum.

Notes regarding the code

Although I have commented the code thoroughly, here is a brief description of the code:

  • Lines 4 to 16 are variables required for setting up and connecting to the server using the Ethernet shield. In my case, the Arduino was connected to my own laptop in a loop-back configuration, so the server address for InfluxDB would be obviously If you are planning to connect your Arduino to a router or you are using WiFi, you must make sure to put the correct address in the eth_server[] variable!
  • In line 20 we have defined an integer equal to 2048, which is the size of our buffer to write the insertion query in it.
  • Line 23 is the definition and initialization of the buffer.
  • the get_place function is just used to get an imaginary place out of 4 places
  • the eth_send_data function is used to generate and send the correct HTTP POST request to the InfluxDB server
  • the loop function is used to continuously send some values in the range of 30 to 50 as temperature and a multiple of it as humidity and light values

So all and all, what you need to pay attention is the correct usage of writing a message into a character array buffer using sprintf function and using its return values to know where to continue inserting into the buffer.

In the next step, we will see how does the data is being transmitted and how to see the raw data in the InfluxDB’s administration panel.

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 *