ESP8266 Writing Data into InfluxDB
Introduction
The ESP8266 is a low-cost Wi-Fi microcontroller that is widely used for IoT applications. One of the most common use cases is collecting sensor data and sending it to a database for storage and analysis. InfluxDB is a powerful time-series database optimized for high-write performance, making it an excellent choice for IoT data logging.
This guide will cover how to send sensor data from an ESP8266 to an InfluxDB database using HTTP requests.
Prerequisites
Before proceeding, ensure you have:
- An ESP8266 (such as NodeMCU or Wemos D1 Mini)
- A sensor (e.g., DHT11, DHT22, BMP280, or any other sensor)
- An InfluxDB instance (local or cloud-based)
- Arduino IDE installed with ESP8266 board support
- A stable Wi-Fi connection
Step 1: Setting Up InfluxDB
1.1 Installing InfluxDB (Local Installation)
If you don’t have InfluxDB installed, you can install it on Ubuntu using:
wget https://dl.influxdata.com/influxdb/releases/influxdb2_amd64.deb
sudo dpkg -i influxdb2_amd64.deb
sudo systemctl start influxdb
sudo systemctl enable influxdb
For Docker users:
docker run -d --name=influxdb -p 8086:8086 influxdb:latest
1.2 Creating a Database in InfluxDB
- Open your browser and go to
http://localhost:8086
- Set up an organization, bucket, and authentication token
- Note down the bucket name, organization name, and API token, as we will use them in the ESP8266 code.
Step 2: Wiring the ESP8266 and Sensor
Connect your ESP8266 and sensor as follows:
For a DHT22 sensor:
Pin | ESP8266 | DHT22 Sensor |
---|---|---|
VCC | 3.3V | VCC |
GND | GND | GND |
Data | D4 (GPIO2) | Data Pin |
Step 3: Writing the ESP8266 Code
3.1 Installing Required Libraries
Open Arduino IDE, and install the following libraries:
ESP8266WiFi
(built-in)ArduinoJson
(for JSON handling)DHT
(if using a DHT sensor)
To install, go to Sketch → Include Library → Manage Libraries, then search and install the libraries.
3.2 Writing the Code
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include <DHT.h>
#define DHTPIN 2 // GPIO2 (D4 on NodeMCU)
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
const char* influxdb_host = "http://your_influxdb_server:8086";
const char* influxdb_org = "your_organization";
const char* influxdb_bucket = "your_bucket";
const char* influxdb_token = "your_api_token";
WiFiClient client;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
dht.begin();
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
String payload = "temperature,location=room1 value=" + String(temperature) + "\n";
payload += "humidity,location=room1 value=" + String(humidity) + "\n";
if (sendToInfluxDB(payload)) {
Serial.println("Data sent successfully");
} else {
Serial.println("Failed to send data");
}
delay(5000); // Send data every 5 seconds
}
bool sendToInfluxDB(String payload) {
WiFiClient client;
if (!client.connect(influxdb_host, 8086)) {
Serial.println("Connection to InfluxDB failed");
return false;
}
String url = "/api/v2/write?org=" + String(influxdb_org) + "&bucket=" + String(influxdb_bucket) + "&precision=s";
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + influxdb_host + "\r\n" +
"Authorization: Token " + influxdb_token + "\r\n" +
"Content-Type: text/plain\r\n" +
"Content-Length: " + payload.length() + "\r\n\r\n" +
payload);
delay(100);
String response = client.readString();
Serial.println("Response: " + response);
return response.indexOf("204 No Content") > 0;
}
Step 4: Uploading and Running the Code
- Connect your ESP8266 to your computer via USB.
- Select the correct board (Tools → Board → NodeMCU 1.0 or your ESP8266 model).
- Select the correct port (Tools → Port).
- Click Upload to flash the code.
- Open the Serial Monitor (
115200 baud
) to check the output.
Step 5: Viewing Data in InfluxDB
- Open InfluxDB UI (
http://your_influxdb_server:8086
). - Navigate to Data Explorer.
- Select your bucket and fields (
temperature
andhumidity
). - Run a query to visualize the sensor data.
Conclusion
This guide demonstrated how to use an ESP8266 to send sensor data to InfluxDB using HTTP. This setup is useful for IoT applications where real-time monitoring and analytics are required. You can expand this project by integrating Grafana for visualizations or using MQTT for efficient communication.