ESP32 with NUS

tags: ESP32 MicroPython

PaoyungMay 29, 2022

ℳ𝒾𝒸𝓇ℴ𝒫𝓎𝓉𝒽ℴ𝓃 隨手記

聽程式在說話

剛開始使用 MicroPython 的時候,發現它與 Python 還是有著很大的差別,MicroPython 為了節省空間和記憶體,許多模組的功能都是 lite 版的,好在 REPL 可以邊試邊學,對於功力不夠的我實在是很便利,因此讓程式邊跑邊吐訊息成為我的日常 但一旦把 ESP32 和其它電路擺在一起,或甚至已經裝設在會移動的設備上了,想監看它的運作狀態便成了難題。雖然可以把數據寫在 log 檔裡事後再行查看,可惜不能即時且得再接線連電腦下指令,多少還是有些不方便。
查詢文件後得知 WebRepl、Wifi Terminal、TCP Client/Server等方法可以達到,不過對於我來說操作上還是不太順手,所以今天就換個角度,要提供的方法是使用 REPL over BLE UART 來完成。ESP32 自身就有藍牙功能,而且也能和 REPL 結合,用手機 APP 即能達到監看的效果。若加上多重開機和 KeyValue 後也可以選擇性的打開或關閉,如果你有以下的需求,就可以試試這個方法。

  • 連著 COM Port 可以看即時訊息,但裝到可移動裝置上就無法連著線
  • 必須拆拆裝裝才能查看得到 ESP32 上的 log 檔
  • 雖然已有 Wi-fi Manager 模組可改 SSID 和密碼,但想改的不只有密碼,還有更多的參數
  • 在不適合連 COM Port 時需要動態改變變數的值以利測試

相闗資訊:

BLE 模組

REPL over BLE UART 需要載入擴充模組,在 MicroPython 的 github 上已經有高手分享程式碼,以下3個檔案我們需下載備用。

  1. ble_uart_repl.py
  2. ble_uart_peripheral.py
  3. ble_advertising.py

在 ble_uart_repl.py 中已將裝置名稱設定固定為 mpy-repl,為能靈活更換我們自己的裝置名稱,所以拿 ble_uart_repl.py 來套娃產生我們的 bur.py

# name: bur.py from ble_uart_repl import * def start(name): ble = bluetooth.BLE() uart = BLEUART(ble, name) stream = BLEUARTStream(uart) os.dupterm(stream)

main.py 中只要宣告裝置名稱即可啟用,這個名稱會出現在手機 APP 藍牙搜尋的裝置名單中。

# name: main.py import bur bur.start('Mqtt_AIoT')

把 github 下載回來的3個檔案,以及 bur.pymain.py 上傳到 ESP32,ESP32 重啟並等待手機搜尋及連線。

APP

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
手機上要安裝此 APP,請從 Google Play 搜尋 Bluefruit Connect 安裝並執行。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Device 選擇 MQTT_AIoT,再選擇 MODULES 中的 UART

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

開始使用

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
打些指令試試吧!
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

這個 BLE over UART 也可以自行撰寫成 WEB 界面,但我用這個 APP 已經足矣,就不再折騰了。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

關於作者  

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

我是 𝙋𝙖𝙤𝙮𝙪𝙣𝙜,是 MicroPythonEspruino 的愛好者,一直認為:

「Maker 應該把重心放在應用上,而不該被語言工具限制了創意」

所以想藉由較為簡捷且詳細的解說讓更多人可以學習進而自由發揮,而非淪為只能將程式碼複製貼上的複製人,如果你認同這樣的理念,請一起為此目標努力,如果我的文章內容對你有幫助,請轉發或協助需要的人。

若有專案需求,請利用 連絡我,謝謝!