IoT with LEGO EV3 MicroPyhton (2)

Este artigo é a parte 2 de 2 da série  EV3 MicroPython and IoT

Wrote a better script to illustrate how can we use MQTT to control multiple EV3 bricks.

On the video bellow I’m using ‘mosquitto’ commands on my Ubuntu laptop but any MQTT client (like Android MQTT Dashboard or even other EV3) can be used:

IoT with LEGO EV3 MicroPyhton

Este artigo é a parte 1 de 2 da série  EV3 MicroPython and IoT

Just a few hours with this new image from LEGO and so much awesomeness already!

So this is all based on the MicroPython project. Being ‘v1.9.4’ it’s not the lastest version but it’s also not too old (current official release is 1.10). But it’s not ‘just’ a version customized for the EV3 devices… it seems a full implementation for ARM with ‘micropython-lib’ included.

What does it means?

Well, you can add micropyhton aditional modules if/when they are released, like for instance ‘pystone’ (a benchmarking tool):

micropython -m upip install pystone

There are already a lot of modules available including one HTTP client and other for sockets (so you can make your own MicroPython EV3 web server)… and two for MQTT!

And even better… at least the MQTT modules are already included within the LEGO image.

So let’s use EV3 for IoT:

#!/usr/bin/env pybricks-micropython

from pybricks import ev3brick as brick
from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor,
InfraredSensor, UltrasonicSensor, GyroSensor)
from pybricks.parameters import (Port, Stop, Direction, Button, Color,
SoundFile, ImageFile, Align)
from pybricks.tools import print, wait, StopWatch
from pybricks.robotics import DriveBase

from umqtt.robust import MQTTClient
import time

def getmessages(topic, msg):
if topic == b'JorgePe/test':
brick.display.text(msg)

client = MQTTClient('52dc166c-2de7-43c1-88ff-f80211c7a8f6','test.mosquitto.org')
client.connect()

client.publish('JorgePe/test','Listening')
client.set_callback(getmessages)
client.subscribe('JorgePe/test')
brick.display.text('Listening...')

while True:
client.check_msg()
time.sleep(0.1)

Just configure Wi-Fi, connect with Visual Studio Code, upload this project… and your EV3 is IoTed!

To be continued…