#include <NewPing.h>
#include <PubSubClient.h>
// #include <ESP8266WiFi.h>
#include <WiFi.h>


const char *ssid = "Wokwi-GUEST";
const char *password = "";

// Define your MQTT broker information
const char *mqtt_server = "test.mosquitto.org";
const int mqtt_port = 1883;


// Define MQTT topics
const char *distance_topic = "device/mano/data/WaterTank1/height_sonar";
const char *volume_topic1 = "device/mano/data/WaterTank1/volume_sonar";
const char *volume_topic2 = "device/mano/data/WaterTank1/volume_gravity";
const char *height_topic = "device/mano/data/WaterTank1/height_gravity";
const char *resetTopic = "device/mano/data/WaterTank1/device1/reset";

const float OffSet = 0.492;
float V, P;
const int levelDetectionPin = 36;
const float tankRadius = 50;
const float tankLength = 100;
float waterVolume;
#define ledPin 13
#define TRIGGER_PIN 4
#define ECHO_PIN 5
NewPing sonar(TRIGGER_PIN, ECHO_PIN);

WiFiClient espClient;
PubSubClient client(espClient);
unsigned long time_now = 0;
int period = 10000;

void setup_wifi() {
  Serial.println();
  Serial.print("Connecting to WiFi...");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    if (millis() >= time_now + period) {
      ESP.restart();
    }
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char *topic, byte *payload, unsigned int length) {
  Serial.println("Inside callback...");
  Serial.print("Message arrived on topic [");
  Serial.print(topic);
  Serial.print("] Payload: ");

  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  String message;
  for (int i = 0; i < length; i++) {
    message += (char)payload[i];
  }

  // Handle LED control messages

  if (String(topic) == resetTopic) {
    if (message == "reset") {
      Serial.println("Resetting ESP32...");
      delay(1000);
      // ESP.wdtDisable();  // Disable Watchdog Timer
      ESP.restart();  // Perform a software reset
    }
  }
}

void reconnect() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    if (client.connect("ArduinoClient")) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}

float waterLevel() {
  V = analogRead(levelDetectionPin) * 3.3 / 4096;  //Sensor output voltage
  P = (V - OffSet) * 250;                          //Calculate water pressure

  Serial.print("Voltage:");
  Serial.print(V, 3);
  Serial.println("V");

  Serial.print(" Pressure:");
  Serial.print(P, 1);
  Serial.println(" KPa");
  Serial.println();

  float Height = (P / 9.81);
  Serial.print(" Height:");
  Serial.print(Height);
  Serial.println(" Metre");
  Serial.println();
  calculateWaterVolume(Height);
  return Height;
}
float calculateWaterVolume(float height) {
  // Calculate the cross-sectional area of the water
  if (height >= 0 && height <= tankRadius * 2) {

    // Calculate the area of the water surface
    float waterArea;
    float angle = acos((tankRadius - height) / tankRadius);
    waterArea = angle * tankRadius * tankRadius - (tankRadius - height) * sqrt(2 * tankRadius * height - height * height);


    // Calculate the volume of water using the area and length
    waterVolume = waterArea * tankLength;
    Serial.print("Volume of water in the tank: ");
    Serial.print(waterVolume);
    Serial.println("Litre");

  } else {
    // Handle the case where the water level is out of the valid range
    printf("Invalid water level\n");
  }
  return waterVolume;
}

void setup() {
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, mqtt_port);
  client.setCallback(callback);
  client.subscribe(resetTopic);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  // Measure height using pressure sensor
  unsigned int height = waterLevel();
  unsigned int distance = sonar.ping_cm();
  unsigned int volume_sonar = calculateWaterVolume(tankRadius-distance);
  unsigned int volume_gravity = calculateWaterVolume(height);


  // Publish distance to MQTT
  char message1[10];
  sprintf(message1, "%d", distance);
  client.publish(distance_topic, message1);

  // Publish volume to MQTT
  char message2[10];
  sprintf(message2, "%d", volume_sonar/1000);
  client.publish(volume_topic1, message2);

  // Publish height to MQTT
  char message3[10];
  sprintf(message3, "%d", height);
  client.publish(height_topic, message3);

  char message4[10];
  sprintf(message4, "%d", volume_gravity);
  client.publish(volume_topic2, message4);

  delay(5000);  // Adjust the delay based on your requirements
}