Presented at the Brisbane and Gold Coast IoT meetup, July 2017.
MicroPython is a Free, Open Source implementation of Python 3.x that is designed to run on microcontrollers - the tiny embedded computers at the heart of many IoT devices. MicroPython is a valuable tool for rapidly prototyping device functionality and is a great way to get started with developing for the Internet of Things.
This presentation walks through the steps you'll need to follow to take your IoT ideas and make them into reality using MicroPython on ESP8266, including getting your microcontroller set up with MicroPython on-board and developing basic MicroPython programs to read from sensors and control actuators.
2. MicroPython
• Python 3.x for microcontrollers
• Runs in 256k and 16k of RAM
• Features: REPL, filesystem, aims to be a
compatible subset of Cpython & core libraries
• machine library for hardware / GPIO
• Open Source: MIT license
• https://github.com/micropython/micropython
9. Preparation
• First install any USB-serial drivers required for
your board e.g. CP2104, CH341
• Install Python and esptool
pip install esptool
https://github.com/espressif/esptool
• Erase the flash of the microcontroller if you
have used it before:
esptool.py --port /dev/tty.SLAB_USBtoUART erase_flash
10. Flash the firmware
• Download the latest version (e.g. 1.9.1) of
MicroPython from GitHub releases:
– https://github.com/micropython/micropython/rel
eases
• Use esptool to upload the firmware file:
• esptool.py --port /dev/tty.SLAB_USBtoUART --baud
460800 write_flash --flash_size=detect 0
~/Downloads/esp8266-20170612-v1.9.1.bin
11. Connect to the board
• Connect to REPL via serial (i.e. USB):
– Default BAUD rate is 115200
– screen /dev/tty.SLAB_USBtoUART 115200
• Hit control-e to enter paste mode (to paste
longer programs)
• You can upload / manage the python program
files on the board using ampy:
– https://github.com/adafruit/ampy
ampy --port /dev/tty.SLAB_USBtoUART put demo.py
12. WebREPL
• You can upload / access the REPL over WiFi
• You’ll need to connect via a wired connection to
set it up:
import webrepl_setup
• You can enable it on boot or start it:
import webrepl
webrepl.start()
• Join the device’s adhoc Wifi network: default
password is micropythoN
– http://micropython.org/webrepl/
13. Using the machine library
from machine import Pin
led = Pin(13, Pin.OUT)
led.on()
led.off()
19. Analog sensors
• Only one ADC pin
• Max voltage of 1V – use a voltage divider
import machine
adc = machine.ADC(0)
adc.read()
20. Change the Wifi config
• Set the default wifi SSID and password on
startup by adding the config to main.py
import network
ap=network.WLAN(network.AP_IF)
ap.config(essid="mycustomssid", password="mypassword")
• upload the main.py file
• restart the microcontroller
21. Neopixels
• WS2812B light source
– Chainable, individually addressable RGB LEDs
– Many formfactors (pixel, ring, bar, strip etc)
– Each unit is connected via 3.3V, GND and data pin
– Use external power if working with more than a
few LEDs
• See https://learn.adafruit.com/adafruit-
neopixel-uberguide/overview
22.
23.
24.
25. Other options for developing with
ESP8266
• NodeMCU Lua
– http://nodemcu.com/index_en.html
• Arduino IDE
– https://github.com/esp8266/Arduino
• Espruino JS
– https://www.espruino.com/EspruinoESP8266
Runs on microcontrollers and other constrained environments
256K code space
Compiler runtime
Which microcontrollers run micropython?
These ports have been developed
Kickstarter in 2013 produced pyboard
Kickstarter early 2016 for official ESP8266 support (on boards like Adafruit feather HUZZAH)
Oficially supported by MicroPython kickstarter
Only 1 ADC pin so use an exernal ADC if you need lots of analog inputs
SPI I2C – communication protocols used to attach peripheral devices like sensors
– same GPIO pins as Arduino for compatibilitiy – it’s software I2C so you can use any pins
Designed to use with the NodeMCU firmware for programming with Lua