Bird
Raised Fist0
Raspberry Piprogramming~10 mins

InfluxDB for time-series data in Raspberry Pi - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - InfluxDB for time-series data
Start Raspberry Pi
↓
Collect Sensor Data
↓
Format Data for InfluxDB
↓
Write Data to InfluxDB
↓
Query Data from InfluxDB
↓
Analyze or Visualize Data
↓
Repeat Data Collection
This flow shows how Raspberry Pi collects sensor data, sends it to InfluxDB, then queries and analyzes the time-series data.
Execution Sample
Raspberry Pi
from influxdb_client import InfluxDBClient
client = InfluxDBClient(url="http://localhost:8086", token="my-token", org="my-org")
write_api = client.write_api()
point = "temperature,sensor=pi value=23.5"
write_api.write(bucket="my-bucket", record=point)
client.close()
This code connects to InfluxDB on Raspberry Pi and writes a temperature data point.
Execution Table
StepActionEvaluationResult
1Create InfluxDBClientConnect to http://localhost:8086 with tokenClient object ready
2Create write_apiPrepare write interfaceWrite API ready
3Create data pointFormat string 'temperature,sensor=pi value=23.5'Point string ready
4Write data pointSend point to bucket 'my-bucket'Data written successfully
5Close clientRelease resourcesClient closed
💡 All steps completed, data point stored in InfluxDB
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
clientNoneInfluxDBClient objectInfluxDBClient objectInfluxDBClient objectInfluxDBClient objectClosed
write_apiNoneNoneWriteApi objectWriteApi objectWriteApi objectWriteApi object
pointNoneNoneNone"temperature,sensor=pi value=23.5""temperature,sensor=pi value=23.5""temperature,sensor=pi value=23.5"
Key Moments - 3 Insights
Why do we need to create a write_api object before writing data?
The write_api object manages sending data to InfluxDB. Without it, the client cannot write points. See execution_table step 2 where write_api is prepared.
What does the data point string format mean?
It follows InfluxDB line protocol: measurement,sensor_tag value_field. This tells InfluxDB what data and tags to store. See execution_table step 3.
Why do we close the client at the end?
Closing the client frees network and memory resources. It is good practice after finishing writes. See execution_table step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result after step 3?
AClient object ready
BPoint string ready
CData written successfully
DWrite API ready
💡 Hint
Check the 'Result' column for step 3 in execution_table.
At which step is the data actually sent to InfluxDB?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look for the action 'Write data point' in execution_table.
If we skip creating write_api, what will happen?
AData will still be written
BClient will close immediately
CWrite operation will fail
DPoint string will be invalid
💡 Hint
Refer to key_moments about the role of write_api.
Concept Snapshot
InfluxDB stores time-series data efficiently.
Use InfluxDBClient to connect.
Create write_api to send data points.
Format data as line protocol string.
Write points to a bucket.
Close client when done.
Full Transcript
This visual execution shows how a Raspberry Pi program connects to InfluxDB, prepares a write interface, formats a temperature data point, writes it to the database, and closes the connection. Each step changes variables like client, write_api, and point. Beginners often wonder why write_api is needed or how the data string works. The quiz checks understanding of these steps and their order.

Practice

(1/5)
1. What is the main purpose of InfluxDB when used with a Raspberry Pi?
easy
A. To run a web server for hosting websites
B. To control GPIO pins directly
C. To compile Python programs
D. To store and manage time-series data like sensor readings

Solution

  1. 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.
  2. Step 2: Compare with other options

    Running a web server, compiling Python, or controlling GPIO pins are not the main functions of InfluxDB.
  3. Final Answer:

    To store and manage time-series data like sensor readings -> Option D
  4. Quick Check:

    InfluxDB = time-series data storage [OK]
Hint: InfluxDB is for time-stamped data storage [OK]
Common Mistakes:
  • Confusing InfluxDB with web servers
  • Thinking InfluxDB compiles code
  • Assuming InfluxDB controls hardware pins
