#include <Ultrasonic.h>
#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

// Define the request URL (replace with your endpoint URL)
String requestUrl = "";

#define TRIG 8
#define ECHO 9

int threshold = 20;
Ultrasonic water_checker(TRIG, ECHO);

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  Serial.println("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  int distance = water_checker.read();
  Serial.println(distance);
  delay(1000);

  if (distance > threshold) {
    sendEmail();
  }
  delay(60000);
}

void sendEmail() {
  if (WiFi.status() == WL_CONNECTED) {
    WiFiClient client;
    HTTPClient http;

    http.begin(client, requestUrl);
    http.addHeader("Content-Type", "application/json");
    String message = "{\"message\":\"true\"}";
    int httpResponseCode = http.POST(message);
    
    if (httpResponseCode > 0) {
      Serial.print("HTTP Response code: ");
      Serial.println(httpResponseCode);
    } else {
      Serial.print("Error code: ");
      Serial.println(httpResponseCode);
    }

    http.end();
  } else {
    Serial.println("WiFi Disconnected");
  }
}