Card cannot detected

im using esp32, mfrc522 and lcd for attendance system.
At first, its all working fine.
Now its not working suddenly, like i havent change anything

#include <SPI.h>
#include <MFRC522.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <driver/adc.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#define SS_PIN 15
#define RST_PIN 13
#define DOOR_PIN 2
#define TERMINAL_NAME "basement"

// SCRIPT LINK MUST BE LIKE THIS "https://script.google.com/macros/s/AKfycbzBCsPz3ZDVUkdP7mloaX1AAKI1mC_NxM802hvDNRwyE4vw4oo/exec"
const char *mainLinkForSpr = "https://script.google.com/macros/s/AKfycbwLmlPHYmb4Ro9pPK30Nf_pU1RnEd9V9h6Ccw2wgkp4wG3RjvwMT6JDQlXp1fCJ8v4U/exec";

const char *ssid = "Zarulmiqhail";
const char *password = "password";
LiquidCrystal_I2C lcd(0x27, 20, 4);
MFRC522 mfrc522(SS_PIN, RST_PIN);

MFRC522::StatusCode status;

uint64_t clearDisplayTimer = 0;
bool needDisplayUpdate = true;

const char *root_ca =
"-----BEGIN CERTIFICATE-----\n" \
"MIIFVzCCAz+gAwIBAgINAgPlk28xsBNJiGuiFzANBgkqhkiG9w0BAQwFADBHMQsw\n" \
"CQYDVQQGEwJVUzEiMCAGA1UEChMZR29vZ2xlIFRydXN0IFNlcnZpY2VzIExMQzEU\n" \
"MBIGA1UEAxMLR1RTIFJvb3QgUjEwHhcNMTYwNjIyMDAwMDAwWhcNMzYwNjIyMDAw\n" \
"MDAwWjBHMQswCQYDVQQGEwJVUzEiMCAGA1UEChMZR29vZ2xlIFRydXN0IFNlcnZp\n" \
"Y2VzIExMQzEUMBIGA1UEAxMLR1RTIFJvb3QgUjEwggIiMA0GCSqGSIb3DQEBAQUA\n" \
"A4ICDwAwggIKAoICAQC2EQKLHuOhd5s73L+UPreVp0A8of2C+X0yBoJx9vaMf/vo\n" \
"27xqLpeXo4xL+Sv2sfnOhB2x+cWX3u+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JT/KYnAK9y8J\n" \
"ZgfIPxz88NtFMN9iiMG1D53Dn0reWVlHxYciNuaCp+0KueIHoI17eko8cdLiA6Ef\n" \
"MgfdG+RCzgwARWGAtQsgWSl4vflVy2PFPEz0tv/bal8xa5meLMFrUKTX5hgUvYU/\n" \
"Z6tGn6D/Qqc6f1zLXbBwHSs09dR2CQzreExZBfMzQsNhFRAbd03OIozUhfJFfbdT\n" \
"6u9AWpQKXCBfTkBdYiJ23//OYb2MI3jSNwLgjt7RETeJ9r/tSQdirpLsQBqvFAnZ\n" \
"0E6yove+7u7Y/9waLd64NnHi/Hm3lCXRSHNboTXns5lndcEZOitHTtNCjv0xyBZm\n" \
"2tIMPNuzjsmhDYAPexZ3FL//2wmUspO8IFgV6dtxQ/PeEMMA3KgqlbbC1j+Qa3bb\n" \
"bP6MvPJwNQzcmRk13NfIRmPVNnGuV/u3gm3c\n" \
"-----END CERTIFICATE-----\n";

// Helper function for LCD and Serial display
void dualPrint(const __FlashStringHelper* text)
{
    lcd.print(text);
    Serial.println(text);
}

// Function to open the door
void openDoor()
{
    digitalWrite(DOOR_PIN, HIGH);
    delay(2000);
    digitalWrite(DOOR_PIN, LOW);
}

