I am working on a Raspberry pi based fast data logger. The idea is to use Pi to interface with lis3dh accelerometer over SPI as fast as we can and send the data to influxdb as time series. I am not sure if it is crazy, but I was planning to push the limit to 5Khz as sample frequency.
My first implementation is getting the data to influx fine using Python, but I'd like to improve the throughout.
Here is a portion of my code that sends data as recorded:
# Loop above that is keeping track of run time and other user control ^
# Setup the variables
lastSampleTime = time.time()
measurement = "accel"
location = "location1"
# May need to include additional location in future
# Run the periodic sampling
while (time.time() - lastSampleTime) < SampleTime:
# Get Values from the device in a list[x, y, z]
AccelData = GetReadingsNow(lis3dh)
# Obtain current time
timenow = time.time_ns()
# construct the data to be sent
data = [{
"measurement": measurement,
"tags": {
"location": location,
},
"time": timenow,
"fields": {
"x": Accels[0],
"y": Accels[1],
"z": Accels[2]
}
}]
# Send the JSON data to InfluxDB
client.write_points(data)
As per documentation on influx, to speed up the write is to use batch write to db. But here is my question.
- Can I use two threads, where 1 thread is responsible for sampling and other to send data to influx?
- If so, how will thread 2 know what has been sent already out of this changing list?
- If I couldn't do threading, then what would be the solution to minimize the data lost both in sampling and in writing to influx?
Thank you for your help.