Hi there, i'm trying to make an automatic door locked using RFID and esp8266, this basically will register the Id card, and only the registered id card can open the door (servo motor).
The problem is how can i retrive a value (like 0 or 1) from php file to esp8266, so that i can say if register is 1 » servo motor rotate, else (serial print "permission negate").
Here is the code of esp8266
#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 = "Noira2";
const char* password = "12345678";
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
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.137.1/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';
Next the php codes
read tag.php
<?php
$Write="<?php $" . "UIDresult=''; " . "echo $" . "UIDresult;" . " ?>";
file_put_contents('UIDContainer.php',$Write);
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
<script src="jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#getUID").load("UIDContainer.php");
setInterval(function() {
$("#getUID").load("UIDContainer.php");
}, 500);
});
</script>
<style>
html {
font-family: Arial;
display: inline-block;
margin: 0px auto;
text-align: center;
}
ul.topnav {
list-style-type: none;
margin: auto;
padding: 0;
overflow: hidden;
background-color: #4CAF50;
width: 70%;
}
ul.topnav li {float: left;}
ul.topnav li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
ul.topnav li a:hover:not(.active) {background-color: #3e8e41;}
ul.topnav li a.active {background-color: #333;}
ul.topnav li.right {float: right;}
@media screen and (max-width: 600px) {
ul.topnav li.right,
ul.topnav li {float: none;}
}
td.lf {
padding-left: 15px;
padding-top: 12px;
padding-bottom: 12px;
}
</style>
<title>Read Tag : NodeMCU V3 ESP8266 / ESP12E with MYSQL Database</title>
</head>
<body>
<h2 align="center">NodeMCU V3 ESP8266 / ESP12E with MYSQL Database</h2>
<ul class="topnav">
<li><a href="home.php">Home</a></li>
<li><a href="user data.php">User Data</a></li>
<li><a href="registration.php">Registration</a></li>
<li><a class="active" href="read tag.php">Read Tag ID</a></li>
</ul>
<br>
<h3 align="center" id="blink">Please Scan Tag to Display ID or User Data</h3>
<p id="getUID" hidden></p>
<br>
<div id="show_user_data">
<form>
<table width="452" border="1" bordercolor="#10a0c5" align="center" cellpadding="0" cellspacing="1" bgcolor="#000" style="padding: 2px">
<tr>
<td height="40" align="center" bgcolor="#10a0c5"><font color="#FFFFFF">
<b>User Data</b>
</font>
</td>
</tr>
<tr>
<td bgcolor="#f9f9f9">
<table width="452" border="0" align="center" cellpadding="5" cellspacing="0">
<tr>
<td width="113" align="left" class="lf">ID</td>
<td style="font-weight:bold">:</td>
<td align="left">--------</td>
</tr>
<tr bgcolor="#f2f2f2">
<td align="left" class="lf">Name</td>
<td style="font-weight:bold">:</td>
<td align="left">--------</td>
</tr>
<tr>
<td align="left" class="lf">Gender</td>
<td style="font-weight:bold">:</td>
<td align="left">--------</td>
</tr>
<tr bgcolor="#f2f2f2">
<td align="left" class="lf">Email</td>
<td style="font-weight:bold">:</td>
<td align="left">--------</td>
</tr>
<tr>
<td align="left" class="lf">Mobile Number</td>
<td style="font-weight:bold">:</td>
<td align="left">--------</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</div>
<script>
var myVar = setInterval(myTimer, 1000);
var myVar1 = setInterval(myTimer1, 1000);
var oldID="";
clearInterval(myVar1);
function myTimer() {
var getID=document.getElementById("getUID").innerHTML;
oldID=getID;
if(getID!="") {
myVar1 = setInterval(myTimer1, 500);
showUser(getID);
clearInterval(myVar);
}
}
function myTimer1() {
var getID=document.getElementById("getUID").innerHTML;
if(oldID!=getID) {
myVar = setInterval(myTimer, 500);
clearInterval(myVar1);
}
}
function showUser(str) {
if (str == "") {
document.getElementById("show_user_data").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("show_user_data").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","read tag user data.php?id="+str,true);
xmlhttp.send();
}
}
var blink = document.getElementById('blink');
setInterval(function() {
blink.style.opacity = (blink.style.opacity == 0 ? 1 : 0);
}, 750);
</script>
</body>
</html>
getUID.php
<?php
$UIDresult=$_POST["UIDresult"];
$Write="<?php $" . "UIDresult='" . $UIDresult . "'; " . "echo $" . "UIDresult;" . " ?>";
file_put_contents('UIDContainer.php',$Write);
?>
also i copy the code from this video Arduino IDE + NodeMCU V3 + MySQL + PHP | NodeMCU V3 with RFID RC522 uses MySQL Database and PHP - YouTube