void setup()
{
    pinMode(DOOR_PIN, OUTPUT);

    lcd.init();
    lcd.backlight();
    lcd.setCursor(0, 0);
    lcd.print("Connecting wifi");
    lcd.setCursor(0, 1);
    Serial.begin(115200); // Initialize serial communications with the PC
    while (!Serial);
    SPI.begin();        // Init SPI bus
    mfrc522.PCD_Init(); // Init MFRC522
    WiFi.begin(ssid, password);
    Serial.println(F("Connecting wifi"));
    byte printedDots = 0;
    while (WiFi.status() != WL_CONNECTED)
    {
        delay(500);
        Serial.print(".");
        if (printedDots++ > 15)
        {
            for (; printedDots >= 1; printedDots--)
            {
                lcd.setCursor(printedDots, 1);
                lcd.print(' ');
            }
            lcd.setCursor(1, 1);
            printedDots = 1;
        }
        else
            lcd.print(".");
    }
    lcd.clear();
    lcd.setCursor(0, 0);
    Serial.println("");
    dualPrint(F("WiFi connected"));
    lcd.setCursor(0, 1);

    Serial.println(WiFi.localIP());

#ifdef OTA
    ArduinoOTA.begin();
#endif

    lcd.print("Ready");
}

void clearDisplayIn(int mSec = 5000)
{
    clearDisplayTimer = millis() + mSec;
    needDisplayUpdate = true;
}

void handleDataFromGoogle(String data) {
    int colonIndex1 = data.indexOf(":");
    String access = data.substring(0, colonIndex1);

    int colonIndex2 = data.indexOf(":", colonIndex1 + 1);
    String name = data.substring(colonIndex1 + 1, colonIndex2);

    int colonIndex3 = data.indexOf(":", colonIndex2 + 1);
    String text = data.substring(colonIndex2 + 1, colonIndex3);

    int colonIndex4 = data.indexOf(":", colonIndex3 + 1);
    String origin = data.substring(colonIndex3 + 1, colonIndex4);

    int colonIndex5 = data.indexOf(":", colonIndex4 + 1);
    String destination = data.substring(colonIndex4 + 1, colonIndex5);

    String carousel = data.substring(colonIndex5 + 1);

    lcd.clear();

    lcd.setCursor(0, 0);
    lcd.print(name);
    lcd.setCursor(17, 0);
    lcd.print(carousel);


    lcd.setCursor(0,1);
    lcd.print(access);

    lcd.setCursor(0, 2);
    lcd.print(origin);
    lcd.print(" - ");
    lcd.print(destination);

        lcd.setCursor(0, 3);
        lcd.print(text);

}




void getGoogleData()
{
    HTTPClient http;
    String data;

    lcd.clear();
    uint64_t time = esp_timer_get_time();
    char url[150];
    int pointerShift = sprintf(url, "%s?uid=", mainLinkForSpr);

    for (size_t i = 0; i < mfrc522.uid.size; i++)
    {
        pointerShift += sprintf(url + pointerShift, "%X", mfrc522.uid.uidByte[i]);
    }

#ifdef TERMINAL_NAME
    pointerShift += sprintf(url + pointerShift, "&terminal=%s", TERMINAL_NAME);
#endif

    Serial.println(url);
    Serial.println(F("Connecting to google"));
    lcd.print(F("Connecting to"));
    lcd.setCursor(7, 1);
    lcd.print(F("Google"));

    http.begin(url, root_ca);
    const char *location = "Location";
    const char *headerKeys[] = {location};
    http.collectHeaders(headerKeys, 1);
    int code = http.GET();
    Serial.printf("code %d\n", code);

    if (code == 302)
    {
        String newUrl = http.header(location);
        http.end();
        Serial.println(newUrl);
        http.begin(newUrl, root_ca);
        code = http.GET();
        Serial.printf("status code %d\n", code);

        data = http.getString();
        Serial.println(data);

        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.setCursor(0, 1);
    }
    else
    {
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print(code);
        if (code == 403 || code == -1)
        {
            lcd.clear();
            lcd.setCursor(0, 0);
            lcd.print(F("Err open terminl"));
            lcd.setCursor(0, 1);
            lcd.print("for help");
            if (code == -1)
            {
                Serial.println(F("If it says something like start_ssl_client error"));
                Serial.print(F("try to update the ssl CERTIFICATE"));
            }
            else
            {
                Serial.print(F("Open this link in any browser "));
                Serial.println(url);
                Serial.println(F("If it says Authorization is ...."));
                Serial.println(F("Open the google script and republish it"));
            }
        }
        else
        {
            lcd.print(F("Something wrong"));
            lcd.setCursor(0, 1);
        }
    }

    if (!data.isEmpty() && data.length() > 1)
    {
        handleDataFromGoogle(data);
    }

    Serial.printf("time=%d\n", esp_timer_get_time() - time);
    clearDisplayIn();
}

