ESP8266 & OLED RFID Cloner
03-25-2020
This is an Arduino program I am working on that clones the UID on Mifare RFID Tags.
Language or Platform: C
Code:
// Creates a Wifi AP, Displays Website, and Reads RFID Tags
#define SS_PIN 0 //D2
#define RST_PIN 2 //D1
#include <SPI.h>
#include <MFRC522.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include "OLED.h"
OLED display(5, 4);
/* For RFID */
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
/* For ESP8266 WiFi */
char *ssid = "RFID";
const char *password = "PASSWORD";
ESP8266WebServer server ( 80 );
//setting the addresses
IPAddress ip(192,168,8,1);
IPAddress gateway(192, 168, 8, 1);
IPAddress subnet(255, 255, 255, 0);
String WebStyle = "";
const String HTTP_HEADER = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/><title>RFID Reader</title>";
const String HTTP_STYLE = "<style>body { background-color: #9FB8BF; font-family: Verdana, Sans-serif, Palatino; Color: #E1F3FA; }</style>";
const String START_BODY = "</head><body><br><p><center><h2>RFID Reader and Cloner</h2></center></p><br>";
const String HTTP_MSG = "<ul type=\"square\"><li>To clone, hold card to device and click Clone. You'll then have 15 seconds to hold blank card to device for write.</li><li>Click Read to put device in read mode.</li></ul>";
const String MAIN_BODY = "<p><center><a href=\"clone\"><button style=\"background:#E1F3FA;height:40px;width:80px\">Clone</button></a> <a href=\"read\"><button style=\"background:#E1F3FA;height;height:40px;width:80px\">Read</button></a></center></p>";
void handleNotFound() {
server.send(404, "text/plain", "404: Not found"); // Send HTTP status 404 (Not Found)
}
void handleRoot() {
String WebStyle = HTTP_HEADER;
WebStyle += HTTP_STYLE;
WebStyle += START_BODY;
WebStyle += HTTP_MSG;
WebStyle += MAIN_BODY;
WebStyle += "<center><H3>www.trigat.com</H3></center></body>";
server.send(200, "text/html", WebStyle);
}
void clone() {
/* RFID Read */
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
//Show UID on serial monitor
Serial.println();
Serial.print(" UID tag :");
// Very basic UID dump
unsigned int hex_num;
hex_num = mfrc522.uid.uidByte[0] << 24;
hex_num += mfrc522.uid.uidByte[1] << 16;
hex_num += mfrc522.uid.uidByte[2] << 8;
hex_num += mfrc522.uid.uidByte[3];
// Get UID
int NFC_id = (int)hex_num;
Serial.print(NFC_id, HEX);
// Convert UID to string using an int and a base (hexadecimal)
String stringUID = String(NFC_id, HEX);
char UIDChar[8];
stringUID.toCharArray(UIDChar,8);
display.print(UIDChar,6,0);
delay(1000);
Serial.println();
// 15 second timer to read and clone
int i;
for ( i = 15; i > 0; --i ) {
// Convert seconds to string using an int and a base (hexadecimal)
String stringseconds = String(i);
char secChar[8];
stringseconds.toCharArray(secChar,8);
display.clear();
display.print(UIDChar,0,3);
display.print(":",4,3);
display.print(secChar,4,5);
delay(1000);
}
/* RFID Write */
display.clear();
display.print("Writing UID",0,2);
// Set new UID
// Change your UID hex string to 4 byte array
byte newUid[] = {0xE1, 0x02, 0x03, 0x04};
if ( mfrc522.MIFARE_SetUid(newUid, (byte)4, true) ) {
Serial.println( "Wrote new UID to card." );
}
// Halt PICC and re-select it so DumpToSerial doesn't get confused
mfrc522.PICC_HaltA();
if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
return;
}
// Dump the new memory contents
Serial.println( "New UID and contents:" );
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
delay(500);
Serial.print( "Card Cloned" );
display.print("Card Cloned", 4,2);
delay(2000);
display.clear();
// Load page again
String WebStyle = HTTP_HEADER;
WebStyle += HTTP_STYLE;
WebStyle += START_BODY;
WebStyle += HTTP_MSG;
WebStyle += MAIN_BODY;
WebStyle += "<center><H3>Clone Complete</H3></center></body>";
server.send(200, "text/html", WebStyle);
}
void readID() {
while(1) {
/* RFID Read Loop */
// Look for new cards
// *Known Issue* After writing to card, you must read new card
// to continue with read function
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
//Show UID on serial monitor
Serial.println();
Serial.print(" UID tag :");
String content= "";
// Very basic UID dump
unsigned int hex_num;
hex_num = mfrc522.uid.uidByte[0] << 24;
hex_num += mfrc522.uid.uidByte[1] << 16;
hex_num += mfrc522.uid.uidByte[2] << 8;
hex_num += mfrc522.uid.uidByte[3];
// Get UID
int NFC_id = (int)hex_num;
Serial.print(NFC_id, HEX);
// Convert UID to string using an int and a base (hexadecimal)
String stringUID = String(NFC_id, HEX);
char UIDChar[10];
stringUID.toCharArray(UIDChar,10);
display.print(UIDChar,6,2);
delay(1000);
Serial.println();
}
}
void setup() {
Serial.begin ( 115200 );
/* RFID */
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
/* WiFi */
WiFi.mode(WIFI_AP); //switching to AP mode
WiFi.softAP(ssid, password); //initializing the AP with ssid and password. This will create a WPA2-PSK secured AP
WiFi.config(ip,gateway,subnet);
WiFi.begin();
Serial.println ( "" );
display.begin();
// Wait for connection
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
display.print(".");
}
Serial.println ( "" );
Serial.print ( "Connected to " );
Serial.println ( ssid );
Serial.print ( "IP address: " );
Serial.println ( WiFi.localIP() );
// Convert IP to char so that it can be displayed on OLED
String tempIP = WiFi.localIP().toString();
char ipChar[20];
tempIP.toCharArray(ipChar,20);
display.print(ipChar);
display.print("WiFI connected",2,0);
display.print(ssid,4,0);
if ( MDNS.begin ( "esp8266" ) ) {
Serial.println ( "MDNS responder started" );
}
// Web server
server.on("/", handleRoot); // Which routine to handle at root location
server.on ( "/inline", []() {server.send ( 200, "text/plain", "Test");});
server.on("/clone", clone);
server.on("/read", []() {
readID();
});
server.begin();
}
void loop() {
/* WiFi */
server.handleClient();
}
Back