Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Program all the things
How to develop IoT devices using
MicroPython
Anna Gerber
About me
• Senior Backend Developer at
Console Connect (Node.js)
• Teach coding and robotics
workshops and classes
• Maker...
Getting started developing IoT devices
• Design architecture & select language,
development tools/platform
• Select hardwa...
MicroPython
• Python 3.x for microcontrollers
• Features: REPL, filesystem, aims to be a
compatible subset of Cpython & co...
SELECTING HARDWARE
MicroPython Compatible Hardware
• pyboard
• TI CC3200 microcontrollers (e.g. WiPy)
• 16 bit PIC microcontrollers
• Teensy ...
Which ESP8266 dev board?
• Adafruit Feather HUZZAH ESP8266:
https://learn.adafruit.com/adafruit-
feather-huzzah-esp8266/ov...
https://learn.adafruit.com/adafruit-feather-huzzah-esp8266/pinouts
Incremental prototyping
• Design the desired interactions
• Work out what components you'll need
• Develop a basic program...
Pet Circuit
Preparing the board
• First install any USB-serial drivers required for
your board e.g. CP2104, CH341
• Install Python and...
Flash the firmware
• Download the latest version (e.g. 1.9.1) of
MicroPython from GitHub releases:
– https://github.com/mi...
Connect to the board
• Connect to REPL via serial (i.e. USB):
– Default BAUD rate is 115200
– screen /dev/tty.SLAB_USBtoUA...
Change the Wifi config
• Set the default wifi SSID and password on
startup by adding the config to main.py
import network
...
WebREPL
• You can upload / access the REPL over WiFi
• You’ll need to connect via a wired connection to
set it up:
import ...
Uploading code using the WebREPL
DEVELOPING PROGRAMS
Button on pin 12
Using machine library: read from a pin
from machine import Pin
button = Pin(12, Pin.IN, Pin.PULL_UP)
# Read the button val...
Poll button value
Analog sensors
• Only one ADC pin
• Max voltage of 1V – use a voltage divider
import machine
adc = machine.ADC(0)
adc.read...
Piezo Buzzer on pin 14
Using the machine library
from machine import Pin, PWM
import time
C5 = 523
D5 = 587
E5 = 659
piezo = PWM(Pin(14, Pin.OUT)...
Neopixels
• WS2812B light source
– Chainable, individually addressable RGB LEDs
– Many formfactors (pixel, ring, bar, stri...
Neopixel ring on pin 2
Loading additional libraries
• Many additional components supported through
third party libraries e.g. OLED display suppor...
COMMUNICATION
Basic web server (1/2)
Basic web server (2/2)
MQTT
Other options for developing with
ESP8266
• NodeMCU Lua
– http://nodemcu.com/index_en.html
• Arduino IDE
– https://github....
Why MicroPython?
• Clean, intuitive programs
• Great for teaching
• Interactive REPL ideal for experimentation &
rapid pro...
Read more
• Anna’s blog:
– http://crufti.com/
• Twitter: @AnnaGerber
• Micropython ESP8266 Quick reference:
– https://docs...
Program all the things - How to develop IoT devices using MicroPython
Program all the things - How to develop IoT devices using MicroPython
Upcoming SlideShare
Loading in …5
×

Program all the things - How to develop IoT devices using MicroPython

410 views

Published on

Presented at PyConAU, Melbourne, August 3 2017
We hear a lot of hype around the Internet of Things - how that myriad of interconnected smart devices monitoring and actuating our everyday activities will transform our lives toward a blissful state of automation. But when IoT rules the world, do you want to have to rely on third parties to provide all of these devices and to define all of the features available within your connected life? Being able to develop your own IoT devices as well as customize existing devices and applications is becoming a critical skill.

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.

In this session, we'll walk through the steps you'll need to follow to take your IoT ideas and make them into reality using MicroPython, including: • selecting hardware and developing circuits to connect sensors and actuators, • getting your microcontroller set up with MicroPython on-board, • developing MicroPython programs to read from sensors and control actuators, • connecting your device and getting it talking with other devices and services over MQTT, to integrate your device within a wider IoT context, for example, integrating with popular IoT frameworks.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Program all the things - How to develop IoT devices using MicroPython

  1. 1. Program all the things How to develop IoT devices using MicroPython Anna Gerber
  2. 2. About me • Senior Backend Developer at Console Connect (Node.js) • Teach coding and robotics workshops and classes • Maker / hobbyist Pet project
  3. 3. Getting started developing IoT devices • Design architecture & select language, development tools/platform • Select hardware (sensors, actuators, dev board) • Develop programs to read from sensors & control actuators • Develop programs to communicate with other devices or services that process / analyze data
  4. 4. MicroPython • Python 3.x for microcontrollers • 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
  5. 5. SELECTING HARDWARE
  6. 6. 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)
  7. 7. Which ESP8266 dev board? • Adafruit Feather HUZZAH ESP8266: https://learn.adafruit.com/adafruit- feather-huzzah-esp8266/overview • WeMos D1 mini: https://wiki.wemos.cc/products:d1:d1 _mini • NodeMCU: http://nodemcu.com/index_en.html • 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 • Feature Huzzah has built in 100mA LiPo charger
  8. 8. https://learn.adafruit.com/adafruit-feather-huzzah-esp8266/pinouts
  9. 9. Incremental prototyping • Design the desired interactions • Work out what components you'll need • Develop a basic program for each individual component (e.g. read from button , play sound on buzzer, display pattern on LEDs) • Develop networking / communication • Use REPL to interactively tweak timings / behaviour
  10. 10. Pet Circuit
  11. 11. Preparing the board • 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
  12. 12. 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
  13. 13. 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
  14. 14. 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
  15. 15. 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/
  16. 16. Uploading code using the WebREPL
  17. 17. DEVELOPING PROGRAMS
  18. 18. Button on pin 12
  19. 19. Using machine library: read from a pin from machine import Pin button = Pin(12, Pin.IN, Pin.PULL_UP) # Read the button value: button.value()
  20. 20. Poll button value
  21. 21. Analog sensors • Only one ADC pin • Max voltage of 1V – use a voltage divider import machine adc = machine.ADC(0) adc.read()
  22. 22. Piezo Buzzer on pin 14
  23. 23. Using the machine library from machine import Pin, PWM import time C5 = 523 D5 = 587 E5 = 659 piezo = PWM(Pin(14, Pin.OUT), freq=440, duty=512) tune = [E5, D5, C5, D5, E5, E5, E5] for tone in tune: piezo.freq(tone) time.sleep(0.5) piezo.deinit()
  24. 24. 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
  25. 25. Neopixel ring on pin 2
  26. 26. Loading additional libraries • Many additional components supported through third party libraries e.g. OLED display supported by SSD1306: https://github.com/adafruit/micropython- adafruit-ssd1306 – Download the mpy file from releases – Upload to board using ampy or WebREPL ampy --port /dev/tty.SLAB_USBtoUART put ssd1306.mpy import ssd1306import machine i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4)) oled = ssd1306.SSD1306_I2C(128, 64, i2c)
  27. 27. COMMUNICATION
  28. 28. Basic web server (1/2)
  29. 29. Basic web server (2/2)
  30. 30. MQTT
  31. 31. 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
  32. 32. Why MicroPython? • Clean, intuitive programs • Great for teaching • Interactive REPL ideal for experimentation & rapid prototyping • Edge computing
  33. 33. Read more • Anna’s blog: – http://crufti.com/ • Twitter: @AnnaGerber • Micropython ESP8266 Quick reference: – https://docs.micropython.org/en/latest/esp8266/ esp8266/quickref.html

×