カーテン自動開閉機の自作 プロトタイプ ソフトウェア編
はじめに
前回、カーテン自動開閉機のプロトタイプのハードウェアについて説明しました。
今回はプロトタイプのソフトウェアと回路について説明します。
動作
カーテン自動開閉機に求められる3つの動作は以下の通りです。
カーテンを開ける動作:Alexaアプリで指定した実行条件(指定時間、「おはよう」と言う等)でモータを10秒回転させた後、3秒逆回転させます。
カーテンを閉める動作(手動):人の手で糸を引っ張る(カーテンを閉める動作)と糸が緩む方向にモータが動きます。
それ以外:何もないときはモータは止まったままです。
使用部品
- 電源
回路図
ソースコード
ソースコードの参考にしたサイトはこちらです。
このサイトのプログラムをダウンロードして、「SingleSwitch.ino」というファイルを以下のソースコードに変更しました。
またセットアップの参考にしたサイトはこちらです。
#include <WiFi.h> #include "WemoSwitch.h" #include "WemoManager.h" #include "CallbackFunction.h" // prototypes boolean connectWifi(); //on/off callbacks void lightOn(); void lightOff(); //------- Replace the following! ------ char ssid[] = "SSID"; // your network SSID (name) char password[] = "password"; // your network key WemoManager wemoManager; WemoSwitch *light = NULL; const int ledmotPin = A13; const int ledmotPin2 = A12; unsigned long motorStartTime = 0; const int SW_PIN = 16; void setup() { Serial.begin(115200); // Set WiFi to station mode and disconnect from an AP if it was Previously // connected WiFi.mode(WIFI_STA); WiFi.disconnect(); delay(100); // Attempt to connect to Wifi network: Serial.print("Connecting Wifi: "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(500); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); IPAddress ip = WiFi.localIP(); Serial.println(ip); wemoManager.begin(); // Format: Alexa invocation name, local port no, on callback, off callback light = new WemoSwitch("DESK lights", 80, lightOn, lightOff); wemoManager.addDevice(*light); //pinMode(motPin, OUTPUT); // initialize digital ledPin as an output. //pinMode(motPin2, OUTPUT); //digitalWrite(motPin, HIGH); // Wemos BUILTIN_LED is active Low, so high is off pinMode(SW_PIN, INPUT_PULLUP); ledcSetup(0, 1024, 8); ledcAttachPin(ledmotPin, 0); ledcSetup(1, 1024, 8); ledcAttachPin(ledmotPin2, 1); delay(10); } void loop() { wemoManager.serverLoop(); if (motorStartTime > 0) { if (millis() < motorStartTime + 10000) { motRotCw(); Serial.print(motorStartTime); Serial.println(""); } else if (millis() < motorStartTime + 13000) { motRotCww(); Serial.print(motorStartTime); Serial.println(""); } else if (digitalRead(SW_PIN) == LOW) { motRotCww(); Serial.println("アルミホイルが導通してます 逆転します"); } else { motStop(); } } else if (digitalRead(SW_PIN) == LOW) { motRotCww(); Serial.println("アルミホイルが導通してます 逆転します"); } else { motStop(); } delay(100); } void motStart() { motorStartTime = millis(); } void motRotCw() { int val = 53; Serial.print("Motor rotates clockwise..."); Serial.println(""); ledcWrite(1, 0); ledcWrite(0, val); } void motRotCww() { int val = 53; Serial.print("Motor rotates counterclockwise..."); Serial.println(""); ledcWrite(1, val); ledcWrite(0, 0); } void motStop() { Serial.print("Motor stops..."); Serial.println(""); ledcWrite(0, 0); ledcWrite(1, 0); } void lightOn() { motStart(); } void lightOff() { motStop(); }
まとめ
Alexaに「おはよう」と言ったり、時間を指定すると、カーテンを開ける動作をする装置を製作することができました。
カーテンを閉めようとすると、カーテンを引っ張っている糸が緩まるようにモータが動きます。
今回は家にあったモータドライバを使用したので、電源電圧が10V以上必要でした。 モータの必要電圧は3Vなので、次製作するときには3Vで動くモータドライバを探してみます。
次回はプロトタイプの動作動画をアップしたいと思います。