This article is an introduction to running MicroPython on ESP8266, a brief assessment of the pros and cons of this development approach while exploring what MicroPython has in mind.

The development environment

  • ESP8266 Development Board: ESP-Launcher (4 MB Flash)
  • Operating system: Win10 64-bit

About MicroPython

MicroPython is a compact and efficient programming language based on Python 3 that includes a small subset of the Python standard library and is optimized for microcontrollers and limited environments.

Simple evaluation

According to the official quick Reference description, MicroPython has implemented ESP8266 basic hardware resource interfaces, including GPIO, PWM, ADC, soft SPI and hard SPI, IIC, Deep sleep mode, OneWire, etc. With APA102, DHT, and DS18B20 libraries. So using MicroPython you can quickly prototype products such as smart sockets, temperature and humidity detection, smart RGB dimmers, etc.

In addition, you can run the program by uploading the PY file. Debugging and modifying the code do not need to be re-burned as SDK development does. Of course, the computing speed is certainly a little slower than native SDK development, as long as it is sufficient for developers. Unfortunately, there is no support for SmartConfig (fast connection), hardware IR (infrared), Sniffer, etc.

Run MicroPython on ESP8266

Download the firmware

First go to the MicroPython official website to download the MicroPython firmware based on ESP8266. The latest stable version is ESP8266-20161110-v1.8.6.bin.

The firmware is about 553KB in size, which is more than enough for a 4MB Flash ESP8266, with 96KB of memory available (some for the system).

Burning the firmware

Burn the firmware to 0x0.

Write configuration reference:

Print on electricity

Open the common serial port tool, set the baud rate to 115200, open the serial port, and restart and power on the ESP8266. If the following characters are displayed, the firmware is successfully burned:

MicroPython v1.8.6-7 - gefd0927 on the 2016-11-10; ESP module with ESP8266 Type "help()" for more information. >>>Copy the code

Power-on printing reference:

Of course, using this serial port tool is not easy to debug, we can use the PuTTY tool. Disable the serial port when switching to PuTTY.

The PuTTY is powered on

completing

From there, the basic ESP8266 MicroPython environment is set up.

Connect the WiFi

Enter a value in the PuTTY session window

>>>help()
Copy the code

The printed message will tell you how to connect to WiFi:

import network
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.scan()                             # Scan for available access points
sta_if.connect("<AP_name>", "<password>") # Connect to an AP
sta_if.isconnected()                      # Check for successful connection
Copy the code

Use WebREPL to upload files

Webrepl client

Webrepl is an official file management tool provided by MicroPython. There is also a WebrePL client tool that allows you to access ESP8266 through a browser.

First we configure webrepl by entering import webrepl_setup in the session window of ESP8266:

>>>import webrepl_setup
Copy the code

Then enter the password as prompted. The password must be at least three characters long. Then enter the following command to start WebREPL.

>>>import webrepl
>>>webrepl.start()
Copy the code

Download the WebREPL client from Github Open or access the MicroPython online client to prepare for uploading and downloading files.

After the ESP8266 is connected to the WiFi, enter sta_if.ifconfig() to view the connection information. The first IP in the tuple returned is the IP address assigned by the wireless router to ESP8266.

If your computer is on the same LAN as ESP8266, change the address that WebREPL wants to Connect to to the IP of ESP8266 and click “Connect” to return “Welcome to MicroPython!” If the connection is successful, enter the password as prompted (the password is not displayed by default). If WebREPL Connected is displayed, the login is successful.

Then you can use this client to upload and download files.

Set automatic WiFi connection on power-on

MicroPython automatically executes the main.py file after initialization, so we just need to set this file to automatically connect to WiFi after power-on. Open your usual editor, enter the following code, and save it as a main.py file:

# main.py
import network
import webrepl

SSID = "SSID"
PASSWORD = "password"

def do_connect() :
    import network
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        print('connecting to network... ')
        wlan.connect(SSID, PASSWORD)
        
    start = utime.time()
    while not wlan.isconnected():
        utime.sleep(1)
        if utime.time()-start > 5:
            print("connect timeout!")
            break
            
    if wlan.isconnected():
        print('network config:', wlan.ifconfig())

do_connect()
Copy the code

Then upload the file through the WebREPL client. After that, try to restart ESP8266 to see if the session window of ESP8266 printed user init! If it prints successfully, the above code does execute after power-on. In addition, after the ESP8266 is restarted, the WebREPL client needs to be connected again.

conclusion

This article briefly describes how to download and write the firmware, and how to use it. Finally, it describes how to upload code to the ESP8266 and configure automatic WiFi connection on power-on.

When I have time I will write another tutorial on the MQTT client for MicroPython based on ESP8266. It’s actually much easier to use MQTT in Python than it is in C; the harder part is how to configure an available environment.

tip

View the file list in the home directory

You can use the os.listdir() command to view the files in your home directory, which can then be downloaded under webrepl.

>>> import os
>>> os.listdir()
['boot.py', 'webrepl_cfg.py', 'main.py']
Copy the code

Viewing Memory Resources

Because MCU development should always pay attention to memory resources, the following is an example of viewing memory resources:

>>> import micropython
>>> micropython.mem_info()
stack: 2144 out of 8192
GC: total: 36288, used: 9488, free: 26800
 No. of 1-blocks: 60, 2-blocks: 18, max blk sz: 263, max free sz: 1287
Copy the code

changelog:

  • 2018-01-22 update: for “the problem of starting webrepl”, try this tool to write.
  • Based on the 2017-05-11 update: ESP8266 MicroPython MQTT client tutorial has finished, link: blog.csdn.net/yannanxiu/a…