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);
}
}
