This tutorial covers how to set up the RFID. You will also learn how to connect RFID to PHP and insert data to Mysql database.
After preparing the requirements, connect your RFID RC522 to your NodeMcu. Follow the diagram below.
The Yellow light will serve as an indicator that we have successfully connected to wifi. Green indicates that the request to the server has been sent successfully. Red light indicates that our HTTP request failed / our server did not return “success”.
|Led w/ 220ohm Resistor (Red)||D0|
|Led w/ 220ohm Resistor (Green)||D1|
|Led w/ 220ohm Resistor (Yellow)||D8|
Setting up Arduino and adding RFID Library
We need to add esp8266 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. Close and restart your Arduino IDE
Plug in your NodeMcu to your computer. You will notice that your RFID scanner will light up.
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.
Extracting MFRC522 Library
Extract the MFRC522 library to your Arduino Libraries folder. The libraries folder is located in C:\Program Files (x86)\Arduino. (Assuming it is the installation path of your Arduino IDE).
Restart your Arduino IDE
Modifying the Codes
Open up the codes attached in Arduino IDE. Change the following lines of code.
Since our RFID Scanner will be wireless, we need a way to transmit the data to our web server. Our NodeMcu will automatically connect to your wifi.
const char *ssid = "yourwifiname";
const char *password = "yourwifipassword";
We will send the decimal value of our card uuid to our server by sending a POST request. The URL must be changed to where you deployed our PHP codes. I run XAMPP on my computer. I inputted my LAN IP address.
String postData = "cardid=" + String(cardId) + "&action=insertRfIdLog";
In our connect.php file in rfid folder. Change the following database credentials.
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "rfid";
After modifying the codes, plugin your NodeMCU, and on Arduino ID, click the upload button.
Deploying our PHP Web App
Start your Apache and Mysql in XAMPP Control Panel. Copy paste the rfid folder provided in your htdocs folder. Create a new database named rfid in your PHPMyAdmin. Import the rfid.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 rfid.sql -> Open -> Go”
If you have done the steps above properly, you should be able to see the logs in http://localhost/rfid/viewlogs.php
That’s it. Enjoy.
Hope you like this tutorial. Please share the love by clicking the social media buttons!