InfluxDB helps you store and analyze data that changes over time, like temperature or sensor readings. It makes it easy to track and understand trends.
InfluxDB for time-series data in Raspberry Pi
from influxdb_client import InfluxDBClient, Point, WritePrecision client = InfluxDBClient(url="http://localhost:8086", token="your-token", org="your-org") write_api = client.write_api() point = Point("measurement_name") \ .tag("tag_key", "tag_value") \ .field("field_key", field_value) \ .time(time_value, WritePrecision.NS) write_api.write(bucket="your-bucket", org="your-org", record=point)
You create a Point to represent one data entry with tags and fields.
Tags are like labels (text), fields are the actual data (numbers or text).
from datetime import datetime point = Point("temperature") \ .tag("location", "living_room") \ .field("value", 23.5) \ .time(datetime.utcnow(), WritePrecision.NS)
from datetime import datetime point = Point("humidity") \ .tag("location", "garden") \ .field("value", 60) \ .time(datetime.utcnow(), WritePrecision.NS)
This program connects to InfluxDB on your Raspberry Pi, creates a temperature data point with a room tag, and writes it to the database. It then prints a confirmation message.
from influxdb_client import InfluxDBClient, Point, WritePrecision from datetime import datetime # Connect to InfluxDB running on Raspberry Pi client = InfluxDBClient(url="http://localhost:8086", token="my-token", org="my-org") write_api = client.write_api() # Create a data point for temperature point = Point("temperature") \ .tag("room", "kitchen") \ .field("value", 22.7) \ .time(datetime.utcnow(), WritePrecision.NS) # Write the point to the bucket write_api.write(bucket="home_data", org="my-org", record=point) print("Data point written to InfluxDB")
Make sure InfluxDB is installed and running on your Raspberry Pi before running the code.
Replace your-token, your-org, and your-bucket with your actual InfluxDB details.
Use UTC time to keep your data consistent across devices and time zones.
InfluxDB stores time-series data like sensor readings with timestamps.
You create data points with tags (labels) and fields (values) to save data.
Use Python client on Raspberry Pi to send data easily to InfluxDB.
Practice
Solution
Step 1: Understand InfluxDB's role
InfluxDB is designed to store time-series data, which means data points collected over time, such as sensor readings.Step 2: Compare with other options
Running a web server, compiling Python, or controlling GPIO pins are not the main functions of InfluxDB.Final Answer:
To store and manage time-series data like sensor readings -> Option DQuick Check:
InfluxDB = time-series data storage [OK]
- Confusing InfluxDB with web servers
- Thinking InfluxDB compiles code
- Assuming InfluxDB controls hardware pins
Solution
Step 1: Recall correct method name and parameters
The InfluxDB Python client uses the methodwrite_pointsto write data points, and it expects a list of dictionaries with keys 'measurement', 'tags', and 'fields'.Step 2: Check each option
client.write_points([{'measurement':'temperature','tags':{'room':'kitchen'},'fields':{'value':22.5}}]) matches the correct method and data structure. Options B, C, and D use incorrect method names or parameter formats.Final Answer:
client.write_points([{'measurement':'temperature','tags':{'room':'kitchen'},'fields':{'value':22.5}}]) -> Option BQuick Check:
Use write_points with list of dicts [OK]
- Using wrong method names like write_point or insert_point
- Passing dict instead of list of dicts
- Incorrect parameter structure
data = [{"measurement": "humidity", "tags": {"room": "bedroom"}, "fields": {"value": 45}}]
client.write_points(data)
result = client.query('SELECT * FROM humidity WHERE room = \'bedroom\'')
print(list(result.get_points()))
What will be the output?Solution
Step 1: Understand data insertion
The code writes one data point with measurement 'humidity', tag 'room'='bedroom', and field 'value'=45. This data is stored in InfluxDB.Step 2: Query and print results
The query selects all points from 'humidity' where 'room'='bedroom'. Since the data was just inserted, the query returns that point. Printing the list of points shows a list with one dictionary containing time, room, and value keys.Final Answer:
[{'time': '...', 'room': 'bedroom', 'value': 45}] -> Option CQuick Check:
Inserted data queried returns list with one point [OK]
- Expecting empty list if data was inserted
- Confusing syntax error with query result
- Assuming None is returned from query
data = {'measurement': 'temperature', 'tags': {'room': 'office'}, 'fields': {'value': 21.0}}
client.write_points(data)
What is the error and how to fix it?Solution
Step 1: Identify method parameter requirements
Thewrite_pointsmethod requires a list of data points, even if only one point is written.Step 2: Analyze given code
The code passes a single dictionary instead of a list containing that dictionary, causing an error.Final Answer:
Error: write_points expects a list of points; fix by wrapping data in a list -> Option AQuick Check:
write_points needs list of dicts [OK]
- Passing dict instead of list to write_points
- Missing 'fields' key in data point
- Assuming no error occurs
Solution
Step 1: Understand data structure for multiple points
Each data point must be a dictionary with 'measurement', 'tags', and 'fields'. Multiple points are passed as a list of such dictionaries.Step 2: Evaluate options
client.write_points([ {'measurement': 'temperature', 'tags': {'room': 'living'}, 'fields': {'value': 20}}, {'measurement': 'temperature', 'tags': {'room': 'bedroom'}, 'fields': {'value': 22}} ]) correctly uses a list of two dictionaries, each with proper tags and fields. Options A, B, and C misuse data structures or method parameters.Final Answer:
client.write_points([ {'measurement': 'temperature', 'tags': {'room': 'living'}, 'fields': {'value': 20}}, {'measurement': 'temperature', 'tags': {'room': 'bedroom'}, 'fields': {'value': 22}} ]) -> Option AQuick Check:
List of dicts for multiple points [OK]
- Passing lists inside tags or fields instead of separate points
- Incorrect method parameters
- Combining tags incorrectly in one dict
Ready to prove it?
Pass the test and this pattern moves unknown
