MicroPython – Bringing Python to the Internet of Things – About me



MicroPython – Bringing Python to the Internet of Things – About me

0 0


micropython-talk

Micropython talk at PyGrunn 2016

On Github todayispotato / micropython-talk

MicroPython

Bringing Python to the Internet of Things

About me

I co-founded

Which is part of

We build

I am a

So, MicroPython

Who had heard of it before?

And who used it before?

For something serious?

For something that is running right now?

So you said no...

MicroPython is a lean and fast implementation of the Python 3 programming language that is optimised to run on a microcontroller.

Kickstarted in November 2013

Other things that happened in 2013

  • Edward Snowden accidentally sends some documents to the wrong email address
  • The EU thought Cyprus was a really good boy and gave it €10 billion
  • People printed a gun and called it the "Lulz Liberator"

Development

  • Original kickstarter ended April 2015
  • Code open source
  • Initially only on the pyboard, now on multiple chips

But why?

This is why

  • Existing community (that includes you)
  • Easy to learn, with powerful features
  • Good separation between int and float (unlike JS/Lua)
  • Native bitwise operations on 1's and 0's (unlike Lua)
  • Ideal for rapid prototyping
  • Bridge between web-world and IoT world
  • Lots of opportunities for optimization!
Features: * Classes, list comprehensions, generators, exceptions, ... Optimization: * Consts (like #define in C; substitute numeric value for the ID) * Function calls * Python is compiled, so compiler optimizations are possible

And why not CPython?

Mainly due to RAM usage. Examples:

  • Preallocation of 257 + 5 ints = 4k RAM
  • Method calls: led.on() creates a bound method object = 20 bytes RAM

So, what is it?

It's Python! (3.4-ish)

>>> print('Hello world!')
Hello world!

>>> with open('pygrunn.txt') as f:
>>>     f.write('Hello PyGrunn!')

>>> try:
>>>     1/0
>>> except ZeroDivisionError as e:
>>>     print("Oh, you!")
Oh, you!
                    

But not all of it

>>> import functools
Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
ImportError: no module named 'functools'
>>> import this
Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
ImportError: no module named 'this'
                    

At least:

  • sys
  • time
  • struct
  • machine - functions related to the board
  • micropython - internals

Specific ports provides specific hooks, REPL and custom modules

Oh, also async keywords

async def ping_pygrunn():
    return await ping_server('pygrunn.org')
                    

Theoretical minimum system requirements

128kb ROM / 8kb RAM (after subtracting other software)

Reference implementation: PyBoard

  • ARM 32bit Cortex M4 @ 168Mhz
  • 1Mb flash, 192kb RAM

Unix port

Around 280kb flash

External standard library

Written in Python

$ micropython -m upip install micropython-functools
$ ./micropython 
MicroPython v1.7-116-g8dd704b on 2016-04-19; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import functools
>>> dir(functools)
['__name__', 'reduce', 'partial', 'update_wrapper', '__file__', 'wraps']
                    
https://github.com/micropython/micropython-lib

How so micro?

Mainly RAM optimizations

  • Many strings predefined in ROM (led, on, read, ...)
  • Optimised method calls
  • Everything that can be in ROM, is in ROM
  • Garbage collection: Mark-and-sweep (no reference counts)

Tagged pointers

A tagged pointer is a pointer (concretely a memory address) with additional data associated with it

  • Integer - xxxx xxxx xxxx xxx1
  • String - xxxx xxxx xxxx xx10
  • Object - xxxx xxxx xxxx xx00

Hardware features

You can:

  • Disable interrupts
  • Trigger and disable GC
  • Write inline assembler
  • Emit bytecode or machine code

Inline assembler

@micropython.asm_thumb
def led_on():
    movwt(r0, stm.GPIOA)
    movw(r1, 1 << 13)
    strh(r1, [r0, stm.GPIO_BSRRL])
                        

Native code emitter

@micropython.native
def foo(self, arg):
    # code
                        

Roughly twice as fast, but larger binary

http://hinch.me.uk/html/reference/speed_python.html

Viper Is Python Embedded in realtime

@micropython.viper
def foo(self, arg: int) -> int:
    # code
                        

Now actually called Zerynth and kind of confusing

Some supported boards

The pyboard

Accerelometer, RTC, 4 LEDs, 2 switches, 30 GPIO

BBC micro:bit

16kb RAM, 256kb flash, Cortex M0 @ 16 MHz

BBC micro:bit

Supplied to 1 million school children

  • 25 LEDs
  • Two programmable buttons
  • Accelerometer & magnetometer
  • Bluetooth
  • 5 GPIO

Comes with:

  • Online Python editor
  • Graphical drag-and-drop editor
  • Mobile app to upload code
  • Tons of documentations, teaching material, etc.

The WiPy

256kb RAM, 2Mb flash, 25 GPIO, Cortex M4 @ 80 MHz

“Small and light to fit in any cavity” ★Dual processor + WiFi radio SoC. ★MCU: Texas Instrument CC3200, Cortex-M4 @ 80MHz ★Network processor handles the WiFi connectivity and the IP stack. ★Main processor entirely free to run the user application. External libraries -Blynk: SMTP, MQTT, URLLIB, ONEWIRE, Accelerometer, Event loop MicroPython PyMakr - Pycom IDE

And LoPy

LoRa + Python

Also runs on ESP8266 chips

Current state of MicroPython

Development

  • One full time developer, two core contributors
  • Partly funded by the European Space Agency
  • Kickstart in May 2016 for proper ESP8266 sockets support
  • Feels like it's maturing
ESA: Research and development will focus on making MicroPython more robust for critical embedded systems, with emphasis on determinism of the virtual machine and memory management.

Is it production ready?

It depends on your board

But it's amazing for prototyping!

Or for embedding in games and apps

Summary

We have Python on microcontrollers.

And it works!

Thank you!

Questions?

MicroPython Bringing Python to the Internet of Things