The ESP8266 is a $5 IOT device with huge capabilities. In this post we will log data to a remote Influx database running on a RaspberryPi.
I am programming the ESP8266 in the Arduino IDE, the ESP8266 library is required, you can find it here. I have a test code file (of copy from below) that you can upload after entering your InfluxDB I.P. Address, SSID & Password and it will start logging data immediately.
Code for InfluxDB Version 1.x (Version 1.6 specifically for me.)
#include <ESP8266WiFi.h> #include <ESP8266WiFiMulti.h> #include <InfluxDb.h> #define INFLUXDB_HOST "192.168.1.1" //Enter IP of device running Influx Database #define WIFI_SSID "SSID" //Enter SSID of your WIFI Access Point #define WIFI_PASS "PASSWORD" //Enter Password of your WIFI Access Point ESP8266WiFiMulti WiFiMulti; Influxdb influx(INFLUXDB_HOST); void setup() { Serial.begin(9600); WiFiMulti.addAP(WIFI_SSID, WIFI_PASS); Serial.print("Connecting to WIFI"); while (WiFiMulti.run() != WL_CONNECTED) { Serial.print("."); delay(100); } Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); influx.setDb("esp8266_test"); Serial.println("Setup Complete."); } int loopCount = 0; void loop() { loopCount++; InfluxData row("data"); row.addTag("Device", "ESP8266"); row.addTag("Sensor", "Temp"); row.addTag("Unit", "Celsius"); row.addValue("LoopCount", loopCount); row.addValue("RandomValue", random(10, 40)); influx.write(row); delay(5000); }
Code for InfluxDB Version 2.x (Version 2.1 specifically for me). Download Arduino library from here.
#include <ESP8266WiFi.h> #include <ESP8266WiFiMulti.h> #include <InfluxDb.h> #define INFLUXDB_URL "http://192.168.1.XXX:8086" // e.g. http://192.168.1.48:8086 (In InfluxDB 2 UI -> Load Data -> Client Libraries), #define INFLUXDB_TOKEN "YOUR_TOKEN" // InfluxDB 2 server or cloud API authentication token (Use: InfluxDB UI -> Load Data -> Tokens -> <select token>) #define INFLUXDB_ORG "influx" // InfluxDB 2 organization id (Use: InfluxDB UI -> Settings -> Profile -> <name under tile> ) #define INFLUXDB_BUCKET "YOUR_BUCKET" // InfluxDB 2 bucket name (Use: InfluxDB UI -> Load Data -> Buckets) #define WIFI_SSID "YOUR_SSID" #define WIFI_PASS "YOUR_PASS" #define MEASUREMENT "esp" #define DEVICE "esp_04" #define ID "Development ESP" ESP8266WiFiMulti WiFiMulti; InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN); Point row(MEASUREMENT); // Setup InfluxDB data point void setup() { Serial.begin(9600); WiFiMulti.addAP(WIFI_SSID, WIFI_PASS); Serial.print("Connecting to WIFI"); while (WiFiMulti.run() != WL_CONNECTED) { Serial.print("."); delay(100); } Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); if (client.validateConnection()) { // Checks if can communicate with InfluxDB server Serial.print("Connected to InfluxDB: "); Serial.println(client.getServerUrl()); } else { Serial.print("InfluxDB connection failed: "); Serial.println(client.getLastErrorMessage()); } Serial.println("Setup Complete."); } int loopCount = 0; void loop() { loopCount++; row.clearFields(); // Clear Influx Fields row.clearTags(); // Clear Influx Tags row.addTag("Device", DEVICE); row.addTag("ID", ID); row.addField("LoopCount", loopCount); row.addField("RandomValue", random(0, 100)); //Helpful for debugging if needed. row.addField("25_Value", 20); row.addField("50_Value", 50); row.addField("100_Value", 100); Serial.print("Writing: "); // Print what are we exactly writing Serial.println(client.pointToLineProtocol(row)); if (!client.writePoint(row)) { Serial.print("InfluxDB write failed: "); Serial.println(client.getLastErrorMessage()); } else { Serial.println("Wrote data successfully"); Serial.println(""); } delay(5000); }
The Arduino Serial Terminal will display something like the below so you can if it is working. (My previous tutorial shows setting up InfluxDB, ensure you have the database “esp8266_test” created as we are going to write to that.)
--> writing to esp8266_test: data,Device=ESP8266,Sensor=Temp,Unit=Celsius LoopCount=256.00,RandomValue=37.00 <-- Response: 204 "" --> writing to esp8266_test: data,Device=ESP8266,Sensor=Temp,Unit=Celsius LoopCount=257.00,RandomValue=20.00 <-- Response: 204
On the Influx Database we can look at the data by:
influx USE esp8266_test select * from data limit 50
Below you can see the export from my database (I have shortened the time field for neatness). You can see I reset the ESP8266 a couple of times due to the LoopCount value.
time Device LoopCount RandomValue Sensor Unit ---- ------ --------- ----------- ------ ---- 52808175073 ESP8266 1 38 Temp Celsius 63108846141 ESP8266 2 35 Temp Celsius 69802517277 ESP8266 1 13 Temp Celsius 79892112240 ESP8266 2 12 Temp Celsius 89961602267 ESP8266 3 14 Temp Celsius 99998928411 ESP8266 4 22 Temp Celsius 10053683452 ESP8266 5 10 Temp Celsius 20120378415 ESP8266 6 28 Temp Celsius 30175745403 ESP8266 7 14 Temp Celsius 40732248123 ESP8266 8 38 Temp Celsius 51232948067 ESP8266 9 15 Temp Celsius 61322347831 ESP8266 10 13 Temp Celsius 71424432515 ESP8266 11 19 Temp Celsius 84740185749 ESP8266 1 18 Temp Celsius 94790343615 ESP8266 2 21 Temp Celsius 04839215465 ESP8266 3 13 Temp Celsius 31864448941 ESP8266 1 32 Temp Celsius 41956355523 ESP8266 2 36 Temp Celsius 52018136222 ESP8266 3 30 Temp Celsius 62083037888 ESP8266 4 22 Temp Celsius
That’s it!
Resources I used:
- https://github.com/davidgs/ESP8266_Influx_DB_V2
- https://www.influxdata.com/blog/writing-data-from-arduino-to-influxdb-v2/