#include <WiFi.h>
#include <HTTPClient.h>
// Ganti dengan nama dan password WiFi Anda
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// URL server yang akan direquest
const String serverURL = "https://script.google.com/macros/s/AKfycbysGbAYwN_buI6FgSjGfjS5FsMYwfv7Jt7NiLKTCIlDuoreTyPzJFN4hF6Nmj4fomTeuQ/exec?action=update&a=14B80404&b=pulang&c=0";
// Pin untuk tombol
const int buttonPin = 4;
// Variabel untuk status tombol
bool lastButtonState = HIGH; // HIGH karena pull-up
bool currentButtonState = HIGH;
void setup() {
// Inisialisasi serial monitor
Serial.begin(115200);
// Inisialisasi pin untuk tombol
pinMode(buttonPin, INPUT_PULLUP);
// Menghubungkan ke WiFi
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
currentButtonState = digitalRead(buttonPin);
// Jika tombol ditekan (dari HIGH ke LOW)
if (lastButtonState == HIGH && currentButtonState == LOW) {
Serial.println("Button pressed, sending request to server.");
sendRequestToServer();
}
lastButtonState = currentButtonState;
delay(50); // Debounce delay
}
void sendRequestToServer() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(serverURL);
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println("HTTP Response code: " + String(httpCode));
Serial.println("Response: " + payload);
} else {
Serial.println("Error on HTTP request");
}
http.end();
} else {
Serial.println("WiFi not connected");
}
}
Loading
esp32-s2-devkitm-1
esp32-s2-devkitm-1