Moving on from Arduino to ESP32 – Part 2, MicroPython

 

ESP32 development board

ESP32 development board

In the previous post I explained the general idea behind getting an ESP32 and expanding your capabilities into the Internet of Things, or IoT.

Let’s assume you have bought your first ESP32 device. Maybe it’s a WROOM, or DevKitC, model like I have. In case you want to keep working with the Arduino IDE and use C on your ESP, this excellent blog posting by Random Nerd Tutorials will let you install the ESP32 expansion to the IDE and you’re flying in no time.

You may also have bought one of the marvellous set of Pycom boards, in which case this blog post of mine is not your best bet. See their site for instructions on which IDE to use – some people use Atom, or Visual Code from Microsoft, with a set of add-ons to form the tool chain for Pycom boards.

But if you are feeling adventurous and want to work in Python on ESP32, you need to take a little more circuitous route desc

ribed below. Python is an interpreted language in which an inerpreter causes things to happen either in your computer or in a MicroPython installation that lives on your ESP. The interpreter has many moving parts commonly called a toolchain. I went through some blog posts, mainly this very helpful one from LeMaRiva, and it helped me along very well. However, I wanted to write up a detailed how-to package so that I can remember how it all happens, if I lose my installation for some reason.

You need a set of things besides your ESP32 chip and its USB cable. Let’s see just what:

  1. A Python installation
  2. A package manager for Python, called Pip
  3. A Python language tool called Esptool
  4. A binary MicroPython file for installation on your ESP32
  5. An IDE or integrated development environment. It can be Java Eclipse, or Atom, or Microsoft Visual Code, all of which can be employed in writing and deploying your program on the ESP32, but I had issues with all of them, especially in the deployment stage. Therefore I recommend uPyCraft, despite its obvious flaws in user interface and language, because it gets the job done.
  6. A terminal program such as PuTTY can be useful in confirming the communication between your computer and the ESP32.

And after you have installed everything, you have an IDE for writing your program, and MicroPython sitting on the ESP32, doing your bidding. It’s a little like Aladdin’s lamp – rub it and your wishes get done. So, let’s get started.

Installing Python and Pip

Python is an open source programming language and it’s getting more users every day. It’s a very nice, readable, and easy to understand language, while giving you lots of power to work with. The current version is 3.7, but I took the one below it, 3.6.0. New versions appear at a regular pace. This is the path to installation.

  • Go to https://www.python.org/downloads/release/python-360/ and locate the version suitable for your computer. Mine is a 64-bit Windows 10 machine, so I took the one that says “Windows x86-64 executable installer”
  • Run the installer
  • There are three things to check. When you are in the dialog box seen below, remember to check the “Add Python to path” check box. It will make life so much easier later when youc an run Python from any directory. You can click on the images to see them larger.
Python path to system path

Python path to system path

  • The other thing is the path you want to install to. The default path is quite long so I changed it to c:\python360
  • The third can be seen in the dialog box below. You will need Pip, a package manager for getting Python stuff from GitHub, so turn on “Install Pip”.
Installing Pip as you install Python

Installing Pip as you install Python

And now you’re done. After Python installation is over, you can open a command window (remember DOS? It’s still around, and for this, it’s instrumental), and work a little in that black box of arcane commands. Just hit the Window key and type cmd and hit Enter. In the black box, you can start your Python interpreter by typing python. It then looks like this

Remember DOS? It's still around!

Remember DOS? It’s still around!

The >>> indicates you are inside the Python shell. Its commands do not fall under the current project, but you can teach yourself on Python using one of the many  tutorials around.

Hit Ctrl+Z to leave the Python shell and return to your command window.

Installing Esptool

Esptool is a set of utilities used to set up your ESP32 for MicroPython, and flashing it clean if need be. To install Esptool, use Pip:

  • when in command window, and Python shell running as indicated by the >>>, type python -m pip install esptool
  • Pip goes fetching the package and delivers it to you like this:
Pip getting you Esptool

Pip getting you Esptool

  • By the way, if you see some error message about Pip being out of date or otherwise dysfunctional, you can issue this command: python get-pip.py which will get you a fresh installation of the latest version of Pip.
  • Now you’re set for using Pip to get you the binary installation package of MicroPython.

Setting up the connection to the ESP32

