當(dāng)我們提及物聯(lián)網(wǎng)(IoT, Internet of Things)開發(fā),可能首先想到的是Arduino或是ESP8266這樣的微控制器開發(fā)板。然而,Raspberry Pi的微控制器開發(fā)板——Raspberry Pi Pico W,也是一個(gè)很好的選擇。
在本項(xiàng)目中,將會(huì)介紹如何使用Raspberry Pi Pico W和MicroPython,并用Thonny IDE的平臺(tái)來撰寫程序。
Raspberry Pi Pico W 簡介
Raspberry Pi Pico W是由RaspberryPi基金會(huì)出品的微控制器開發(fā)板。它配備了一個(gè)RP2040微控制器,有264KB的內(nèi)部RAM,并且支持MicroPython程序語言,這讓我們可以更方便地開發(fā)物聯(lián)網(wǎng)應(yīng)用。
使用上和Raspberry Pi Pico沒有多大差別,只是Raspberry Pi Pico W 還支持Wi-Fi 802.11n無線網(wǎng)絡(luò)和藍(lán)牙,更多Raspberry Pi Pico相關(guān)介紹連結(jié)如下:
Raspberry Pi Pico family
Raspberry Pi Pico介紹(含使用ArduinoIDE和擴(kuò)充板)
MicroPython簡介
MicroPython是一種針對(duì)微控制器和受限環(huán)境設(shè)計(jì)的Python 3 程序語言編譯程序和執(zhí)行環(huán)境。這種程序語言實(shí)現(xiàn)了Python 3的大部分語法和特性,并對(duì)于開發(fā)板所需的低功耗和實(shí)時(shí)響應(yīng)有進(jìn)一步優(yōu)化。MicroPython提供了豐富的API,可以直接控制微控制器的GPIO、I2C、SPI等各種硬件資源。
Thonny IDE簡介
Thonny是一個(gè)專為Python初學(xué)者設(shè)計(jì)的集成開發(fā)環(huán)境(IDE)。它的介界面簡單,功能強(qiáng)大,對(duì)于學(xué)習(xí)Python語言非常有幫助。而且,Thonny IDE 也支持 MicroPython,我們可以直接在Thonny IDE中編寫MicroPython 程序,并上傳到Raspberry Pi Pico W上執(zhí)行。
請由 Thonny 官方網(wǎng)站下載 Thonny。
電路接線圖
本次項(xiàng)目分享如何透過Thonny IDE來撰寫MicroPython程序,并使用PMS5003粉塵傳感器取得數(shù)值和經(jīng)由Raspberry Pi Pico W的Wi-Fi功能取得目前臺(tái)灣時(shí)間,并顯示在OLED中。
以下介紹Raspberry Pi Pico W、Raspberry Pi Pico W擴(kuò)充板、OLED、PMS5003接線圖。
Raspberry Pi Pico與擴(kuò)充板接法(擴(kuò)充板可兼容于Raspberry Pi Pico W)
https://cavedu.gitbook.io/cavedu/raspberry_pi_pico_info/pico_breakout
Raspberry Pi Pico W擴(kuò)充板與OLED接線圖
Raspberry Pi Pico W擴(kuò)充板與PMS5003接線圖
接下來使用 Thonny IDE 來編寫程序,請先下載 Raspberry Pi Pico W 的 uf2 韌體檔,除非有更新版本的韌體,否則更新只要一次即可。
如何上傳Raspberry Pi Pico W的uf2檔
按住 Raspberry Pi Pico W 的 BOOTSET 按鈕時(shí),插上USB連接到計(jì)算機(jī)。
Raspberry Pi Pico W 會(huì)被計(jì)算機(jī)辨識(shí)為一個(gè)磁盤,將 uf2 韌體檔拖放到其中就會(huì)自動(dòng)更新韌體。
Raspberry Pi Pico W連接Thonny IDE
●將Thonny IDE下載至計(jì)算機(jī),并解壓縮后開啟,請選擇執(zhí)行>設(shè)定直譯器
●選Micro Python (RP2040)
●端口選擇USB序列裝置(COM X)
[注] COM號(hào)要記住,后續(xù)上傳程序需要正確指定 COM 號(hào),概念如同 Aduino IDE
●確認(rèn)是否抓到 Raspberry Pi Pico W
檢查 Thonny IDE 下方的互動(dòng)環(huán)境(Shell),如果沒有紅字錯(cuò)誤訊息即可上傳程序,這時(shí)候還沒寫,繼續(xù)看下去吧。
匯入OLED函式庫至 Raspberry Pi Pico W
本項(xiàng)目需要透過 OLED 顯示模塊來顯示數(shù)值,故需要先匯入OLED 函式庫
1. 下載 OLED 函式庫,檔名ssd1306.py到您的計(jì)算機(jī)
2. 在 Thonny IDE 中安裝套件
我們可由 Thonny IDE 呼叫系統(tǒng)命令行來安裝 python 套件,請由工具–> 開啟系統(tǒng)命令行
開啟系統(tǒng)命令行,如下圖
3. 安裝 adafruit-ampy 套件
為了順利執(zhí)行程序,我們需要安裝 adafruit-ampy 套件,用于透過串行端口與 CircuitPython 或 MicroPython 開發(fā)板互動(dòng),
安裝方式就是一般的 pip 指令,相當(dāng)簡單:
4. 匯入OLED函式庫到RaspberryPi Pico W
cd module_library
ampy --port COMX put ssd1306.py
[注]請注意,并不是開啟下方的窗口,若出現(xiàn)下方窗口,則要再按一次開啟系統(tǒng)命令行。
5. 如何查看已匯入的函式庫
在 Thonny IDE 中,點(diǎn)選檢視 -> 檔案,可以查看Raspberry Pi Pico W中匯入的檔案
確認(rèn)已匯入先前所操作的檔案
執(zhí)行程序
本專題有兩個(gè)程序 (或下載原始碼,或由以下復(fù)制也可以):
在 OLED 顯示時(shí)間(
OLED_wifitime.py
)
在 OLED 顯示 PMS5003 粉塵傳感器數(shù)值(
PMS5003_OLED.py
)
OLED_wifitime.py
import machine
import utime
import ssd1306
from time import sleep
import network
import ntptime
import urequests
i2c = machine.SoftI2C(scl=machine.Pin(15),sda=machine.Pin(4))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width,oled_height, i2c)
# 初始化 UART
uart = machine.UART(0, baudrate=9600,tx=machine.Pin(0), rx=machine.Pin(1), timeout=1000)
uart.init(9600, bits=8, parity=None,stop=1)
# 初始化WiFi
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
wifi_ssid = "XXXXX"
wifi_password = "XXXXXXX"
sta_if.connect(wifi_ssid, wifi_password)
# 顯示W(wǎng)iFi連接狀態(tài)
oled.fill(0)
oled.text('WiFi:', 0, 0)
if sta_if.isconnected():
oled.text('connected', 40, 0)
else:
oled.text('connceting', 40, 0)
oled.show()
#設(shè)定時(shí)區(qū)
TIME_ZONE = 8
# 取得NTP時(shí)間
#ntptime.settime()
for i in range(5):
try:
ntptime.settime()
break
except OSError:
print('Error connecting to NTP server, retrying...')
utime.sleep(5)
else:
print('Could not connect to NTP server after 5 retries.')
# 等待一段時(shí)間
utime.sleep(3)
while True:
t= utime.localtime(utime.time() + TIME_ZONE * 3600)
oled.fill(0)
oled.text(str("%d/%02d/%02d" % (t[0], t[1], t[2])), 0, 0)
oled.text(str("%02d:%02d:%02d" % (t[3], t[4], t[5])), 0, 16)
oled.show()
utime.sleep(1)
PMS5003_OLED.py
import machine
import utime
import ssd1306
from time import sleep
i2c = machine.SoftI2C(scl=machine.Pin(15),sda=machine.Pin(4))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width,oled_height, i2c)
# 初始化 UART
uart = machine.UART(0, baudrate=9600,tx=machine.Pin(0), rx=machine.Pin(1), timeout=1000)
uart.init(9600, bits=8, parity=None,stop=1)
while True:
#讀取 PMS5003 數(shù)據(jù)
data = bytearray(uart.read(32))
# 判斷是否為正確的PMS5003 資料
if data is not None and len(data) >= 10 and data[0] == 0x42 anddata[1] == 0x4d:
pm1_cf = int.from_bytes(data[4:6], 'big')
pm25_cf = int.from_bytes(data[6:8], 'big')
pm10_cf = int.from_bytes(data[8:10], 'big')
#清除 OLED
oled.fill(0)
# 顯示 PMS5003 數(shù)據(jù)
oled.text("PM1.0: %d ug/m3" % pm1_cf, 0, 22)
oled.text("PM2.5: %d ug/m3" % pm25_cf, 0, 38)
oled.text("PM10 : %d ug/m3" % pm10_cf, 0, 54)
# 更新 OLED
oled.show()
utime.sleep(1)
實(shí)際展示
執(zhí)行程序之后,可在 OLED 顯示模塊上看到相關(guān)信息,恭喜成功啰!
審核編輯:湯梓紅
-
微控制器
+關(guān)注
關(guān)注
48文章
7840瀏覽量
153307 -
物聯(lián)網(wǎng)
+關(guān)注
關(guān)注
2921文章
45663瀏覽量
384917 -
開發(fā)板
+關(guān)注
關(guān)注
25文章
5379瀏覽量
100705 -
Arduino
+關(guān)注
關(guān)注
188文章
6485瀏覽量
189634 -
Micropython
+關(guān)注
關(guān)注
0文章
68瀏覽量
5057
原文標(biāo)題:使用 Raspberry Pi Pico W 和 MicroPython 開發(fā)物聯(lián)網(wǎng)應(yīng)用
文章出處:【微信號(hào):易心Microbit編程,微信公眾號(hào):易心Microbit編程】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評(píng)論請先 登錄
相關(guān)推薦
Raspberry Pi Pico是什么
微控制器開發(fā)板Raspberry Pi Pico
使用raspberry pi Pico的原因
raspberry pi Pico使用MicroPython變磚后的解決方法

適用于Raspberry Pi 4的Raspberry Pi Pico開發(fā)板

使用MicroPython在Raspberry Pi上通過雙核編程的多線程控制LED

Raspberry Pi Pico:使用PIO驅(qū)動(dòng)伺服

使用Raspberry Pi Pico的LED序列

基于樹莓派Raspberry Pi Pico的自動(dòng)澆花系統(tǒng)
Arduino Raspberry Pi Pico/RP2040以太網(wǎng):W5100S EVB Pico

遠(yuǎn)程編程Raspberry Pi Pico

Raspberry Pi Pico 2 W 發(fā)布

評(píng)論