Logging Sensor Data (new version)
In this tutorial, we will log sensor data from a simulated thermostat into thermostat.mcap file.
Setup our tutorial project
In this tutorial we will use uv.
Let's create a new uv project and add the MCAP Logger package as dependency.
After this we should have the following elements in the project's folder:
We will change the hello.py to thermostat.py shown below.
Simulated Thermostat
The thermostat readings comes from the THERMOSTAT_DATA dictionary.
Installing ProtoBuf
Protocol Buffers (a.k.a ProtoBuf) are Google's language-neutral mechanism for serializing
structured data. It is used as the serialisation protocol for mcap-logger.
We need to install the ProtoBuf compiler called protoc in order to work with ProtoBuf.
Download the package and follow the instructions in the README.
We can confirm the installation with running the protoc --version command in the terminal.
Creating our protocol format
To specify our protocol format for the sensor data, we need to create a thermostat_data.proto file in our project
directory. The definitions in a .proto file are simple: we add a message for each data structure we want to
serialise, then specify a name and a type for each field in the message.
Our thermostat has temperature and humidity readings, so our .proto file will look like the following.
syntax = "proto3";
message ThermostatData {
int32 temperature = 1;
int32 humidity = 2;
}
ProtoBuf Syntax
This is the Language Guide for proto3.
Compiling our protocol buffers
After we defined our protocol, we need to generate the Python classes to read and write messages.
To do that we will run the protoc compiler on our .proto file.
This will generate a new Python file called thermostat_data_pb2.py in our project directory.
Our project directory should look something like this by now:
.
├── .python-version
├── README.md
├── hello.py
├── pyproject.toml
├── thermostat_data.proto
└── thermostat_data_pb2.py
Importing ProtoBuf
To create a thermostat data in our hello.py script, we need to import the ThermostatData class from the generated
thermostat_data_pb2.py.
Warning
In some cases the import can confuse the linter and generate "Cannot find reference" errors. However the interpreter will work with this import. This can be resolved if you add plugins to your IDE for ProtoBuf support.
For example, now we can define our thermostat data as the following:
Log the sensor data
After our first info log message, we will create a TopicLogger instance as following:
When the TopicLogger is initialized with a logger name, it checks if the logger has a McapHandler and if yes then it
will get the reference of its ProtoBuf writer. This means that the TopicLogger will use the same file writer to log
data as the log message logging.
Next, we will create a for loop with sleeping for 0.5 second to simulate the reading out of a thermostat sensor.
for data in THERMOSTAT_DATA:
time.sleep(0.5)
temperature = data["temp"]
humidity = data["humid"]
thermostat_data = ThermostatData(temperature=temperature, humidity=humidity)
To log the thermostat_data, we need to specify a Topic for our log. In this case, we will call it /thermostat
topic, and we will call the TopicLogger's topic function to create it. Topics have a write() function that
will log the data in its argument into the log file. So with calling topic_logger.topic(<topic_name>).write(<data>) we
can log any ProtoBuf data into our log file.
To make it a bit more interesting, let's log a warning message when the temperature goes below zero!
Now our thermostat.py script should look like this:
Running the thermostat
After finishing the thermostat.py script, we can run it to generate the log file.
When the script is finished running, we should have the generated thermostat.mcap in our project directory.
Opening our log file
Open Foxglove Studio and use the Open local file... command to open our thermostat.mcap log file.
We need to change the layout so that we have
a Log panel and a Plot panel.
In the settings we will configure the Log panel to use the /log topic, and for the Plot panel we will add two
Series: one for the /thermostat.humidity and one for the /thermostat.temperature.
log panel settings
plot panel settings
After this, we can play back the log and see the visualisation of the thermostat data alongside with the log messages.
thermostat log in Foxglove
Now you know how to make full use of the mcap-logger package.


