Getting Started with MicroPython

Nick Moore
nick@mnemote.com
@mnemote

MicroPython

micropython.org

MicroPython is an MIT-licensed Python ~3.4 for ...

Created by Damien George ...

ESP8266 / ESP32

Espressif ESP8266

ArduinoESP8266ESP32RPi 0 W
AVR
ATMega328P
Tensilica
Xtensa LX106
Tensilica
Xtensa LX6
ARM
BCM 2835
8 bit32 bit32 bit32 bit
1 core1 core2 core
plus ULP
1 core
plus GPU
20 MHz80-160 MHz240 MHz1 GHz
2 KB RAM160 KB RAM520 KB RAM512 MB RAM
32 KB Flash4 MB Flash16 MB FlashMicroSD

Espressif ESP32

ArduinoESP8266ESP32RPi 0 W
AVR
ATMega328P
Tensilica
Xtensa LX106
Tensilica
Xtensa LX6
ARM
BCM 2835
8 bit32 bit32 bit32 bit
1 core1 core2 core
plus ULP
1 core
plus GPU
20 MHz80-160 MHz240 MHz1 GHz
2 KB RAM160 KB RAM520 KB RAM512 MB RAM
32 KB Flash4 MB Flash16 MB FlashMicroSD

original image: zeptobars.com

original image: zeptobars.com

original image: zeptobars.com

ESP8266 / ESP32 Modules

ESP-WROOM-32

original images: Random Nerd Tutorials / itead.cc / espressif.com

ESP8266 Boards

Witty Cloud / NodeMCU / WeMos D1

ESP32 Boards

ESP32-DevKitC / Sparkfun ESP32 Thing / AdaFruit Huzzah / WeMos LoLin32 Lite


original images: espressif.com and sparkfun.com and adafruit.com and wemos.cc

Using MicroPython 1 — Rocket Surgery

http://nick.zoic.org/art/rocket-surgery-airborne-iot-telemetry-buzzconf/ https://buzzconf.io/sessions/airborne-iot-build-a-rocket/

Using MicroPython 2 — LoliBot

http://www.openhardwareconf.org/wiki/OHMC2018

Getting Started with MicroPython

https://github.com/nickzoic/mpy-tut/
Time Activity
10:45 Introduction (Nick Moore / Damien George)
10:55 Hands on: Installing, the REPL, I/O
11:30 Short Break
11:40 Hands on: Networks, Libraries, Building from source
12:15 Developing on MicroPython (Nick Moore)
12:25 Lunch / More hacking

Developing MicroPython

image from iconsplace.com

MicroPython Repo Files

ACKNOWLEDGEMENTS    drivers   LICENSE    py
CODECONVENTIONS.md  examples  logo       README.md
CONTRIBUTING.md     extmod    mpy-cross  tests
docs                lib       ports      tools

MicroPython Repo — Files (Docs)

ACKNOWLEDGEMENTS    drivers   LICENSE    py
CODECONVENTIONS.md  examples  logo       README.md
CONTRIBUTING.md     extmod    mpy-cross  tests
docs                lib       ports      tools

Documentation, Examples, Tests

MicroPython Repo — Files (Shared)

ACKNOWLEDGEMENTS    drivers   LICENSE    py
CODECONVENTIONS.md  examples  logo       README.md
CONTRIBUTING.md     extmod    mpy-cross  tests
docs                lib       ports      tools

Shared between ports

MicroPython Repo — Files (Ports)

ACKNOWLEDGEMENTS    drivers   LICENSE    py
CODECONVENTIONS.md  examples  logo       README.md
CONTRIBUTING.md     extmod    mpy-cross  tests
docs                lib       ports      tools

Ports to specific platforms

ports/ — Files (Ports)

bare-arm/  esp32/    minimal/   qemu-arm/  teensy/  windows/
cc3200/    esp8266/  pic16bit/  stm32/     unix/    zephyr/

ports/esp32 — Files

