So you want to check out Micropython, but aren't sure where to begin? Confused by the many guides, boards, and instructions out there that seemingly only work for everyone but you? Want a guide to take ALL of the guesswork out of the equation for you, and show you step by step where to start?
Great - because you found one! BOOM!
MicroPython is a lean and efficient implementation of the Python 3 programming language that includes a small subset of the Python standard library and is optimised to run on microcontrollers and in constrained environments. The differences between Micropython and the regular Python 3 are so small and meaningless, that it's not really worth getting into. With that out of the way, what are we gearing up for?
With Micropython & this tutorial, we'll be able to:
- Use Python on freggin' microcontrollers (arduino devices, etc)!
- Use a python interpreter in a web browser or from a program to change code ON THE FLY with your creations, no compiling necessary!
- Connect to your device WIRELESSLY via a broadcasted SSID! WHAT!? CRAZY!
Let's get started!
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Materials Needed
To take the guesswork out of which guide to use or what instructions to use, we're going to work with a single board - the NodeMCU 12-E. We'll be referencing this board and it's instructions for the entirety of this particular instructable. If you're using a different board, I will list some other tutorials you may want to follow at the end of this guide.
Things we'll need:
Step 2: Install + Configure Python 2.7
Unfortunately, the tool we need to flash the appropriate binary on the NodeMCU 12E ESP8266 board for Micropython only works with Python 2.7.x (not the latest Python 3.x). So, we'll need to install it and make sure it works.
- Download python 2.7 here and begin the installation
- Install python somewhere easy to find. I choose root C. We'll need to access the folder to place some files and scripts later. (check screenshot)
- On the next screen, be sure to add python.exe to PATH. This will be required for later. (check screenshot)
- Once you've installed it successfully, check that it's working right by going to a command prompt (Run --> CMD, or Windows Key + R --> CMD) and typing 'python'. It should return the version installed (screenshot).
Great! Now we'll need to install the thing that will actually do the flashing - esptool.
Step 3: Install Esptool
From a command prompt, we'll need to go into our installed python 2.7 directory.
We'll need to type:
- cd /
- cd Python27 (or whatever directory you installed it to)
- cd Scripts
- pip install esptool
Check the screenshot for a visual of the commands and the output of the install.
esptool is a Python-based, open source, platform independent, utility to communicate with the ROM bootloader in Espressif ESP8266.
Step 4: Connect to Board + Install Micropython
The moment of truth has arrived!
Now we'll hook up the board and flash it with the binary to run Micropython! :-)
- Plug your USB cable into the NodeMCU board, and then into your computer.
- Go into device manager and confirm what port your device is on. We'll need this information later. I'm on COM port 3 (screenshot).
- Download the micropython ESP8266 code I've conveniently hosted for you on my github, and place it in your python/Scripts directory from earlier (screenshot).
- Place the NodeMCU board into bootloader mode by holding the flash button, pressing the RST button, then releasing the flash button. You'll see the blue light briefly flash once when you've done this (screenshot).
- Open a command prompt, and return to the scripts directory.
- Run the following command, keeping in mind to substitute COM3 with YOUR particular COM port:
esptool.py --port COM3 --baud 460800 write_flash --flash_size=detect 0 esp8266-20161110-v1.8.6.bin
Congratulations! You've successfully flashed Micropython onto a microcontroller! AWWW YESS!!!
Now it's time to access it, and enable easy web access!
Step 5: Connect Using PuTTY + WebREPL
- Unplug and plug your NodeMCU board back into USB. This should let the code compile and work properly after flashing.
- Open up PuTTY. Use a serial connection to your COM port, with a baud rate of 115200. You may want to save this for quick access later on. (check screenshot)
- Once we've got our command prompt, we should be at the interpreter. AWESOME! Now we can run and do all kinds of crazy cool python stuff right from the command line!
- Let's give ourselves the capability to control it via the web! type import webrepl_setup in order to start the WebREPL daemon on startup. type E to enable on boot, then type y to change the default password. Use something you'll remember, but strong enough to keep others from guessing. Reference the screenshot for help.
- Unplug and plug your NodeMCU board back in again.
- We can now see a new SSID being broadcast. It's our Micropython-enabled NodeMCU board! WHOA! Connecting to this right now will cause us to lose connectivity, so just make note of it. The default password is: micropythoN (capital N at the end)
- To save space on the board, the required files to connect to our web interface must be downloaded on your local machine. Go here to download the webREPL files. Extract them somewhere you can find them, then open up webrepl.html
- Go ahead and connect to the Micropython SSID with the micropythoN password. Be careful, you'll lose internet connectivity at this point! Make sure you have these instructions loaded and ready to go in another window.
- Go to your webrepl browser window you opened up previously. You should see the ws://192.168.4.1:8266/ address by default in the connect window. Click connect, and enter the password you set earlier. You now have access to your Micropython-enabled ESP8266 board via wireless! BLACK MAGIC! (screenshot)
Step 6: AWESOME IT WORKS....Now What?
You have access to an amazing microcontroller and all it's powerful sensors + actions in REAL TIME! You can access it over the web from your couch in another room.
Want to dynamically draw on LED displays? How about control a servo motor? Why not some kind of funky watch? It's all possible!
I highly recommend you check out the Micropython guides over at Adafruit. You will learn the intricacies of Micropython, as well as discover some easy projects to get you started. Most of these projects are headed by the fabulous Tony DiCola, a fantastic and prolific Micropython communicator and expert. Stop reading this - get going! :)
Step 7: Non-Specific Guides + Resources
- A guide for boards other the NodeMCU board can be found here: https://learn.adafruit.com/micropython-basics-how...
- The official Micropython.org guide to flashing your ESP8266 can be found here: https://docs.micropython.org/en/latest/esp8266/es...
- To download the binary to flash the NodeMCU 12E board back to it's original state, download this file from my github.