I have been an immense fan of LEGO for my entire life - so when MINDSTORMS came along and combined my passion for brick building with my love of programming and robotics, I of course dived in head first! But for some reason, the latest generation, MINDSTORMS EV3 had eluded me, despite having been out for years - so I leapt at the excuse to dive in once again for this challenge! I found the provided Hackster projects extremely helpful in getting going, but thought them rather ambitious and complex for someone who is just getting started with MINDSTORMS EV3/ev3dev and the Alexa Gadgets Toolkit - so for my entry, I wanted to provide a simple, easy-to-understand example. I looked at my own extensive LEGO collection for inspiration, and the 40155 Piggy Coin Bank inspired me to create an interactive, voice-controlled piggy bank that could help kids with counting while encouraging them to save money in a fun way!
Getting Started ๐ฐ ๐ฉโ๐ปAs I mentioned, there is already a series of excellent getting started projects on Hackster, so I'm not going to reproduce that content here; instead, work your way through at least Mission 1, then let's meet back here... โ
All set? Great! Where were we? Oh right - piggy bank! So, the first steps are to either build 40155 if you have it, recreate it or something similar using the instructions (also attached below since I couldn't figure out how to include them as "just a link" and still allow the project to submit!? ๐ ), or - if you don't feel like building right now, all you really need is the EV3 Brick connected to the EV3 Infrared Sensor.
If you're using 40155 or similar, replace the original face with the IR sensor, positioned as shown here:
If you're skipping the build portion, it might be helpful to set some kind of object about 5cm in front of the sensor as a reference. Now, onto the code! ๐
Development Process ๐ช ๐ปYou can find all of the code for this project in the PIGGI3-BANK3R repo - I used the same structure as the example Missions, so this should be familiar to you if you've walked through at least some of them. In the root you will find piggi3.py
- this code runs on the EV3, creating the Bluetooth connection with your compatible Echo device and continually polling the IR sensor to see if the distance has changed. When it does, we interpret that as a coin falling through the slot of the piggy bank, and increase the balance
accordingly. You can use the same Send workspace to device feature from the Missions to get the code on the EV3, then run it using:
cd ~/PIGGI3-BANK3R
sudo python3 piggi3.py
(default password: maker
) - but hold up for a moment, because we still need to set up the Alexa side of things before everything will work! ๐
Create an AWS account if you don't already have one, then head to the Alexa Skills Kit section and create a new skill called MINDSTORMS
(or whatever you'd like it to be called!). You can also follow Mission 3's Skill creation instructions for this portion (with all of their nice screen caps etc.!) since the steps are the same - only the file contents differ. Create a Custom, Alexa-Hosted skill, then click Interfaces in the left nav, toggle Custom Interface Controller to on, and then, critically (this was omitted in the guide I followed!) click Save Interfaces at the top of the page!
Next, click JSON Editor under Interaction Model in the left nav to define the model. The required JSON can be found in skill-nodejs/model.json
in the cloned repo - just copy and paste that into the editor, then click Save Model and Build Model. With the model defined, it's now time to add the code that ties everything together - this is JavaScript running on the "Alexa-Hosted" AWS Lambda that you selected earlier. Copy the contents of skill-nodejs/lambda/index.js
, util.js
and package.json
into the corresponding files that you see when clicking the Code tab in the top nav. Then create a New File called /lambda/common.js
and populate it with the contents of the corresponding local file in skill-nodejs/lambda
. Finally, click Deploy to push your code to the cloud. โ๏ธ
One last important step: you'll need to update piggi3.ini
with your own [GadgetSettings]
values - these are the Amazon ID and Alexa Gadget Secret that you were given when you created your Alexa Gadget - refer back to Mission 1 for more details if you need to.
[GadgetSettings]
amazonId = YOUR_GADGET_AMAZON_ID
alexaGadgetSecret = YOUR_GADGET_SECRET
OK, now, after syncing your workspace to the EV3 in order to grab those latest changes, you can run piggi3.py
and start interacting with your MINDSTORMS using an Echo device! To begin, say "Alexa, open mindstorms" (if nothing happens, check your EV3 terminal output and ensure that MINDSTORMS has connected to the Echo device - see Mission 1 for troubleshooting ideas if you have trouble connecting!). Then "Alexa, activate detect mode" to enable coin detection. When a coin is detected, your Echo device will inform you of this, as well as of the updated balance. You can also inquire as to your balance any time by saying "Alexa, get balance" - or set it (i.e. if your piggy bank already has some coins in when you start) by saying "Alexa, set balance to {number of coins}".
That's all there is to it! Despite a large number of moving parts, it's quite a simple system once you understand what they all do! Now find your favorite little LEGO fan and get them on the right path to retirement - it's never to soon! ๐๐ญ๐ง๐ฐ
Results and Conclusions โ๏ธ ๐Voice-enabling MINDSTORMS/ev3dev via the Alexa Gadgets Toolkit requires a fairly diverse skillset: Python for the EV3 device, Node.js for the Alexa logic, and a fair heap of AWS configuration. Hopefully this simple example will allow even novice developers to voice-enable their LEGO bricks and then continue to grow and learn from there! ๐
Next Steps/Future Enhancements ๐ ๐ฎThis project was kept intentionally simple in order to be understandable - but I plan on creating additional projects beyond "v1" that leverage all of the many EV3 sensors and other capabilities - for example, theft detection and prevention, detecting coin types, and adding a motorized door to enable withdrawals - if you say the magic word! ๐ง๐ธ
Comments