// include the library code: (ada beberapa yang di add di library manager)
#include <LiquidCrystal.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include "ThingSpeak.h"

//WiFi dan password
const char ssid[] = "Wokwi-GUEST";
const char password[] = "";

//http and wifi client
HTTPClient http;
WiFiClient client;

//define pengembilan data dari thingspeak
unsigned long myChannelNumber = 2025579;
const char* myReadAPIKey = "6LY7SR1XZCUU5Z4N";
int statusCode = 0;
unsigned int HumidityFieldNumber = 1;
unsigned int TemperatureFieldNumber = 2;
unsigned int pinStateCurrentFieldNumber =3;

//define variable yang digunakan 
const int buzzer = 4;
float hum = 0;
float temp = 0;
int pinStateCurrent = 0;
int LAMP = 5;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(19, 23, 18, 17, 16, 15);


void setup() {

  Serial.begin(115200);
  //output untuk aktuator 
  pinMode (LAMP,OUTPUT);
  pinMode(buzzer, OUTPUT);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  
  // Untuk connect ke wifi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected");
  ThingSpeak.begin(client);
  WiFi.mode(WIFI_MODE_STA);
  Serial.println(WiFi.macAddress());
}
 
void loop() {
  // Mengambil data dari thingspeak
  int statusCode = 0;
  hum = ThingSpeak.readLongField(myChannelNumber, HumidityFieldNumber, myReadAPIKey);
  temp = ThingSpeak.readLongField(myChannelNumber, TemperatureFieldNumber, myReadAPIKey);
  pinStateCurrent = ThingSpeak.readLongField (myChannelNumber,pinStateCurrentFieldNumber, myReadAPIKey);
  statusCode = ThingSpeak.getLastReadStatus();

  //set untuk tampilan di lcd
  lcd.setCursor(0, 1);
  //suhu dan kelembapan
  if (statusCode == 200){
    lcd.println("Humidity: " + String(hum));
    lcd.println("Temperature: " + String(temp));
    Serial.println("Humidity: " + String(hum));
    Serial.println("Temperature: " + String(temp));
  }
  else{
    Serial.println("Problem reading channel. HTTP error code" + String (statusCode));
  }

  //wifi info di terminal
  Serial.println ("Scan WiFi....");
  int networks = WiFi.scanNetworks();
  if(networks == 0){
    Serial.println ("No WiFi Networks!");
  }else{
    Serial.print(networks);
    Serial.println("Networks Found!");
    for(int i=0; i < networks; i++){
      Serial.print("WiFi ke- ");
      Serial.println(i+1);
      Serial.println(WiFi.SSID(i));
      Serial.println(WiFi.RSSI(i));
      delay(10);
    }
  }
  // memanggil fungsi untuk lampu dan buzzer yang dimana akan mengeluarkan output sesuai dengan data terakhir di thingspeak
  lampu(LAMP,temp,hum);
  motion(pinStateCurrent,buzzer);
}

// fungsi lampu
void lampu(int LAMP,int temp, int hum){
  if(hum < 20 || temp > 35){
    digitalWrite(LAMP, HIGH);
    delay(1000);
  }
  else{
    digitalWrite(LAMP, LOW);
  }
  
}

//fungsi untuk buzzer 
void motion(int pinStateCurrent, int buzzer){
  if (pinStateCurrent == HIGH){
    tone(buzzer, 1000);
    delay(1000);
  }
  else if(pinStateCurrent == LOW){
    noTone(buzzer);
  }
}