WiFiスイッチ側増設ソース
WiFi増設チャイムスイッチ側Client説明
私は、家に元々ついていたチャイムに増設する形で、ブザーを設置しました。
私の使っているチャイムEC730は、スイッチの端子にいつも1Vほどの電流が流れていて、ボタンを押したときだけ一瞬0Vに近くなります。
Wemos D1のA0とGND端子にこのスイッチ端子からの電流を並列で受け取って、アナログリードで電圧測定をします。
A0端子への入力はそのまま受け取っても支障はないのかもしれませんが、あまり電流を流したくはないので、適当な抵抗を挟んでおきました。
それで、スイッチが押されたときに、増設側のServerを呼び出して、回路をONにします。
電源はDCACコンバーターから取り、Wemos D1はタッパーに入れて、コンバーターごとチャイムの裏の壁に隠しました。
100V電源は、元のチャイムに来ていた線を、コンセントを付けて二股にしてつないでいます。壁裏で塵が積もることによる自然発火が怖いので、コンセントの差し込み部分は、ビニールテープでしっかりふさいでおきました。
なお、このプログラムの改造ベースは、「スケッチ例>ESP8286WiFi>WiFiClient」です。
WiFiは、スイッチの方に接続するClient側は、IPアドレスを自動取得にして、子機になるServer側は、固定IPアドレスになるプログラムにします。
WiFiのアクセスポイントを用意
私の母の家のように、インターネット接続がないような家でも、無線LANのアクセスポイントを置きさえすれば、家庭内では無線通信ができるので、このチャイムも使用できます。
ただ、このアクセスポイントの設定が、一筋縄ではいかず、とっても面倒くさいですが。
WiFi増設チャイムスイッチ側ソースコード
/* * チャイムスイッチから無線送信用 元 * 元付けチャイム機器への増設用 * 送信元では警告を鳴らさない */ #include <ESP8266WiFi.h> //***************************************************** const char* ssid = "Your ID"; const char* password = "Your Password"; #define ANALOG_SWITCH_SOCKET A0 // 接続サーバーの数ホストアドレスを指定 const char* host1 = "192.168.***.***"; const char* host2 = "192.168.***.***"; //***************************************************** static int switch1= 0; // デバッグ設定 #define DEBUG // 有効にするとシリアル出力が有効になります。 #ifdef DEBUG #define Serialbegin Serial.begin #else #define Serialbegin #endif void setup() { pinMode(ANALOG_SWITCH_SOCKET,INPUT); Serialbegin(115200); delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.println(); Serial.print(F("Connecting to ")); Serial.println(ssid); /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default, would try to act as both a client and an access-point and could cause network-issues with your other WiFi-devices on your WiFi-network. */ WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print(F(".")); } Serial.println(F("")); Serial.println(F("WiFi connected")); Serial.println(F("IP address: ")); Serial.println(WiFi.localIP()); } void voltagehantei(){ int v1; static int vt = -2; int sensorValue = analogRead(ANALOG_SWITCH_SOCKET); // delay(500); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 3.2V): float voltage = sensorValue * ( 3.2 / 1023.0); if(voltage>=0.5){ v1 = 3; switch1=0; }else{ v1 = 0; switch1=1; } if(v1 != vt){ Serial.print(F("Voltage=")); Serial.println(voltage); vt = v1; if(switch1==1){ on(); } } } void on(){ // 接続サーバーの数 送信する //***************************** send_server(host1); send_server(host2); //***************************** } void send_server(const char* host){ int value = 0; ++value; Serial.println(F("On")); Serial.print(F("connecting to ")); Serial.println(host); // Use WiFiClient class to create TCP connections WiFiClient client; const int httpPort = 80; if (!client.connect(host, httpPort)) { Serial.println(F("connection failed")); return; } // We now create a URI for the request String url ="/"; Serial.print(F("Requesting URL: ")); Serial.println(url); // This will send the request to the server client.print(String("GET ") + url + " HTTP/1.1rn" + "Host: " + host + "rn" + "Connection: closernrn"); unsigned long timeout = millis(); while (client.available() == 0) { if (millis() - timeout > 5000) { Serial.println(F(">>> Client Timeout !")); client.stop(); return; } } // Read all the lines of the reply from server and print them to Serial int i=1; while (client.available()) { String line = client.readStringUntil('r'); if(i==6){ // 6行目にテキスト内容が表示される Serial.println(line); } ++i; } Serial.println(""); Serial.println(F("closing connection")); } void loop() { voltagehantei(); }