O’Reilly MicroPython Complete: The Definitive Guide to Python for Microcontrollers Using MicroPython and ESP32Luca-Dorin Anton.

ESP8266 is the ESP32 sucessor. The most relevant New Features: It combines WiFi and Bluetooth wireless capabilities and it’s dual core.

Use Expressif ESP32 board to follow along here

SOURCE REFERENCE: https://learning.oreilly.com/videos/micropython-complete-the/9781634626514/9781634626514-9781634626514_3

FURTHER REFERENCE: http://docs.micropython.org/en/latest/ (main MicroPython documents)

Here’s some examples of ESP32 boards:

Specifications

When it comes to the ESP32 chip specifications, you’ll find that:

  • The ESP32 is dual core, this means it has 2 processors.
  • It has Wi-Fi and bluetooth built-in.
  • It runs 32 bit programs.
  • The clock frequency can go up to 240MHz and it has a 512 kB RAM.
  • This particular board has 30 or 36 pins, 15 in each row.
  • It also has wide variety of peripherals available, like: capacitive touch, ADCs, DACs, UART, SPI, I2C and much more.
  • It comes with built-in hall effect sensor and built-in temperature sensor.

To learn more about the ESP32 GPIOs, read our GPIO reference guide: ESP32 Pinout Reference: Which GPIO pins should you use?

Programming Environments

The ESP32 can be programmed in different programming environments. You can use:

  • Arduino IDE
  • Espressif IDF (IoT Development Framework)
  • Micropython
  • JavaScript
  • LUA

ESP32 Pinout Guide

The ESP32 has more GPIOs with more functionalities compared to the ESP826.

With the ESP32 you can decide which pins are UART, I2C, or SPI – you just need to set that on the code. This is possible due to the ESP32 chip’s multiplexing feature that allows to assign multiple functions to the same pin. If you don’t set them on the code, the pins will be used as default – as shown in the figure below (the pin location can change depending on the manufacturer).

Version with 30 GPIOs

Version with 36 GPIOs

You can read our detailed ESP32 Pinout Reference Guide.

  • Install Python 3.7 or higher
  • Install uPyCraft

uPyCraft IDE is the easiest way of programming ESP based boards with MicroPython at the moment.

You can learn more about uPyCraft IDE on their GitHub repository or explore the uPyCraft IDE source code.

Downloading uPyCraft IDE for Windows

Click here to download uPyCraft IDE for Windows or go to this link https://randomnerdtutorials.com/uPyCraftWindows.

Flash/Upload MicroPython Firmware to ESP32

Note: After installing MicroPython firmware on your ESP32 or ESP8266, you can go back and use Arduino IDE again. You just need to upload code using Arduino IDE. Then, if you want to use MicroPython again, you need to flash MicroPython firmware.

[Part 1 – ESP32] Downloading and Flashing the MicroPython Firmware on ESP32

To download the latest version of MicroPython firmware for the ESP32, go to the MicroPython Downloads page and scroll all the way down to the ESP32 section.

You should see a similar web page (see figure below) with the latest link to download the ESP32 .bin file – for example: esp32-20181007-v1.9.4-631-g338635ccc.bin.

Note: if you’re using a different board (like a PyBoard, WiPy, or other), go to MicroPython Downloads page and download the right firmware for your board.

Selecting Serial Port

Go to Tools > Serial and select your ESP32 COM port (in our case it’s COM5).

Important: if you plug your ESP32 board to your computer, but you can’t find the ESP32 Port available in your uPyCraft IDE, it might be one of these two problems: 1. USB drivers missing or 2. USB cable without data wires.

1. If you don’t see your ESP’s COM port available, this often means you don’t have the USB drivers installed. Take a closer look at the chip next to the voltage regulator on board and check its name.

The ESP32 DEVKIT V1 DOIT board uses the CP2102 chip.

Go to Google and search for your specific chip to find the drivers and install them in your operating system.

You can download the CP2102 drivers on the Silicon Labs website.

After they are installed, restart the uPyCraft IDE and you should see the COM port in the Tools menu.

2. If you have the drivers installed, but you can’t see your device, double-check that you’re using a USB cable with data wires.

USB cables from powerbanks often don’t have data wires (they are charge only). So, your computer will never establish a serial communication with your ESP32. Using a a proper USB cable should solve your problem.

Selecting the Board

Go to Tools > Board. For this tutorial, we assume that you’re using the ESP32, so make sure you select the “esp32” option:

Flashing/Uploading MicroPython Firmware

Finally, go to Tools > BurnFirmware menu to flash your ESP32 with MicroPython.

Select all these options to flash the ESP32 board:

  • board: esp32
  • burn_addr: 0x1000
  • erase_flash: yes
  • com: COMX (in our case it’s COM5)
  • Firmware: Select “Users” and choose the ESP32 .bin file downloaded earlier

After pressing the “Choose” button, navigate to your Downloads folder and select the ESP32 .bin file:

Having all the settings selected, hold-down the “BOOT/FLASH” button in your ESP32 board:

While holding down the “BOOT/FLASH“, click the “ok” button in the burn firmware window:

When the “EraseFlash” process begins, you can release the “BOOT/FLASH” button. After a few seconds, the firmware will be flashed into your ESP32 board.

Note: if the “EraseFlash” bar doesn’t move and you see an error message saying “erase false.“, it means that your ESP32 wasn’t in flashing mode. You need to repeat all the steps described earlier and hold the “BOOT/FLASH” button again to ensure that your ESP32 goes into flashing mode.



WebREPL

WebREPL is the latest standard (in the sense of an Internet RFC) for communicating with and controlling a MicroPython-based board. Following were the requirements for the protocol design:

  1. Single connection/channel, multiplexing terminal access, filesystem access, and board control.
  2. Network ready and Web technologies ready (allowing access directly from a browser with an HTML-based client).

