Try   HackMD

D1 mini 安裝軔體及測試

作者:王一哲
日期:2021/2/8

D1 mini 簡介

D1 mini 是一款內建 WiFi 模組的開發板,詳細的資訊請參考官方網站,它採用的晶片是由上海樂鑫科技製造的 ESP8266,連接埠為 Micro USB。WEMOS 官方版本的 D1 mini 開發板售價約為250元,如果從蝦皮或是淘寶買大陸製的板子,售價大約為80元。買來的板子通常無法直接使用,需要先安軔體才行,由於我比較常寫 Python,所以我選擇安裝 MicroPython

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 →
D1 mini 副廠開發板

安裝 MicroPython

先從 MicroPython 網站下載給 ESP8266 使用的軔體,下載頁面在此,目前最新版本為 esp8266-20210203-unstable-v1.14.bin。以下的測試環境為 Ubuntu 20.04.1 LTS,開啟文字界面輸入以下指令安裝 esptool

pip3 install esptool

進到存放 esp8266-20210203-unstable-v1.14.bin 檔案的資料夾,輸入以下指令清除 ESP8266 已儲存資料,其中 ttyUSB0 是連接埠的名稱,請依照自己的電腦狀態修改名稱。

esptool.py --port /dev/ttyUSB0 erase_flash

接著寫入軔體,理論上就完成了。

esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 esp8266-20210203-unstable-v1.14.bin

安裝 Thonny

我習慣使用 Thonny 作為開發用的 IDE,按照官方網站的教學使用以下指令安裝

bash <(wget -O - https://thonny.org/installer-for-linux) pip3 install thonny

於文字界面輸入以下指令開啟軟體

thonny

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 →
Thonny

從上方的選單依序點選工具選項,直譯器設定為為MicroPython (ESP8266),連接埠設定為USB2.0-Serial (/dev/ttyUSB0)

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 →
Thonny 選項

如果有成功連上 D1 mini 開發板畫面會像下圖,如果無法成功連上開發板,可以拔掉 USB 傳輸線再插上,理論上多試幾次就能連上。

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 →
已連上開發板

連上開發板之後按 Ctrl + O 或是點選工具列從左側數來的第二個按鈕開啟舊檔,選擇 MicroPython 設備,裡面應該會有一個名為 boot.py 的檔案,這樣就能使用板子了。

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 →
Thonny 開啟舊檔

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 →
boot.py

用 D1 Mini 內建 LED 測試開發板

D1 Mini 開發板有內建一個 LED,接腳名稱為 D4、編號為2。於 Thonny 編輯視窗中貼上以下的程式碼,按 F5 執行程式碼,選擇儲存到 MicroPython 設備,檔名為 blinkBasic.py,接下來如果看到板子上的 LED 每秒閃爍一次就成功了。

from machine import Pin import utime led = Pin(2, Pin.OUT) # D4及內建LED while True: led.value(1) utime.sleep_ms(500) led.value(0) utime.sleep_ms(500)

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 →
內建 LED 每秒閃爍一次

參考資料

  1. WeMos D1 mini 官方網站:https://www.wemos.cc/en/latest/d1/d1_mini.html
  2. ESP8266 官方網站:https://www.espressif.com/zh-hans/products/socs/esp8266
  3. MicroPython 官方網站安裝 ESP8266 說明書:http://docs.micropython.org/en/latest/esp8266/quickref.html
  4. Thonny 官方網站:https://thonny.org/

tags:D1 miniPython