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.
The Codex of Business Writing Software for Real-World Solutions 2.pptx
Program all the things - How to develop IoT devices using MicroPython
1. Program all the things
How to develop IoT devices using
MicroPython
Anna Gerber
2. About me
• Senior Backend Developer at
Console Connect (Node.js)
• Teach coding and robotics
workshops and classes
• Maker / hobbyist
Pet project
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. 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
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
12. 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
13. 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
14. 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
15. 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
16. 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/
24. 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()
25. 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
28. 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)
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.
About me:I teach weekly kids coding/robotics classes and I used MicroPython successfully with my students last year. I have also taught introductory IoT classes using Arduino (C++) and Node.js for both kids and adults
The context of my talk is a particular project I've been building (it's a healthy habits tracking "pet", built with ESP8266 that is designed to motivate me to develop healthier lifestyle habits. custom IoT device acts as a personal assistant that helps to track and transform personal habits like keeping fit.
So my talk is fairly applied - it's a walk through of how I built the project and why I think MicroPython is great for rapidly prototyping IoT projects.
The steps I will cover
The first two will depend on you requirements
In my pet project : Single device communicating to cloud service over MQTT
What is MicroPython?MicroPython is a Free, Open Source implementation of Python 3.x that is designed to run in constrained environment like 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.
256K code space, 16K memory
Compiler runtime
high level tips on selecting hardware / architecture / IoT networking technologies etc based on project requirements and why I chose the ones I did for my project
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)
Excellent docs
Open source libraries & Adafruit tutorials
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
Requirements: remind me to do exercise (lights, sound)
Allow me to record when I do it in response (button)
Main.py is the program that runs after boot.py
Use Pulse Width Modulation to create different tones with buzzer