import serial,sqlite #laster inn bindinger for serie og database, her sqlite
db=sqlite.connect("eksempeldatabase") #Kobler til
cursor=db.cursor()
ser = serial.Serial('/dev/ttyS1', 19200, timeout=1)
while True:
line = ser.readline()
cursor.execute("INSERT into table VALUES(?,?,?,?,?), line.split(","))
#############################################################################
# Butler script
# Copyleft Rasmus Hildonen 2008
#
#############################################################################
# Inkludes
import serial, MySQLdb, time
# Sette opp serieport
ser=serial.Serial('/dev/ttyS0', 2400, timeout=1)
# Koble til database
db = MySQLdb.connect(host="localhost", user="xxx", passwd="xxx",db="xxx")
# Lage en cursor
cursor = db.cursor()
# Loopen
while True:
# Lese inn data i basen
line = ser.readline() # Lese data fra serieport
data = line.split(",") # Splitte opp data
if data[0] == '0' : # Sjekke om det er data til server
print data[0]+" "+data[1]+" "+data[2]+" "+data[3]+" "+data[4]+" "+data[5]+" "+data[6] # debug
cursor.execute("""INSERT INTO logg (mottager,avsender,temperatur,s1,s2,s3,s4) VALUES ("%s","%s","%s","%s","%s","%s","%s")""" % (data[0],data[1],data[2],data[3],data[4],data[5],data[6])) # Legge inn data i basen
# Hente kommandoer og sende til moduler
cursor.execute("""SELECT * FROM oppgaver WHERE sendt LIKE 0""") # Velge oppgaver som ikke er sendt
while(1):
row = cursor.fetchone ()
if row == None:
break
print "%s, %s" % (row[2], row[3]) # debug
kommando = row[2] + row[3]
ser.write(kommando) # sende kommando
cursor.execute("""UPDATE oppgaver SET sendt = 1 WHERE id LIKE %s""" % (row[0]))
# Lukke pent etter seg
ser.close
Registrerte brukere: Google [Bot]