Reading RFID via USB on Raspberry with Python

2.7k views Asked by At

i got this 2 python script

import serial
serial = serial.Serial("/dev/ttyUSB0", baudrate=9600)

code = ''

while True:
        data = serial.read()
        if data == '\r':
                print(code)
                code = ''
        else:
                code = code + data

and

import time
import datetime
import MySQLdb

localtime = time.localtime(time.time())

day = localtime.tm_wday
time = localtime.tm_hour

print day
print time

data = 'DOSEN1'

db = MySQLdb.connect("localhost", "root", "", "skripsi")
cur = db.cursor()

cond1 = "SELECT nama_dosen FROM dosen WHERE kode_dosen = '%s'" %data
cur.execute(cond1)
hitung = cur.rowcount
res1 = cur.fetchall()
for row in res1:
        nama_dosen = row[0]
if hitung == 1:
        res1 = nama_dosen
elif hitung != 1:
        print "Dosen tidak Terdaftar"

how can i join this 2 script so that the data = 'DOSEN1' can be replaced with the RFID tag number?

i really new to this programming languange, really need help. thanks

1

There are 1 answers

3
Michael Roland On

Assuming that print(code) gives you the value for data in the second script, something like this should work:

import serial
import time
import datetime
import MySQLdb

serial = serial.Serial("/dev/ttyUSB0", baudrate=9600)

db = MySQLdb.connect("localhost", "root", "", "skripsi")
cur = db.cursor()

code = ''

while True:
    data = serial.read()
    if data == '\r':
        print(code)

        localtime = time.localtime(time.time())
        day = localtime.tm_wday
        time = localtime.tm_hour
        print day
        print time

        cond1 = "SELECT nama_dosen FROM dosen WHERE kode_dosen = '%s'" %code
        cur.execute(cond1)
        hitung = cur.rowcount
        res1 = cur.fetchall()
        for row in res1:
            nama_dosen = row[0]
        if hitung == 1:
            res1 = nama_dosen
        elif hitung != 1:
            print "Dosen tidak Terdaftar"

        code = ''
    else:
        code = code + data