#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include <WiFi.h>
#include <HTTPClient.h>
// Deklarasi variabel untuk ukuran layar OLED
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET -1
// Inisialisasi pin
int ledPin = 12;
int inputPin = 5;
int pirState = LOW;
int val = 0;
const char *ssid = "Wokwi-GUEST";
const char *password = "";
// Inisialisasi objek display
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(inputPin, INPUT);
WiFi.begin(ssid, password);
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("Gagal menginisialisasi SSD1306"));
for (;;)
;
}
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
}
void loop() {
val = digitalRead(inputPin);
if (val == HIGH) {
digitalWrite(ledPin, HIGH);
if (pirState == LOW) {
Serial.println("Gerak Terdeteksi!");
pirState = HIGH;
}
} else {
if (WiFi.status() == WL_CONNECTED) {
WiFiClient client;
HTTPClient http;
String url = "http://colortex.my.id/cek_lampu.php";
http.begin(client, url.c_str());
int httpCode = http.GET();
if (httpCode > 0) {
String response = http.getString();
int responseValue = response.toInt();
if (responseValue == 1) {
http.begin("http://colortex.my.id/iot_lampu.php?tekan=1");
httpCode = http.GET();
delay(500);
} else {
http.begin("http://colortex.my.id/iot_lampu.php?tekan=0");
httpCode = http.GET();
digitalWrite(ledPin, LOW);
if (pirState == HIGH) {
Serial.println("Gerak Berhenti!");
pirState = LOW;
}
}
}
}
}
}