void loop()
{
    if (needDisplayUpdate && millis() > clearDisplayTimer)
    {
        lcd.clear();
        lcd.setCursor(1, 1);
        lcd.print("Ready To Scan");
        needDisplayUpdate = false;
        lcd.noBacklight();
    }

#ifdef OTA
    ArduinoOTA.handle();
#endif
    if (!mfrc522.PICC_IsNewCardPresent())
    {
        return;
    }

    if (!mfrc522.PICC_ReadCardSerial())
    {
        return;
    }

    lcd.backlight();
    for (size_t i = 0; i < mfrc522.uid.size; i++)
    {
        Serial.printf("%X", mfrc522.uid.uidByte[i]);
    }
    getGoogleData();

    mfrc522.PICC_HaltA();
    mfrc522.PCD_StopCrypto1();
}

it just show ready to scan but as i scan the card, nothing happens

var timeZone = "Asia/Kuala_Lumpur";
var dateTimeFormat = "dd/MM/yyyy HH:mm:ss";
var logSpreadSheetId = "";
var attendanceLogSheetName = "Luggage logs";
var mainTabName = "main tab";

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu("Anyboards Menu")
    .addItem("Initial Setup", "initialSetup")
    .addItem("Add New UIDs", "addNewUIDsFromAttendanceLogUiHandler")
    .addItem("Add One Selected UID", "addOneSelectedUID")
    .addToUi();
}

function addOneSelectedUID() {
  var tabName = SpreadsheetApp.getActiveSheet().getName();
  if (tabName != attendanceLogSheetName) {
    SpreadsheetApp.getUi().alert("It should be " + attendanceLogSheetName + " sheet");
  }
  var row = SpreadsheetApp.getActiveSheet().getActiveCell().getRow();
  addNewUIDsFromAttendanceLog(row);
}

function addNewUIDsFromAttendanceLogUiHandler() {
  var ui = SpreadsheetApp.getUi();
  var response = ui.alert(
    "All new UIDs from " + attendanceLogSheetName + " will be added to the main tab",
    "Are you sure?",
    ui.ButtonSet.YES_NO
  );

  if (response == ui.Button.YES) {
    addNewUIDsFromAttendanceLog();
  }
}

function addNewUIDsFromAttendanceLog(row) {
  var mainTab = getMainSheet();
  var data = mainTab.getRange(2, 1, mainTab.getLastRow(), 1).getValues();
  var registeredUIDs = [];
  data.forEach((x) => registeredUIDs.push(x[0]));
  registeredUIDs = [...new Set(registeredUIDs)];

  var attendanceSheet = getAttendanceLogSheet();

  if (row) data = attendanceSheet.getRange(row, 1, 1, 2).getValues();
  else data = attendanceSheet.getRange(2, 1, attendanceSheet.getLastRow(), 2).getValues();

  var arr = [];

  for (var i = 0; i < data.length; i++) {
    var visit = [];
    var uid = data[i][1];
    if (!registeredUIDs.includes(uid)) {
      visit.date = data[i][0];
      visit.uid = uid;
      arr.push(visit);
      registeredUIDs.push(uid);
    }
  }

  var startRow = mainTab.getLastRow() + 1;
  data = [];
  for (var i = arr.length - 1; i >= 0; i--) {
    var row = [];
    row[0] = arr[i].uid;
    row[1] = "Person " + (startRow - 2 + arr.length - i);
    row[2] = -1;
    row[3] = "You're unregistered";
    row[4] = 0;
    row[5] = arr[i].date;
    row[6] = "unknown"; // Default origin
    row[7] = "unknown"; // Default destination
    row[8] = "unknown"; // Default carousel
    data.push(row);
  }

  if (data.length > 0) mainTab.getRange(startRow, 1, data.length, data[0].length).setValues(data);
}

