#include <WiFi.h>
#include <PubSubClient.h>
#include <LiquidCrystal_I2C.h>
#include "DHTesp.h"


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

//***Set server***
const char* mqttServer = "broker.mqtt-dashboard.com"; 
int port = 1883;

//Set wifi
WiFiClient espClient;
PubSubClient client(espClient);

//Set temperature sensor
const int DHT_PIN = 15;
DHTesp dhtSensor;

//Set lcd 
LiquidCrystal_I2C lcd(0x27, 20, 4);

//Set ultrosonic sensor
const int TRIG_PIN = 2;
const int ECHO_PIN = 4;

//Set relay, motor simulation by led
bool isAutoPump = false;
bool turnOnPump = false;
const int RELAY_PIN = 23;


//Init minimum amount of temp, water, pH
float minOfWater = 5;
float minOfTemp = 20;
float maxOfTemp = 50;
float minOfpH = 0;
float maxOfpH = 14;

//Config warning led
const int WARN_LED = 5;


// 
bool isSafeTemp = true;
bool isSafeWater = true;
bool isSafepH = true;

void wifiConnect() {
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(" Connected!");
}

void mqttReconnect() {
  while(!client.connected()) {
    Serial.println("Attemping MQTT connection...");
    //***Change "123456789" by your student id***
    if(client.connect("20127465")) {
      Serial.println("connected");

      //***Subscribe all topic you need***
      client.subscribe("physhics/Gr01/pump_button");
      client.subscribe("physhics/Gr01/set_max_temp");
      client.subscribe("physhics/Gr01/set_min_temp");
      client.subscribe("physhics/Gr01/set_water");
      client.subscribe("physhics/Gr01/auto_pump");
      client.subscribe("physhics/Gr01/set_ph_max");
      client.subscribe("physhics/Gr01/set_ph_min");
    }
    else {
      Serial.println("try again in 5 seconds");
      delay(5000);
    }
  }
}

float getDistance() {
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  float duration_us = pulseIn(ECHO_PIN, HIGH);
  float distance_cm = 0.017 * duration_us;

  return distance_cm;
}

void checkTemp(float celsius, float minTemp, float maxTemp) {
  lcd.setCursor(0, 1);
  lcd.print("Nhiet do: ");
  lcd.setCursor(13, 1);
  lcd.print(String(celsius));

  if (celsius < minOfTemp || celsius > maxOfTemp) {
    isSafeTemp = false;
  } 
  else isSafeTemp = true;
}

void checkAmountOfWater(float distance, float minAmountOfWater) {
  lcd.setCursor(0, 0);
  lcd.print("Luong nuoc: ");
  lcd.setCursor(13,0);
  lcd.print(String(distance));
  if (distance < minAmountOfWater) {
    if (isAutoPump || turnOnPump) {
      digitalWrite(RELAY_PIN,1);
    }
    else {
      digitalWrite(RELAY_PIN,0);
    }
    isSafeWater = false;
  } 
  else if (distance < 12.9) {
    if (turnOnPump) {
      digitalWrite(RELAY_PIN,1);
    }
    isSafeWater = true;
  }
  else {
    digitalWrite(RELAY_PIN,0);
    turnOnPump = false;
    isAutoPump = false;
    isSafeWater = true;
  }
}

void checkpH(float pH) {
  lcd.setCursor(0, 2);
  lcd.print("pH: ");
  lcd.setCursor(13, 2);
  lcd.print(String(pH));

  if (pH < minOfpH || pH > maxOfpH) {
    isSafepH = false;
  }
  else isSafepH = true;
}

void checkWarnLed() {
  if (!isSafeTemp || !isSafeWater || !isSafepH) {
    digitalWrite(WARN_LED, 1);
  }
  else {
    digitalWrite(WARN_LED, 0);
  }
}

void setup() {
  lcd.init();
  lcd.backlight();

  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);

  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(RELAY_PIN, OUTPUT);
  pinMode(WARN_LED, OUTPUT);

  Serial.begin(9600);
  Serial.print("Connecting to WiFi");

  wifiConnect();
  client.setServer(mqttServer, port);
  client.setCallback(callback);
}

//MQTT Send
void sendTemp(String info) {
  char* c = const_cast<char*>(info.c_str());
  
  client.publish("physhics/Gr01/temperature_sensor", c);
}
void sendpH(String info) {
  char* c = const_cast<char*>(info.c_str());
  
  client.publish("physhics/Gr01/pH_sensor", c);
}
void sendHeightOfWater(String info) {
  char* c = const_cast<char*>(info.c_str());
  
  client.publish("physhics/Gr01/water_sensor", c);
}

//MQTT Receiver
void callback(char* topic, byte* message, unsigned int length) {
  String _topic;
  for(int i=0; i < strlen(topic); i++) {
    _topic += topic[i];
  }
  Serial.println(_topic);
  String strMsg;
  for(int i=0; i<length; i++) {
    strMsg += (char)message[i];
  }
  Serial.println(strMsg);
  //***Insert code here to control other devices***
  if (_topic == "physhics/Gr01/auto_pump") {
    if (strMsg == "true") {
      isAutoPump = true;
    }
    else isAutoPump = false;
  }
  else if (_topic == "physhics/Gr01/pump_button") {
    if (strMsg == "true") {
      turnOnPump = true;
    }
    else {
      turnOnPump = false;
    }
  }
  else if (_topic == "physhics/Gr01/set_max_temp") {
    maxOfTemp = strMsg.toInt();
  }
  else if (_topic == "physhics/Gr01/set_min_temp") {
    minOfTemp = strMsg.toInt();
  }
  else if (_topic == "physhics/Gr01/set_water") {
    minOfWater = strMsg.toInt();
  }
  else if (_topic == "physhics/Gr01/set_ph_min") {
    minOfpH = strMsg.toInt();
  }
  else if (_topic == "physhics/Gr01/set_ph_max") {
    maxOfpH = strMsg.toInt();
  }
}


void loop() {
  if(!client.connected()) {
    mqttReconnect();
  }
  client.loop();
  
  TempAndHumidity data = dhtSensor.getTempAndHumidity();
  float celsius = data.temperature;
  float pH = data.humidity / 100 * 14;
  float heightOfWater = 17 - getDistance();

  checkAmountOfWater(heightOfWater, minOfWater);
  checkTemp(celsius, minOfTemp, maxOfTemp);
  checkpH(pH);
  checkWarnLed();

  sendHeightOfWater(String(heightOfWater));
  sendTemp(String(celsius));
  sendpH(String(pH));
}
$abcdeabcde151015202530fghijfghij
NOCOMNCVCCGNDINLED1PWRRelay Module