#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#endif

#include "ThingsBoard.h"
#include "DHTesp.h"
#include <LiquidCrystal_I2C.h>

#define CURRENT_VIRMWARE_TITLE   "TEST"
#define CURRENT_VIRMWARE_VERSION "1.0.0"

#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""

#define TOKEN "ICrrVieAoI5Y8jRErgRE"//"IFzGi11WpGx9CVpuQRAf"//"UVMVMAolCKWe25Vs8JQ2"//UVMVMAolCKWe25Vs8JQ2"//xwhxhjyvzjv92dswabak //h64s923p58h6w0dhsjh1 //6tfu8tr0tcjhr1hw6h0j//lumfeqwSsRBXSaXR6p3h
#define THINGSBOARD_SERVER "thingsboard.cloud"

#define SERIAL_DEBUG_BAUD 115200

const int DHT_PIN = 15;
const int PH_SENSOR_PIN = 34;

DHTesp dhtSensor;

WiFiClient espClient;

ThingsBoard tb(espClient);

int status = WL_IDLE_STATUS;

LiquidCrystal_I2C lcd(0x27, 16, 2);
void InitWiFi()
{
  Serial.println("Connecting to AP...");
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay (500);
    Serial.println(".");
  }
}

void reconnect() {
  status = WiFi.status();
  if ( status !=WL_CONNECTED ) {
    WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
    while (WiFi.status() != WL_CONNECTED) {
      delay (500);
      Serial.println(".");
    }
    Serial.println("Connected to AP");
  }
}

void setup() {
      lcd.init();
     // lcd.createChar(0,);
      lcd.backlight();
      lcd.setCursor(0, 0);
      lcd.print("Temp &");
      lcd.setCursor(0, 1);
      lcd.print("Humudity");
      delay (4000);
      lcd.clear();

  Serial.begin(SERIAL_DEBUG_BAUD);
  Serial.println();
  InitWiFi();
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
}
// Rest of your code remains the same...

void loop() {
  delay (1000);

  if (WiFi.status() != WL_CONNECTED) {
    reconnect();
  }

  if (!tb.connected()) {
    Serial.println("Connecting to:");
    Serial.print(THINGSBOARD_SERVER);
    Serial.print(" with token ");
    Serial.print(TOKEN);
    if (!tb.connect(THINGSBOARD_SERVER, TOKEN)) {
      Serial.println("Failed to Connect");
      return;
    }
  }
  
  Serial.println("Sending Data...");
  float pHValue =7.7 ;
  float  DO = 4.85 ;
  float temperature = dhtSensor.getTemperature();
  float humidity = dhtSensor.getHumidity();
  tb.sendTelemetryFloat("temperature", temperature);
  tb.sendTelemetryFloat("humidity", humidity);
   tb.sendTelemetryFloat("pH", pHValue);
   tb.sendTelemetryFloat("DO", DO);
  Serial.print("Temperature : ");
  Serial.print(temperature);
  Serial.print("°C, Humidity : ");
  Serial.print(humidity);
  Serial.println("%");

  // Read pH sensor analog value
  int pH_sensor_value = analogRead(PH_SENSOR_PIN);
  float pH_value = convertToPH(pH_sensor_value); // Convert analog value to pH value

  tb.sendTelemetryFloat("pH_value", pH_value);
  Serial.print("pH Value: ");
  Serial.println(pH_value);

  lcd.setCursor(0, 0);
  lcd.print("Temp :" + String(temperature));
  lcd.write(0);
  lcd.print("C");

  lcd.setCursor(0, 1);
  lcd.print("Humidity :" + String(humidity) + "%");
  delay(1000);
  lcd.setCursor(0, 1);
  lcd.print("pH Value: " + String(pH_value)); // Added semicolon here
lcd.print("      ");

  tb.loop();
}

// Function to convert analog pH sensor value to pH value
float convertToPH(int sensorValue) {
    // Implement conversion logic specific to your pH sensor
    // You'll need to calibrate your sensor and use appropriate conversion formulas
    // Adjust the following line according to your sensor's range and calibration
    float pH = map(sensorValue, 0, 4095, 0, 14); // Example conversion
    return pH;
}