build/                  machine_uart.c  moduos.c
esp32.custom_common.ld  machine_wdt.c   modutime.c
espneopixel.c           main.c          mpconfigport.h
esponewire.c            Makefile        mphalport.c
fatfs_port.c            makeimg.py      mphalport.h
gccollect.c             memory.h        mpthreadport.c
gccollect.h             modesp.c        mpthreadport.h
help.c                  modesp.h        network_lan.c
machine_adc.c           modmachine.c    partitions.csv
machine_dac.c           modmachine.h    qstrdefsport.h
machine_hw_spi.c        modnetwork.c    README.md
machine_pin.c           modnetwork.h    sdkconfig.h
machine_pwm.c           modsocket.c     uart.c
machine_timer.c         moduhashlib.c   uart.h
machine_touchpad.c      modules/

How do C modules work? — 1

>>> import esp
>>> dir(esp)
['__name__', 'flash_read', 'flash_write', 'flash_erase',
 'flash_size', 'flash_user_start']
>>> esp.flash_size()
4194304

How do C modules work? — 2

esp-idf/components/spi_flash/include/esp_spi_flash.h

/**
 * @brief  Get flash chip size, as set in binary image header
 *
 * @note This value does not necessarily match real flash size.
 *
 * @return size of flash chip, in bytes
 */
size_t spi_flash_get_chip_size();

How do C modules work? — 3

ports/esp32/modesp.c

#include "esp_spi_flash.h"

STATIC mp_obj_t esp_flash_size(void) {
    return mp_obj_new_int_from_uint(spi_flash_get_chip_size());
}

How do C modules work? — 3

ports/esp32/modesp.c

#include "esp_spi_flash.h"

STATIC mp_obj_t esp_flash_size(void) {
    return mp_obj_new_int_from_uint(spi_flash_get_chip_size());
}

How do C modules work? — 4

ports/esp32/modesp.c

#include "esp_spi_flash.h"

STATIC mp_obj_t esp_flash_size(void) {
    return mp_obj_new_int_from_uint(spi_flash_get_chip_size());
}

How do C modules work? — 5

ports/esp32/modesp.c

#include "esp_spi_flash.h"

STATIC mp_obj_t esp_flash_size(void) {
    return mp_obj_new_int_from_uint(spi_flash_get_chip_size());
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_flash_size_obj, esp_flash_size);

How do C modules work? — 6

ports/esp32/modesp.c

#include "esp_spi_flash.h"

STATIC mp_obj_t esp_flash_size(void) {
    return mp_obj_new_int_from_uint(spi_flash_get_chip_size());
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_flash_size_obj, esp_flash_size);

STATIC const mp_rom_map_elem_t esp_module_globals_table[] = {
    { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_esp) },
    { MP_ROM_QSTR(MP_QSTR_flash_size), MP_ROM_PTR(&esp_flash_size_obj) },
};
    

How do C modules work? — 7

ports/esp32/modesp.c

#include "esp_spi_flash.h"

STATIC mp_obj_t esp_flash_size(void) {
    return mp_obj_new_int_from_uint(spi_flash_get_chip_size());
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_flash_size_obj, esp_flash_size);

STATIC const mp_rom_map_elem_t esp_module_globals_table[] = {
    { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_esp) },
    { MP_ROM_QSTR(MP_QSTR_flash_size), MP_ROM_PTR(&esp_flash_size_obj) },
};
STATIC MP_DEFINE_CONST_DICT(esp_module_globals, esp_module_globals_table);

const mp_obj_module_t esp_module = {
    .base = { &mp_type_module },
    .globals = (mp_obj_dict_t*)&esp_module_globals,
};

How do C modules work? — 8

ports/esp32/mpconfigport.h

extern const struct _mp_obj_module_t esp_module;

#define MICROPY_PORT_BUILTIN_MODULES \
    { MP_OBJ_NEW_QSTR(MP_QSTR_esp), (mp_obj_t)&esp_module }, \

esp32/Makefile

SRC_C = \
        modesp.c \

How do C modules work? — 9

>>> import esp
>>> dir(esp)
['__name__', 'flash_read', 'flash_write', 'flash_erase',
 'flash_size', 'flash_user_start']
>>> esp.flash_size()
4194304

Questions?

Nick Moore
nick@mnemote.com
@mnemote