How to Remote Control your MINDSTORMS hub with an Android app in Python

Anton

Updated on:

Python BLE Remote control for MINDSTORMS

Together with Ste7an, I developed an android remote control app for MINDSTORMS and SPIKE Prime. Our goal was to enable remote control without the LEGO apps in the middle. We wanted it directly from Smart device to hub. We also wanted it to work with Python because LEGO’s solution does not support Python. The app works, but the code is still very rough. In this article, I’ll explain how to use the app. I hope you like it, and maybe you can contribute to the code.

Click the play button for a short video showing the end result

Downloading the MINDSTORMS Remote BLE app

The app is available in the Google Play store. When you run it, it will ask for access to your location. The app needs this to detect Bluetooth devices. Without a hub, the app won’t do much. Read on to find out how to connect it to MINDSTORMS.

Programming the robot for use with an RC app

On Github, I created a repository where I share robot scripts that work with the app. Currently, I only have a script that works with the Robot Inventor Hot Rod. The script is pretty rough too. Feel free to fork it and make some pull requests. A cleaner code structure and more comments are high on my wishlist. I don’t have much time at the moment.

To use the app, build the Hot Rod first. The create a new project within the MINDSTORMS app. Copy and paste the code from Github. Then run it. 

The robot now shows an ‘advertising’ animation. The animation means that the app can scan for it and connect to it. Now you can have fun and drive around!

How the MINDSTORMS and SPIKE Remote Control app works

We made the app to support two-way communication between the robot and the app. The app sets up a UART link over Bluetooth Low Energy. BLE or BT4.0 are popular names for the same protocol. 

Thirty times per second, the app sends the state of the buttons and sticks to the robot. To save bandwidth, we compressed that data into a few bytes. The bytes contain the positions of all sticks, sliders, and buttons. The robot then unpacks the data like this:

l_stick_hor, l_stick_ver, r_stick_hor, r_stick_ver, l_trigger, r_trigger, setting1, setting2, buttons_char = struct.unpack("bbbbBBhhB", data)

Using these values, the robot script sets the appropriate motor speeds and voltages at thirty times per second. 

Conversely, the robot can also send messages to the app. There are few possibilities. 

  1. A representation of an image. Send repr(Image(00000:11111:22222:33333:44444)) from the robot to show an icon in the center display.
  2. A text message for the large display. Send "TYour message here" to display it in the app.
  3. A setting for the sliders. Send "L%d"%some_number to change the position of the Left slider.

What’s next for the MINDSTORMS Android Remote Control app

We plan to update and expand the examples, add a better description in the play store, and expand the documentation. For now, we just wanted to share what we have so everyone can play with it!

Like this article? Help us make more!

Your support matters

We appreciate your support on Patreon and YouTube! Small things count.

Become a Patron

Don't miss a thing

Subscribe below to get an email when we publish a new article.

Share this with someone who needs to know

17 thoughts on “How to Remote Control your MINDSTORMS hub with an Android app in Python”

  1. Hellow, can you share android app, for control or connect snippet, i wanna make voice control to robot inventor

    Reply
  2. Hello, I tried the app on a Samsung A12 and I am not able to start it.
    After the installation there is no icon to do so. Even in the Google Play Store App I only can deinstall,
    but not start it.

    Reply
  3. Hello. First thanks for your work. It’s impressive. My students loved to see it at work.
    I downloaded the app. It works and works very well. Although I can’t figure out what are the exact steps to connect to my Spike Car all the time. I’d say I can’t reproduce the steps. I feel it’s a lucky shot when I get it to work…

    Any suggestion?

    Reply
  4. It would have been useful to have to code for the app present.

    I see in earlier communications that you are not conformable with showing the code, however, giving the open source community visibility of this would have turned this $400 useless toy into something more useful, ultimately giving kids a better chance at understanding coding.

    Lego obviously aren’t comfortable with releasing the Bluetooth code either…Leaves me asking the question what s the point? I’m better off letting my kids learn Arduino for a fraction of the price!

    Reply
    • Your post sounds bitter, but I don’t understand why. My code is free and public on GitHub – check the links in the article. LEGO is releasing some docs in small batches. And then there’s the fully open-source Pybricks.com.

      Reply
  5. I just bought Spike Prime and found this article, downloaded the “MINDSTORMS Remote BLE app” but it does not connect to the Hub. Another article in internet also mentions connection of Spike Prime Hub to Mindstorms application, which also does not work for me. Spike Prime app though connects without any issue. Is it possible that Lego updated the firmware so that Hub only connects to Spike Prime app? I dont’t have a serial cable at hand to try it, but Bluetooth does not work 🙁

    Reply
  6. It only works on SPIKE2.0. Not the newest. I’m a bit in the dark how to connect as LEGO does not provide much technical information. I will add a warning on top of this article. You can downgrade to SPIKE2.0 through Pybricks.

    Reply
  7. Hi Anton. Are you planning to update the app so that it can be installed on the latest version of Android? Regards, Phil Norman

    Reply

Leave a Reply

Item added to cart.
0 items - 0.00