Receiving notifications when the lock is opened

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:

  1. First, go to IFTTT, and add two channels if that's not been done yet: the Maker channel and the Pushover channel. Also install the Pushover app on your smartphone. To learn more about this, refer to the Chapter 7, Sending Notifications from the ESP8266.
  2. Then, create a new recipe, and choose the Maker channel as the trigger:
    Receiving notifications when the lock is opened
  3. We need to use the Maker channel here, as it will allow us to use custom projects like ours with IFTTT. As the trigger for the channel, put the lock_opened event:
    Receiving notifications when the lock is opened
  4. As the action, simply look for the Pushover channel, which we will again use to receive notifications from IFTTT:
    Receiving notifications when the lock is opened
  5. Then, select the notification type of your choice:
    Receiving notifications when the lock is opened
  6. This will simply indicate to IFTTT whether it should send normal or high-priority notifications to your Pushover app. After that, put the message you want to receive every time an event is received:
    Receiving notifications when the lock is opened
  7. Finally, confirm the creation of the channel:
    Receiving notifications when the lock is opened
  8. Let's now see what we need to add to the previous code to integrate the notifications. First, we need to define two variables to store the current status of the lock:
    bool lockStatus;
    bool previousLockStatus;
  9. Then, inside the 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;
  10. After that, inside the loop() function, we again read the state from pin number 5:
    lockStatus = digitalRead(5);
  11. If it has changed compared to the last read, and if the lock is open (which means that there is a 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;
    
      }

    Note

    To learn more about how to send notifications via IFTTT, please check out Chapter 7, Sending Notifications from the ESP8266.

  12. It's now time to test the project! Upload the code to the board, and then go back to the online dashboard that you used before to control the lock. This time, every time you open the lock, you should immediately get a notification on your smartphone.

This way, even if you shared your dashboard with somebody else, you will be informed when they open the door lock.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.134.151