How to Use InfluxDB for IoT Data Storage and Analysis
Use
InfluxDB to store time-series IoT data by writing sensor readings as points with timestamps into measurements. Query this data using Flux or InfluxQL to analyze trends and monitor devices in real time.Syntax
InfluxDB stores IoT data as points in measurements with tags and fields. Each point has a timestamp, tags (metadata), and fields (actual sensor values). You write data using the Line Protocol format:
measurement,tag_key=tag_value field_key=field_value timestamp
Here:
- measurement: The name of the data category (e.g., temperature)
- tag_key=tag_value: Metadata like device ID or location
- field_key=field_value: The sensor reading
- timestamp: Unix nanosecond timestamp (optional, current time used if missing)
plaintext
temperature,device=thermometer1,location=room1 value=23.5 1650000000000000000
Example
This example shows how to write IoT temperature data to InfluxDB using the HTTP API and query it with Flux to get the last 5 readings.
bash
# Write data using curl (Line Protocol format) curl -i -XPOST "http://localhost:8086/api/v2/write?org=myorg&bucket=mybucket&precision=ns" \ -H "Authorization: Token mytoken" \ --data-raw "temperature,device=thermometer1,location=room1 value=23.5" # Query last 5 temperature readings using Flux curl -XPOST "http://localhost:8086/api/v2/query?org=myorg" \ -H "Authorization: Token mytoken" \ -H "Accept: application/csv" \ -H "Content-type: application/vnd.flux" \ -d 'from(bucket:"mybucket") |> range(start: -1h) |> filter(fn: (r) => r._measurement == "temperature" and r._field == "value") |> limit(n:5) |> sort(columns:["_time"], desc:true)'
Output
#datatype,string,long,dateTime:RFC3339,string,string,string,double
#group,false,false,false,true,true,true,false
#default,_result,,,,,
,result,table,_time,_measurement,_field,_value
,,0,2024-06-01T12:04:00Z,temperature,value,23.9
,,0,2024-06-01T12:03:00Z,temperature,value,23.8
,,0,2024-06-01T12:02:00Z,temperature,value,23.6
,,0,2024-06-01T12:01:00Z,temperature,value,23.7
,,0,2024-06-01T12:00:00Z,temperature,value,23.5
Common Pitfalls
- Missing timestamps: If you omit timestamps, InfluxDB uses the server time, which may cause data ordering issues.
- Improper tag usage: Use tags for metadata with low cardinality (few unique values) to optimize queries.
- Writing fields as tags: Fields are for sensor values; storing them as tags can slow down writes and queries.
- Not batching writes: Sending many single writes hurts performance; batch points together.
plaintext
Incorrect: temperature,device=thermometer1 value=23.5 Correct: temperature,device=thermometer1 value=23.5 1650000000000000000
Quick Reference
| Concept | Description | Example |
|---|---|---|
| Measurement | Category of data | temperature |
| Tag | Metadata for filtering | device=thermometer1 |
| Field | Sensor value | value=23.5 |
| Timestamp | Time of data point | 1650000000000000000 (ns) |
| Write API | Send data to InfluxDB | HTTP POST to /api/v2/write |
| Query Language | Analyze data | Flux or InfluxQL |
Key Takeaways
Use InfluxDB's Line Protocol to write IoT sensor data with measurement, tags, fields, and timestamps.
Tags should store metadata with low unique values; fields store actual sensor readings.
Batch writes to improve performance and avoid missing timestamps for accurate data ordering.
Query IoT data using Flux for flexible time-series analysis and monitoring.
Avoid storing sensor values as tags to keep queries fast and storage efficient.