#include <WiFi.h>
#include <HTTPClient.h>
#include <ESP32Servo.h>
#define merah 23
#define hijau 22
#define biru 21
Servo myservo1;
Servo myservo2;
const char* ssid = "Wokwi-GUEST"; // Replace with your WiFi SSID
const char* password = ""; // Replace with your WiFi password
const char* server = "https://api.thingspeak.com/update";
const char* apiKey = "8ZD9M3MAJ5WXZILB"; // Replace with your ThingSpeak Write API Key
const int LDR_PIN = 34;
const int LDR_PIN2= 35;
unsigned long previousMillis = 0; // Menyimpan waktu sebelumnya
const long interval = 20000; // Interval waktu (10 detik)
void setup() {
pinMode(merah, OUTPUT);
pinMode(hijau, OUTPUT);
pinMode(biru, OUTPUT);
Serial.begin(115200);
WiFi.begin(ssid, password);
myservo1.attach(14);
myservo2.attach(15);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
unsigned long currentMillis = millis(); // Ambil waktu saat ini
int ldrValue = analogRead(LDR_PIN); // Read LDR value
int ldrValue2 = analogRead(LDR_PIN2); // Read LDR value
int sudut = map(ldrValue, 0, 4095, 0, 180);
int sudut2 = map(ldrValue2, 0, 4095, 0, 180);
Serial.print("LDR Value1: ");
Serial.println(ldrValue); // Print LDR value
Serial.print("LDR Value1: ");
Serial.println(ldrValue2); // Print LDR value
myservo1.write(sudut);
myservo2.write(sudut2);
if (currentMillis - previousMillis >= interval) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(String(server) + "?api_key=" + apiKey + "&field1=" + String(ldrValue) + "&field2=" + String(ldrValue2));
int httpCode = http.GET();
if (httpCode > 0) {
Serial.println("Data sent to ThingSpeak");
} else {
Serial.println("Error sending data: " + String(httpCode));
}
http.end();
}
previousMillis = currentMillis;
}
if (sudut = sudut2) {
}
}