Top Banner
Get started with IoT development using MicroPython Anna Gerber
26

Getting started with IoT development using MicroPython

Jan 29, 2018

Download

Technology

Anna Gerber
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Getting started with IoT development using MicroPython

Get started with IoT development using MicroPython

Anna Gerber

Page 2: Getting started with IoT development using MicroPython

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

Page 3: Getting started with IoT development using 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)

Page 4: Getting started with IoT development using MicroPython

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

Page 5: Getting started with IoT development using MicroPython

Adafruit Feather HUZZAH ESP8266

• Built-in 100mA LiPo charger

• Fantastic docs and tutorials

• https://learn.adafruit.com/adafruit-feather-huzzah-esp8266/overview

Page 6: Getting started with IoT development using MicroPython

https://learn.adafruit.com/adafruit-feather-huzzah-esp8266/pinouts

Page 7: Getting started with IoT development using MicroPython

WeMos D1 mini

• Low cost

• https://wiki.wemos.cc/products:d1:d1_mini

Page 8: Getting started with IoT development using MicroPython

NodeMCU DevKitv1.0

• Open Source Hardware

• http://nodemcu.com/index_en.html

Page 9: Getting started with IoT development using MicroPython

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

Page 10: Getting started with IoT development using MicroPython

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

Page 11: Getting started with IoT development using MicroPython

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

Page 12: Getting started with IoT development using MicroPython

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/

Page 13: Getting started with IoT development using MicroPython

Using the machine library

from machine import Pin

led = Pin(13, Pin.OUT)

led.on()

led.off()

Page 14: Getting started with IoT development using MicroPython

Connect an LED

Page 15: Getting started with IoT development using MicroPython

Read from a pin

from machine import Pin

button = Pin(12, Pin.IN, Pin.PULL_UP)

# Read the button value:

button.value()

Page 16: Getting started with IoT development using MicroPython

Read from a button

Page 17: Getting started with IoT development using MicroPython

Button program

Page 18: Getting started with IoT development using MicroPython

Uploading code using the WebREPL

Page 19: Getting started with IoT development using MicroPython

Analog sensors

• Only one ADC pin

• Max voltage of 1V – use a voltage divider

import machine

adc = machine.ADC(0)adc.read()

Page 20: Getting started with IoT development using MicroPython

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

Page 21: Getting started with IoT development using MicroPython

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

Page 22: Getting started with IoT development using MicroPython
Page 23: Getting started with IoT development using MicroPython
Page 24: Getting started with IoT development using MicroPython
Page 25: Getting started with IoT development using MicroPython

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

Page 26: Getting started with IoT development using MicroPython

Read more

• Anna’s blog:

– http://crufti.com/

• Twitter: @AnnaGerber

• Micropython ESP8266 Quick reference:

– https://docs.micropython.org/en/latest/esp8266/esp8266/quickref.html