SlideShare a Scribd company logo
MicroPython簡介
Max Lai
自我簡介
• Max Lai
• Taichung.py共同發起人
• 靜宜大學資訊學院兼任助理教授
• 專長是電腦視覺, 敏捷開發, 目前業餘的時間嘗試研究 python 與
IOT 領域相關的 project
MicroPython
• 由劍橋大學數學科學中心的物理學家 Damien P. George 工
作之餘的side project, 將Python移植到ARM Cortex M微處理
器(STM32F405 chip).
• 2013, Damien於 KickStarter 發起 pyboard 這個專案,成功籌
得近10萬英鎊
• pyboard
• STM32F405RG: 192k RAM, 1M ROM, 168MHz, Cortex M4F.
• USB micro connector for device (and host).
• Micro SD card.
• 3-axis accelerometer (MMA7660).
• Real-time clock, 4 LEDs, 2 switches.
• 30 GPIO: symmetric pin layout, plus extra pins.
• Internal file system. ”/flash” and ”/sd”.
https://goo.gl/mhLBl7
MicroPython
• a lean and efficient implementation of the Python 3 programming
language
• that includes a small subset of the Python standard library and
• is optimised to run on microcontrollers and in constrained
environments.
http://micropython.org/
ESP8266
AVR (Arduino) ESP8266 ARM / Broadcom (RPi 2)
ATMega328P Tensilica Xtensa LX106 Cortex A53
8 bit 32 bit 32 bit
1 core 1 core 4 core
20 MHz 80-160 MHz 900 MHz
2KB RAM 160KB RAM 1GB RAM
32KB Flash DIO/QIO Flash MicroSDHC
$5
http://nick.zoic.org/pycon2016/esp8266-and-micropython/
NodeMCU v2
• 3.3v regulator
• USB to serial interface
• uses CP2102 as UART bridge
• Automatic flash & reset functions
• 採用內建 WiFi 通訊功能之 ESP8266
• 新板 ESP-12E 具備大容量 4MB Flash
• 基於 eLua 之開源專案、可自行編譯
• 第二代電路板設計適合進行麵包板實
驗
https://www.seeedstudio.com/
MicroPython
firmware
• http://micropython.org/download
/#esp8266
esptool
• 使用 NodeMCU 必須另外安裝 CP210x 驅動程式
• https://www.silabs.com/products/mcu/Pages/USBtoUARTBridgeVCPDrivers.aspx
• need Python 2.7 or newer
• depends on pySerial version 2.5 or newer for serial communication
$ pip install pyserial
$ git clone https://github.com/themadinventor/esptool
$ cd esptool
$ python esptool.py --port COM15 --baud 115200 write_flash
--flash_size=32m 0 esp8266-20161110-v1.8.6.bin
PuTTY
• COM port: 請看裝置管理員
• Baud rate: 115200
數位I/O
LED on/off
• 連上NodeMCU的 serial REPL 之後應該會出現 “>>>" 提示號
import machine
led = machine.Pin(2, machine.Pin.OUT)
led.high() #關閉LED
led.low() #點亮LED
https://goo.gl/lhlbsm
實驗電路圖
Blink LED
• 測試外接LED (LED 正極接在 pin D2), 底下的程式碼能讓LED閃爍十
遍
import machine
import time
ledD2 = machine.Pin(4, machine.Pin.OUT)
for i in range(10):
ledD2.high()
time.sleep(0.5)
ledD2.low()
time.sleep(0.5)
數位輸入-Button
import machine
button = machine.Pin(12, machine.Pin.IN, machine.Pin.PULL_UP)
while True:
if not button.value():
print('Button pressed!')
數位輸入-Button
import machine
import time
ledD2 = machine.Pin(4, machine.Pin.OUT)
button = machine.Pin(12, machine.Pin.IN, machine.Pin.PULL_UP)
while True:
first = button.value()
time.sleep(0.01)
second = button.value()
if first and not second:
print('Button pressed!')
ledD2.high()
elif not first and second:
print('Button released!')
ledD2.low()
類比I/O
PWM 調控燈光亮度實驗
• Pulse Width Modulation 簡稱PWM,譯為脈衝寬度調製,簡稱脈寬調製。
• PWM是一種對類比信號電平進行數位編碼的方法
• 由於電腦不能輸出類比電壓,只能輸出0 或5V 的的數位電壓值,
• 我們就通過使用高解析度計數器,利用方波的占空比被調製的方法來對一個具
體類比信號的電位進行編碼。
• PWM 信號仍然是數位的,因為在給定的任何時刻,滿幅值的直流供電要麼是
5V(ON),要麼是0V(OFF)。
• 電壓或電流源是以一種通(ON)或斷(OFF)的重複脈衝序列被加到類比負載上去的。
• 通的時候即是直流供電被加到負載上的時候,斷的時候即是供電被斷開的時候。
• 只要頻寬足夠,任何模擬值都可以使用PWM 進行編碼。
• 輸出的電壓值是通過通和斷的時間進行計算的。
• 輸出電壓=(接通時間/脈衝時間)*最大電壓值
18
PWM 調控燈光亮度實驗
19
類比輸出-調控燈光亮度
• On the ESP8266 the pins 0, 2, 4, 5, 12, 13, 14 and 15 all support PWM.
The limitation is that they must all be at the same frequency, and the
frequency must be between 1Hz and 1kHz.
import time
import machine
pwm = machine.PWM(machine.Pin(4))
pwm.freq(60)
while True:
for i in range(0,1024,5):
pwm.duty(i)
time.sleep(0.005)
for i in range(1023, -1, -5):
pwm.duty(i)
time.sleep(0.005)
類比輸入
import machine
import time
adc = machine.ADC(0)
while True:
print(adc.read())
time.sleep(0.5)
Load File & Run Code
• 如果開發板有1MB以上的Flash, MicroPython 啟動後會配置一個內
部的 filesystem
• 可以將程式碼存入, 當ESP8266啟動時可以自行載入程式執行
(就像是 Arduino 執行 Arduino sketch一樣)
• Start up scripts
• boot.py : is executed first (if it exists)
• main.py : is executed after boot.py completes
ampy
• https://github.com/adafruit/ampy
• With ampy you can
• send files from your computer to a MicroPython board's file system,
• download files from a board to your computer,
• and even send a Python script to a board to be executed.
$ pip install adafruit-ampy
$ ampy --port COM15 ls
$ ampy --port COM15 run blink10times.py
$ ampy --port COM15 put blink10times.py main.py
$ ampy --port COM15 rm main.py
MQTT簡介
• 一個 machine-to-machine (M2M) 的
發佈(Publish)/訂閱(Subscribe)訊息
的傳輸協定
• 當發佈者將訊息送至Topic平台,
Topic會將這個訊息送到所註冊的訂
閱者
• 發佈者可以是一個Sensors也可以是
一個推播訊息的入口
• 訂閱者可以是個伺服器上的應用服
務也可以是個手機
MQTT 整合應用
ESP8266
(DHT)
MQTT
Gateway
WebServer
MQTT
Broker
Publish
(8266)
Subscribe
(8266)
upload data
類似 ThingSpeak 的角色
NodeMCU
(Relay, LED)
Subscribe
(Relay)
Publish
(Relay)
CloudMqtt申請
https://www.youtube.com/watch?v=zx0kMbwxnak
輸入email
CloudMqtt申請
https://www.youtube.com/watch?v=zx0kMbwxnak
告訴你去收確認的email
CloudMqtt申請
https://www.youtube.com/watch?v=zx0kMbwxnak
點選確認連結
CloudMqtt申請
https://www.youtube.com/watch?v=zx0kMbwxnak
公司資料不必為真,
VAT可不填
CloudMqtt申請
https://www.youtube.com/watch?v=zx0kMbwxnak
新建Instances
CloudMqtt申請
https://www.youtube.com/watch?v=zx0kMbwxnak
CloudMqtt申請
https://www.youtube.com/watch?v=zx0kMbwxnak
CloudMqtt申請
https://www.youtube.com/watch?v=zx0kMbwxnak
新建User
CloudMqtt申請
https://www.youtube.com/watch?v=zx0kMbwxnak
CloudMqtt申請
https://www.youtube.com/watch?v=zx0kMbwxnak
CloudMqtt申請
https://www.youtube.com/watch?v=zx0kMbwxnak
CloudMqtt-訊息發佈及訂閱
https://www.youtube.com/watch?v=zx0kMbwxnak
Remote control by Mqtt
• 遠端控制LED on/off
• Sample code:
https://github.com/cclai999/micropython-lab
Reference
• http://docs.micropython.org/en/latest/esp8266/
• https://learn.adafruit.com/category/micropython

