VOOZH about

URL: https://thenewstack.io/how-to-build-an-interactive-lego-robot-using-python/

⇱ How to Build an Interactive Lego Robot Using Python - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2022-06-17 04:00:01
How to Build an Interactive Lego Robot Using Python
contributed,sponsor-influxdata,sponsored,sponsored-post-contributed,
Edge Computing / Python / Software Development

How to Build an Interactive Lego Robot Using Python

Jun 17th, 2022 4:00am by Charles Mahler
👁 Featued image for: How to Build an Interactive Lego Robot Using Python
Feature image via Pixabay
InfluxData sponsored this post.
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.
InfluxData is the creator of InfluxDB, the leading time series platform. More than 1,900 customers use InfluxDB to collect, store, and analyze all time series data at any scale. Developers can query and analyze their time-stamped data to predict, respond, and adapt in real-time.
Learn More
The latest from InfluxData

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:

👁 Image

Python

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

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

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

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.

👁 Image
Docker

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:

👁 Image

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

👁 Image

Aperture

👁 Image

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.

InfluxData is the creator of InfluxDB, the leading time series platform. More than 1,900 customers use InfluxDB to collect, store, and analyze all time series data at any scale. Developers can query and analyze their time-stamped data to predict, respond, and adapt in real-time.
Learn More
The latest from InfluxData
TRENDING STORIES
Charles Mahler is a technical writer at InfluxData, where he creates content to help educate users on the InfluxData and time series data ecosystem. Charles' background includes working in digital marketing and full-stack software development.
Read more from Charles Mahler
InfluxData sponsored this post.
SHARE THIS STORY
TRENDING STORIES
TNS owner Insight Partners is an investor in: Pragma, Docker.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
👁 Image
Join the millions of developers using InfluxDB to predict, respond, and adapt in real-time.