Based on these requirements, WebREPL uses a single connection over WebSocket as a transport protocol. Note that while WebREPL is primarily intended for network (usually, wireless) connection, due to its single-connection, multiplexed nature, the same protocol can be used over a lower-level, wired connection like UART, SPI, I2C, etc.

Few other traits of WebREPL:

  1. It is intended (whenever possible) to work in background, i.e. while WebREPL operations are executed (like a file transfer), normal REPL/user application should continue to run and be responsive (though perhaps with higher latency, as WebREPL operations may take its share of CPU time and other system resources). (Some systems may not allow such background operation, and then WebREPL access/operations will be blocking).
  2. While it’s intended to run in background, like a Unix daemon, it’s not intended to support multiple, per-connection sessions. There’s a single REPL session, and this same session is accessible via different media, like UART or WebREPL. This also means that there’s usually no point in having more than one WebREPL connection (multiple connections would access the same session), and a particular system may actually limit number of concurrent connections to ease implementation and save system resources.

WebREPL protocol consists of 2 sub-protocols:

  • Terminal protocol

This protocol is finalized and is very simple in its nature, akin to Telnet protocol. WebSocket “text”-flagged messages are used to communicate terminal input and output between a client and a WebREPL- enabled device (server). There’s a guaranteed password prompt, which can be detected by the appearance of characters ‘:’, ‘ ‘ (at this point, server expected a password ending with ‘\n’ from client). If you’re interested in developing a 3rd-party application to communicate using WebREPL terminal protocol, the information above should be enough to implement it (or feel free to study implementation of the official clients in this repository).

  • File transfer/board control protocol

This protocol uses WebSocket “binary”-flagged messages. At this point, this protocol is in early research/design/proof-of-concept phase. The only available specification of it is the reference code implementation, and the protocol is subject to frequent and incompatible changes. The webrepl_cli.py module mentioned above intended to be both a command-line tool and a library for 3rd-party projects to use, though it may not be there yet. If you’re interested in integrating WebREPL transfer/control capabilities into your application, please submit a ticket to GitHub with information about your project and how it is useful to MicroPython community, to help us prioritize this work.

While the protocol is (eventually) intended to provide full-fledged filesystem access and means to control a board (all subject to resource constraints of a deeply embedded boards it’s intended to run on), currently, only “get file” and “put file” operations are supported. As above, sharing information with us on features you miss and how they can be helpful to the general MicroPython community will help us prioritize our plans. If you’re interested in reducing wait time for new features, you’re also welcome to contribute to their implementation. Please start with discussing the design first, and with small changes and improvements. Please keep in mind that WebREPL is just one of the many features on which MicroPython developers work, so having sustainable (vs revolutionary) development process is a must to have long-term success.

https://github.com/micropython/webrepl

2. Setup WebREPL

a. Connect ESP8266 to the computer using USB to Serial converter.

b. Connect to REPL prompt over USB serial UART using any terminal program (e.g. picocom on Linux and TeraTerm on Windows).

For example

Copy

c. Run import webrepl_setup in the prompt. Follow on-screen instructions to enable WebREPL daemon and to set a desired password.

MicroPython v1.8.6-7-gefd0927 on 2016-11-10; ESP module with ESP8266
Type "help()" for more information.
>>> import webrepl_setup
WebREPL daemon auto-start status: disabled

Would you like to (E)nable or (D)isable it running on boot?
(Empty line to quit)
> E
To enable WebREPL, you must set password for it
New password: srccodes
Confirm password: srccodes
Changes will be activated after reboot
Would you like to reboot now? (y/n) y

Copy

WebREPL daemon auto-start

3. Note WIFI Access Point (AP) name

Run following micropython commands and note the WIFI access point (AP) name (e.g. MicroPython-xxxxxx). Default password is micropythoN.

MicroPython v1.8.6-7-gefd0927 on 2016-11-10; ESP module with ESP8266
Type "help()" for more information.
>>> import network;
>>> ap = network.WLAN(network.AP_IF);
>>> print(ap.config('essid'));
MicroPython-d0fa00
>>>

How to change Access Point (AP) name and password?

MicroPython v1.8.6-7-gefd0927 on 2016-11-10; ESP module with ESP8266
Type "help()" for more information.
>>> import network;
>>> ap = network.WLAN(network.AP_IF);
>>> ap.active(True);
>>> ap.config(essid='MyESP8266', authmode=network.AUTH_WPA_WPA2_PSK, password='mypassword');
>>> print(ap.config('essid'));
MyESP8

Copy

4. Install WebREPL client and run program over WIFI

a. Download the WebREPL client from https://github.com/micropython/webrepl/archive/master.zip and unzip the same.

b. Disconnect USB to Serial converter from computer.

c. Connect computer to the WIFI access point of ESP8266.
Connect the computer to the WIFI access point

d. Open webrepl.html using Chrome or Firefox browser.
webrepl.html

e. Enter WebREPL url and click ‘Connect’ button.
Open webrepl.html using Chrome or Firefox browser

f. Use the password set during webrepl_setup. Run print(‘Hello World!’); command in WebREPL remotely. It’ll print ‘Hello World!’ in the console.

Welcome to MicroPython!
Password:
WebREPL connected
>>> print('Hello World!');
Hello World!

Copy

Enter WebREPL url and click ?Connect? button. Use the password set during webrepl_setup

References

http://docs.micropython.org/en/latest/esp32/quickref.html?highlight=webrepl

Use this site to SETUP WebREPL https://www.techcoil.com/blog/how-to-setup-micropython-webrepl-on-your-esp32-development-board/

MicroPython