More Related Content

PPTX
Arduino 與 raspberry pi 硬體差異與應用
PDF
Arduino 底層原始碼解析心得
PDF
使用 Arduino 控制 ESP8266 的各種方式
PPTX
Raspberry Pi 溫濕度發報機
PDF
Arduino AMA中級認證術科實作 all
PDF
3D Printer 韌體原始碼解析心得 (以 Marlin 為對象)
PDF
Python與Ardinio整合應用
PDF
Arduino程式快速入門
Arduino 與 raspberry pi 硬體差異與應用
Arduino 底層原始碼解析心得
使用 Arduino 控制 ESP8266 的各種方式
Raspberry Pi 溫濕度發報機
Arduino AMA中級認證術科實作 all
3D Printer 韌體原始碼解析心得 (以 Marlin 為對象)
Python與Ardinio整合應用
Arduino程式快速入門

What's hot (20)

PDF
Arduino overview
PDF
PPT
3D Printer 關鍵軟體控制技術之分析與探討 @ COSCUP 2014
PDF
Arduino基礎IO控制
PDF
Arduino程式快速入門
PDF
Arduino感測應用
PDF
PDF
PDF
Arduino藍牙傳輸應用
PDF
Arduino序列通訊
PDF
Arduino Yún使用Http restful api控制io
PPTX
LinkIt 7697 開發平台簡介 (Traditional Chinese)
PDF
瞻營全電子_六足機器人(二)
PDF
nodeMCU IOT教學03 - NodeMCU導論
PPTX
141118 Raspberry Pi 電鈴工作坊@松山文創園區
PPT
認識 RoBoard 硬體
PDF
86Duino 小六足機器人 DIY 課程教材
PDF
Arduino Basic
PPT
RoBoard 與 Lego NXT Sensors 之連接
PPTX
物聯網技術分享 使用ESP8266
Arduino overview
3D Printer 關鍵軟體控制技術之分析與探討 @ COSCUP 2014
Arduino基礎IO控制
Arduino程式快速入門
Arduino感測應用
Arduino藍牙傳輸應用
Arduino序列通訊
Arduino Yún使用Http restful api控制io
LinkIt 7697 開發平台簡介 (Traditional Chinese)
瞻營全電子_六足機器人(二)
nodeMCU IOT教學03 - NodeMCU導論
141118 Raspberry Pi 電鈴工作坊@松山文創園區
認識 RoBoard 硬體
86Duino 小六足機器人 DIY 課程教材
Arduino Basic
RoBoard 與 Lego NXT Sensors 之連接
物聯網技術分享 使用ESP8266
Ad

