#include <ThingSpeak.h>            // Menambahkan Library ThingSpeak
#include <WiFi.h>                  // Menambahkan Library WiFi

const char * SSID = "Wokwi-GUEST"; // Nama WiFI
const char * PASS = "";            // Password WiFi
WiFiClient  client;               

int lampu1 = 14;                   // Lampu1 dihubungkan ke D14
int lampu2 = 4;                    // Lampu2 dihubungkan ke D4

unsigned long ChannelNumber =  1769382;       // Nomor Channel ThingSpeak           
const char * ReadAPIKey = "FJSAEFWZI8V2HFJO"; // Nomor Read API key
const int FieldNumber1 = 1;                   // Nomor field 1                         
const int FieldNumber2 = 2;                   // Nomor field 2                    

void setup()
{

  pinMode(lampu1, OUTPUT);
  pinMode(lampu2, OUTPUT);
  Serial.begin(115200);
  Serial.println();

  WiFi.begin(SSID, PASS);                                
  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());
  ThingSpeak.begin(client);
}

void loop() 
{
 int A = ThingSpeak.readLongField(ChannelNumber, FieldNumber1, ReadAPIKey);
 int B = ThingSpeak.readLongField(ChannelNumber, FieldNumber2, ReadAPIKey);

  // Menghidupkan dan mematikan lampu1
  if(A == 1){
    Serial.println("lampu1 Nyala");
    digitalWrite(lampu1, HIGH);
  } else if(A == 0) {
    Serial.println("lampu1 Mati");
    digitalWrite(lampu1, LOW);
  }

  // Menghidupkan dan mematikan lampu2
  if(B == 1){
    Serial.println("lampu2 Nyala");
    digitalWrite(lampu2, HIGH);
  } else if(B == 0){
    Serial.println("lampu2 Mati");
    digitalWrite(lampu2, LOW);
  } 
}