Date post: | 29-Jan-2018 |
Category: |
Technology |
Upload: | anna-gerber |
View: | 664 times |
Download: | 5 times |
Get started with IoT development using MicroPython
Anna Gerber
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
Compatible Hardware
• pyboard
• TI CC3200 microcontrollers (e.g. WiPy)
• 16 bit PIC microcontrollers
• Teensy 3.1
• Unix (for x86/x64/ARM/MIPS)
• ESP8266
• ESP32 (experimental)
Which ESP8266 dev board?
• Adafruit Feather HUZZAH ESP8266
• WeMos D1 mini• NodeMCU
• Features:• WiFi• 4MB flash• 80MHz processor• 3.3V logic• Up to 11 digital I/O pins, 1 analog I/O
pin• Support for (software) I2C, SPI
Adafruit Feather HUZZAH ESP8266
• Built-in 100mA LiPo charger
• Fantastic docs and tutorials
• https://learn.adafruit.com/adafruit-feather-huzzah-esp8266/overview
https://learn.adafruit.com/adafruit-feather-huzzah-esp8266/pinouts
WeMos D1 mini
• Low cost
• https://wiki.wemos.cc/products:d1:d1_mini
NodeMCU DevKitv1.0
• Open Source Hardware
• http://nodemcu.com/index_en.html
Preparation
• First install any USB-serial drivers required for your board e.g. CP2104, CH341
• Install Python and esptoolpip install esptool
https://github.com/espressif/esptool
• Erase the flash of the microcontroller if youhave used it before:esptool.py --port /dev/tty.SLAB_USBtoUART erase_flash
Flash the firmware
• Download the latest version (e.g. 1.9.1) of MicroPython from GitHub releases:
– https://github.com/micropython/micropython/releases
• 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
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/ampyampy --port /dev/tty.SLAB_USBtoUART put demo.py
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 webreplwebrepl.start()
• Join the device’s adhoc Wifi network: default password is micropythoN– http://micropython.org/webrepl/
Using the machine library
from machine import Pin
led = Pin(13, Pin.OUT)
led.on()
led.off()
Connect an LED
Read from a pin
from machine import Pin
button = Pin(12, Pin.IN, Pin.PULL_UP)
# Read the button value:
button.value()
Read from a button
Button program
Uploading code using the WebREPL
Analog sensors
• Only one ADC pin
• Max voltage of 1V – use a voltage divider
import machine
adc = machine.ADC(0)adc.read()
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
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
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
Read more
• Anna’s blog:
– http://crufti.com/
• Twitter: @AnnaGerber
• Micropython ESP8266 Quick reference:
– https://docs.micropython.org/en/latest/esp8266/esp8266/quickref.html