{"id":1543,"date":"2019-04-15T12:13:45","date_gmt":"2019-04-15T11:13:45","guid":{"rendered":"https:\/\/ofalcao.pt\/blog\/?p=1543"},"modified":"2019-04-16T17:43:56","modified_gmt":"2019-04-16T16:43:56","slug":"iot-with-lego-ev3-micropyhton","status":"publish","type":"post","link":"https:\/\/ofalcao.pt\/blog\/2019\/iot-with-lego-ev3-micropyhton","title":{"rendered":"IoT with LEGO EV3 MicroPyhton"},"content":{"rendered":"<div class=\"seriesmeta\">This post is part 1 of 2 of \u00a0<a href=\"https:\/\/ofalcao.pt\/blog\/series\/ev3-micropython-and-iot\" class=\"series-353\" title=\"EV3 MicroPython and IoT\">EV3 MicroPython and IoT<\/a><\/div>\n<p>Just a few hours with this new image from LEGO and so much awesomeness already!<\/p>\n\n\n\n<p>So this is all based on the <a href=\"http:\/\/micropython.org\/\">MicroPython<\/a> project. Being &#8216;v1.9.4&#8217; it&#8217;s not the lastest version but it&#8217;s also not too old (current official release is 1.10). But it&#8217;s not &#8216;just&#8217; a version customized for the EV3 devices&#8230; it seems a full implementation for ARM with &#8216;micropython-lib&#8217; included.<\/p>\n\n\n\n<p>What does it means?<\/p>\n\n\n\n<p>Well, you can add micropyhton aditional modules if\/when they are released, like for instance &#8216;pystone&#8217; (a benchmarking tool):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">micropython -m upip install pystone<\/pre>\n\n\n\n<p>There are already <a href=\"https:\/\/github.com\/pfalcon\/micropython-lib\">a lot of modules<\/a> available including one HTTP client and other for sockets (so you can make your own MicroPython EV3 web server)&#8230; and two for MQTT!<\/p>\n\n\n\n<p>And even better&#8230; at least the MQTT modules are already included within the LEGO image.<\/p>\n\n\n\n<p>So let&#8217;s use EV3 for IoT:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/usr\/bin\/env pybricks-micropython<br><br>from pybricks import ev3brick as brick<br>from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor,<br>                                 InfraredSensor, UltrasonicSensor, GyroSensor)<br>from pybricks.parameters import (Port, Stop, Direction, Button, Color,<br>                                 SoundFile, ImageFile, Align)<br>from pybricks.tools import print, wait, StopWatch<br>from pybricks.robotics import DriveBase<br><br>from umqtt.robust import MQTTClient<br>import time<br><br>def getmessages(topic, msg):<br>    if topic == b'JorgePe\/test':<br>        brick.display.text(msg)<br><br>client = MQTTClient('52dc166c-2de7-43c1-88ff-f80211c7a8f6','test.mosquitto.org')<br>client.connect()<br><br>client.publish('JorgePe\/test','Listening')<br>client.set_callback(getmessages)<br>client.subscribe('JorgePe\/test')<br>brick.display.text('Listening...')<br><br>while True:<br>    client.check_msg()<br>    time.sleep(0.1)<\/pre>\n\n\n\n<p>Just configure Wi-Fi, connect with Visual Studio Code, upload this project&#8230; and your EV3 is IoTed!<\/p>\n\n\n\n<p>To be continued&#8230;<\/p>\n\n\n\n<p> <br><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"seriesmeta\">This post is part 1  of 2 of \u00a0<a href=\"https:\/\/ofalcao.pt\/blog\/series\/ev3-micropython-and-iot\" class=\"series-353\" title=\"EV3 MicroPython and IoT\">EV3 MicroPython and IoT<\/a><\/div><p>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 &#8216;v1.9.4&#8217; it&#8217;s not the lastest version but it&#8217;s also not too old (current official release is 1.10). But it&#8217;s not &#8216;just&#8217; a version customized for the EV3 devices&#8230; it seems &hellip; <a href=\"https:\/\/ofalcao.pt\/blog\/2019\/iot-with-lego-ev3-micropyhton\" class=\"more-link\">Continuar a ler<span class=\"screen-reader-text\"> &#8220;IoT with LEGO EV3 MicroPyhton&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[1],"tags":[328,352,331],"series":[353],"class_list":["post-1543","post","type-post","status-publish","format-standard","hentry","category-sem-categoria","tag-iot","tag-micropython","tag-mqtt","series-ev3-micropython-and-iot"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2Mhyv-oT","_links":{"self":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/1543","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/comments?post=1543"}],"version-history":[{"count":0,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/1543\/revisions"}],"wp:attachment":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/media?parent=1543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/categories?post=1543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/tags?post=1543"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/series?post=1543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}