Bliss Posted January 27 Posted January 27 (edited) First, here are links to dropbox folders for this project that I will update once in a while. MicroPython files for the V1 version which uses uasyncio to achieve Reading of inputs in a separate task. The MQTT (using mqtt_as library) example appears to be reliable. Most reliable. MicroPython files for the V2 version which is not using uasyncio so it is simpler. It is using Timer's interrupt only. Seems reliable with mqtt_as too. Not stable with built-in umqtt.simple. UIFlow2 Demo (LegoInterfaceB.m5f2) and Custom block (Lego9751.m5b2). The demo should already include the custom block for the LegoB blocky blocks. Tested only on M5Stack Core2. I talked about this project not long ago in the thread: https://www.eurobricks.com/forum/forums/topic/67665-dacta-control-lab-software/page/12/#comment-3739089 The project goal is to control the Lego Interface B using a micropython program running on an ESP32 microcontroller (MCU). I made a class module to interface with the Control Lab serial Box to read the inputs and write to the outputs. It's basically the same commands as in the Pyton Module I created for a PC. see : https://www.eurobricks.com/forum/forums/topic/200778-project-programs-to-allow-interactions-between-old-lego-control-interfaces-rcx-lego-interface-b-others/ So once setup, you only need to power the Interface B. (I powered the ESP32 MCU with the 9V DC port on the front on the Interface B ) An image is worth thousand words: I used a small rectifier Bridge to be independant of the orientation of the lego connector which would reverse polarity. You also need a TTL to RS232 converter because an ESP32 has uarts (Serial communication intrface) but the voltage level is 0-3.3v. RS232 is like -12 to +12. So this little converter can run at 3.3v and generate rs232 signals! (Using the chip MAX3232 not the MAX232). https://www.amazon.ca/dp/B09L1BB6F8 You might also need gender changers too. https://www.amazon.ca/DKARDU-Changer-Transfer-Adapter-Connector/dp/B09BQ4HF53 I tested on ESP32 Mini (WROOM) and ESP32-S2 (Cheap). I did not managed have a working ESP32-S3-Zero in Thonny. I did not try ESP32-C3 and C6 but I suspect it might work well. I recommand using Thonny : https://thonny.org/ Because you can install micripython on you ESP32 directly from there. And you can make and test your micropythons programs from there. Setup procedure with Windows: 1- First thing first, you plug your ESP32 to you computer with its usb port. 2- Open Device Manager to identify the COM port. (But you can also find it in Thonny if you don't have too much device connected) 3- In Thonny, menu Run -> Configure Interpreter, at the bottom right of this windows, there is a link for Install or Update MicroPython (esptool). Click on this link. (Not UF2). 4- Choose your ESP32 com port in Target Port., In MicroPython family, choose yout board (ESP32 for ex.), variant (Expressif ESP32 / WROOM), in version, choose the latest. Click install. 5- Once the ESP32 is flashed successfully with MicroPython, reboot the board. 6- In Thonny, menu Run -> Configure Interpreter, in Which kind of interpreter... choose MicroPython (ESP32) 7- In Thonny, menu Run -> Configure Interpreter, in port or WebREPL, choose the com port for your ESP32. Click OK. Eventually, once the whole setup is completed, we will be able to use the WebREPL which is the access to the Python Shell in Thonny through wifi, no more USB cable to the ESP... 8- You should now have the MicroPython device folder that appears on the left side and Thonny should create a boot.py file automatically 9- Always in the left pane of Thonny, you should also open the folder where you have put all the file downloaded from my dropbox (Link at the beginning of this post). 10- There are 3 files you have to modifiy from the files you downloaded from dropbox. secret.py : you have to specify your wifi SSID and Passord. webrepl_cfg.py : this is a password for the WebREPL. in the demob.py and demob_02.py, (and also the demob_mqtt_v2_) in the line: lego.start(17,16), 17 is the Tx Pin, 16 is the Rx Pin. This is what I used for the serial comm but you can use other pins. (If using ESP32-C3, 17, 16 are not allowed) 11. Once you modified the files, Select all files in you working folder (even the lib sub-folder for the mqtt demo, see notes at the bottom) on you computer in Thonny Left Pane, Right click on your selection and choose "upload to/" to copy the files on your device. (Overwrite boot.py) 12. if everything goes well, you might need to press red STOP in Thonny so it reboot the ESP. It should show in the shell window that it is connecting to your wifi and give you the IP address. 13. you can test the WebREPL now. Go in menu Run -> Configure Interpreter, Port or WebREPL: choose WebREPL and make sure it has the right IP address and WebREPL Password (See step 10) The hardware: 1. I soldered some Dupont pins to my ESP to use Dupont wires... But you can do whatever you want. There are ESP32 NodeMcu board with Pins already soldered and some socket board with screw terminals if you prefer. 2. From the ESP32 to the RS232 converter: ESP 3V3 goes to Converter Vcc, ESP Pin 17 (Tx) goes to Converter Tx pin, ESP pin 16 (Rx) goes to Converter Rx pin, ESP G (GND) pin goes to Converter GND pin. 3. You have to power the ESP32. You have many choice. From the USB port on the ESP32 Board to a Charger or PC USB port. OR you can use the 9V DC port on the front of the Interface B (Using an olf Lego cable you will cut at one end) and connect the +9v on the ESP32 Vcc Pin (NOT 3V3), and the - (0v) to the ESP32 G (GND) Pin. Usually there are many GND pins available on the ESP32. I personnaly used as I said earlier, a smal Rectifier bridge to protect againg reverse polarity of the 9V lego plug. There is a 3.3v regulator on these ESP32 Board that can take the 9V as an input voltage. (However, see update below) ***WARNING*** Do not plug USB cable to ESP32 and 9V on the external Vcc pin at the same time. UPDATE I finally used a buck step down converter to convert the 9V to 5V. Since I will not change the orientation of the Lego connector, I also removed the Rectifier (Well, I broke it and I did not have others)... 9v was working just fine on this ESP32-S2 board but might not be the case for all board... The external Vcc on the ESP is also used I believe for the FTDI chip and not sure what 9v can do to it... Notes on using the demob_mqtt_v2.py: I'm using asynchio for the legob module. The built-in MQTT module is not async friendly. I found an async mqtt library and installed it. It is in the lib folder that I copied in the dropbox link. using Thonny, copy the whole lib folder on your ESP32 device. Then, you should be able to run the demob_mqtt_v2.py. UPDATE: how ever, in the legobplc version, since i'm not using asyncio in this version, I might be able to implement the built-in mqtt... I'll keep you informed... Please do not hesitate to ask questions. Bliss. Edited February 11 by Bliss Quote
Gunners TekZone Posted January 27 Posted January 27 Ohh, nice! You have been a busy beaver, Eh! I don't normally search on Eurobricks, but I had a gut feeling... And I found this recent gem! Yes, I recall you mentioning something like this. I don't have that serial board, but I do believe the ESP32 can send serial through the USB (as used in an IDE serial monitor) so I wonder if I can make this work on my modded Interface-B? I will look into that when I can. Quote
Bliss Posted January 27 Author Posted January 27 Hey @Gunners TekZone, My intent was to not modifiy the Interface B at all. But since you already have a modified one, you could indeed use the internal 5V to power the ESP. The ESP could even be put inside the box but I would leave the USB port available and accessible from the outside. However, I think you cannot use both Vcc external supply on an ESP and a 5V coming from USB... So pluggin a USB cable to a PC while ESP is powered from the B Box might not work... Unless you modify the ESP... I used directly the 9V from lego to power my ESP32-S2 and it seems to work fine with no heating but I might rather sugest to use a buck step down converter to bring the voltage to 5V... Quote
Bliss Posted January 28 Author Posted January 28 (edited) For the serial board, (RS232), I took mine on amazon for fast delivery. https://www.amazon.ca/dp/B09L1BB6F8 But they are cheaper on aliexpress obviously. Edited January 28 by Bliss Quote
Bliss Posted January 28 Author Posted January 28 (edited) Buck step down converter board I used to convert 9v to 5v: https://www.aliexpress.com/item/1005005105181120.html Edited January 28 by Bliss Quote
Bliss Posted January 28 Author Posted January 28 (edited) Let's add some fun! I updated the Dropbox for this project. I added the file: demob_mqtt.py Since a simple MQTT client is built-in Micropython, I decided to make a small demo. In this demo, you can activate motor on output A by publishing a 1 payload to the topic lego/outa. Stop the motor by publishing a 0 in the payload of the same topic. Each time you press a touch sensor connected to input 1, it publishes 1 or 0 in the payload of the topic lego/inp1 for anyone subscribing to this topic. I use my Home Assistant to test. I also use the excellent MQTT Explorer (https://mqtt-explorer.com/). MQTTX is also very simple to make test. (https://mqttx.app/) I also modified the secret.py where you can put your MQTT user and password if you have any. This MQTT demo is working but not for very long as it eventually throws an OS Error... This might be because I'm using asyncio for the lego interface B and the mqtt module is not asynch... I wanted to make a separate task for the Lego Interface B input frame continuous reading to make sure no data is loss... There might be some solutions to this problem with MQTT... There are other mqtt libraries that could be more compatible like: https://github.com/peterhinch/micropython-mqtt I'll investigate. Edited January 28 by Bliss Quote
Gunners TekZone Posted January 28 Posted January 28 3 hours ago, Bliss said: My intent was to not modifiy the Interface B at all. I hear ya, these are getting rarer as time flows and shouldn't be modded "just cuz"... I only did so as it was otherwise already "dead". What I was referring to was that, using my already modified one, would mean that the communication between the ESP32 and Int-B would need to be via the USB-USB connection (not serial) as that is now that particular devices only link to a PC. But that would probably require the ESP32 to act as a USB host. Something I haven't messed with yet. 4 hours ago, Bliss said: you could indeed use the internal 5V to power the ESP. Actually, I am kinda dreaming of a battery powered option for both the Interface-B and ESP32 for true portability. 49 minutes ago, Bliss said: Since a simple MQTT client is built-in Micropython, I decided to make a small demo. I can barely hope to keep up with you!! Quote
Bliss Posted January 28 Author Posted January 28 (edited) Ok for the MQTT demo, I added the file demob_mqtt_v2.py which is using another mqtt library (https://github.com/peterhinch/micropython-mqtt)... It appears to be more stable. To use it, you will have to install the library. To do so, in Thonny, while you are connected to your ESP32 with the USB port and the content of the MocroPython Device is shown on the left pane, in the Shell (>>>) type one line at a time: (You obviously need internet connectivity) import mip mip.install("github:peterhinch/micropython-mqtt") This will install the necessary files in a "lib" folder on your ESP32. Then, you should be able to execute the demob_mqtt_v2.py with no problems. EDIT: I put the "lib" folder on the dropbox. The lib folder contains this mqtt library so you do not need to install. Just copy the folder on your ESP32 using Thonny. Edited January 31 by Bliss Quote
Bliss Posted January 28 Author Posted January 28 (edited) For some people having a big project involving interaction between many Lego Interface B Boxes, another protocol that might be interesting to implement that seems to be built-in the MicroPython for ESP, is the ESP-NOW protocol. This would allow direct communication between Lego Interface B boxes even without the need of wifi access point. Extract from MicroPython web site: ESP-NOW is a connection-less wireless communication protocol supporting: Direct communication between up to 20 registered peers: Without the need for a wireless access point (AP), Encrypted and unencrypted communication (up to 6 encrypted peers), Message sizes up to 250 bytes, Can operate alongside Wifi operation (network.WLAN) on ESP32 and ESP8266 devices. It is especially useful for small IoT networks, latency sensitive or power sensitive applications (such as battery operated devices) and for long-range communication between devices (hundreds of metres). Edited January 28 by Bliss Quote
Toastie Posted January 28 Posted January 28 Ahh - the wonderful ESP32. Very nice project! On 1/27/2025 at 4:24 AM, Bliss said: buck step down converter to convert the 9V to 5V A 7805 would also do it, right? Even a 78L05 should work, I believe. You could solder that directly onto the MAX board? This is all way over my head - but I love the project; it is really cool. Congratulations on your achievements! This is tremendous progress. All the best, Thorsten Quote
Bliss Posted January 28 Author Posted January 28 (edited) 2 hours ago, Toastie said: A 7805 would also do it, right? Even a 78L05 should work, I believe. You could solder that directly onto the MAX board? Indeed, a 78L05 could probably do the job to bring the lego 9vdc to 5vdc which is safe for the ESP32 and Max 3232 board. I did not have any... But I have a bunch of buck converter boards. You also could do a more compact design with more soldering for sure. Double face tape the ESP32 on the back of the Max board and have the 5v voltage regulator also there... I might give it a try with a ESP32-S3-Zero super mini board... Edited January 28 by Bliss Quote
Toastie Posted January 28 Posted January 28 6 minutes ago, Bliss said: I might give it a try with a ESP32-S3-Zero super mini board... You should! That would be simply slick! Best, Thorsten Quote
Bliss Posted January 29 Author Posted January 29 (edited) I updated demob_mqtt_v2.py the file in the dropbox. The mqtt is now more responsive and I added few more published inputs. Will add more eventually. I tested the phone app called IoT MQTT Panel (I used the free app) with which you can easily make your own dashboard based on mqtt topic: (Also available on android device) *** WITH AN IPHONE 14, IT DOES NOT CONNECT TO MY LOCAL MQTT BROKER BUT WORKS WITH test.moquitto.orq broker... IT WORKS LOCALY WITH AN OLD IPHONE SE *** It is very responsive and the switch to start the motor on output A is quite fast. We could also configure the demo program to start a lego interface b sequence... UPDATE 2025-01-29: Updated the demob_mqtt_v2.py again and updated the image above as well Edited January 31 by Bliss Quote
Bliss Posted January 31 Author Posted January 31 (edited) On 1/28/2025 at 5:44 PM, Toastie said: You should! That would be simply slick! Best, Thorsten Here it is: Powered from the Power port on the front of the Interface B. So I added a Rectifier bridge to be independant of lego connector orientation. The ESP32-S3-Zero is tricky to setup micropython. Had to try with different firmwares. Had to choose the 4Mb version on the ESP32-S3 page. Also, once micropython installed and working, when enabling the wifi, the ESP32-S3 was crashing and reseting itself. After lots of readings I finally tried the trick of adding a 1000uF cap between 3.3v and GND... Those extra mini versions of ESP32 are missing some noise filtering and shielding I think... Now it works great with the demob_mqtt_v2.py file... I also put the folder "lib" on the dropbox that you can copy on your esp32 micropython device. With this, you will not need to install the improved mqtt async I talked before... It will be already there. Edited January 31 by Bliss Quote
Bliss Posted January 31 Author Posted January 31 If you want to test the ESP32 micropyton legob without soldering, there are easy solutions: You will still need to get ESP32 board and a RS232 / TTL converter... and bread board or scew terminal breakout board. Those ESP32 board are easily available with pin headers already soldered... Quote
Bliss Posted January 31 Author Posted January 31 (edited) Just updated the legob.py file. I added retries if it does not succeed to connect within 5 sec. (Will print a message asking to check serial connection and power to the box). Also, if the connection is lost while it's reading inputs, it will proceed to stop (shutdown) the box... EDIT: I updated again, more proofing and changed the version number... Edited January 31 by Bliss Quote
Toastie Posted January 31 Posted January 31 Oh man, this is all so cool! I will never comprehend what you are doing, but it simply looks beautiful, elegant, and well-thought-out! One "aside" question: You are using Fritzing for the showing the wiring. I like that software as well, have registered with them long ago. Where did you get the modules from? I tried diyables.io, but I am too dumb to find them ... And finally: What are you going to do with your beautiful setup? I am curious what LEGO inventions will be brought to life remotely! Or do you have other things in mind, Interface A will activate? This is all very nice! All the best, Thorsten Quote
Bliss Posted January 31 Author Posted January 31 1 hour ago, Toastie said: I will never comprehend what you are doing, but it simply looks beautiful, elegant, and well-thought-out! One "aside" question: You are using Fritzing for the showing the wiring. I like that software as well, have registered with them long ago. Where did you get the modules from? I tried diyables.io, but I am too dumb to find them ... And finally: What are you going to do with your beautiful setup? I am curious what LEGO inventions will be brought to life remotely! Or do you have other things in mind, Interface A will activate? What is Fritzing? I just picked up some pictures over the internet that looked cool. I was wondering though how these very easy to understand schematics were done. So thank you for the hint. I usually buy this kind of electronic stuff on Aliexpress. There are also on amazon of course but more expensive. I have no particular projects in mind beside this project of making these "Modern Drivers" for interface B hoping it might interest some people. Quote
Gunners TekZone Posted January 31 Posted January 31 (edited) 5 minutes ago, Bliss said: There are also on amazon of course but more expensive. I just ordered a 4x pack of the Serial modules for $15 CAD from Amazon... Ali would take to long. I should get mine this weekend, and then I can try out your latest interface driver Edited January 31 by Gunners TekZone Quote
Bliss Posted January 31 Author Posted January 31 23 minutes ago, Gunners TekZone said: I just ordered a 4x pack of the Serial modules for $15 CAD from Amazon... Ali would take to long. I should get mine this weekend, and then I can try out your latest interface driver Did you also buy some gender changer like: https://www.amazon.ca/DKARDU-Changer-Transfer-Adapter-Connector/dp/B09BQ4HF53 ? (Unless you aready have some...) Quote
Gunners TekZone Posted January 31 Posted January 31 (edited) Yes... I ended up needed a few when I got my last two Interface-B's... And NULL-Modem adapters. Seams one can never have enough variety on hand. Next on my "when I can afford it hit list" is this... As I seem to be getting back into the older PC tech stuff :P https://www.amazon.ca/dp/B08CDQ76Q8 Wow... Why are these posted image links soooo large??? EDIT: OK better Edited January 31 by Gunners TekZone Quote
Bliss Posted January 31 Author Posted January 31 (edited) The M5Stack series of ESP32 products (Atom, Stick, Core) can be also a great options for those NOT wanting to solder... And their products are often LEGO compatible (HW Size, studs...) I will order for fun: ATOMIC RS232 Base TTL-RS232 ATOM Lite ESP32 IoT Dev Board I have a M5Stack Core2 already. M5Stack Core2 But I'm missing: RS232 Module 13.2 with DB9 Male Connector (Expensive, I will rather use Grove cable to a DB9 RS232/TTL converter module. But good to know it exists) Grove2Dupont Conversion Cable 20cm (5Pairs) I also have a M5Stick CPlus: M5StickC PLUS ESP32-PICO Mini IoT Development Kit . I might try it this weekend. I have grove connector cable with a bare end that I will adapt with dupont wires somehow... This small M5Stick has a small battery and a OLED screen and it also has some IR transmitter (maybe could interface the RCX?) and buttons and 6-Axis IMU, RED Led, RTC, Passive Buzzer, Microphone... Edited January 31 by Bliss Quote
Gunners TekZone Posted February 1 Posted February 1 Ahh, the variety of microcontroller toys out there is wonderful. And costly I have a Seeedstudio Wio Terminal that I purchased on a wim, ages ago, but never did anything with. I just realised it supports UART(Serial), micropython and even USB HOST. I'll see if I can use it. Might even be able to make a functional GUI with it for simple output port activations... hmmmm? Quote
Bliss Posted February 1 Author Posted February 1 (edited) I played a bit with the M5Stack ESP32 devices I have in hands. Notably the M5Stick CPlus. But I did not yet installed a bare micropython firmware in it. I was wondering if I could implement a LegoB Block in their UIFlow Blocky... I found out that their blocky thing is a bit limited and does not allow ASYNCH tasks really... ASYNCH allows to simulate some multitasking... (Micropython does not have real mutli-threading... not yet). So in blocky, you have a setup and a loop. But behind the scene they use micropython. They allow us to make our own Blocky custom blocks... I decided to try to make a LegoB blocky block that do not use asynchio... It works. I'm not finished implementing, but the basic works... If anyone is interested to my blocky LegoB code, let me know and I will finish implementing the methods and make it available in dropbox. That made me think it could be interesting to have a version of LegoB module that does not use ASYNCHIO as an alternative to the version using ASYNCHIO. So here it is: UPDATED 2025-02-04: LegoBv2 : LegoB Driver not using Asyncio. The main difference, is that your program is now responsible of the reading of the IO's. (see demob_plc01 et demob_plc02). It acts somewhat like an industrial PLC (Programmable Logic Controller which is my field of work). The code to achieve the same think as the multi tasking version seems bigger but for me it is more "logic" but I'm not objective cause I worked with PLC for over 30 yrs... So you always have to put your program in a FAST forever loop and ALWAYS include the lego.readinputs in the loop. And you have to think differently when you want to implement a sequence cause you cannot block the code for too long or you will miss input frames and might become totally un sync with the serial comm. demob_plc02 implements a sequence some how with delays between output commands... Let me know what version you prefer... The LegoB V2 is using Machine (ESP32 MCU) Timers to achieve separates tasks for KeppAlive and Read Inputs. Now the user program does not need to worry to much about timings... Edited February 5 by Bliss Quote
Gunners TekZone Posted February 1 Posted February 1 42 minutes ago, Bliss said: And you have to think differently when you want to implement a sequence cause you cannot block the code for too long or you will miss input frames and might become totally un sync with the serial comm. That was a big thing a bunch of us regulars on the BLYNK forum kept having to teach the noobs, never use blocking code!!!... That was with basic Arduino code and Legacy BLYNK (the new version is not as nice, and I gave up on it long ago). 45 minutes ago, Bliss said: Let me know what version you prefer... Still awaiting my Serial adapter, should be here today?? And I think I will have to simply duplicate what you have done with a basic ESP32, as I am finding micropython a learning curve as is, without also trying to make use of the Wio Terminal or PICO displays I have. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.