iT邦幫忙

2025 iThome 鐵人賽

DAY 1
0
Software Development

以MicroPython在ESP32上實作Insulin Delivery Service系列 第 1

Day 01 - 安裝、設定 Thonny 和 VS Code

  • 分享至 

  • xImage
  •  

各位看官好呀~
本喵要在此野人獻曝~分享在 ESP32-DevKitC (WROOM-32D) 上,用 MicroPython 開發 Insulin Delivery Service BLE peripheral 的經驗。
預計會實作以下 Characteristics:

  • IDD Status Changed
  • IDD Status
  • IDD Annunciation Status
  • IDD Features
  • IDD Status Reader Control Point
  • IDD Command Control Point
  • IDD Record Access Control Point
  • IDD History Data

因為各 Control Point 有太多指令,所以只會挑選 Bolus 和 Annunciation 相關的來做。
現在先讓咱們把開發環境架設好吧!

1. 安裝 ESP32 驅動程式

ESP32-DevKitC (WROOM-32D) 使用的驅動程式是 (https://www.silabs.com/documents/public/software/CP210x_Universal_Windows_Driver.zip)

2. 安裝 Thonny IDE

直接到 (https://thonny.org/) 下載並安裝好後,咱們就來對它進行一些設定。

2-1. 編輯器設定

點選「Tools」>「Options」
http://ithelp.ithome.com.tw/upload/images/20250801/201777997FNTjeVOUl.png

選擇「Editor」標籤頁
https://ithelp.ithome.com.tw/upload/images/20250801/20177799mqm46LWjZN.png

2-1-1. 設定函數參數資訊提示

勾選「Automatically show parameter info after typing '('」
當輸入一個函數名和一個小括號後,會出現如下圖的參數提示:
https://ithelp.ithome.com.tw/upload/images/20250801/20177799mCaVxxFNPj.png

2-1-2. 設定輸入補全功能

勾選「Automatically propose completions while typing」
文字輸入期間可以顯示後續可接的關鍵字、函數、參數等訊息:
https://ithelp.ithome.com.tw/upload/images/20250801/20177799sMIgvFQvob.png

2-1-3. 設定在編輯區內輸入Tab鍵以執行補全功能

勾選「Request completions with Tab-key in editors」
某些情況自動補全功能不會運作,此時可按下 Tab 鍵以強制執行自動補全功能。
但此功能啟用後,只要游標所在行有文字,Tab 鍵將無法輸入 Tab 字元。

要注意的是,這些功能只針對 Thonny 安裝時附帶的套件有效,對於之後自行安裝的套件未必有作用。

2-2. 設定直譯器

點選「Tools」>「Options」後進入「Interpreter」頁面
將直譯器版本設為「MicroPython (ESP32)」
https://ithelp.ithome.com.tw/upload/images/20250801/20177799JcjNMWE5fO.png

選擇 ESP 裝置所在 COM 編號
https://ithelp.ithome.com.tw/upload/images/20250801/20177799LkHJXAvJpW.png

點擊「Install or update MicroPython (esptool)」

  • 將「Target port」設為 ESP 裝置所在位址
  • 將「MicroPython family」設為「ESP32」
  • 將「variant」設為「Espressif‧ESP32 / WROOM」
  • 選擇所需版本
  • 按下「Install」將 MicroPython 環境燒錄到裝置裡。

https://ithelp.ithome.com.tw/upload/images/20250801/20177799ToUH87vjEP.png

一切就緒後,Shell 窗格應類似如下:
https://ithelp.ithome.com.tw/upload/images/20250801/201777992ZGpLOp5l0.png

至此,就可以在 Thonny 裡開發程式,並且將程式上傳到 ESP32 後運行程式。

3. 安裝 Visual Studio Code

雖然 Thonny 已經做得不錯了,但在開發非單一檔案的專案時稍嫌不足:

  • 語法提示只支援 Thonny 安裝時的套件
    無法支援額外安裝的第三方套件,也不支援同專案裡的檔案
  • 無法進行程式碼導航

因此,本喵習慣用 VS Code 來編寫程式,要執行時才使用 Thonny。

首先下載 (https://code.visualstudio.com) 進行安裝,然後安裝 Python 必備的 extensions:ms-python.python、ms-python.debugpy、ms-python.vscode-pylance。

為了使 ms-python.vscode-pylance 能執行程式碼補全功能:

  • 在專案下建立一個資料夾「.vscode」
  • 在其內建立一檔案「settings.json」
  • 將 "python.analysis.extraPaths" 的值指定為 Thonny 的 base_api_stubs 所在位置

https://ithelp.ithome.com.tw/upload/images/20250801/20177799TnyPRMVOVj.png
username 為實際使用者名稱

之後在輸入 MicroPython 相關模組與函數時即可顯示提示:
https://ithelp.ithome.com.tw/upload/images/20250801/20177799wbaKQE1MKX.png


下一篇
Day 02 - 開發基本 GATT 伺服器
系列文
以MicroPython在ESP32上實作Insulin Delivery Service31
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言