0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

M5Stack ESP32CAMERAで簡易設置タイムラプスカメラ

Posted at

定点観察用のタイムラプスカメラを簡易設置できるように検討したのでメモ。

[仕様]
・M5Stack ESP32CAMERAを5秒ごとにキャプチャしてPCに保存する。
・PC - ESP32CAMERA間はアクセスポイントなしで直接APモードで繋ぐ

[使用方法]

  1. PCからWifiで"m5camera-wifi"に接続する
  2. ping 192.168.4.1が接続出来たらcapture_request.pyを起動
  3. capture_request.py と同じパスにimagesフォルダができ、"日付時間.jpg"でキャプチャ画像が保存される

M5Stack ESP32CAMERA側

CameraWebServerをベースにAPモードで接続できるようにする。

CameraWebServer.ino.c
#include <WiFiAP.h>
・・・
#define CAMERA_MODEL_M5STACK_V2_PSRAM // M5Camera version B Has PSRAM
・・・
const char* ssid = "m5camera-wifi";
const char* password = "XXXXXXXXX";
void startCameraServer();
void setupLedFlash(int pin);

void setup() {
・・・
  // WiFi.begin(ssid, password); // 子機モード
  WiFi.softAP(ssid, password);  // 親機モード

/*
  // 子機モードの場合
  WiFi.setSleep(false);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");  
*/

  startCameraServer();

  Serial.print("Camera Ready! Use 'http://");
  // Serial.print(WiFi.localIP()); // 子機モード
  Serial.print(WiFi.softAPIP());
  Serial.println("' to connect");
}

void loop() {
  // Do nothing. Everything is done in another task by the web server
  delay(10000);
}


PC側キャプチャアプリ(Python)

capture_request.py
import requests
import os
import time
import datetime

# 画像をダウンロードする
def download_image(url, timeout = 10):
    response = requests.get(url, allow_redirects=False, timeout=timeout)
    if response.status_code != 200:
        e = Exception("HTTP status: " + response.status_code)
        raise e

    content_type = response.headers["content-type"]
    if 'image' not in content_type:
        e = Exception("Content-Type: " + content_type)
        raise e

    return response.content

# 画像のファイル名を決める
def make_filename():
    picdir = os.path.join(os.getcwd(), 'images')
    if not os.path.exists(picdir):  # 無ければ作成
        os.makedirs(picdir)

    t_delta = datetime.timedelta(hours=9)
    JST = datetime.timezone(t_delta, 'JST')
    now = datetime.datetime.now(JST)
    d = now.strftime('%Y%m%d_%H%M%S')
    filename = d + '.jpg'

    fullpath = os.path.join(picdir, filename)
    return fullpath

# 画像を保存する
def save_image(filename, image):
    with open(filename, "wb") as fout:
        fout.write(image)

# メイン
if __name__ == "__main__":
    while True:
        url = 'http://192.168.4.1/capture'
        try:
            image = download_image(url)
            filename = make_filename()
            save_image(filename, image)
            time.sleep(5)
        except KeyboardInterrupt:
            break
        except Exception as err:
            print("%s" % (err))

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?