#include <WiFi.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#include "ThingSpeak.h"

const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
const int WIFI_CHANNEL = 6; 

const int myChannelNumber = 2586267;  // Nomor channel ThingSpeak
const char* myWriteApiKey = "09183N3ORDC23K8A";  // Kunci API untuk menulis data ke ThingSpeak
const char* myReadApiKey = "9LO51I9C3W8XV3BO";  // Kunci API untuk membaca data dari ThingSpeak
const char* server = "api.thingspeak.com";  // Alamat server ThingSpeak
WiFiClient client;

#define DHT_PIN 25
#define DHT_TYPE DHT22
#define POT_PIN 34
#define LED_NORMAL 14
#define LED_ABNORMAL 12
#define LED_WARNING 13
DHT dht{ DHT_PIN, DHT_TYPE };
LiquidCrystal_I2C lcd(0x27, 16, 4);

const int BUZZER = 23;

void setup() {
  Serial.begin(115200);
  lcd.init();
  lcd.backlight();
  lcd.clear();

  pinMode(LED_NORMAL, OUTPUT);
  pinMode(LED_WARNING, OUTPUT);
  pinMode(LED_ABNORMAL, OUTPUT);

  Serial.print("Connecting to WiFi \n");
  WiFi.begin(WIFI_NAME, WIFI_PASSWORD, WIFI_CHANNEL);
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.print(".");
  }
  Serial.println("\nConnected!");
  ThingSpeak.begin(client);  // Mulai koneksi dengan ThingSpeak
}

void loop() {
  // Membaca value temperatur dan kelembaban dari komponen DHT22 
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  // Konversi nilai potensiometer menjadi nilai pH
  // Potentiometer hanya digunakan sebagai alat simulasi untuk nilai Ph
  // Karena di wokwi tidak terdapat PhMeter yang dapat digunakan untuk
  // Menganalisis Ph Tanah
  int potValue = analogRead(POT_PIN);
  float ph = potValue/292.5;

  noTone(BUZZER);
  setValueToLcd("Melon", ph, temperature, humidity);

  if((ph >= 5.5 && ph < 6.3) || (humidity > 60 && humidity < 70)) {
    switchLed("yellow");
    tone(BUZZER, 50);

    if(ph >= 5.5 && ph < 6.3) {
      lcd.setCursor(0,3);
      lcd.print("PH Tanah < 6.3");
      delay(3000);
    }

    if(humidity > 60 && humidity < 70) {
      lcd.setCursor(0,3);
      lcd.print("Kelembaban < 70");
      delay(3000);
    }
  }
  
  if(
    (ph < 5.5 || ph > 7.0) ||
    (humidity < 60 || humidity > 80) ||
    (temperature < 25 || temperature > 30)
  ) {
    switchLed("red");
    tone(BUZZER, 400);

    if(ph < 5.5 || ph > 7.0) {
      lcd.setCursor(0,3);
      lcd.print("cek PH Tanah");
      delay(3000);
    }

    if(humidity < 60 || humidity > 80) {
      lcd.setCursor(0,3);
      lcd.print("cek Kelembaban");
      delay(3000);
    }

    if(temperature < 25 || temperature > 30) {
      lcd.setCursor(0,3);
      lcd.print("cek Suhu Tanah");
      delay(3000);
    }
  } else if(
    (ph > 6.3 && ph < 7.0) ||
    (humidity > 70 && humidity < 80) ||
    (temperature > 25 && temperature < 30)
  ) {
    switchLed("green");

    lcd.setCursor(0,3);
    lcd.print("Tanaman normal");
  }

  //Set value ke dalam field sesuai channel Thing yang telah dibuat
  ThingSpeak.setField(1, ph);
  ThingSpeak.setField(2, temperature);
  ThingSpeak.setField(3, humidity);

  //Kirim data ke ThingSpeak
  int result = ThingSpeak.writeFields(myChannelNumber, myWriteApiKey);  // Kirim data ke ThingSpeak

  //Check apakah pengiriman data ke ThingSpeak berhasil atau tidak
  if(result == 200) Serial.println("Berhasil");
  delay(20000);
}

void setValueToLcd(String tanaman, float ph, float temp, float humidity) {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Ph Tanah: ");
  lcd.print(ph);
  lcd.setCursor(0, 1);
  lcd.print("Suhu: ");
  lcd.print(temp);
  lcd.print(" C");
  lcd.setCursor(0, 2);
  lcd.print("Kelembaban: ");
  lcd.print(humidity);
  lcd.print(" %");
}

void switchLed(String color) {
  digitalWrite(LED_NORMAL, LOW);
  digitalWrite(LED_WARNING, LOW);
  digitalWrite(LED_ABNORMAL, LOW);

  if(color == "red") digitalWrite(LED_ABNORMAL, HIGH);
  else if(color == "yellow") digitalWrite(LED_WARNING, HIGH);
  else if(color == "green") digitalWrite(LED_NORMAL, HIGH);
}
$abcdeabcde151015202530354045505560fghijfghij
esp:0
esp:2
esp:4
esp:5
esp:12
esp:13
esp:14
esp:15
esp:16
esp:17
esp:18
esp:19
esp:21
esp:22
esp:23
esp:25
esp:26
esp:27
esp:32
esp:33
esp:34
esp:35
esp:3V3
esp:EN
esp:VP
esp:VN
esp:GND.1
esp:D2
esp:D3
esp:CMD
esp:5V
esp:GND.2
esp:TX
esp:RX
esp:GND.3
esp:D1
esp:D0
esp:CLK
pot1:GND
pot1:SIG
pot1:VCC
dht1:VCC
dht1:SDA
dht1:NC
dht1:GND
led1:A
led1:C
led2:A
led2:C
led4:A
led4:C
lcd2:GND
lcd2:VCC
lcd2:SDA
lcd2:SCL
bz1:1
bz1:2