function initialSetup() {
  if (!getAttendanceLogSheet()) {
    var mainSheet = SpreadsheetApp.getActiveSheet().setName(mainTabName);
    var rowData = [
      "UID",
      "Name",
      "Access",
      "Text To Display",
      "Visits Count",
      "Last Visit",
      "Origin",
      "Destination",
      "Carousel",
    ];
    mainSheet.getRange(1, 1, 1, rowData.length).setValues([rowData]);
    mainSheet.setColumnWidths(1, rowData.length + 1, 150);

    rowData = ["Date Time", "UID", "Name", "Result"];
    var attendanceSheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet(attendanceLogSheetName);
    attendanceSheet.getRange(1, 1, 1, rowData.length).setValues([rowData]);
    attendanceSheet.setColumnWidths(1, rowData.length + 1, 150);
  } else {
    var ui = SpreadsheetApp.getUi();
    ui.alert("The spreadsheet system has already been initialized");
  }
}

function doGet(e) {
  var access = "";
  var text = "";
  var name = "invalid";
  var origin = "";
  var destination = "";
  var carousel = "";

  var dateTime = Utilities.formatDate(new Date(), timeZone, dateTimeFormat);
  Logger.log(JSON.stringify(e));
  var result = "Ok";

  if (e.parameter == "undefined") {
    result = "No Parameters";
  } else {
    var uid = "";
    for (var param in e.parameter) {
      var value = stripQuotes(e.parameter[param]);

      switch (param) {
        case "uid":
          uid = value;
          break;
        default:
          result = "unsupported parameter";
      }
    }

    var mainSheet = getMainSheet();
    var data = mainSheet.getDataRange().getValues();
    if (data.length == 0) return;

    for (var i = 0; i < data.length; i++) {
   if (data[i][0] == uid) { // UID matches
      name = data[i][1]; // Name
      access = data[i][2]; // Access
      text = data[i][3]; // Text To Display
      origin = data[i][6]; // Origin
      destination = data[i][7]; // Destination
      carousel = data[i][8]; // Carousel

        var numOfVisits = mainSheet.getRange(i + 1, 5).getValue();
        mainSheet.getRange(i + 1, 5).setValue(numOfVisits + 1);
        mainSheet.getRange(i + 1, 6).setValue(dateTime);
        break;
      }
    }

    var attendanceSheet = getAttendanceLogSheet();
    data = [dateTime, uid, name, access, text, origin, destination, carousel];
    attendanceSheet.getRange(attendanceSheet.getLastRow() + 1, 1, 1, data.length).setValues([data]);
  }

result = access + ":" + name + ":" + text + ":" + origin + ":" + destination + ":" + carousel;
  return ContentService.createTextOutput(result);
}

function getAttendanceLogSheet() {
  return SpreadsheetApp.getActiveSpreadsheet().getSheetByName(attendanceLogSheetName);
}

function getMainSheet() {
  return SpreadsheetApp.getActiveSpreadsheet().getSheetByName(mainTabName);
}

function stripQuotes(value) {
  return value.replace(/^["']|['"]$/g, "");
}

this is apps script for google sheet

Check the wiring for bad contact somewhere.

1 Like

That typically suggests a hardware issue. Please post an annotated schematic and a clear picture of your assembly. Before doing so, double-check that all connections are secure, as @Railroader suggested.

i already change the cable and nothing happens, its still not detect the card

2 lines:

1). As @gilshultz requests
2). For how long time did it work?

2 replies, please.