How to integrate NodeMCU ESP8266 and IFTTT in order to make garage door alert

Trying to make a garage door tracker that tells me when my garage door is open and closed. I have been able to figure out the hardware components using the NodeMCU ESP8266 and a SHARP infrared sensor attached to the analog input. Also sorted out how to set up a maker event on IFTTT.

My main issue is integrating the two, when the sensor value is <= 800 I want IFTTT to send out “Closed” and when the sensor value is >= 800 I want IFTTT to send out “Open”. The issue is where the NodeMCU will continuously send out requests to the IFTTT, for example, if the door is “closed” IFTTT will send out “closed” messages non-stop without reading its last value.

I’m having trouble figuring out the code that will read this last value and only send out a request to IFTTT if the value has changed. Specifically, the change has happened when the sensor value has gone from <= 800 to >= 800 or vice-versa.

This is my code so far (note some of the declared variables are not being used in the program):

#include <ESP8266WiFi.h>
#include <arduino.h>




int inputVal = 0;


const char* myKey = ""; // your maker key here
const char* ssid = ""; // your router ssid here
const char* password = ""; // your router password here
const char* host = "maker.ifttt.com";





bool connectedToWiFI = false;
volatile int flag = false;
const char* PIN_state = "Closed";


void PINStatus() {
  PIN_state = "Open";  
  flag = true;
}

const int sharp = A0;

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
bool lastcurrentState = false;     // previous state of the button

void setup() {
  
  
  // initialize the button pin as a input:
  pinMode(sharp, INPUT);
  // initialize the LED as an output:

  // initialize serial communication:
  Serial.begin(9600);
  delay(200);
   
  WiFi.begin(ssid, password);
    
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    }
  
  Serial.println("test");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
    
}





void loop() {
  
  
  Serial.print("connecting to ");
  Serial.println(host);
          
  WiFiClient client;
  const int httpPort = 80;
      
  if (!client.connect(host, httpPort)) {
  Serial.println("connection failed");
  }    
  //return;
  buttonState = analogRead(sharp);
  
  bool currentState = false;
 
  
  
    if (currentState != lastcurrentState) {
      // if the state has changed, increment the counter
      if (buttonState <= 800) {
        // door closed
        currentState = false;
        buttonPushCounter++;
        Serial.println("closed");
        String url = "/trigger/garage_door/with/key/";
        url += myKey;
        Serial.print("Requesting URL: ");
        Serial.println(url);
        client.print(String("POST ") + url + " HTTP/1.1\r\n" + "Host: " + "maker.ifttt.com" + "\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n" + "Content-Length: 13\r\n\r\n" + "value1=" + "Closed" + "\r\n");
        
      } else if (buttonState >= 800) {
        // if the current state is LOW then the button went from on to off:
        currentState = true;
        Serial.println("open");
        String url = "/trigger/garage_door/with/key/";
        url += myKey;
        Serial.print("Requesting URL: ");
        Serial.println(url);
        client.print(String("POST ") + url + " HTTP/1.1\r\n" + "Host: " + "maker.ifttt.com" + "\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n" + "Content-Length: 13\r\n\r\n" + "value1=" + "Open" + "\r\n");
      }
      // Delay a little bit to avoid bouncing
      delay(50);
    }
    // save the current state as the last state, for next time through the loop
    lastcurrentState = currentState;
  }

Any help would be much appreciated.

Well at the moment you are saving currentState then on the next loop iteration setting it to false again each time.

You need the bool currentState = false in Setup first run, not each time in loop.

I would also remove the wifi preamble to Setup as you are connecting each loop run and printing it out.

It's your code but why pushbutton and a counter? Is it to count how many times door open for some other purpose. If you just want notifying it is superfluous.

I would also suggest a bit of renaming for better understanding. You are working with a door so why not use that. i.e. doorState