From 24b863d87b418bccd49cfce0ebc257e1dbf181b8 Mon Sep 17 00:00:00 2001 From: Shamir Date: Wed, 16 Jul 2025 10:47:41 +0700 Subject: [PATCH] code arduino --- esp8266_code.ino | 112 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 esp8266_code.ino diff --git a/esp8266_code.ino b/esp8266_code.ino new file mode 100644 index 0000000..50006dc --- /dev/null +++ b/esp8266_code.ino @@ -0,0 +1,112 @@ +#include +#include +#include +#include +#include + +#define RST_PIN 5 // Pin RST pada ESP8266 = d1 +#define SS_PIN 4 // Pin SDA (SS) pada ESP8266 = d2 +#define BUZZER_PIN 15 // Pin untuk buzzer (gunakan GPIO2 / D4) + +const char* ssid = "jtilt3"; // Ganti dengan SSID Wi-Fi +const char* password = "12345678"; // Ganti dengan password Wi-Fi + +const String serverUrl = "http://192.168.5.238/keranjang.php"; // URL server + +MFRC522 mfrc522(SS_PIN, RST_PIN); // Inisialisasi RFID reader + +void setup() { + Serial.begin(115200); + SPI.begin(); + mfrc522.PCD_Init(); + + pinMode(BUZZER_PIN, OUTPUT); // Atur pin buzzer sebagai output + digitalWrite(BUZZER_PIN, LOW); // Matikan buzzer di awal + + // Koneksi Wi-Fi + Serial.println("Connecting to Wi-Fi..."); + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(700); + Serial.print("."); + buzz(150); + } + Serial.println("\nConnected to Wi-Fi"); + Serial.println("IP Address: " + WiFi.localIP().toString()); + buzz(600); +} + +void loop() { + // Deteksi RFID baru + if (!mfrc522.PICC_IsNewCardPresent() || !mfrc522.PICC_ReadCardSerial()) { + delay(50); + return; + } + + // Ambil UID dari RFID tag + String tagID = ""; + for (byte i = 0; i < mfrc522.uid.size; i++) { + tagID += String(mfrc522.uid.uidByte[i], HEX); + } + tagID.toUpperCase(); + Serial.println("Tag detected: " + tagID); + + + buzz(100); + + // Kirim data ke server + sendToServer(tagID); + delay(1000); +} + +void sendToServer(String tagID) { + if (WiFi.status() == WL_CONNECTED) { + WiFiClient client; + HTTPClient http; + + String postData = "keranjang=1&rfid=" + tagID; // Kirim data keranjang dan rfid_tag + + http.begin(client, serverUrl); + http.addHeader("Content-Type", "application/x-www-form-urlencoded"); + + Serial.println("Sending data to server..."); + int httpResponseCode = http.POST(postData); // Kirim data + + if (httpResponseCode > 0) { + String response = http.getString(); + Serial.println("Server response: " + response); + + + buzz(200); + } else { + Serial.println("Failed to send data. HTTP Response code: " + String(httpResponseCode)); + + + buzz(500); + } + + http.end(); + } else { + Serial.println("Wi-Fi disconnected, cannot send data."); + + + buzz(300); + } +} + +void checkServerConnection() { + WiFiClient client; + if (client.connect("192.168.5.238", 80)) { + Serial.println("Server is reachable!"); + client.stop(); + } else { + Serial.println("Cannot reach the server."); + } +} + +// Fungsi untuk mengontrol buzzer +void buzz(int duration) { + digitalWrite(BUZZER_PIN, HIGH); + delay(duration); + digitalWrite(BUZZER_PIN, LOW); +}