#include <Wire.h>
#include <WiFi.h>
#include <HTTPClient.h>
#define SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""

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

int ledPin = 12;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;  
int t;                  // variable for reading the pin status

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input

  Serial.begin(9600);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print("Menghubungkan ke WiFi ");
    Serial.println(ssid);
  }

  Serial.print("Terhubung ke ");
  Serial.println(ssid);

}

void loop() {
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);
    Serial.println("gerak terdeteksi");  // turn LED ON
    String url = "http://colortex.my.id/iot_lampu.php?tekan=1";

  } else {
    digitalWrite(ledPin, LOW); // turn LED OFF
    Serial.println("gerak berhenti !");
    String url = "http://colortex.my.id/iot_lampu.php?tekan=0";

  }

 if (WiFi.status() == WL_CONNECTED) {
    WiFiClient client;
    HTTPClient http;

    //String url = "http://colortex.my.id/iot_lampu.php?tekan=" + t;
    http.begin(client, "http://colortex.my.id/iot_lampu.php?tekan=0");
    int httpCode = http.GET();

    if (httpCode > 0) {
      String response = http.getString();
      int responseValue = response.toInt(); // Mengubah respons ke tipe data int

      if (responseValue == 1) { // Membandingkan dengan nilai angka
        digitalWrite(7, HIGH);


      } else {
        digitalWrite(7, LOW);

      }
    }

    http.end();
  }

  delay(1000);


}