#include #include #include #include #include #include #define SS_PIN D4 // SDA / SS is connected to pinout D2 #define RST_PIN D3 // RST is connected to pinout D1 MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance. #define ON_Board_LED 2 #define BUZZER_PIN D8 // Define the pin for the buzzer const char* ssid = "Safirudin 3"; const char* password = "safirudin1234"; ESP8266WebServer server(80); // Server on port 80 int readsuccess; byte readcard[4]; char str[32] = ""; String StrUID; String receivedData = ""; String newBalance = ""; // Set the LCD address to 0x27 for a 16 chars and 2 line display LiquidCrystal_I2C lcd(0x27, 16, 2); void setup() { Wire.begin(); Serial.begin(115200); // Initialize serial communications with the PC SPI.begin(); // Init SPI bus mfrc522.PCD_Init(); // Init MFRC522 card delay(400); // Initialize the LCD lcd.begin(16, 2); lcd.clear(); lcd.backlight(); WiFi.begin(ssid, password); // Connect to your WiFi router Serial.println(""); pinMode(ON_Board_LED, OUTPUT); pinMode(BUZZER_PIN, OUTPUT); // Set buzzer pin as output digitalWrite(ON_Board_LED, HIGH); // Turn off Led On Board digitalWrite(BUZZER_PIN, LOW); // Ensure the buzzer is off initially Serial.print("Connecting"); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); digitalWrite(ON_Board_LED, LOW); delay(250); digitalWrite(ON_Board_LED, HIGH); delay(250); } digitalWrite(ON_Board_LED, HIGH); // Turn off the On Board LED when it is connected to the wifi router. Serial.println(""); Serial.print("Successfully connected to : "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); // Setup server routes server.on("/", HTTP_GET, handleRoot); server.begin(); Serial.println("Please tag a card or keychain to see the UID !"); Serial.println(""); } void loop() { // Handle client requests server.handleClient(); // Read RFID card readsuccess = getid(); if (readsuccess) { digitalWrite(ON_Board_LED, LOW); digitalWrite(BUZZER_PIN, HIGH) ; //DO note 523 Hz delay (250); // Turn on the buzzer digitalWrite(BUZZER_PIN, LOW) ; HTTPClient http; // Declare object of class HTTPClient WiFiClient client; String UIDresultSend = StrUID; String postData = "UIDresult=" + UIDresultSend; http.begin(client, "http://192.168.171.31/Ridho/getUID.php"); http.addHeader("Content-Type", "application/x-www-form-urlencoded"); // Specify content-type header int httpCode = http.POST(postData); // Send the request String payload = http.getString(); // Get the response payload Serial.println(UIDresultSend); Serial.println(httpCode); // Print HTTP return code Serial.println(payload); // Print request response payload receivedData = payload; // Store received data http.end(); // Close connection delay(2000); // Make a GET request to receive new balance data if (WiFi.status() == WL_CONNECTED) { http.begin(client, "http://192.168.171.31/Ridho/sendNewBalance.php"); int httpCode = http.GET(); Serial.println("HTTP GET code: " + String(httpCode)); if (httpCode > 0) { if (httpCode == HTTP_CODE_OK) { newBalance = http.getString(); Serial.println("New balance received: " + newBalance); // Parse newBalance to extract newName and dataBalance int separatorIndex = newBalance.indexOf('|'); if (separatorIndex != -1) { String dataName = newBalance.substring(0, separatorIndex); String dataBalance = newBalance.substring(separatorIndex + 1); // Display data on LCD lcd.clear(); lcd.setCursor(0, 0); lcd.print(dataName); lcd.setCursor(0, 1); lcd.print(dataBalance); delay(4000); lcd.clear(); } else { Serial.println("Failed to parse response: no delimiter found"); } } else { Serial.println("Failed to get new balance, response code: " + String(httpCode)); } } else { Serial.printf("[HTTP] GET request failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); } else { Serial.println("WiFi not connected"); } delay(250); digitalWrite(BUZZER_PIN, LOW); // Turn off the buzzer digitalWrite(ON_Board_LED, HIGH); } } int getid() { if (!mfrc522.PICC_IsNewCardPresent()) { return 0; } if (!mfrc522.PICC_ReadCardSerial()) { return 0; } Serial.print("THE UID OF THE SCANNED CARD IS : "); for (int i = 0; i < 4; i++) { readcard[i] = mfrc522.uid.uidByte[i]; // Storing the UID of the tag in readcard array_to_string(readcard, 4, str); StrUID = str; } mfrc522.PICC_HaltA(); return 1; } void array_to_string(byte array[], unsigned int len, char buffer[]) { for (unsigned int i = 0; i < len; i++) { byte nib1 = (array[i] >> 4) & 0x0F; byte nib2 = (array[i] >> 0) & 0x0F; buffer[i * 2 + 0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA; buffer[i * 2 + 1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA; } buffer[len * 2] = '\0'; } void handleRoot() { server.send(200, "text/plain", receivedData); } // Function to scroll text on the LCD from right to left void scrollText(String message) { message = " " + message + " "; // Add spaces to the start and end for scrolling effect for (int i = 0; i < message.length() - 16; i++) { lcd.clear(); lcd.setCursor(0, 0); lcd.print(message.substring(i, i + 16)); // Print a substring of the message delay(200); // Delay to control the scroll speed lcd.clear(); } }