下午所有,
我有一个用于编写NFC标签的网页。当我在页面上提交表单时,PHP将变量发送到python脚本。此脚本(write.py)检查read.py脚本是否已终止并处理写入请求。这部分工作正常。
我遇到的问题是当我回想起read.py时,它包含一个循环,它会阻止网页在写完成后刷新。我试过从PHP脚本内部和write.py结束时调用read.py,同样的事情发生了。我也试过添加一个&到read.py命令的末尾。
有人可以看一下我到目前为止的情况,让我知道我做错了什么以及如何解决这个问题。
干杯,
的Blinky
HTML / PHP
<?php
session_start();
if( !empty( $_POST ) )
{
echo "Write to card using python";
$company = $_POST[ 'company' ];
$name = $_POST[ 'name' ];
$email = $_POST[ 'email' ];
$cmd = "sudo python write.py '$company' '$name' '$email' &";
$resp = shell_exec( $cmd );
if( strpos( $resp, 'written' ) !== false )
{
header("Location: http://192.168.1.176/index.php");
}
unset( $_POST );
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<input type="text" name="name" id="name">
<input type="text" name="email" id="email">
<select name="company" id="company">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<input type="submit">
</form>
read.py
#!/usr/bin/env python
# -*- coding: utf8 -*-
import RPi.GPIO as GPIO
import MFRC522
import signal
import MySQLdb
import os
from datetime import datetime
os.system("sudo kill $(ps aux | grep 'write.py' | awk '{print $2}')")
db = MySQLdb.connect(host="", user="", passwd="", db="")
cursor = db.cursor()
continue_reading = True
# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
global continue_reading
print "Ctrl+C captured, ending read."
continue_reading = False
GPIO.cleanup()
# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)
# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()
# Welcome message
print "Welcome to the MFRC522 data read example"
print "Press Ctrl-C to stop."
# This loop keeps checking for chips. If one is near it will get the UID and authenticate
while continue_reading:
# Scan for cards
(status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
# If a card is found
if status == MIFAREReader.MI_OK:
print "Card detected"
# Get the UID of the card
(status,uid) = MIFAREReader.MFRC522_Anticoll()
# If we have the UID, continue
if status == MIFAREReader.MI_OK:
uid_str = str(uid[0])+"."+str(uid[1])+"."+str(uid[2])+"."+str(uid[3])
# Print UID
#print "Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3])
#print "\n".join(str(uid))
# This is the default key for authentication
key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
# Select the scanned tag
MIFAREReader.MFRC522_SelectTag(uid)
# Authenticate
status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)
# Check if authenticated
if status == MIFAREReader.MI_OK:
print "Inserting data"
data = MIFAREReader.MFRC522_Read(8)
try:
date_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
query = "INSERT INTO %s (tag_uid, company, date_time, card_data) VALUES ('%s', '%s', '%s', '%s')"%('attendance_reader',uid_str,1,str(date_time),data)
cursor.execute(query)
db.commit()
print "Data committed"
except (MySQLdb.Error, MySQLdb.Warning) as e:
print(e)
print "Error: the database is being rolled back"
db.rollback()
MIFAREReader.MFRC522_StopCrypto1()
else:
print "Authentication error"
write.py
#!/usr/bin/env python
# -*- coding: utf8 -*-
import RPi.GPIO as GPIO
import MFRC522
import signal
import sys
import binascii
import os
import time
from random import randint
os.system("sudo kill $(ps aux | grep 'read.py' | awk '{print $2}')")
company = sys.argv[1]
name = sys.argv[2]
email = sys.argv[3]
ident_code = [int(company)]
for x in range(0,15):
ident_code.append(randint(001, 255))
print ident_code
continue_reading = True
# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
global continue_reading
print "Ctrl+C captured, ending read."
continue_reading = False
GPIO.cleanup()
# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)
# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()
# This loop keeps checking for chips. If one is near it will get the UID and authenticate
while continue_reading:
# Scan for cards
(status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
# If a card is found
if status == MIFAREReader.MI_OK:
print "Card detected"
# Get the UID of the card
(status,uid) = MIFAREReader.MFRC522_Anticoll()
# If we have the UID, continue
if status == MIFAREReader.MI_OK:
# This is the default key for authentication
key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
# Select the scanned tag
MIFAREReader.MFRC522_SelectTag(uid)
# Authenticate
status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)
#print "\n"
# Check if authenticated
if status == MIFAREReader.MI_OK:
#print "Sector 8 will now be filled with 0xFF:"
# Write the data
MIFAREReader.MFRC522_Write(8, ident_code)
# Stop
MIFAREReader.MFRC522_StopCrypto1()
#time.sleep(5)
continue_reading = False
os.system( "sudo python read.py" )
# os.system( "sudo python read.py &" )
# os.system( "sudo kill $(ps aux | grep 'write.py' | awk '{print $2}')" )
# Make sure to stop reading for cards
else:
print "Authentication error"