カメラのシャッターをネット経由で切る

投稿日:

 先週、Arduinoを使ってデジカメのシャッターを切る遊びがやりたくて、ソレノイドでやろうとしたら、ソレノイドの力不足でうまくいかなかった。

 よく考えてみたら、最初に買った入門キットの中に小さいサーボがあり、これは力があるからシャッターぐらい押せるだろう、と思って工夫した。

 そうしたら、うまくいった。こんな具合である。



 ネットワーク経由で動くようになっており、そのプログラムは次の通りである。

//
//  WebServerでサーボを動かし、カメラのシャッターを切る。
//    佐藤俊夫
//    27.5.30(土)1300~
//

#include <SPI.h>
#include <Ethernet2.h>
#include <Servo.h>

byte mac[] = {
  0x90, 0xA2, 0xDA, 0x0F, 0xF6, 0x74
};
IPAddress ip(192, 168, 1, 129);
EthernetServer server(80);
Servo shutter;
const int SERVO = 9;

void setup() {
  Ethernet.begin(mac, ip);
  server.begin();
  shutter.attach(SERVO);
  shutter.write(90);
}

void loop() {
  String recvbuf;
  EthernetClient client = server.available();
  if (client) {
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        recvbuf += c;
        if (c == '\n' && currentLineIsBlank) {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println("<head></head>");
          client.println("<body>");
          client.println("<center><h3>Drive solenoid.</h3>");
          client.println("<hr>");
          client.println("<form method=\"POST\">");
          client.println("<input type=\"submit\" value=\"Do!\">");
          client.println("</form>");
          client.println("</center>");
          client.println("</body>");
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          currentLineIsBlank = true;
          if(recvbuf.indexOf("POST") == 0){
            shutter.write(50);
            delay(5000);
            shutter.write(90);            
          } 
          recvbuf = "";
        }
        else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    client.stop();
  }
}

投稿者: 佐藤俊夫

 50代後半の爺。技術者。元陸上自衛官。2等陸佐で定年退官。ITストラテジストテクニカルエンジニア(システム管理)基本情報技術者

「カメラのシャッターをネット経由で切る」への1件のフィードバック

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください