Here an outline of the setup:
I have several sources for sensor input data (GPS, and a bunch of other sensor data from different sources).
- All of those individual inputs consist of a timestamp, the name of the input and the value.
- There is no defined list of inputs, but new inputs can appear whose name we didn’t know before. Just as well inputs can disappear again.
- The update rate of the Inputs is also neither predifined nor fixed. It could be periodic or sporadic, but we don’t know its recurrence before and this is also subject to change.
I want to report the current state of Inputs (only the newest value) in a fixed recurrence, e.g. every second, via MQTT to a Server.
There is no need to store the input data after sending the MQTT message. When a specific sensor input is not available for several seconds, it shall not be part of the MQTT message anymore.
How do I store the input data before sending the current “snapshot” in the fixed recurrence to the MQTT server?
My current solution (?!ugly hack?!):
- Everytime a new sensor input value appears, I write the timestamp and the value in a file with the name of the sensor input in a single folder which is used for all inputs.
- If the file already existed, I simply overwrite it, as only the newest value of the sensor is important.
- To not have too old sensor input data lying around, I remove files that were created more than a defined number of seconds ago.
Example: Sensor Input GPS latitude is stored in file
latitude.dat, which has one line containing the timestamp and the value of latitude:
pi@raspberrypi ~ $ cat latitude.dat
The solution works and is flexible enough to handle this task, but I am not at all happy about it. I thought about using a SQL-database, but I somehow think due to the nature of the inputs, it’s also not optimally suited. Does anybody have a good idea, what other data structure I could use for the storage of the input data?
EDIT: The sensor data does not need to survive a power failure.