2. Which Python code snippet correctly writes a data point to InfluxDB on a Raspberry Pi?
easy
A. client.write_point('temperature', tags={'room':'kitchen'}, fields={'value':22.5})
B. client.write_points([{'measurement':'temperature','tags':{'room':'kitchen'},'fields':{'value':22.5}}])
C. client.write('temperature', {'room':'kitchen'}, 22.5)
D. client.insert_point('temperature', {'room':'kitchen'}, {'value':22.5})

Solution

  1. Step 1: Recall correct method name and parameters

    The InfluxDB Python client uses the method write_points to write data points, and it expects a list of dictionaries with keys 'measurement', 'tags', and 'fields'.
  2. 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.
  3. Final Answer:

    client.write_points([{'measurement':'temperature','tags':{'room':'kitchen'},'fields':{'value':22.5}}]) -> Option B
  4. Quick Check:

    Use write_points with list of dicts [OK]
Hint: Use write_points with list of dicts for data [OK]
Common Mistakes:
  • Using wrong method names like write_point or insert_point
  • Passing dict instead of list of dicts
  • Incorrect parameter structure
3. Given this Python code on Raspberry Pi using InfluxDB client:
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?
medium
A. SyntaxError
B. []
C. [{'time': '...', 'room': 'bedroom', 'value': 45}]
D. None

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    [{'time': '...', 'room': 'bedroom', 'value': 45}] -> Option C
  4. Quick Check:

    Inserted data queried returns list with one point [OK]
Hint: Inserted points appear in query results as dicts [OK]
Common Mistakes:
  • Expecting empty list if data was inserted
  • Confusing syntax error with query result
  • Assuming None is returned from query
4. This Python code snippet on Raspberry Pi tries to write data to InfluxDB but fails:
data = {'measurement': 'temperature', 'tags': {'room': 'office'}, 'fields': {'value': 21.0}}
client.write_points(data)
What is the error and how to fix it?
medium
A. Error: write_points expects a list of points; fix by wrapping data in a list
B. Error: 'fields' key missing; fix by adding 'fields' key
C. Error: 'measurement' must be a string; fix by converting to string
D. No error; code works fine

Solution

  1. Step 1: Identify method parameter requirements

    The write_points method requires a list of data points, even if only one point is written.
  2. Step 2: Analyze given code

    The code passes a single dictionary instead of a list containing that dictionary, causing an error.
  3. Final Answer:

    Error: write_points expects a list of points; fix by wrapping data in a list -> Option A
  4. Quick Check:

    write_points needs list of dicts [OK]
Hint: Always pass a list to write_points even for one point [OK]
Common Mistakes:
  • Passing dict instead of list to write_points
  • Missing 'fields' key in data point
  • Assuming no error occurs
5. You want to store temperature readings from multiple rooms on a Raspberry Pi using InfluxDB. Which Python code correctly writes two points with tags and fields in one call?
hard
A. client.write_points([ {'measurement': 'temperature', 'tags': {'room': 'living'}, 'fields': {'value': 20}}, {'measurement': 'temperature', 'tags': {'room': 'bedroom'}, 'fields': {'value': 22}} ])
B. client.write_points({ 'measurement': 'temperature', 'tags': {'room': ['living', 'bedroom']}, 'fields': {'value': [20, 22]} })
C. client.write_points([ {'measurement': 'temperature', 'tags': {'room': 'living', 'bedroom'}, 'fields': {'value': 20, 22}} ])
D. client.write_points('temperature', tags={'room': ['living', 'bedroom']}, fields={'value': [20, 22]})

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    client.write_points([ {'measurement': 'temperature', 'tags': {'room': 'living'}, 'fields': {'value': 20}}, {'measurement': 'temperature', 'tags': {'room': 'bedroom'}, 'fields': {'value': 22}} ]) -> Option A
  4. Quick Check:

    List of dicts for multiple points [OK]
Hint: Send multiple points as list of dicts with tags and fields [OK]
Common Mistakes:
  • Passing lists inside tags or fields instead of separate points
  • Incorrect method parameters
  • Combining tags incorrectly in one dict
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard