見出し画像

【工作】オートロックに抗う装置

概要

鍵をかけないような田舎からオートロック付きのアパートに引っ越した.
いつもの癖で鍵を持たずに外に出たら締め出されたので,本装置を作成した.

我が家は門が2重になっており,外の門がオートロックとなっている.インターホンで室内に繋ぐと室内から施錠を解除することができる.

画像2

この図の対応者の部分を機械にやらせる,頼れる友達がいないため.

用いたESP-WROOM-02はWi-fiにつながるarduinoみたいな感じらしい.ラズパイゼロがこの世にないので助かった.

画像1

必要部品

・ESP-WROOM-02開発ボード
先述の通りWi-fiのついたarduino(暴論),本来はWi-fiモジュールだったみたい.だけどいろいろついて便利になったらしい.安い.

・サーボモータ(SG90の中華クローン)
ホンモノよりトルクがないらしいがよくわからん,うるさい.

・ジャンパ線など
開発ボードとサーボを繋ぐ,後はボードへの給電用のやつ.

ほんへ

繋ぎ方は上記の図のように.
VoutをVccに,IO16(任意)を信号線に,GND同士をつなぐ.

以下,つぎはぎコード.
PCに繋いでシリアルモニタを見るとIPが表示される.
”そのIP”/gpio/0にアクセスする毎に,push_b()が呼ばれる仕組み.

#include <Servo.h>
#include <ESP8266WiFi.h>
const char* ssid = "自分のSSID";
const char* password = "自分のパスワード";
WiFiServer server(80);
WiFiClient client;
int sw_pin = 16;
int val;
int fre = 0;
Servo myservo;
void setup() 
{
 Serial.begin(115200);
 // Wifi接続開始
 WiFi.begin(ssid, password);
 
 Serial.print("Connecting Wifi");
 while (WiFi.status() != WL_CONNECTED) 
 {
   delay(500);
   Serial.print(".");
 }
 Serial.println("");
 Serial.println("Complete Connect Wifi!!");
 
 server.begin();
 Serial.println("Server started");
 Serial.print("local IP : ");
 Serial.println(WiFi.localIP());
 //pinMode
 pinMode( sw_pin,INPUT );
 pinMode(5, OUTPUT);
 digitalWrite(2, 0);
 //servo
 myservo.attach(16);
 myservo.write(fre);
}
void loop() {
 // Check if a client has connected
 WiFiClient client = server.available();
 if (!client) {
   return;
 }
 
 // Wait until the client sends some data
 Serial.println("new client");
 while(!client.available()){
   delay(1);
 }
 
 // Read the first line of the request
 String req = client.readStringUntil('\r');
 Serial.println(req);
 client.flush();
 
 // Match the request
 int val;
 if (req.indexOf("/gpio/0") != -1){
   val = 0;
   push_b();tasuka
 }
 else if (req.indexOf("/gpio/1") != -1){
   val = 1;
 }
 else {
   Serial.println("invalid request");
   client.stop();
   return;
 }
 // Set GPIO5 according to the request
 digitalWrite(5, val);
 client.flush();
 // Prepare the response
 String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\n\r\nGPIO is now ";
 s += (val)?"high":"low";
 s += "\n";
 // Send the response to the client
 client.print(s);
 delay(1);
 Serial.println("Client disonnected");
 // The client will actually be disconnected 
 // when the function returns and 'client' object is detroyed
}
void push_b()
{
 int f = 45;
 myservo.write(f);
 Serial.println("Servo 45");
 delay(1000);
 myservo.write(0);
 Serial.println("Servo 0");
 delay(1000);
}

あとはインターホンに固定して終わり.

conclusion

鍵くらい持ち歩け

この記事が気に入ったらサポートをしてみませんか?