DEV Community

David G. Simmons
David G. Simmons

Posted on • Originally published at davidgs.com on

Writing to InfluxDB 2.0 from Arduino ESP8266

As InfluxData moves ever closer to releasing v2.0, it’s becoming increasingly important to be able to get data into InfluxDBv2, of course. Makes sense, right? Since the vast majority (like, indistinguishable from 100%) of my data comes from IoT devices, I decided it was time to start making those devices InfluxDB v2-capable.

I’m happy to say that the first step in that direction is now complete! One of my favorite sensors is a particulate matter sensor that measures the amount of very small particulate in the air (from 2.5µM to 100µM in diameter). This stuff, it turns out, is really really bad for you, so knowing how much is in the air is a good idea. To that end, I ordered one of these sensors from Adafriut:

3686 10

It’s small, and easy to hook up to pretty much anything since it just spews data out via UART. Since I have a giant pile of ESP8266 boards lying around (I typically order them by the dozens since they are so cheap and easy to deal with), I hooked it up to one of those. The code was simple, thanks to Adafruit providing it, and there was a handle InfluxDB library to write data with, but it only supported InfluxDB v1.x. The first thing I did (because I was in a hurry) was to grab the 1.x library and just re-write it for 2.x. Took me about 1/2 hour or less, and it worked great! (you can use that version here if you’d like). That really wasn’t the right solution though. So today I went back and created a proper fork of the original repository, and updated it to support either version 1.x or version 2.x of InfluxDB. I’ve of course submitted a proper Pull Request against the original library and hope that it will be accepted/merged soon.

Let’s walk through what it takes to use this new library then. It’s dead simple, really. At least with Arduino, all you have to do is add the Library, then include it in your sketch:

#include <InfluxDb.h>
//#include <InfluxDataV2.h> // if you want to use the other library I built and that’s in my GitHub 
#define INFLUXDB_HOST “myhost.com"
Influxdb influx(INFLUXDB_HOST);
Enter fullscreen mode Exit fullscreen mode

That gets you started. Next you’re going to need some specific information from your InfluxDB v2.0 (alpha still!) installation. Notably, you will need the organization, bucket, and token that are associated with your account. You can find these by pointing your web browser at your InfluxDB server, port 9999, entering your username and password, and going to the Configuration Page:

Screen Shot 2019 03 22 at 1 26 56 PM

You can then enter them into the Arduino Sketch:

influx.setBucket(“myBucket");
influx.setVersion(2);
influx.setOrg(“myOrg");
influx.setPort(9999);
influx.setToken(“myToken");
Enter fullscreen mode Exit fullscreen mode

Once you’ve done that, in your setup() function, you can start writing data to your v2.0 Influx server!

void loop() {
loopCount++;

InfluxData row("temperature");
row.addTag("device", "alpha");
row.addTag("sensor", "one");
row.addTag("mode", "pwm");
row.addValue("loopCount", loopCount);
row.addValue("value", random(10, 40));

influx.write(row);

delay(5000);
}
Enter fullscreen mode Exit fullscreen mode

See? I told you it was easy!

The post Writing to InfluxDB 2.0 from Arduino ESP8266 appeared first on David G. Simmons.

Top comments (2)

Collapse
 
davidgs profile image
David G. Simmons

I'm seriously disappointed in InfluxData at this point. They have introduced a breaking change in 2.0 that essentially invalidates all my data over the past almost 2 years. There is no migration path for the data from alpha/beta versions of 2.0 to the latest beta/GA.

Collapse
 
thibmaek profile image
Thibault Maekelbergh

Nice! We used this to monitor server room temperature and humidity at work.