#include <Arduino.h>
#include <digitalWriteFast.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>


LiquidCrystal_I2C lcd(0x27, 16, 2);
// WiFi Credentials:
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqtt_server = "test.mosquitto.org";

// MQTT topics
const char*TANK_PERCENTAGE = "device/mano/data/home/watermonitoringsystem/tank/percentage";
const char*TANK_VOLUME = "device/mano/data/home/watermonitoringsystem/tank/volume";
const char*PUMP = "device/mano/data/home/watermonitoringsystem/tank/pump";
WiFiClient espClient;
PubSubClient client(espClient);
const int triggpin = 18;
const int echopin = 19;
const int pump = 13;
const float SPEED_OF_SOUND = 0.034 / 2;
///////////////////////////////////////////////////////////////////////////
const int tanksensorheight = 120;
const int tankheight = 80;
const int tankradius = 45;
const int tanktriggerlevel = 87;
const int tanktriggeroff = 15;
///////////////////////////////////////////////////////////////////////////
const double pi = 3.1415926535897932384626433832795;
int lastDistance = -1;
long duration;
int distance;

void setup()
{
  
  Serial.begin(115200);
  lcd.init();
  lcd.backlight();
  intro();
  // Set pin modes
  pinMode(triggpin, OUTPUT);
  pinMode(echopin, INPUT);
  pinMode(pump, OUTPUT);

  lcd.clear();
  setupWiFi();
  client.setServer(mqtt_server, 1883);
  client.setKeepAlive(60);
}
void loop()
{
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  ULSensor();
  int waterheight;
  if (distance <= 2) {
    waterheight = tankheight;
  } else if (distance >= tanksensorheight) {
    waterheight = 0;
  } else {
    waterheight = tanksensorheight - distance;
    if (waterheight > tankheight) {
      waterheight = tankheight;
    } else if (waterheight < 0) {
      waterheight = 0;
    }
  }
  float tankFilledPercentage = (waterheight / (float)tankheight) * 100;
  float tankvolume = (pi * (tankradius * tankradius) * waterheight) * 0.001;

  char tankFilledPercentageStr[10];
  char tankVolumeStr[10];

  dtostrf(tankFilledPercentage, 4, 2, tankFilledPercentageStr);
  dtostrf(tankvolume, 6, 2, tankVolumeStr);

  client.publish(TANK_PERCENTAGE, tankFilledPercentageStr);
  client.publish(TANK_VOLUME, tankVolumeStr);

  if (distance != lastDistance)
  {
    Serial.println("------------------------------------");
    Serial.print("Tank - Distance: ");
    Serial.print(distance);
    Serial.println(" cm");
    Serial.print("Tank - Water Height: ");
    Serial.print(waterheight);
    Serial.println(" cm");
    Serial.print("Tank - Tank Filled Percentage: ");
    Serial.print(tankFilledPercentage);
    Serial.println("%");
    Serial.print("Tank Volume :");
    Serial.print(tankvolume);
    Serial.println("L");
    Serial.println("------------------------------------");

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("TankLvL: ");
    lcd.print(tankFilledPercentage);
    lcd.print("%");
    lcd.setCursor(3, 1);
    lcd.print("Vol: ");
    lcd.print(tankvolume);
    lcd.print("L");

    if (distance >= tanktriggerlevel) {
      digitalWriteFast(pump, HIGH);
      client.publish(PUMP, "on");
    } else if (distance <= tanktriggeroff) {
      digitalWriteFast(pump, LOW);
      client.publish(PUMP, "off");
    }
    lastDistance = distance;
  }
  delay(250);
}

void intro() {
  lcd.clear();
  lcd.println("WATER MONITORING");
  lcd.setCursor(3, 1);
  lcd.println("SYSTEM:1.0");
  delay(1500);
  lcd.clear();
  lcd.setCursor(7, 0);
  lcd.println("ALL");
  lcd.setCursor(1, 1);
  lcd.println("RIGHTSRESERVED");
  delay(750);
  lcd.clear();
  lcd.setCursor(1, 0);
  lcd.println("MANOMOY MAITY");
  delay(750);
}
void ULSensor() {
  digitalWriteFast(triggpin, LOW);
  delayMicroseconds(2);
  digitalWriteFast(triggpin, HIGH);
  delayMicroseconds(10);
  digitalWriteFast(triggpin, LOW);
  duration = pulseIn(echopin, HIGH);
  distance = duration * SPEED_OF_SOUND + 1;
}
void setupWiFi() {
  delay(1000);
  Serial.print("Connecting to ");
  lcd.setCursor(0, 0);
  lcd.print("Connecting to ");
  Serial.println(ssid);
  lcd.setCursor(0, 1);
  lcd.print(ssid);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    spinner();
    Serial.print(".");
    delay(500);
  }
  Serial.println("\nConnected to WiFi");
}
void reconnect() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");

    if (client.connect("espClientWaterSystem")) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}
void spinner() {
  static int8_t counter = 0;
  const char* glyphs = "\xa1\xa5\xdb";
  lcd.setCursor(15, 0);
  lcd.print(glyphs[counter++]);
  if (counter == strlen(glyphs)) {
    counter = 0;
  }
}
NOCOMNCVCCGNDINLED1PWRRelay Module