Flask application, when I click the "Start" button on the 'home.html' page, I want to run the RFID reader and also redirect to another page. To achieve this, I used threading to run the RFID reader function, and successfully rendered the second page. However, I encountered an issue where the RFID reader function is not saving data to the database.
from flask import Flask, render_template
import threading
import serial
import datetime
import sqlite3
DB_NAME = ""
con = sqlite3.connect(DB_NAME)
cursor = con.cursor()
app = Flask(__name__)
def read_start():
rfid_tags = []
diff_tag = []
i=0
ser = serial.Serial(port='/dev/ttyUSB0', baudrate=115200, timeout=0.01)
cmd_bytes = b''
cmd_inven = b''
ser.write(cmd_bytes);
while True:
ser.write(cmd_inven);
data = ser.readline()
datastring = data.hex(' ').upper()
rfid_tags.append(datastring)
if rfid_tags[i][0:5] == 'A0 13' and rfid_tags[i] not in diff_tag:
byte_array = rfid_tags[i]
data = {
'PC' : byte_array[15:20],
'EPC': byte_array[21:56],
'time' : datetime.datetime.now().strftime("%H:%M:%S")
}
try:
sql_query = ''' INSERT INTO readedTag (epc, pc, read_time) VALUES (?,?,?); '''
cursor.execute(sql_query,(data['EPC'],data['PC'],data['time']))
con.commit()
cursor.close()
con.close()
except:
pass
i+=1
thread = threading.Thread(target=read_start)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/start_read')
def start():
thread.start()
return render_template('second_page.html')
if __name__ == '__main__':
app.run(debug=True)