Similar to MicroPython簡介 (20)

PDF
開放硬體認知學習指引
PDF
程式人雜誌 -- 2013 年 2 月號
PDF
nodeMCU IOT教學03 - NodeMCU導論
PDF
Arduino工作坊 - 羅伯特幫我寫作業
PDF
學習歷程 期中專題-機器人動手做
PDF
可調式電源供應器之研究
PDF
Python與Ardinio整合應用
PDF
程式人雜誌 -- 2014 年8月號
PPT
第三章Ti msp430平台介紹 v3
PDF
S4 a sensor board
PDF
程式人雜誌 -- 2013 年 1 月 (創刊號)
PDF
Arduino應用系統設計 - Arduino程式快速入門
PDF
Arduino L2
PPTX
BBC Micro:bit beginner project
PPT
「電腦硬體」教學示例與研討
PDF
Python 工作坊 (NCTU)
PPT
成果展簡報 嵌入式無線數位廟宇求籤管理系統
PDF
Arduino overview
PDF
程式人雜誌 2015年七月
PDF
程式人雜誌 -- 2015 年7月號
開放硬體認知學習指引
程式人雜誌 -- 2013 年 2 月號
nodeMCU IOT教學03 - NodeMCU導論
Arduino工作坊 - 羅伯特幫我寫作業
學習歷程 期中專題-機器人動手做
可調式電源供應器之研究
Python與Ardinio整合應用
程式人雜誌 -- 2014 年8月號
第三章Ti msp430平台介紹 v3
S4 a sensor board
程式人雜誌 -- 2013 年 1 月 (創刊號)
Arduino應用系統設計 - Arduino程式快速入門
Arduino L2
BBC Micro:bit beginner project
「電腦硬體」教學示例與研討
Python 工作坊 (NCTU)
成果展簡報 嵌入式無線數位廟宇求籤管理系統
Arduino overview
程式人雜誌 2015年七月
程式人雜誌 -- 2015 年7月號
Ad

MicroPython簡介