#include <SPI.h> #include <Ethernet.h> #include <PubSubClient.h> int flag = 0; #define ring1_pin 0 // 1 #define open1_pin 2 // 1 #define open2_pin 3 // 2 #define mon1_pin 5 // #define ID_CONNECT "DoorbellControll" byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; byte server[] = { 192, 168, 55, 170 }; byte ip[] = { 192, 168, 55, 40 }; EthernetClient ethClient; PubSubClient client(server, 1883, callback, ethClient); void callback(char* topic, byte* payload, unsigned int length) { payload[length] = '\0'; String strTopic = String(topic); String strPayload = String((char*)payload); if (strTopic == "myhome/DoorbellControll/open1") { if (strPayload == "false") { digitalWrite(2, HIGH); } else if (strPayload == "true") { digitalWrite(2, LOW); } } else if (strTopic == "myhome/DoorbellControll/open2") { if (strPayload == "false") digitalWrite(3, LOW); else if (strPayload == "true") digitalWrite(3, HIGH); } else if (strTopic == "myhome/DoorbellControll/mon1") { if (strPayload == "false") digitalWrite(5, LOW); else if (strPayload == "true") digitalWrite(5, HIGH); } } void reconnect() { while (!client.connected()) { if (client.connect(ID_CONNECT)) { client.subscribe("myhome/DoorbellControll/#"); } else { delay(5000); } } } void setup() { pinMode(ring1_pin, INPUT); pinMode(open1_pin, OUTPUT); digitalWrite(open1_pin, HIGH); pinMode(open2_pin, OUTPUT); digitalWrite(open2_pin, LOW); pinMode(mon1_pin, OUTPUT); digitalWrite(mon1_pin, LOW); Ethernet.begin(mac, ip); if (client.connect("DoorbellControll")) { client.publish("myhome/DoorbellControll/open1", "false"); client.publish("myhome/DoorbellControll/open2", "false"); client.publish("myhome/DoorbellControll/mon1", "false"); client.publish("myhome/DoorbellControll/ring1", "false"); client.subscribe("myhome/DoorbellControll/#"); } } void loop() { client.loop(); // if (digitalRead(ring1_pin) == LOW && flag == 0) { client.publish("myhome/DoorbellControll/ring1", "true"); flag = 1;// flag } else if (digitalRead(ring1_pin) == HIGH && flag == 1) { client.publish("myhome/DoorbellControll/ring1", "false"); flag = 0; // flag } if (!client.connected()) { reconnect(); client.subscribe("myhome/DoorbellControll/#"); } }
//************ ******************** createState('doorbellcontroll.rings', 'false'); createState('doorbellcontroll.visopen', 'false'); // vis on("mqtt.0.myhome.DoorbellControll.ring1", function (obj){ if (obj.newState.val == "true" || obj.newState.val === true) { setState('javascript.0.doorbellcontroll.rings', true); setTimeout(function() { }, 60000); } }); //******** , **************** on("javascript.0.doorbellcontroll.rings", function (obj){ if (obj.newState.val == "true" || obj.newState.val === true) { setState('sayit.0.tts.text', ' '); // sendTo('telegram.0', ' '); // setState('mqtt.0.myhome.DoorbellControll.mon1', true); // setState("vis.0.control.command", '{"instance": "FFFFFFFF", "command": "changeView", "data": "Camers"}'); // "" //*********** 30 ******************** setTimeout(function () { setState('mqtt.0.myhome.DoorbellControll.mon1', false); // setState("vis.0.control.command", '{"instance": "FFFFFFFF", "command": "changeView", "data": "StartView"}'); // "Home" setState('javascript.0.doorbellcontroll.rings', false); }, 30000); } }); //******************* "" Vis************** on("javascript.0.doorbellcontroll.visopen", function (obj){ if (obj.newState.val == "true" || obj.newState.val === true) { setState('mqtt.0.myhome.DoorbellControll.open1', true); // toLog(' . ', true, 'orange'); // setTimeout(function () { setState('mqtt.0.myhome.DoorbellControll.open1', false); setState('javascript.0.doorbellcontroll.visopen', false); // }, 7500); } });
Source: https://habr.com/ru/post/400399/
All Articles