Introduction to MicroPython on the ESP8266

One way you can get the most out of your ESP8266 is by using MicroPython. Also, the ESP8266 module is one of the best platforms on which to learn how to use MicroPython. This is because the ESP8266 provides simple GPIO pin control functions as well as wireless functionality, allowing you to test all aspects of the MicroPython programming language.

MicroPython & the ESP8266

MicroPython is an implementation of the Python 3 programming language that features a small portion of the Python standard library. It is designed to be lightweight and more efficient in order to run effectively on microcontrollers and other environments that have limited processing power and storage space.

Despite being lean, MicroPython has many advanced features, such as an interactive prompt, exception handling, arbitrary precision integers, list comprehension, and closures, among several other features. You can access and run all these features in devices that have less than 256 KB flash memory and 16 KB RAM. This way you are able maximize the functionality of your projects.

MicroPython is designed to be compatible with normal Python as much as possible. It has a complete Python compiler and runtime, and provides an interactive prompt known as REPL that executes commands immediately. MicroPython also provides the ability to import and execute scripts saved in the built-in filesystem. If you know how to code using Python, you will automatically be able to code using MicroPython.

MicroPython is designed to support many microcontroller architectures. Some of the most notable architectures include Xtensa, ARM, x86m ARM Thumb, and x86-64. This makes it easy to move firmware between different microcontrollers without having to overhaul the whole code.

The ESP8266 chip is popular in the open source development industry. There are many development boards from different manufacturers that use the ESP8266 chip. MicroPython has been designed to provide a generic port that can run on most of those boards, with as few limitations as possible. The port is based on the Adafruit Feather HUZZAH board. When using other ESP8266 boards, make sure you check their schematics and datasheets so that you can identify the differences between them and the Adafruit Feather HUZZAH board. That way, you can accommodate the differences in your code.

The MicroPython ESP8266 port follows several design rules and considerations. They include:

  • GPIO pin numbers are based on the ESP8266 chip pin numbering. To make sure your board conforms to this, get its pin diagram and check how its pins correspond to the ESP8266 pin numbers. If the pin numbers do not match, note the differences so that you can account for them when writing your code.
  • All relevant pins that are dedicated to a specific function, such as the pins connected to the SPI flash memory, are supported by MicroPython. Some boards will not expose these pins. That is why you should confirm your board's pin configuration before proceeding.
  • Not all boards have external pins or internal connectivity that supports the ESP8266 deep sleep mode.

When programming the ESP8266 using MicroPython, it is important for you to remember that the chip has scarce runtime resources. For instance, you will only have about 32 KB of RAM to use. Therefore, avoid creating big container objects and buffers. Moreover, since there is no software that manages the on-chip resources, it is up to you to make sure that you close any open files as soon as you finish using them.

The booting process involves several actions. It starts by executing the boot.py script from the internal modules that had been frozen. The filesystem gets mounted in the flash memory. If the filesystem is not available, the MicroPython ESP8266 port does the initial setup of the module and creates the filesystem.

After the successful mounting of the filesystem, the boot.py script is executed. This starts the WebREPL daemon. Once that is done, the main.py script gets executed from the filesystem. This file starts the user application every time the ESP8266 boots up.

The ESP8266 has a Real-Time Clock (RTC). However, it is, limited to tracking time up to 7:45 hrs. If you want to use the RTC to track long periods of time, you will have to call the time() and localtime() functions every seven hours.

See also

Now that you have an idea of what MicroPython is you should be itching to create your first program with it. Proceed to the next recipe to learn a few of the basics you need when programming using MicroPython.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.97.157