In this tutorial, we will learn how to Connect RFID to PHP & MySQL Database with NodeMcu ESP8266. Here I have interfaced RFID-RC522 Module with NodeMcu ESP8266 and then I’m sending data of RFID to MySQL Database. Actually, we are just reading the serial data coming from NodeMcu ESP8266 and then publishing that to MySQL Database through python code. Unlike others, This project is Protable because we can access data from any device connected to the Internet. Basically, this project can store the Credentials of Students or employes.
- Hardware Components Required
- Software Required with Download Links
- RFID RC522 Module
- Specifications & Pin Details
- Interfacing RFID RC522 with NodeMcu ESP8266 Module
- Installation of XAMPP server:
- Deploying our PHP Web App
- Setting up Arduino IDE for ESP8266 Board
- Program/Sketch Code
- Video Tutorials
- Conclusion
Hardware Components Required
- NodeMcu ESP8266
- RFID-RC522 Module
- Jumper Wires
- Micro USB Cable
- Mini Breadboard (optional)
Software Required with Download Links
Now before getting started with this project, let’s learn What is RFID? with its specifications and Features.
We Have Few More IoT Based Projects Resources:
RFID Based Attendance System Using NodeMCU with PHP Web App
IoT Based RFID Smart Door Lock System Using NodeMCU ESp8266
ESP8266 based IoT Health Care Panic Alarm for Elderly Folks
ESP8266 Plot Sensor readings to Webserver in Real-Time Chart
RFID RC522 Module
At first, let’s learn a little bit about RFID. RFID is the short form of Radio Frequency Identification. RFID modules use electromagnetic fields for transferring data between the card and the reader. Different RFID tags are attached to objects like Keychain, cards, etc. and whenever we place that object in front of the RFID reader, the reader reads that tags. The next benefit of RFID is that it doesn’t require to be in a straight line to get detected. Unlike a barcode, in RFID there’s no such restriction. So, here are some features of RFID RC522.
Features:
– Module Name: MF522-ED
– Working current:13—26mA/ DC 3.3V
– Standby current:10-13mA/DC 3.3V
– Sleeping current:<80uA
– Peak current:<30mA
– Working frequency:13.56MHz
– Card reading distance :0~60mm(mifare1 card)
– Protocol:SPI
– Data communication speed:Maximum 10Mbit/s
– Card types supported:mifare1 S50、mifare1 S70、 Mifare UltraLight、mifare Pro、 mifare Desfire
– Dimension:40mm×60mm
– Working temperature:-20—80 degree
– Storage temperature:-40—85 degree
– Humidity:relevant humidity 5%—95%
– Max SPI speed: 10Mbit/s
Specifications & Pin Details
The simple specifications of RC522 module from left to right first pins are as follows:
Pin Name Details
1. 3.3V +3.3V Power Supply
2. RST Reset
3. GND Ground Pin
4. IRO Not Connected
5. MISO Serial Communication
6. MOSI Serial Communication
7. SCK TX/RX with ESP8266
8. SDA TX/RX with ESP8266
Interfacing RFID RC522 with NodeMcu ESP8266 Module
NodeMCU ESP8266/ESP12E RFID MFRC522 / RC522
D2 <———-> SDA/SS
D5 <———-> SCK
D7 <———-> MOSI
D6 <———-> MISO
GND <———-> GND
D1 <———-> RST
3V/3V3 <———-> 3.3V
Installation of XAMPP server:
Actually, we can Deploy PHP Web App on Hosting Server. But, you will need Public IP from Your Broadband connections to send data to the hosting server.
Demo of PHP Web App on My Hosting Server can be Access from here
However, we are using the XAMPP server it can be used both in Windows and Linux. Basically, Ubuntu users use LAMP it’s better than XAMPP. But, I am using Windows so I am going to use the XAMPP server. So you can download the XAMPP server from this link.
Deploying our PHP Web App
- Start your Mysql and Apache from XAMPP Control Panel.
- Copy-paste the RFID PHP Source Code folder provided in your htdocs folder.
- Create a new Database named RFID in your PHPMyAdmin.
- Import the nodemcu_rfid_iot_projects.sql file. If you do not know how to import an SQL file in PHPMyAdmin, simply click on your database, click “Import -> Choose File -> Click nodemcu_rfid_iot_projects.sql -> Open -> Go”
If you have done the steps above properly, you should be able to open the URL and see a PHP Web Apps.
Also Read: DIY Dual Axis Solar Tracker using Arduino
Setting up Arduino IDE for ESP8266 Board
We need to add the esp8266 board to our Arduino IDE. Open up your IDE then go to “File -> Preferences” or simply hit “Ctrl + comma”.
Paste http://arduino.esp8266.com/stable/package_esp8266com_index.json in additional board manager URLs.
Go to “Tools -> Board -> Boards Manager” search and install esp8266.
Selecting Port and Board
Go to Tools -> Port and select the COM Port of your NodeMCU. If you are not sure what port your NodeMcu is plugged in, go to Device Manager -> Ports (COM & LPT).
Now select NodeMcu 1.0 (ESP-12E Module) by clicking Tools -> Board. Scroll down until you find it.
Install the MFRC522 library to your Arduino Libraries folder – Download RFID Library
More Interesting Projects:
Home Automation with ESP8266 Web Server & Relay Module Control Appliances from Local Network
IoT Web Controlled Smart Notice Board using NodeMCU ESP8266
Capacitive Soil Moisture Sensor with OLED Display & Arduino
Program/Sketch Code
/*
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# RFID MFRC522 / RC522 Library : https://github.com/miguelbalboa/rfid #
# #
# Installation : #
# NodeMCU ESP8266/ESP12E RFID MFRC522 / RC522 #
# D2 <----------> SDA/SS #
# D5 <----------> SCK #
# D7 <----------> MOSI #
# D6 <----------> MISO #
# GND <----------> GND #
# D1 <----------> RST #
# 3V/3V3 <----------> 3.3V #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Subscribe To The IoT Projects YouTube Channel : https://www.youtube.com/channel/UC49xSqiQ6gBrxUMQ9zvzO6A :) :) :) #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
*/
//----------------------------------------Include the NodeMCU ESP8266 Library---------------------------------------------------------------------------------------------------------------//
//----------------------------------------see here: https://teiotprojects.com/connect-rfid-to-php-mysql-database-with-nodemcu-esp8266/ to add NodeMCU ESP8266 library and board
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
//----------------------------------------Include the SPI and MFRC522 libraries-------------------------------------------------------------------------------------------------------------//
//----------------------------------------Download the MFRC522 / RC522 library here: https://github.com/miguelbalboa/rfid
#include <SPI.h>
#include <MFRC522.h>
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
#define SS_PIN D2 //--> SDA / SS is connected to pinout D2
#define RST_PIN D1 //--> RST is connected to pinout D1
MFRC522 mfrc522(SS_PIN, RST_PIN); //--> Create MFRC522 instance.
#define ON_Board_LED 2 //--> Defining an On Board LED, used for indicators when the process of connecting to a wifi router
//----------------------------------------SSID and Password of your WiFi router-------------------------------------------------------------------------------------------------------------//
const char* ssid = "Alsan Air WiFi 4";
const char* password = "[email protected]";
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
ESP8266WebServer server(80); //--> Server on port 80
int readsuccess;
byte readcard[4];
char str[32] = "";
String StrUID;
//-----------------------------------------------------------------------------------------------SETUP--------------------------------------------------------------------------------------//
void setup() {
Serial.begin(115200); //--> Initialize serial communications with the PC
SPI.begin(); //--> Init SPI bus
mfrc522.PCD_Init(); //--> Init MFRC522 card
delay(500);
WiFi.begin(ssid, password); //--> Connect to your WiFi router
Serial.println("");
pinMode(ON_Board_LED, OUTPUT);
digitalWrite(ON_Board_LED, HIGH); //--> Turn off Led On Board
//----------------------------------------Wait for connection
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
//----------------------------------------Make the On Board Flashing LED on the process of connecting to the wifi router.
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.
//----------------------------------------If successfully connected to the wifi router, the IP Address that will be visited is displayed in the serial monitor
Serial.println("");
Serial.print("Successfully connected to : ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Please tag a card or keychain to see the UID !");
Serial.println("");
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
//-----------------------------------------------------------------------------------------------LOOP---------------------------------------------------------------------------------------//
void loop() {
// put your main code here, to run repeatedly
readsuccess = getid();
if (readsuccess) {
digitalWrite(ON_Board_LED, LOW);
HTTPClient http; //Declare object of class HTTPClient
String UIDresultSend, postData;
UIDresultSend = StrUID;
//Post Data
postData = "UIDresult=" + UIDresultSend;
http.begin("http://192.168.1.8/NodeMCU-and-RFID-RC522-IoT-Projects/getUID.php"); //Specify request destination
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
http.end(); //Close connection
delay(1000);
digitalWrite(ON_Board_LED, HIGH);
}
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
//----------------------------------------Procedure for reading and obtaining a UID from a card or keychain---------------------------------------------------------------------------------//
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;
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
//----------------------------------------Procedure to change the result of reading an array UID into a string------------------------------------------------------------------------------//
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';
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
Video Tutorials
Conclusion
Finally, we have Connect RFID to PHP & MySQL Database with NodeMcu ESP8266. Now, you can use this Project to save user credentials on PHP Web App. Whenever you scan the RFID Card the user Credentials will be displayed on the Web App. We hope you found this IoT project useful! Drop a comment below if you have any doubts or queries. We’ll do our best to answer your questions.
Did you use any libraries like bootstrap or others and what programming language did you use for designing the web page interface? One last question, is this web application accessible across all operating system?
This is a web app designed using PHP which includes html and css as well. This web app can be hosted on a cloud services and can be easily access from anywhere in the world.
Notice: Undefined index: UIDresult in C:\xampp\htdocs\NodeMCU-and-RFID-RC522-IoT-Projects\getUID.php on line 2
Can you help me, i cannt acces it when the url is
“http://192.168.1.108/NodeMCU-and-RFID-RC522-IoT-Projects/getUID.php”
and before that, icannt acces it too (This site can’t be reached
iotrfid.theiotprojects.com’s server IP address could not be found.)
“http://iotrfid.theiotprojects.com/”
did you solved? Notice: Undefined index: UIDresult in C:\xampp\htdocs\NodeMCU-and-RFID-RC522-IoT-Projects\getUID.php on line 2
SQLSTATE[HY000] [1045] Access denied for user ‘theiotprojects’@’localhost’ (using password: YES)
i have this error
can someone help ?
would you please show us how to save the records of the scanned RFID please ?