2024年11月6日 星期三

MicroPython 學習筆記 : ESP32-WROVER 開發板測試

十月底在露天買了兩片 ESP32-WROVER 開發板, 但賣家卻寄錯品項, 收到的是 ESP32-WROOM, 向賣家反映後表示會補寄, 參考 : 


今天收到補寄的 ESP32-WROVER 後馬上燒錄韌體來確認看看有無 PSRAM : 

D:\ESP32>esptool --port COM6 flash_id   
esptool.py v4.6.2
Serial port COM6
Connecting.....
Detecting chip type... Unsupported detection protocol, switching and trying again...
Connecting....
Detecting chip type... ESP32
Chip is ESP32-D0WD-V3 (revision v3.1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: a8:42:e3:ae:72:64
Uploading stub...
Running stub...
Stub running...
Manufacturer: 68
Device: 4016
Detected flash size: 4MB   
Hard resetting via RTS pin...

抹除 Flash 記憶體 : 

D:\ESP32>esptool --chip esp32 --port COM6 erase_flash    
esptool.py v4.6.2
Serial port COM6
Connecting....
Chip is ESP32-D0WD-V3 (revision v3.1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: a8:42:e3:ae:72:64
Uploading stub...
Running stub...
Stub running...
Erasing flash (this may take a while)...
Chip erase completed successfully in 3.9s
Hard resetting via RTS pin...

燒錄 PSRAM 版韌體 : 

D:\ESP32>esptool --chip esp32 --port COM6 write_flash -z 0x1000 ESP32_GENERIC-SPIRAM-20240602-v1.23.0.bin    
esptool.py v4.6.2
Serial port COM6
Connecting......
Chip is ESP32-D0WD-V3 (revision v3.1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: a8:42:e3:ae:72:64
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Flash will be erased from 0x00001000 to 0x0018afff...
Compressed 1611216 bytes to 1055046...
Wrote 1611216 bytes (1055046 compressed) at 0x00001000 in 93.2 seconds (effective 138.3 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

然後開啟 Thonny 連線開發板 :

MicroPython v1.23.0 on 2024-06-02; Generic ESP32 module with SPIRAM with ESP32

Type "help()" for more information.

可見此為有 SPIRAM  (即 PSRAM) 存取功能的 MicroPython. 

用 gc.mem_free() 檢視 SRAM : 

>>> import gc   
>>> gc.mem_free()   
4174928  

這顯示 ESP32-WROVER 的堆積 (Heap) 記憶體空間超過 4MB, 一般的 ESP32-WROOM 則僅有 520KB 的 SRAM 可用而已, 這使得 WROVER 可應用在較耗記憶體的運算上. 

用 micropython.mem_info() 檢視記憶體使用情形 : 

>>> import micropython      
>>> micropython.mem_info()          
stack: 736 out of 15360    
GC: total: 56000, used: 16624, free: 39376, max new split: 4128768  
 No. of 1-blocks: 385, 2-blocks: 44, max blk sz: 32, max free sz: 2448

第一列顯示堆疊記憶體配置 15360 bytes, 目前已用掉 736 bytes. 堆疊主要用在函式呼叫與儲存區域變數. 第二列顯示堆積 (Heap) 記憶體配置與使用情形, total 為 MicroPython 管理的 Heap 記憶體容量為 56 KB, used 為已使用的堆積記憶體大小, free 是閒置可分配的空間. max new split 是 PSRAM (SPI RAM) 可用的最大外部記憶體空間. 可見 WROVER 有 4MB 的 PSRAM

上傳 config.py 設定檔與 xtools.py 函式庫後測試網路應用 (Line Notify 與 OpenAI API) :

# test.py
import config
import xtools

ip=xtools.connect_wifi(config.SSID, config.PASSWORD)
line_token=config.LINE_NOTIFY_TOKEN
openai_api_key=config.OPENAI_API_KEY
message='test'
image_url='https://cdn.pixabay.com/photo/2024/03/15/17/50/dogs-8635461_1280.jpg'
xtools.line_msg(line_token, message)
xtools.line_sticker(line_token, message, 1, 4)
xtools.line_image_url(line_token, message, image_url)
prompt='Who are you'
print(xtools.ask_gpt(prompt, openai_api_key))

結果正常值形 : 

>>> import config   
>>> import xtools   
>>> ip=xtools.connect_wifi(config.SSID, config.PASSWORD)    
Connecting to network...
network config: ('192.168.50.199', '255.255.255.0', '192.168.50.1', '192.168.50.1')
>>> line_token=config.LINE_NOTIFY_TOKEN   
>>> openai_api_key=config.OPENAI_API_KEY    
>>> message='test'    
>>> image_url='https://cdn.pixabay.com/photo/2024/03/15/17/50/dogs-8635461_1280.jpg'    
>>> xtools.line_msg(line_token, message)    
Message has been sent.
>>> xtools.line_sticker(line_token, message, 1, 4)     
'The sticker has been sent.'
>>> xtools.line_image_url(line_token, message, image_url)    
'The image URL has been sent.'
>>> prompt='Who are you'    
print(xtools.ask_gpt(prompt, openai_api_key))
I am an AI language model developed by OpenAI, designed to assist with a wide range of queries by providing information, answering questions, and engaging in conversation. How can I assist you today?

LINE Notify 結果如下 : 




掃描附近基地台 :

>>> xtools.scan_ssid()    
ASUS_RT_AC52-Plus 5c:27:d4:f3:82:22 -60dBm
7N8F-1 b4:f2:67:13:36:5a -85dBm
OPPO A55 c6:b1:51:54:e8:3f -86dBm
HOME bc:f6:85:ff:a0:e0 -94dBm
ASUS_RT_AC52 10:7c:61:9d:30:18 -96dBm

沒有留言 :