Now you can plug in your ESP32 with the USB cable. It should install itself without any issues. Open Device Manager by pressing the Window key and typing Device Manager. If all goes well, you should see your device with the port when you open the Device Manager:

Device Manager, note Ports with COM6

Device Manager, note Ports with COM6

If it should happen that your device does not appear in the Ports, but instead you see it appearing in the Other devices list, you need a driver for it. I am not entirely sure why that happens, but you can get the driver from Silicon Labs:

Installing a VCP driver

Installing a VCP driver

When you see your device in the Ports list, you’re ready to go forward. Note the COM port number; to confirm it, unplug the device, see the list, plug it in again, and note the newest port number.

Getting and installing MicroPython

Like Python, MicroPython is open source software. It is developed by people who donate their time and effort to get things done so you can fiddle with IoT without paying anyone anything. The site that hosts the latest version of MicroPython for ESP32 at any time is http://micropython.org/download#esp32, so just go there and find the latest binary file. Its name is something like esp32-20180927-v1.9.4-596-g8a84e08dc.bin so it’s not very intuitive, but you can just click on it to download it.

First, erase whatever is on the device now (if anything) with this command:

  • python -m esptool –port COM17 erase_flash

where the COM17 needs to be your COM port, not necessarily 17.

The next bit is going to be a little tricky.

  • Open the folder where you saved the BIN file in Windows
  • Open Notepad
  • Confirm that your ESP32 is plugged in, and you remember the port number
  • Open a command window as before
  • Prepare the following, rather complex, command by copying it to Notepad first and editing it there:
  • python -m esptool –port COM17 –baud 460800 write_flash –flash_mode dio –flash_size=detect 0x1000 <firmware_name>.bin
  • Change COM 17 to whatever number your device has
  • Change <firmware_name> to the name of your BIN file in Downloads by selecting it, pressing F2 for Rename, then copying the name of the file. It’s too complex to type correctly anyhow. DO NOT insert the <> brackets.
  • Your final command should look like this (do not copy this, but use the one from Notepad), and paste it to the Python shell prompt by right-clicking at the prompt:
  • python -m esptool –port COM8 –baud 460800 write_flash –flash_mode dio –flash_size=detect 0x1000 esp32-20180927-v1.9.4-596-g8a84e08dc.bin
  • Make sure you don’t do anything to the double dashes or any other part of the command – such meddling will result in error messages bordering on occultism
  • Press Enter to run the command

When the command runs, Esptool sometimes wants you to press a button on the device. If you see this on the screen:

 Connecting ....._____....._____....._____

you need to hold the device in your hand, pins away from you, the USB pointing down, and press the little button in the lower right corner until the process continues. Release the button at that point.

If you don’t do the button dance, you will see this:

A fatal error occurred: Failed to connect to Espressif device: Invalid head of packet (0x08)

It’s nothing fatal. Just press the Up arrow in the command window to recall the command, then be prepared to push the button at the Connecting prompt, then it will go down all right.

This is what you should see when it is finished:

Successful Esptool run

Successful Esptool run

After this, you have successfully flashed MicroPython to the ESP32, and can congratulate yourself – you’re getting to be a master of IoT.

Testing the connection with PuTTY

PuTTY is a free tool for connecting to the device. You can get it from the download page. Download it, then install it.

When you run PuTTY, it gives you this window:

PuTTY launch screen

PuTTY launch screen

Click on the Serial radio button, then edit to suit your setup for the Serial line (in my case, COM6, but in your case the one you see in Device Manager). Also, change the speed of the connection to 115200.

You can save this session if you wish by pressing Save. When you now click Open, you will see this:

PuTTY settings, note yellow fields

PuTTY settings, note yellow fields

And now, when you hit Open to actually connect to your device, you should see this window:

PuTTY getting an answer

PuTTY getting an answer

This is your device talking to you. You can see the familiar  >>> prompt on the last row of text. That means the Python shell is working and can now be used for stuff. In case your PuTTY window has garbage on the top, and not much else, your connection is too slow. Close the PuTTY window, reopen it, and double-check the speed is set at 115200. It can be more if you want, but not slower than that.

Part 3 will explain to you how to install uPyCraft through its somewhat quirky process, and then use it to do the basic Blink example right off the bat.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.