0% found this document useful (0 votes)
40 views9 pages

Esp32 Proj (4) Rfid

The document outlines a project involving Electronic Engineering II, detailing the use of Google Scripts and Arduino code for managing RFID data. It includes instructions for setting up the ESP32, handling card reading and writing, and troubleshooting common issues. Additionally, it provides a list of students involved in the project and their respective IDs.

Uploaded by

yomnamostafa02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views9 pages

Esp32 Proj (4) Rfid

The document outlines a project involving Electronic Engineering II, detailing the use of Google Scripts and Arduino code for managing RFID data. It includes instructions for setting up the ESP32, handling card reading and writing, and troubleshooting common issues. Additionally, it provides a list of students involved in the project and their respective IDs.

Uploaded by

yomnamostafa02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

No ID Name

1 231094 Ahmed Tamer Ahmed (Rep)

2 231099 Malek Moustafa Mohamed

3 231285 Saif Ahmed Hussein

Course: Electronic Engineering II


Project Number: (4)
Circuit Diagram
Google script Code
var ss = SpreadsheetApp.openById('sheet URL’);
var sheet = ss.getSheetByName('Sheet1');
var timezone = "Africa/Cairo";

function doGet(e) {
Logger.log(JSON.stringify(e));

if (e.parameter == 'undefined') {
return ContentService.createTextOutput("Received data is undefined");
}

var Curr_Date = new Date();

var Curr_Time = Utilities.formatDate(Curr_Date, timezone, 'hh:mm:ss a');

var name = stripQuotes(e.parameters.name);


var nextRow = sheet.getLastRow() + 1;

sheet.getRange("A" + nextRow).setValue(name);
sheet.getRange("B" + nextRow).setValue(Curr_Date);
sheet.getRange("C" + nextRow).setValue(Curr_Time);

return ContentService.createTextOutput("Card holder name is stored in


column A");
}

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

function doPost(e) {
var val = e.parameter.value;

if (e.parameter.value !== undefined) {


var range = sheet.getRange('A2');
range.setValue(val);
}
}
Arduino Code
1.define MIFARE
#include <SPI.h>
#include <MFRC522.h>

constexpr uint8_t RST_PIN = 22; // Reset pin


constexpr uint8_t SS_PIN = 5; // SDA/SS pin (connected to RC522 SDA)

MFRC522 mfrc522(SS_PIN, RST_PIN);


MFRC522::MIFARE_Key key;

int blockNum = 2; // Data block to read/write

byte blockData[16] = { "Name" };


byte bufferLen = 18;
byte readBlockData[18];

MFRC522::StatusCode status;

void setup() {
Serial.begin(115200);
SPI.begin();
mfrc522.PCD_Init();
Serial.println("Scan a MIFARE 1K Tag to write data...");
}

void loop() {

for (byte i = 0; i < 6; i++) {


key.keyByte[i] = 0xFF;
}

if (!mfrc522.PICC_IsNewCardPresent()) return;
if (!mfrc522.PICC_ReadCardSerial()) return;

Serial.println("\n**Card Detected**");

Serial.print("Card UID:");
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.print("\nPICC Type: ");
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));

Serial.println("\nWriting to data block...");


WriteDataToBlock(blockNum, blockData);
Serial.println("\nReading from data block...");
ReadDataFromBlock(blockNum, readBlockData);

Serial.print("Data in block ");


Serial.print(blockNum);
Serial.print(" --> ");
for (int j = 0; j < 16; j++) {
Serial.write(readBlockData[j]);
}
Serial.println();

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

void WriteDataToBlock(int blockNum, byte blockData[]) {


status = mfrc522.PCD_Authenticate(
MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid)
);
if (status != MFRC522::STATUS_OK) {
Serial.print("Auth failed (Write): ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
status = mfrc522.MIFARE_Write(blockNum, blockData, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print("Write failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
} else {
Serial.println("Data written successfully.");
}
}

void ReadDataFromBlock(int blockNum, byte readBlockData[]) {


status = mfrc522.PCD_Authenticate(
MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid)
);
if (status != MFRC522::STATUS_OK) {
Serial.print("Auth failed (Read): ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
status = mfrc522.MIFARE_Read(blockNum, readBlockData, &bufferLen);
if (status != MFRC522::STATUS_OK) {
Serial.print("Read failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
} else {
Serial.println("Block read successfully.");
}
}
2.Attendance Code
#include <SPI.h>
#include <MFRC522.h>
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <WiFiClientSecure.h>

#define RST_PIN 22
#define SS_PIN 21
#define BUZZER 15

MFRC522 mfrc522(SS_PIN, RST_PIN);


MFRC522::MIFARE_Key key;
MFRC522::StatusCode status;

int blockNum = 2;

byte bufferLen = 18;


byte readBlockData[18];

String card_holder_name;
const String sheet_url = "URLgoogle script?name=";

#define WIFI_SSID ""


#define WIFI_PASSWORD ""

void setup() {
Serial.begin(115200);
Serial.println();
Serial.print("Connecting to AP");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(200);
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println();
pinMode(BUZZER, OUTPUT);
SPI.begin();
}

void loop() {
mfrc522.PCD_Init();
if (!mfrc522.PICC_IsNewCardPresent()) { return; }
if (!mfrc522.PICC_ReadCardSerial()) { return; }
Serial.println();
Serial.println(F("Reading last data from RFID..."));
ReadDataFromBlock(blockNum, readBlockData);

Serial.println();
Serial.print(F("Last data in RFID:"));
Serial.print(blockNum);
Serial.print(F(" --> "));
for (int j = 0; j < 16; j++) {
Serial.write(readBlockData[j]);
}
Serial.println();
digitalWrite(BUZZER, HIGH);
delay(200);
digitalWrite(BUZZER, LOW);
delay(200);
digitalWrite(BUZZER, HIGH);
delay(200);
digitalWrite(BUZZER, LOW);

if (WiFi.status() == WL_CONNECTED) {
WiFiClientSecure client;
client.setInsecure();
card_holder_name = sheet_url + String((char*)readBlockData);
card_holder_name.trim();
Serial.println(card_holder_name);
HTTPClient https;
Serial.print(F("[HTTPS] begin...\n"));

if (https.begin(client, (String)card_holder_name)) {
Serial.print(F("[HTTPS] GET...\n"));
int httpCode = https.GET();

if (httpCode > 0) {
Serial.printf("[HTTPS] GET... code: %d\n", httpCode);

} else {
Serial.printf("[HTTPS] GET... failed, error: %s\n",
https.errorToString(httpCode).c_str());
}
https.end();
delay(1000);
} else {
Serial.printf("[HTTPS] Unable to connect\n");
}
}
}

void ReadDataFromBlock(int blockNum, byte readBlockData[]) {


for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A,
blockNum, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print("Authentication failed for Read: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
} else {
Serial.println("Authentication success");
}
status = mfrc522.MIFARE_Read(blockNum, readBlockData, &bufferLen);
if (status != MFRC522::STATUS_OK) {
Serial.print("Reading failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
} else {
Serial.println("Block was read successfully");
}
}

1. Solving ESP32 Write Timeout & Serial Port Issues


-Driver Installation

1. Install proper USB drivers:

o For CP210x chips: Download CP210x drivers

o For CH340 chips: Download CH340 drivers


2. Check Device Manager:

o Press Win+X → Device Manager

o Look under "Ports (COM & LPT)"

o You should see something like:

 "Silicon Labs CP210x USB to UART Bridge (COMx)" or

 "USB-SERIAL CH340 (COMx)"

-Arduino IDE Configuration

Tool>boards>boards manager>search for esp32 by Espressif systems and install it

Select correct settings:

o Board: "ESP32 Dev Module"

Change Board Selection

Go to Tools > Board in Arduino IDE

Select "ESP32 Dev Module" instead of "ESP32P4 Dev Module"

(If you don't see this option, follow the installation steps below)

o Make sure these settings are configured:


o Upload Speed: "921600"

o Flash Mode: "QIO"

o Partition Scheme: "Default 4MB with spiffs"

2. How to install library on esp32

Tools>manage library> search for library you want to install

Search for the suitable library for your project

Some hardware due to bad soldring

You might also like