Favorite Social Media Timesink
When you take a break from work, where are you going?
Video clips on TikTok/YouTube
X, Bluesky, Mastodon et al...
Web surfing
I do not get distracted by petty amusements
Edge Computing / Python / Software Development

How to Build an Interactive Lego Robot Using Python

Jun 17th, 2022 4:00am by
Featued image for: How to Build an Interactive Lego Robot Using Python
Feature image via Pixabay

Charles Mahler
Charles is a technical marketing writer at InfluxData. His background includes working in digital marketing and full-stack software development.

One of the great things about Python is that there is a library for almost everything and that includes controlling Lego robots. Recently, our team was brainstorming some ideas for how to make more exciting and attention-drawing demos for events compared to our standard dashboards and demos run on a laptop.

The end result was repurposing a Lego robot to make an interactive InfluxDB demo using Python to communicate with the robot via Bluetooth connection. In this article, I’ll go over some of the technical challenges and architecture decisions that went into making this robot a reality and how even a demo project as simple as a Lego robot can be applied to more complex use cases.

InfluxDB Lego Robot Design Decisions

One of our team members came up with the idea to use a Lego Boost Robot as the core of the demo after working on one with his kid, who wasn’t exactly pleased to have his toy temporarily commandeered for the prototype phase. This coworker knew there were a few available Python libraries for controlling the robot.

The next stage was figuring out how to connect the different components involved with the robot demo, which basically involved the tradeoff between simplicity and reliability. We needed the setup to be easy enough to use so that nontechnical event staff would be able to start the demo. There were a few proposed architectures that involved using a Raspberry Pi or ESP32 to interact with the robot. In the end, we determined that it would be easiest to connect to the robot directly from a laptop running the other software required for the demo, rather than using other devices that could be lost or broken while traveling to events.

The first iteration of the robot demo does the following when activated:

  • A Python script creates a Bluetooth connection to the robot and sends commands for one of two different preprogrammed routines — a dance routine or random movement patterns.
  • Sensor data from the robot is sent back to the Python program, which then forwards that data to a MQTT broker.
  • Telegraf transforms the data received from the MQTT broker, and then sends it to be stored by a local InfluxDB instance for long-term storage.
  • IoT Center also receives the data from the MQTT broker and displays the sensor data with real-time dashboards. IoT Center also connects to InfluxDB and can query and display historical data as well.

Lego Robot Technical Deep Dive

Now let’s take a deeper look at the different components that make up the Lego robot project. For such a simple demo, there are quite a few moving pieces. Here’s a diagram showing how everything is connected:


Python acts as the glue of the application by connecting to the robot via Bluetooth, giving it commands and then sending the sensor data to the MQTT broker. The Python script uses the following libraries:

  • Bleak — This is the underlying Bluetooth library used to connect to the broker.
  • Pylgbst — This is a high-level wrapper around the official Lego Boost Bluetooth protocol, providing an API to control the robot and connect to it. It uses the connection created by Bleak to send these commands.
  • Paho MQTT — This library is used to connect to the MQTT broker.
  • InfluxDB Python Client library — The InfluxDB Client is used to simplify interacting with InfluxDB by transforming the sensor data into standard line protocol, although this could also be done with Telegraf instead.


InfluxDB is used for storing all the time-stamped sensor data being generated by the robot as it runs. These types of IoT workloads are ideal for a time-series database like InfluxDB. InfluxDB OSS is run locally for long-term storage, but it could also be configured to push data to a cloud instance using the edge replication feature.


Telegraf is used to forward sensor data to InfluxDB in this demo, but it also has support for over 40 other data stores. Telegraf is ingesting MQTT data in this use case but also has support for many other different input formats and has processor plugins, which can be used to transform data before outputting it for storage.


Mosquitto is an open source MQTT broker that we chose to use for this project. MQTT is commonly used for IoT projects, so it makes sense from an implementation perspective and also makes the project more relevant to potential users as a reference architecture.

IoT Center

IoT Center is an application built by InfluxData as an example IoT app and reference architecture for how to integrate InfluxDB with various IoT devices. For the robot demo, IoT Center receives the data over MQTT and displays it in real time via dashboards. IoT Center can also display historical data by querying InfluxDB directly using JavaScript client libraries.

The robot has sensors that provide the following values that are displayed in the dashboards:

  • The direction the two motors are moving.
  • A distance reading from the visual sensor.
  • Angle values for the X,Y and Z axes.
  • The voltage of the battery.


Docker is used to simplify managing and running the demo for nontechnical users. All users need to do is activate the demo environment using Docker Desktop with a single click and then InfluxDB, Telegraf, Mosquitto and IoT Center are ready to go.

From Lego Robots to Plasma and Particle Physics

While making a Lego robot might seem like a toy problem, it really shows the power and flexibility of InfluxDB as a platform and the value of learning how to use it. For example, one of the interns who helped to create the Lego robot demo took this knowledge back to school and reused a good chunk of the code to perform real-time monitoring of a lab experiment involving the detection of atomic hydrogen:

This might seem crazy, but conceptually, it’s the same thing, just collecting data from a sensor.


Hydrogen detector inside the aperture


InfluxDB is also used for even larger-scale experiments, with CERN using InfluxDB to store monitoring data for the ALICE experiment, which uses the 27-kilometer-long Large Hadron Collider to study the physical properties of the Quark-Gluon Plasma.

Extending the InfluxDB Lego Robot

The Lego robot demo made its debut at AWS re:Invent and did a good job of drawing in visitors by making them curious to see what it could do. There are currently a few ideas for how to improve the demo by adding functionality. One idea is to create a more interactive demo by combining a color sensor that can trigger automated action by creating tasks and alerts using InfluxDB. Another option would be to allow the robot to be steered by either keyboard or mouse input. These ideas are really just the tip of the iceberg, and there are a ton of other possibilities thanks to the Python ecosystem. If you feel like building your own robot, you can check out the GitHub repo for inspiration.

Group Created with Sketch.
TNS owner Insight Partners is an investor in: Pragma, Docker.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.