MicroPython | 5 min | 5574
atom.ioesp32micropythontutorialvisual studio codewemoswipy
Another quick tutorial, this time for MicroPython on ESP32/ESP8266. You can find all the information that I'm writing in this post in different articles on the Internet but I've not found anything that summarizes all the topics. That is why I am writting this post. I included some links at the end of each topic to extend the info. Feel free to visit them if you want more info, or write a comment I will try to respond it.
MicroPython & Board
Quoting the official micropython page results in the following
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.
As you see, in my last posts (
sorry about the link, I need some pagekit taxonomy!), I have been programming the Wipy 2.0. This board is a tiny MicroPython enabled WiFi & Bluetooth IoT development platform.
Other development boards/kits, that support MicroPython are the following:
ESP32 DevTool (WROOM) includes a slot for a battery. x 1 ESP32 DevkitC V4/V1 (WROOM) x 1 ESP32 TTGO (WROOM) includes a SX1276 (LoRa) and a SSD1306 (display) x 1 ESP32 (WROVER) x 1 ESP8266 x 1 WiPy 3.0 x 1
The cheapest boards are the ESP8266. The have only 160kb RAM which could be a problem (you usually get a memory error exception, when you import 3 or 4 libraries). The ESP32 WROOM has about 512kb RAM and the WROVER about 4MB. There is also a new WROVER-B that has 8MB RAM.
To download the firmware to the ESP32 microcontroller, you have to install Python. You can use either Python 2.7 or Python 3.6. Then, you need to install
pip. A tutorial for installing
pipon Windows can be found here.
pipcan be downloaded from get-pip. After that, you need to install the
esptoolsusing the following command
pip install esptool # or usually on windows python -m pip install esptool
You should get something like the Fig 1 (I typed
python3because I've the two versions (2.7 & 3.6) on Windows, so I changed the name of the executable file to
python3.exe). Remember to include the Python directory to the Windows path in the Environment Variables.
Fig. 1: Installing
You should also install
ampy. This tool is useful for uploading files to the ESP32 via serial interface. To do that, you need to type the following on a command prompt
python3 -m pip install adafruit-ampy
Firmware Download & Installation
Download the MicroPython firmware from here. You have to download the image under
Firmware for ESP32 boards. Do not try another firmware; it is not going to work.
Connect the Wemos to the PC using an USB cable, and check the serial interface number where the CP201x is connected (turn the board on, if it is not). On Windows open the
Device Managerand look under
Ports (COM & LPT). In my case, the port is
COM17. On Linux, it should be under
xto the corresponding port number). Using
dmesgyou should look for something like this:
usb 5-3: cp210x converter now attached to ttyUSB0.
Fig. 2: Windows Device Manager.
First, open a cmd/terminal. The flash of the ESP32 need to be erased. To do so, just give the command bellow on the command line:
# for Windows python -m esptool --port COM17 erase_flash # for Linux python -m esptool --port /dev/ttyUSB0 erase_flash
You need to change the
/dev/ttyUSB0to the serial port where your ESP32 is connected.
After this, we can upload the firmare (
.binfile) to the ESP32 with the command bellow.
# for Windows python -m esptool --port COM17 --baud 460800 write_flash --flash_mode dio --flash_size=detect 0x1000 <firmware_name>.bin # for Linux python -m esptool --port /dev/ttyUSB0 --baud 460800 write_flash --flash_mode dio --flash_size=detect 0x1000 <firmware_name>.bin
Again, you need to replace the port with your ESP32 COM port. In addition, you need to change the
<firmware_name>.binwith the path where your firmware file is located and the name of the file. If you get an error like Fig. 3, you should close all open connections to the board (e.g. putty, serial console etc.).
Fig. 3: Error by uploading MicroPython firmware
If everything goes right, you get some results as the one on Fig. 4.
Fig. 4: Uploading MicroPython Firmware
Programming using Atom and PyMakr plugin
Download Atom.io from here and install it. Then, search for the plugin
pymakr. To do so, go to
Packages, and then
Install Packages/Themes(see Fig. 5).
Fig. 5: Install Packages on Atom.io
pymakrto search the plugin, wait a second, and then when it comes in the plugin list, install it using the
installbutton (Fig. 6) -version 1.2.0 is available.
If everything goes right, you get a console on Atom (lower side of the right panel), that can be configured to connect to the ESP32. The console should look like Fig. 7.
Fig. 7: Atom
It is usually closed at the beginning; you need to click on the
openbutton on the right. Before that, you should click on
Settingsto configure again the port on which the Wemos board is connected. You can set a
Global settings(Fig. 8) for all your projects or a
Project settingsif you have more than one board connected to your PC and different projects running on these boards.
- Note for Windows users:
COMXXshould be written in uppercase, otherwise
pymakrdoesn't find the port and asks the DNS for that address, which usually does not exist.
Pymakr size problem - Atom 1.30:
If your console size is too small and you cannot resize it, like this one:
pymakrConsole too small and not resizable
you need to disable the
It is a bug.
ESP32 Firmware Atom.io Pymark Plugin
Visual Studio Code
I am not using Atom anymore, I am now using Visual Studio Code. Read this for more info.
Uploading files using adafruit-ampy
The MicroPython firmware for ESP32 doesn't have a FTP server as standard function as the version for the Wipy2.0. Then, you have to upload the files using the serial interface.
The(Update: Uploading the files using the PyMakr console works - read next section)
syncbutton of the
pymarkplugin on Atom doesn't work with this firmware (with the Wipy 2.0, it works).
Write the following code in a file called
<wpa2_passvalues to the right ones of your WiFi configuration
# boot.py ssid_ = <ssid> wp2_pass = <wpa2_pass> def do_connect(): import network sta_if = network.WLAN(network.STA_IF) if not sta_if.isconnected(): print('connecting to network...') sta_if.active(True) sta_if.connect(ssid_, wp2_pass) while not sta_if.isconnected(): pass print('network config:', sta_if.ifconfig()) do_connect()
You can upload the
boot.pyfile to the Wemos board using
ampywith the following command line
# for Windows ampy --port COM17 put /pathToFile/boot.py # for Linux ampy --port /dev/ttyUSB0 put /pathToFile/boot.py
You can upload a simple FTP server to upload files using e.g. Filezilla. You will find the FTP Python file on my GitHub repository, together with a
boot.pyto connect to the WIFI Router.
Uploading files using PyMakr console in Atom
With the PyMakr plugin version newer than 1.30, it is possible to upload files not only to the WiPy2.0/3.0 but also to the ESP32/ESP8266 modules. You need to open your project, configure the
Settingsas described above and click on the
Uploadbutton (see Fig. 11). The files are uploaded and after that the board is rebooted.
If you have a
mainroutine with a
while True:and you want to stop it, you can press
CTRL+Cafter clicking inside the console. The
whilewill be breaked and you can type your commands on it. This could be helpfull when the microcontroller is doing too many task, and the
Uploadfile action fails.
If you have further questions, please don't hesitate to comment this article.
Installing MicroPython dependencies
You can find info about this topic here !
Tutorial Part 2: Connecting Sensors to MicroPython!
You can find info about this topic here !
- Note for Windows users: