Controlling the lock from the cloud is great, but you can only find out its current state by opening the dashboard on your computer or mobile phone. But what if you are on the road, and the lock is attached to a quite important door in your home? You would want to be alerted when the door is opened.
This is exactly what we are going to do now using IFTTT. We are going to set up the board so it sends notifications to your smartphone when the door lock is opened:
lock_opened
event:bool lockStatus; bool previousLockStatus;
setup()
function of the sketch, we read data from pin number 5, to check whether the lock is initially activated or not:lockStatus = digitalRead(5); previousLockStatus = lockStatus;
loop()
function, we again read the state from pin number 5:lockStatus = digitalRead(5);
LOW
status on the pin), we send a notification via IFTTT:if (lockStatus != previousLockStatus && lockStatus == 0) { Serial.print("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("connection failed"); return; } // We now create a URI for the request String url = "/trigger/"; url += eventName; url += "/with/key/"; url += key; Serial.print("Requesting URL: "); Serial.println(url); // This will send the request to the server client.print(String("GET ") + url + " HTTP/1.1 " + "Host: " + host + " " + "Connection: close "); int timeout = millis() + 5000; while (client.available() == 0) { if (timeout - millis() < 0) { Serial.println(">>> Client Timeout !"); client.stop(); return; } } // Read all the lines of the reply from server and print them to Serial while(client.available()){ String line = client.readStringUntil(' '); Serial.print(line); } Serial.println(); Serial.println("closing connection"); // Set previous status previousLockStatus = lockStatus; }
To learn more about how to send notifications via IFTTT, please check out Chapter 7, Sending Notifications from the ESP8266.
This way, even if you shared your dashboard with somebody else, you will be informed when they open the door lock.
3.133.134.151