ESP32 CAM + micro:bit + Web:bit + Smart 搭配登月
小車說明
ESP32-CAM 內建 Wi-Fi,並結合 OV2640 影像感測器模組(解析度 1632 x 1232)、PSRAM
記憶體、MicroSD 記憶卡卡槽(存照片),使其適合做為網路攝影機應用,同時與過往 ESP8266 一
樣相容於 Arduino。重要的是,只要透過 Arduino 的一個範例程式:CameraWebServer,將該程
式透過 PC 燒寫到 ESP32-CAM 板卡上,就可以在 ESP32-CAM 上建立一個網頁伺服器(Web
Server),而後只要用瀏覽器瀏覽 192.168.x.x 內部網址,就可以看到攝影機角度的影像內容,進
一步也可以作一些影像辨識應用,如人臉位置標記等,這等於也是一個
WoT(Web of Things),
ESP32-CAM 的 GPIO4 已接閃光燈、GPIO0 是燒錄用(燒錄要接 GND,平時不可接 GND)、GPIO1
是當
TxD、GPIO3 是當 RxD,Vcc 不是外接電源而是提供電源(POUT),外接電源(POW)標示 5V,
外觀與腳位如下。
1.
至
https://www.arduino.cc/en/Main/Software
網站下載最新開發套件(ESP32-CAM 需要最新的
核心程式,建議下載
1.8.8 以上版本)
2.
開啟
Arduino IDE 後,選擇『
檔案/偏好設定
』,出現偏好設定視窗,在『額外的開發板管理員
網址』輸入『
https://dl.espressif.com/dl/package_esp32_index.json
』,按『確定』。
3.
在開發板管理員中輸入『ESP32』關鍵字,找到 ESP32 套件後,點選『安裝』。勿安裝 1.0.4
最新版,因為在編譯時會出現錯誤,請選擇『
1.0.2
』舊版本,安裝需要一點時間。
4.
最後點選『工具→開發板』,在右側往下拉,就可以找到
ESP32 系列的板子,選擇『
ESP32
Wrover Module
』。由於程式包含串流
stream server 及臉部辨識,因此檔案較大,將燒錄區域
改成『
Huge APP(3MB No OTA)
』,否則編譯會錯誤。
5.
由於
ESP32-CAM 並無燒錄介面,需要準備一個『USB 轉 TTL』工具 ,桌面『
本機右鍵→內
容→裝置管理員
』,確認驅動程式
COM?安裝成功,連接方式是兩邊的 TxD 與 RxD 互接,然
後在
Arduino 的『
工具→序列埠
』設定
COM?,否則無法燒錄,產品網址。
https://www.playrobot.com/interface-signal-conversion/1632-ftdi-basic-usbttl-ft232.html
6.
當燒錄時要將
GPIO0 與 GND 連接,燒錄完成後,將 GPIO0 與 GND 分開,若燒錄時看到『A
fatal error occurred: Failed to connect to ESP32: Timed out waiting for packet header』錯誤訊
7.
燒錄過程會依序出現
1% 到 100% 進度,時間大約至少一分鐘,當燒錄成功會出現『上傳完
畢』,如下圖所示,GPIO0 不可接地,Wi-Fi 基地台先開啟,然後按 RESET 重置按鈕,方可使
用,重開機時,閃光燈會閃爍點亮。
8.
查看手機分配給
ESP32 CAM 位置,然後電腦也要跟 ESP32 CAM 同網域方可,開啟瀏覽器輸
入
IP 位置,即可看到如下畫面(程式是由法蘭斯老師提供),功能說明如下:
9.
Turn Decelerate 是轉彎速度
10.
持續按下
11.
等這一行消失"Please wait for loading model."
您要等模組載入完才能點按鈕
12.
並沒有上雲端分析
13.
下載模型到瀏覽器後是在近端瀏覽器上分析
14.
按
get-still 相當於是 start 按鈕,開始截圖分析
16.
Get Still:取得靜態視訊截圖。
17.
Start Stream:可以減少網路流量,有時不見得要用視
訊,有需要再開啟。
18.
No Stop:按下前、後、左、右按鈕後,是否持續動作 ,
不需要一直按著按鈕就一直動作。
19.
Servo : 伺 服 馬 達 控 制 , 接 GPIO2( 不 可 透 過 ESP32
CAM 的 Vcc 供電)。
20.
Flash:閃光燈控制。
21.
SpeedL 、 SpeedR : 馬 達 分 別 接 到
GPIO12、13、14、15,可以單獨控制轉速,自行用杜
邦線連接。
22.
Toggle settings:可針對鏡頭參數做詳細設定。
23.
Rsolution:鏡頭解析度,不建議設太大,要考慮網路傳
輸與影像辨識的速度。
24.
Quality:鏡頭影像品質。
25.
Brightness:亮度。
26.
Contrast:對比。
27.
Saturation:飽和度。
28.
Special Effect:特效。
29.
AWB :是 Automatic white balance 縮寫,在日光燈的
房間裡拍攝的影像會顯得發綠,在室內鎢絲燈光下拍攝
出來的景物就會偏黃,而在日光陰影處拍攝到的照片則
莫名其妙地偏藍,其原因就在於“白平衡”的設置上,
白平衡的作用就是在這些場景下恢復圖像的正常顏色。
30.
結合登月小車,使用
ESM32 CAM 就可以直接控制小車車囉!利用超音波 5V 電源,轉接板電
路如下。
31.
登月小車外觀與接腳電路如下:
ESP32 CAM
BBC Micro:bit
Web:bit
GPIO12
2
6
GPIO13
13
13
GPIO15
8
8
GPIO14
14
14
GPIO2
SERVER
IO2 5V GND SERVO5V G
燒錄開關
Web:bit 腳位
14 08 13 06
GPIO4
高亮度閃光燈
32.
登月小車搭配 Web:Bit 範例:利用擴充功能,直接使用廠商內建積木,比較簡單,但無法利用
遙控器網頁範本與行動裝置陀螺儀控制登月小車。將滑鼠移到四隻可愛怪獸上面,即可控制登
月小車,離開圖示則自動停止,按下登月小車按鈕,進入循跡模式。
33.
登月小車搭配
Webduino bit 範例:可利用遙控器網頁範本與行動裝置陀螺儀控制登月小車,缺
點是僅能利用腳位控制,無法使用
8 顆全彩 LED
34.
登月小車搭配
micro:bit 範例:按下 A 鍵切換功能,功能 1 是自動行駛,碰到障礙物先倒退再轉
彎避開障礙物,然後繼續前進,再按下
A 鍵切換到功能 2(循跡),再按下 A 鍵,切換到功能
Pin14
Pin08
Pin13
Pin06
ESP32-CAM_CAR_2pwm_knn-classifier.ino 程式碼
/*
ESP32-CAM Remote Control Car (knn-classifier)
Author : ChungYi Fu (Kaohsiung, Taiwan) 2019-11-3 14:00 https://www.facebook.com/francefu
Motor Driver IC -> PWM1(gpio12, gpio13), PWM2(gpio14, gpio15)
If you output PWM to GPIO 15 using ledcWrite and the wheel loses control, you could use the codes. https://github.com/fustyles/Arduino/tree/master/ESP32-CAM_CAR_2digitalwrite
https://github.com/fustyles/Arduino/tree/master/ESP32-CAM_CAR_1pwm1digitalwrite
http://APIP http://STAIP */
const char* ssid = "ipoe";
const char* password = "12345678";
#include "esp_camera.h" #include <WiFi.h> #include "soc/soc.h"
#include "soc/rtc_cntl_reg.h"
// WARNING!!! Make sure that you have either selected ESP32 Wrover Module, // or another board which has PSRAM enabled
//CAMERA_MODEL_AI_THINKER #define PWDN_GPIO_NUM 32 #define RESET_GPIO_NUM -1 #define XCLK_GPIO_NUM 0 #define SIOD_GPIO_NUM 26 #define SIOC_GPIO_NUM 27 #define Y9_GPIO_NUM 35 #define Y8_GPIO_NUM 34 #define Y7_GPIO_NUM 39 #define Y6_GPIO_NUM 36 #define Y5_GPIO_NUM 21 #define Y4_GPIO_NUM 19 #define Y3_GPIO_NUM 18
#define VSYNC_GPIO_NUM 25 #define HREF_GPIO_NUM 23 #define PCLK_GPIO_NUM 22 void startCameraServer(); void setup() { WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); Serial.begin(115200); Serial.setDebugOutput(true); Serial.println(); camera_config_t config; config.ledc_channel = LEDC_CHANNEL_0; config.ledc_timer = LEDC_TIMER_0; config.pin_d0 = Y2_GPIO_NUM; config.pin_d1 = Y3_GPIO_NUM; config.pin_d2 = Y4_GPIO_NUM; config.pin_d3 = Y5_GPIO_NUM; config.pin_d4 = Y6_GPIO_NUM; config.pin_d5 = Y7_GPIO_NUM; config.pin_d6 = Y8_GPIO_NUM; config.pin_d7 = Y9_GPIO_NUM; config.pin_xclk = XCLK_GPIO_NUM; config.pin_pclk = PCLK_GPIO_NUM; config.pin_vsync = VSYNC_GPIO_NUM; config.pin_href = HREF_GPIO_NUM; config.pin_sscb_sda = SIOD_GPIO_NUM; config.pin_sscb_scl = SIOC_GPIO_NUM; config.pin_pwdn = PWDN_GPIO_NUM; config.pin_reset = RESET_GPIO_NUM; config.xclk_freq_hz = 20000000; config.pixel_format = PIXFORMAT_JPEG; //init with high specs to pre-allocate larger buffers if(psramFound()){ config.frame_size = FRAMESIZE_QVGA; config.jpeg_quality = 10; config.fb_count = 2; } else { config.frame_size = FRAMESIZE_QVGA; config.jpeg_quality = 12;
14
config.fb_count = 1; }
// camera init
esp_err_t err = esp_camera_init(&config); if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err); return;
}
//drop down frame size for higher initial frame rate sensor_t * s = esp_camera_sensor_get();
s->set_framesize(s, FRAMESIZE_QVGA);
//ESP32-CAM Remote Control Car //Flash ledcAttachPin(4, 4); ledcSetup(4, 5000, 8); //Wheel ledcAttachPin(12, 5); ledcSetup(5, 2000, 8); ledcAttachPin(13, 6); ledcSetup(6, 2000, 8); ledcWrite(6, 0); ledcAttachPin(15, 7); ledcSetup(7, 2000, 8); ledcAttachPin(14, 8); ledcSetup(8, 2000, 8); Serial.println("ssid: " + (String)ssid); Serial.println("password: " + (String)password); WiFi.begin(ssid, password);
long int StartTime=millis();
while (WiFi.status() != WL_CONNECTED) {
delay(500);
startCameraServer();
char* apssid = "ESP32-CAM";
char* appassword = "12345678"; //AP password require at least 8 characters. Serial.println("");
Serial.println("WiFi connected"); Serial.print("Camera Ready! Use 'http://"); if (WiFi.status() == WL_CONNECTED) { Serial.print(WiFi.localIP());
Serial.println("' to connect");
WiFi.softAP((WiFi.localIP().toString()+"_"+(String)apssid).c_str(), appassword);
for (int i=0;i<5;i++) { ledcWrite(4,10); delay(200); ledcWrite(4,0); delay(200); } } else { Serial.print(WiFi.softAPIP()); Serial.println("' to connect"); WiFi.softAP((WiFi.softAPIP().toString()+"_"+(String)apssid).c_str(), appassword);
for (int i=0;i<2;i++) { ledcWrite(4,10); delay(1000); ledcWrite(4,0); delay(1000); } } } void loop() {
// put your main code here, to run repeatedly: delay(10000);
}
如果一直連不到ESP32 CAM,可以按 RESET,若還是很慢,應該是電力不足, 不建議用電腦USB 供電(含 USB 轉 TTL),建議用手機充電器,搭配自製電源線