A simple and lightweight program that connects the LEGO MINDSTORMS EV3 brick to an MQTT server and registers its sensors and motors under unique topics. The program itself is written in MicroPython and can be found here: main.py.
While setting up the program, make sure to check the config.py file. Here, you should definitely change the UUID to your own, preferably unique for every brick you set up. You might also want to use your own MQTT broker for security reasons.
- LEGO MINDSTORMS EV3 brick
- MicroSD card (size at least 2GB, can't be larger than 32GB!)
- USB Wi-Fi dongle (most modern dongles will work)
- Visual Studio Code with the LEGO® MINDSTORMS® EV3 MicroPython extension
- [Optional] Custom MQTT broker (a public one (test.mosquitto.org) is used by default)
- Add the EV3DEV image to your EV3 brick using a microSD card as described in Getting Started with ev3dev.
- Connect your EV3 brick to the internet using a USB Wi-Fi dongle. This can be done in the settings of the EV3 brick.
- Connect your EV3 brick to your computer using a USB cable.
- Open VS Code and clone this repository to your computer.
- Use the EV3DEV Device Browser to connect to your EV3 brick. This option is in the bottom left corner of VS Code by default.
- Run the code with the
Download and Runconfiguration in theRun and Debugtab of VS Code. (Shortcut:Ctrl + Shift + D) - Start sending messages to the MQTT server on the correct topics. For example, to make a motor on port A run at 10 degrees per second, send the message
10to the topicev3/1234/A, where 'ev3' and '1234' are unique identifiers defined in the program.
Alternative setup instructions can be found in the Pybricks for EV3 documentation.
Here are the specifications of the built-in MQTT connection handler:
- The client ID is purposefully set to an empty string. This is because most MQTT brokers will create a random client ID for you if you don't send one.
- The keepalive timeout is purposefully disabled. This is because the program doesn't send any messages when no sensors are connected, which would force the MQTT broker to disconnect the client after a while, causing the program to crash.
- The program does not support SSL by default. However, most MQTT brokers don't require SSL.
Here are some additional resources that might be useful: