SlideShare a Scribd company logo
1 of 39
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

What's hot

3D Printer 關鍵軟體控制技術之分析與探討 @ COSCUP 2014
3D Printer 關鍵軟體控制技術之分析與探討 @ COSCUP 20143D Printer 關鍵軟體控制技術之分析與探討 @ COSCUP 2014
3D Printer 關鍵軟體控制技術之分析與探討 @ COSCUP 2014
roboard
 
認識 RoBoard 硬體
認識 RoBoard 硬體認識 RoBoard 硬體
認識 RoBoard 硬體
roboard
 
Arduino Basic
Arduino BasicArduino Basic
Arduino Basic
mmiwwcom
 
RoBoard 與 Lego NXT Sensors 之連接
RoBoard 與 Lego NXT Sensors 之連接RoBoard 與 Lego NXT Sensors 之連接
RoBoard 與 Lego NXT Sensors 之連接
roboard
 

What's hot (20)

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

Similar to MicroPython簡介

突破 計算機概論複習講義-電子試閱本
突破 計算機概論複習講義-電子試閱本突破 計算機概論複習講義-電子試閱本
突破 計算機概論複習講義-電子試閱本
lungtengtech
 
STM32F4 for 智慧型電動輪椅系統Part1
STM32F4 for 智慧型電動輪椅系統Part1STM32F4 for 智慧型電動輪椅系統Part1
STM32F4 for 智慧型電動輪椅系統Part1
Jack Wang
 
Dmdx介绍及使用
Dmdx介绍及使用Dmdx介绍及使用
Dmdx介绍及使用
sancoyh
 
S3 cev40getting startv2.1 cn
S3 cev40getting startv2.1 cnS3 cev40getting startv2.1 cn
S3 cev40getting startv2.1 cn
Vidur Garg
 

Similar to MicroPython簡介 (20)

IoT 與 WoT 物聯網裝置實作:使用 Arch Pro 與 mbed
IoT 與 WoT 物聯網裝置實作:使用 Arch Pro 與 mbedIoT 與 WoT 物聯網裝置實作:使用 Arch Pro 與 mbed
IoT 與 WoT 物聯網裝置實作:使用 Arch Pro 與 mbed
 
LinkIt Smart 7688程式開發
LinkIt Smart 7688程式開發LinkIt Smart 7688程式開發
LinkIt Smart 7688程式開發
 
建置Python開發環境
建置Python開發環境建置Python開發環境
建置Python開發環境
 
突破 計算機概論複習講義-電子試閱本
突破 計算機概論複習講義-電子試閱本突破 計算機概論複習講義-電子試閱本
突破 計算機概論複習講義-電子試閱本
 
STM32F4 for 智慧型電動輪椅系統Part1
STM32F4 for 智慧型電動輪椅系統Part1STM32F4 for 智慧型電動輪椅系統Part1
STM32F4 for 智慧型電動輪椅系統Part1
 
電腦基礎認識(4x3)
電腦基礎認識(4x3)電腦基礎認識(4x3)
電腦基礎認識(4x3)
 
期末專題報告書
期末專題報告書期末專題報告書
期末專題報告書
 
LinkIt 7697 IoT tutorial
LinkIt 7697 IoT tutorialLinkIt 7697 IoT tutorial
LinkIt 7697 IoT tutorial
 
電腦基礎認識(16x9)
電腦基礎認識(16x9)電腦基礎認識(16x9)
電腦基礎認識(16x9)
 
[students AI workshop] Pytorch
[students AI workshop]  Pytorch[students AI workshop]  Pytorch
[students AI workshop] Pytorch
 
LinkIt Smart 7688 Duo and MCS basics
LinkIt Smart 7688 Duo and MCS basicsLinkIt Smart 7688 Duo and MCS basics
LinkIt Smart 7688 Duo and MCS basics
 
Windows Mobile 多媒體應用程式開發
Windows Mobile 多媒體應用程式開發Windows Mobile 多媒體應用程式開發
Windows Mobile 多媒體應用程式開發
 
Getting started with amarino
Getting started with amarinoGetting started with amarino
Getting started with amarino
 
BBC Micro:bit beginner project
BBC Micro:bit beginner projectBBC Micro:bit beginner project
BBC Micro:bit beginner project
 
Iot 自動販賣機
Iot 自動販賣機Iot 自動販賣機
Iot 自動販賣機
 
C.C. Agile#30 – Coding Dojo – Prepared Kata
C.C. Agile#30 – Coding Dojo – Prepared KataC.C. Agile#30 – Coding Dojo – Prepared Kata
C.C. Agile#30 – Coding Dojo – Prepared Kata
 
Dmdx介绍及使用
Dmdx介绍及使用Dmdx介绍及使用
Dmdx介绍及使用
 
Raspberry pi 基本操作
Raspberry pi 基本操作Raspberry pi 基本操作
Raspberry pi 基本操作
 
S3 cev40getting startv2.1 cn
S3 cev40getting startv2.1 cnS3 cev40getting startv2.1 cn
S3 cev40getting startv2.1 cn
 
第三章Ti msp430平台介紹 v3
第三章Ti msp430平台介紹 v3第三章Ti msp430平台介紹 v3
第三章Ti msp430平台介紹 v3
 

MicroPython簡介