I'm currently doing a simple project using a Raspberry Pi. I have a simple Apache 2 server running, with MySQL and few PHP/HTML pages. One of the pages is the one below call barcode.php. On the barcode.php you enter a User_ID on the user_id field and press enter or press the find button and it will search in the database for that user and display that user information and picture.
All of the above works manually and even with a barcode scanner by scanning a QR code. Now, I want to integrate an RFID reader (RFID READER RC522). When the user scans his/her card on the reader I want it to work like the barcode scanner and input the numeric code in the user_id field and press enter so the page can return the user data.
I'm very new to this. How can I implement this into my barcode.php? Where a user scans, and it will put it on the user_id field.
Here's the barcode.php:
<?php
// Initialize the variables
$user_id = "";
$osha = "";
$firstname = "";
$lastname = "";
$company = "";
$trade = "";
$email = "";
$picture = "";
$reg_date = "";
// PHP code to search data in a MySQL database and set it in input text
if(isset($_POST['search']))
{
// Connect to MySQL
$dbc = mysqli_connect("127.0.0.1", "root", "1234", "demodb");
// id to search
$user_id = mysqli_real_escape_string($dbc, $_POST['user_id']);
$query = "SELECT * FROM Users WHERE user_id = '$user_id' LIMIT 1";
$rs = mysqli_query($dbc, $query);
if (mysqli_num_rows($rs) == 1)
{
$row = mysqli_fetch_array($rs);
$osha = $row['osha'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$company = $row['company'];
$trade = $row['trade'];
$email = $row['email'];
$picture = $row['picture'];
$reg_date = $row['reg_date'];
$query1 = "INSERT INTO scan (user_id, osha, firstname, lastname, company, trade, email, picture) VALUES (" .
"'" . $user_id . "'," .
"'" . mysqli_real_escape_string($dbc, $osha ) . "', " .
"'" . mysqli_real_escape_string($dbc, $firstname) . "', " .
"'" . mysqli_real_escape_string($dbc, $lastname ) . "', " .
"'" . mysqli_real_escape_string($dbc, $company ) . "', " .
"'" . mysqli_real_escape_string($dbc, $trade ) . "', " .
"'" . mysqli_real_escape_string($dbc, $email ) . "', " .
"'" . mysqli_real_escape_string($dbc, $picture ) . "')";
mysqli_query($dbc, $query1);
}
else
{
echo "Undefined ID";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>User Search</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--===============================================================================================-->
<link rel="icon" type="image/png" href="images/icons/favicon.ico"/>
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="vendor/bootstrap/css/bootstrap.min.css">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="fonts/font-awesome-4.7.0/css/font-awesome.min.css">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="vendor/animate/animate.css">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="vendor/css-hamburgers/hamburgers.min.css">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="vendor/select2/select2.min.css">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="css/util.css">
<link rel="stylesheet" type="text/css" href="css/main.css">
<!--===============================================================================================-->
<style>
.button {
background-color: #4CAF50;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="contact1">
<div class="container-contact1">
<div class="contact1-pic js-tilt" data-tilt>
<?php echo "<img src= '$picture' height='400' width='400' alt='Image not found' onerror=this.onerror=null;this.src='images/scanbarcode.png'; >" ?>
</div>
<form action="barcode.php" method="post">
<div class="wrap-input1 validate-input" data-validate = "SCAN ID TO SEARCH">
<input class="input1" type="text" name="user_id" placeholder="SCAN ID TO SEARCH" autofocus>
<span class="shadow-input1"></span>
</div>
<div class="wrap-input1 validate-input" >
<input class="input1" type="text" name="osha" placeholder="OSHA #" value="<?= htmlspecialchars($osha) ?>">
<span class="shadow-input1"></span>
</div>
<div class="wrap-input1 validate-input" >
<input class="input1" type="text" name="firstname" placeholder="First Name" value="<?= htmlspecialchars($firstname) ?>">
<span class="shadow-input1"></span>
</div>
<div class="wrap-input1 validate-input" >
<input class="input1" type="text" name="lastname" placeholder="Last Name" value="<?= htmlspecialchars($lastname) ?>">
<span class="shadow-input1"></span>
</div>
<div class="wrap-input1 validate-input" >
<input class="input1" type="text" name="company" placeholder="Company Name" value="<?= htmlspecialchars($company) ?>">
<span class="shadow-input1"></span>
</div>
<div class="wrap-input1 validate-input" >
<input class="input1" type="text" name="trade" placeholder="Trade Name" value="<?= htmlspecialchars($trade) ?>">
<span class="shadow-input1"></span>
</div>
<input type="button" onclick="window.location='auth.php'" class="button" value="Admin"/>
<input type="submit" name="search" value="Find" class="button" >
</form>
</div>
</div>
<!--===============================================================================================-->
<script src="vendor/jquery/jquery-3.2.1.min.js"></script>
<!--===============================================================================================-->
<script src="vendor/bootstrap/js/popper.js"></script>
<script src="vendor/bootstrap/js/bootstrap.min.js"></script>
<!--===============================================================================================-->
<script src="vendor/select2/select2.min.js"></script>
<!--===============================================================================================-->
<script src="vendor/tilt/tilt.jquery.min.js"></script>
<script >
$('.js-tilt').tilt({
scale: 1.1
})
</script>
</body>
</html>
Here's the simple Python script called Read.py
#!/usr/bin/env python
import RPi.GPIO as GPIO
import SimpleMFRC522
import MFRC522
import signal
continue_reading = True
reader = SimpleMFRC522.SimpleMFRC522()
def end_read(signal,frame):
print "Ctrl+C captured, ending read."
continue_reading = False
GPIO.cleanup()
signal.signal(signal.SIGINT, end_read)
MIFAREReader = MFRC522.MFRC522()
while continue_reading:
id, text = reader.read()
print(id)
print(text)
On the terminal, when scanning a card it returns the following data:
pi@raspberrypi:/var/www/html/app/RFID/SPI-Py/MFRC522-python $ sudo python Read1.py
140248588882
I want to send that numeric data (140248588882) to the user_id field in my barcode.php page. How can I accomplish this?
You need a client to punch text into the web browser, which is not a PHP problem.
Typically a barcode scanner will act as a HID, so it appears to type out the number and press enter wherever you scan something. You can either plug in an alernate RFID reader that acts as a HID, or simulate keyboard events from python.
This is a quick hack, but you can create a PHP page like this:
<?php
print `sudo python Read1.py`;
Then use JavaScript in your main PHP page to poll that PHP script, and if/when it returns a valid ID, insert it into the input and submit the form.
You should probably make it so that Read1.py can be run without sudo (having PHP be able to run passwordless sudo is a security problem).
You will also need to deal with the occurrence that the polling times out, in which case the JavaScript should just poll again.
Your problem is that you need a Python file to send the barcode to the PHP script.
In your Python script add this code:
from webbrowser import open as start_webpage
...
...
...
...
if barcode_scanned:
start_webpage(url + "?user_id=" + str(barcode))
However, you need to make user_id
a GET parameter in barcode.php:
$user_id = $_GET["user_id"]