Programando para IoT con Micropython

@woakas CTO at Ubidots

PyConAr 2017

Gustavo Angulo

  • I'm Colombian
  • 40 coffes per week
  • Co-Founder - CTO - Ubidots.
  • Hacker.
  • Musician.

Noviembre 13, 2013

What? Why Python?

Ready

Each module is either written from scratch or ported from CPython.

The board

  • Based on the STM32F405 microcontroller(32 bit ARM Cortex M4)
  • CPU 168MHz
  • 1MiB flash
  • 192KiB RAM

Differences to CPython

  • MicroPython does not implement complete CPython
  • MicroPython implements only subset of functionality and parameters of particular functions and classes.
  • print() function does not check for recursive data structures in the same way CPython does.

Libraries

  • asyncio
  • calendar
  • datetime
  • functools
  • gzip
  • mqtt
  • ....
  • ....

Examples

Subtitle

import machine
import time

pin = machine.Pin(2, machine.Pin.OUT)
for i in range(10):
    pin.on()
    time.sleep(1)
    pin.off()
    time.sleep(1)


Hello World

REPL (Read-Eval-Print-Loop)

Eval

import time
import machine

led = machine.Pin(2, machine.Pin.OUT)

eval("led.on()")

Save program


a = """import time
from machine import Pin
led = Pin(2, mode=Pin.OUT, value=0)
for i in range(10):
    led.on()
    time.sleep(0.1)
    led.off()
    time.sleep(0.1)
"""

with open('main.py', 'w') as f:
    f.write(a)

Threads

import _thread
import time

def th_func(delay, id):
    while True:
        time.sleep(delay)
        print('Running thread %d' % id)

for i in range(3):
    _thread.start_new_thread(th_func, (i + 1, i))

Weeeeee

Asyncio

import uasyncio
import uasyncio as asyncio
loop = asyncio.get_event_loop()

async def bar():
    count = 0
    while True:
        count += 1
        print(count)
        await asyncio.sleep(1)



loop.create_task(bar())
loop.run_forever()

Asyncio

Micropython-lib

  • uasyncio
  • pystone
  • pickle
  • ....
  • ....
  • pyb
  • random
  • smtplib
  • ....
  • ....

IoT

Micropython

References

Made with Slides.com