Pinging IFTTT with Arduino Wifi Rev 2 using if statements

Hi all,
I am trying to create an overflow detection system to alert me via gmail whenever my fish tank overflows.
I am using an Arduino Wifi Rev 2 that is connected to a float switch which can detect when the water level has overflowed.
When this happens I would like the Arduino to ping a website on IFTTT using their webhooks feature to send a gmail to myself.
I have frankensteined some code together from other tutorials and have made something that does ping the website continuously and spams me with emails even when the IF statement has not been triggered.
How do I make it so that it only sends me emails when the IF statement has been triggered.
Code is attached below.
Cheers,
Will


#define Fail 2
 #include <WiFiNINA.h> // UNO WiFi Rev.2 
  #include <Arduino.h>
  #define AP_NAME "WIFI NAME" 

  #define AP_PASSWORD "WIFI PASSWORD" 

  #define IFTTT_HOST "maker.ifttt.com" 

 #define IFTTT_KEY "KEY" 

  #define EVENT_NAME "OVERFLOW" 
  
  
  void send_event(const char *event)
  { 
    Serial.print("Connection to "); 
    Serial.println(IFTTT_HOST); 
    WiFiClient client; 
    const int httpPort = 80; 
    if(!client.connect(IFTTT_HOST, httpPort))
      { 
        Serial.print("Connection failed"); 
        return; 
      } 
    String url = "/trigger/"; 
    url += event; 
    url += "/with/key/"; 
    url += IFTTT_KEY; 
    String mJson = String("{\"value1\":\"") + "TES Gom EE~\"}"; 
    Serial.print("Requesting URL: "); 
    Serial.println(url); 
    client.println(String("POST ") + url + " HTTP/1.1"); 
    client.println(String("Host: ") + IFTTT_HOST); 
    client.println("Content-Type: application/json"); 
    client.print("Content-Length: "); 
    client.println(mJson.length()); 
    client.println(); 
    client.println(mJson); 
    while(client.connected())
      { 
        if(client.available())
          { 
            String line = client.readStringUntil('\r'); 
            Serial.print(line); 
          }else{delay(50);}; 
      } 
    Serial.println(); 
    Serial.println("closing connection"); 
    client.stop(); 
  } 

  
void setup() {

  pinMode(Fail, INPUT_PULLUP);
   Serial.begin(9600); 
    //WiFi.mode(WIFI_STA); 
    WiFi.begin(AP_NAME, AP_PASSWORD); 
    while(WiFi.status()!= WL_CONNECTED)
      { 
        delay(1000); 
        Serial.println("WiFi connecting ..."); 
      } 
    Serial.println("Connected to AP"); 
    Serial.println(WiFi.localIP()); 
}

 
void loop() {
  
  if (digitalRead(Fail)==HIGH) {
    send_event(EVENT_NAME);
  }
}

  

 

You shouldn't send an alarm every time when the sensor is HIGH, but only when it changed its state from LOW to HIGH.
Please search and read at the "State change tutorial" in the forum.

Thanks for the feedback and that will be an awesome implementation to avoid spam,
but will this fix the original issue however as I don't think that any emails should be sent when the Fail is LOW but that is what is happening.
Sorry if I'm asking a dumb question I am still new to this (:

Please post a little schematic showing the float sensor wiring. How long are the wires? Maybe an additional 1 kOhm pullup is needed.

image
Thankyou for your reply!
Above is wiring diagram,
the resistance in the wire should be negligible and is under 1m.
But if the wires did have a resistance greater than the digital 2 could detect shouldn't that still read as LOW and not trigger the email?

How do you know?

Just print the value you are getting from the digitalRead() on the fail pin. See if it is giving you the truth.

So state change detection shoukd fix everything, but you may not be getting sensible readings yet.

See https://docs.arduino.cc/built-in-examples/digital/StateChangeDetection

Instead of counting up, you can just make that be the place where a message is launched.

a7

Thanks for the reply,
I'll try when I get home (: