2026年2月11日 星期三

MicroPython 學習筆記 : ESP32-S3 CAM 開發板測試 (二)

前一篇測試使用了 cnadler86 的 Generic 的韌體, 裡面沒有 camera 模組無法拍照, 本篇改下載燒錄檔名有 CAM 的韌體 : 





同樣解壓縮 zip 檔後用 esptool 燒錄到 ESP32-S3 CAM 中 (最好先卸除鏡頭) :

D:\ESP32>esptool --port COM5 flash_id   
esptool.py v4.6.2
Serial port COM5
Connecting..................................
Detecting chip type... ESP32-S3
Chip is ESP32-S3 (revision v0.2)
Features: WiFi, BLE
Crystal is 40MHz
MAC: e0:72:a1:d7:e0:38
Uploading stub...
Running stub...
Stub running...
Manufacturer: 68
Device: 4018
Detected flash size: 16MB
Flash type set in eFuse: quad (4 data lines)
Hard resetting via RTS pin...

D:\ESP32>esptool --chip esp32s3 --port COM5 erase_flash   
esptool.py v4.6.2
Serial port COM5
Connecting...................
Chip is ESP32-S3 (revision v0.2)
Features: WiFi, BLE
Crystal is 40MHz
MAC: e0:72:a1:d7:e0:38
Uploading stub...
Running stub...
Stub running...
Erasing flash (this may take a while)...
Chip erase completed successfully in 3.5s
Hard resetting via RTS pin...

D:\ESP32>esptool --chip esp32s3 --port COM5 write_flash -z 0 firmware.bin
esptool.py v4.6.2
Serial port COM5
Connecting..............
Chip is ESP32-S3 (revision v0.2)
Features: WiFi, BLE
Crystal is 40MHz
MAC: e0:72:a1:d7:e0:38
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Flash will be erased from 0x00000000 to 0x001dbfff...
Compressed 1948208 bytes to 1238683...
Wrote 1948208 bytes (1238683 compressed) at 0x00000000 in 108.5 seconds (effective 143.7 kbit/s)...
Hash of data verified.

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

燒錄完需拔掉 USB 斷電重插, 這樣 Thonny 的 "執行/重新啟動" 才會 Boot 進去 : 

MicroPython v1.27.0-dirty on 2026-01-12; Generic ESP32S3 module with Octal-SPIRAM with ESP32S3
Type "help()" for more information.
>>> import esp   
>>> print(f"Flash 大小: {esp.flash_size() / 1024 / 1024:.2f} MB")   
Flash 大小: 16.00 MB  

用下列程式檢查記憶體與 camera 模組 : 

import gc   
import camera
import micropython

# 分配記憶體與整理
gc.collect()

print("="*30)
print("🚀 N16R8 Vibe Check")
print("="*30)

# 1. 檢查 RAM (關鍵指標)
# N16R8 成功驅動後,這裡應該要顯示 4MB ~ 8MB 之間的數值
free_ram_mb = gc.mem_free() / 1024 / 1024
print(f"✅ 剩餘 RAM: {free_ram_mb:.2f} MB")

if free_ram_mb > 4:
    print("   -> 狀態:完美!Octal PSRAM 已啟用。")
else:
    print("   -> 警告:RAM 過少,可能燒錄成 Non-Octal 版本。")

# 2. 檢查 Camera 模組
try:
    print(f"✅ Camera 模組版本: {camera}")
    print("   -> 驅動載入成功。")
except Exception as e:
    print(f"❌ Camera 模組載入失敗: {e}")

print("="*30)
==============================
🚀 N16R8 Vibe Check
==============================
✅ 剩餘 RAM: 7.93 MB
   -> 狀態:完美!Octal PSRAM 已啟用。
✅ Camera 模組版本: <module 'camera'>
   -> 驅動載入成功。
==============================

可見此韌體有 camera 模組, 且 8MB 的 PSRAM 可用, 這巨量的 RAM 讓拍照不會 Out of memory. 

但用下列 Gemini 提供的程式拍照卻有馬賽克 :

import camera
import time
import os
import gc

gc.collect()

print("========================================")
print("🚀 Vibe Cam - 最終勝利存檔版")
print("========================================")

cam = None
try:
    # 1. 啟動
    cam = camera.Camera()
    cam.init()
    time.sleep(0.5)

    # 2. 設定 (使用剛剛驗證成功的 Keyword 方式)
    try:
        cam.reconfigure(pixel_format=camera.PixelFormat.JPEG, frame_size=camera.FrameSize.VGA)
        print("✅ 格式設定成功 (JPEG/VGA)")
    except:
        pass # 已經設過了或失敗都沒關係,反正數據看起來是對的

    time.sleep(1.5) # 等待緩衝區穩定

    # 3. 拍照
    print("📸 拍攝中...")
    # 為了清空舊緩衝區,建議空拍一張
    try:
        cam.capture()
        time.sleep(0.1)
    except:
        pass
        
    buf = cam.capture()
    print(f"📦 取得數據: {len(buf)} bytes")

    # 4. 【關鍵】不做任何檢查,直接暴力存檔!
    filename = "vibe_victory.jpg"
    with open(filename, "wb") as f:
        f.write(buf)
        
    print(f"💾 已強制儲存至: {filename}")
    print("🎉 請立刻用 Thonny 下載這張照片,它絕對是好的!")
    
    # 5. 釋放
    cam.deinit()

except Exception as e:
    print(f"❌ 錯誤: {e}")

print("========================================")

輸出結果如下 :

========================================
🚀 Vibe Cam - 最終勝利存檔版
========================================
✅ 格式設定成功 (JPEG/VGA)
📸 拍攝中...
<memoryview>
📦 取得數據: 28189 bytes
28189
💾 已強制儲存至: vibe_victory.jpg
🎉 請立刻用 Thonny 下載這張照片,它絕對是好的!
========================================

按 Thonny 左邊開發板資料夾框右上角的三條槓按鈕, 點選 "重新整理" 就會出現拍攝的照片 JPG 檔, 點選 "下載到 D:\ESP32-S3-CAM" 即可複製此 JPG 到 PC 瀏覽 :





來回試了好幾次都無法得到完整的照片只好放棄了. 

沒有留言 :