それに対し、今回は、ビデオストリームを配信しつつ、カメラサーバ(ラズパイ等)にメッセージを送信、一定時間経過でdeep sleep、タクトスイッチ(タクタイルスイッチ)を押下した時にext0復帰するようにしてみた話。
保存だけだと後になって過去の画像や映像を見て訪問があったことを確認することしかできませんが、スマホのLINEやメールに通知すれば、どこにいても、手があいていれば、すぐにでも確認できますしね。
技適を通ったOV2640カメラ付きのESP32ボード、ストリームを無線を介して保存する方法、トリガーを無線を介して送信する方法があった、まさに3拍子揃って、これらを併用すればできちゃうというかできちゃった。
// ESP32-WROVER+OV2640でストリーム配信
// タイマーでDeep sleep/タクトスイッチでext0復帰
#include "esp_camera.h"
#include <WiFi.h>
#include <ESPmDNS.h>
#include "soc/soc.h" // Disable brownour problems
#include "soc/rtc_cntl_reg.h" // Disable brownour problems
#include <WiFiUdp.h>
//
// WARNING!!! PSRAM IC required for UXGA resolution and high JPEG quality
// Ensure ESP32 Wrover Module or other board with PSRAM is selected
// Partial images will be transmitted if image exceeds buffer size
//
// You must select partition scheme from the board menu that has at least 3MB APP space.
// Face Recognition is DISABLED for ESP32 and ESP32-S2, because it takes up from 15
// seconds to process single frame. Face Detection is ENABLED if PSRAM is enabled as well
// ===================
// Select camera model
// ===================
#define CAMERA_MODEL_WROVER_KIT // Has PSRAM
//#define CAMERA_MODEL_ESP_EYE // Has PSRAM
//#define CAMERA_MODEL_ESP32S3_EYE // Has PSRAM
//#define CAMERA_MODEL_M5STACK_PSRAM // Has PSRAM
//#define CAMERA_MODEL_M5STACK_V2_PSRAM // M5Camera version B Has PSRAM
//#define CAMERA_MODEL_M5STACK_WIDE // Has PSRAM
//#define CAMERA_MODEL_M5STACK_ESP32CAM // No PSRAM
//#define CAMERA_MODEL_M5STACK_UNITCAM // No PSRAM
//#define CAMERA_MODEL_AI_THINKER // Has PSRAM
//#define CAMERA_MODEL_TTGO_T_JOURNAL // No PSRAM
// ** Espressif Internal Boards **
//#define CAMERA_MODEL_ESP32_CAM_BOARD
//#define CAMERA_MODEL_ESP32S2_CAM_BOARD
//#define CAMERA_MODEL_ESP32S3_CAM_LCD
// ===========================
// Enter your WiFi credentials
// ===========================
const char* ssid = "SSID";
const char* password = "PASSPHRASE";
IPAddress local_IP(192, 168, 0, 250);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
//IPAddress primaryDNS(8, 8, 8, 8); //optional
//IPAddress secondaryDNS(8, 8, 4, 4); //optional
WiFiUDP wifiUdp;
const char *remote_ip = "raspberry.local"; //送信先IP
const int remote_udp_port = 9998; //送信先のポート
void startCameraServer();
const char common_name[40] = "esp32_wrover_cam";
const char *mdns_name = common_name;
unsigned long on_start;
const long on_time = 15000;
//RTC_DATA_ATTR int bootCount = 0; // RTCスローメモリに変数を確保
void print_wakeup_reason(){
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch(wakeup_reason)
{
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
}
}
void startWiFi() { // Start a Wi-Fi access point, and try to connect to some given access points. Then wait for either an AP or STA connection
WiFi.disconnect(); //added to start with the wifi off, avoid crashing
// if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
if (!WiFi.config(local_IP, gateway, subnet)) {
Serial.println("STA Failed to configure");
}
static const int local_port = 9998; // ESP32-WROVER側のポート
// WiFi.softAP(ssid, password); // Start the access point
WiFi.mode(WIFI_STA); // Start the access point
WiFi.begin(ssid, password); // Start the access point
while ( WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("SSID \"");
Serial.print(ssid);
Serial.println("\" started\r\n");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("Camera Ready! Use 'http://");
Serial.print(WiFi.localIP());
Serial.println("' to connect");
Serial.print("ESP Mac Address: ");
Serial.println(WiFi.macAddress());
Serial.print("Subnet Mask: ");
Serial.println(WiFi.subnetMask());
Serial.print("Gateway IP: ");
Serial.println(WiFi.gatewayIP());
Serial.print("DNS: ");
Serial.println(WiFi.dnsIP());
wifiUdp.begin(local_port);
}
void startMDNS() { // Start the mDNS responder
MDNS.begin(mdns_name); // start the multicast domain name server
Serial.print("mDNS responder started: http://");
Serial.print(mdns_name);
Serial.println(".local");
}
void setup() {
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
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.frame_size = FRAMESIZE_UXGA;
config.pixel_format = PIXFORMAT_JPEG; // for streaming
//config.pixel_format = PIXFORMAT_RGB565; // for face detection/recognition
config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
config.fb_location = CAMERA_FB_IN_PSRAM;
config.jpeg_quality = 12;
config.fb_count = 1;
// if PSRAM IC present, init with UXGA resolution and higher JPEG quality
// for larger pre-allocated frame buffer.
if (config.pixel_format == PIXFORMAT_JPEG) {
if (psramFound()) {
config.jpeg_quality = 10;
config.fb_count = 2;
config.grab_mode = CAMERA_GRAB_LATEST;
} else {
// Limit the frame size when PSRAM is not available
config.frame_size = FRAMESIZE_SVGA;
config.fb_location = CAMERA_FB_IN_DRAM;
}
} else {
// Best option for face detection/recognition
config.frame_size = FRAMESIZE_240X240;
#if CONFIG_IDF_TARGET_ESP32S3
config.fb_count = 2;
#endif
}
#if defined(CAMERA_MODEL_ESP_EYE)
pinMode(13, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
#endif
// camera init
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
sensor_t * s = esp_camera_sensor_get();
// initial sensors are flipped vertically and colors are a bit saturated
if (s->id.PID == OV3660_PID) {
s->set_vflip(s, 1); // flip it back
s->set_brightness(s, 1); // up the brightness just a bit
s->set_saturation(s, -2); // lower the saturation
}
// drop down frame size for higher initial frame rate
if (config.pixel_format == PIXFORMAT_JPEG) {
s->set_framesize(s, FRAMESIZE_QVGA);
}
#if defined(CAMERA_MODEL_M5STACK_WIDE) || defined(CAMERA_MODEL_M5STACK_ESP32CAM)
s->set_vflip(s, 1);
s->set_hmirror(s, 1);
#endif
#if defined(CAMERA_MODEL_ESP32S3_EYE)
s->set_vflip(s, 1);
#endif
pinMode(GPIO_NUM_13, INPUT_PULLUP);
pinMode(GPIO_NUM_4, INPUT_PULLUP);
startWiFi();
startMDNS();
delay(1000);
on_start = millis();
esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, 0);
}
void loop() {
wifiUdp.beginPacket(remote_ip, remote_udp_port);
std::string str = "espcam1";
uint8_t arr[sizeof(str)];
std::copy(str.begin(), str.end(), std::begin(arr));
wifiUdp.write(arr, sizeof(arr));
if (millis() - on_start >= on_time) {
esp_deep_sleep_start();
}
WiFiサーバの他、IP固定化及びmDNSとbrownoutとUDP送信対応し、SSID/PASSPHRASEを適宜編集、[#define CAMERA_MODEL_WROVER_KIT]行を有効にします。
あと、このESP32-WROVERボード、電源ON時、WiFiサーバ自体は、起動しつつ、カメラが落ちる(電源ON直後は、ハードウェアリセットしないとストリームを開始できない)ことに気づきました。
よって対策として[esp_err_t err = esp_camera_init(&config);]の戻り値判定[if (err != ESP_OK) {}]内にESP32のソフトウェアリセット[ESP.restart()]を入れてあります。
ESP32のディープスリープは、復帰後、setup()から始まるのでsetup(){}内に開始時間をセットすることで経過時間を計測、設定時間を経過していたらスリープ。
尚、Arduino IDEにおけるボード設定は、ESP32 Wrover Board、Partition Schemeは、ESP32-CAM同様、RAMの関係でArduinoOTAは使えない[Huge APP(3MB No OTA/1MB SPIFFS)]。