Paso 8: El programa completo (lectura A/D y tienda resultados en base de datos)
de smbus importar SMBus
de import Tkinter *
importación matemáticas
tiempo de importación
fecha y hora de la importación
importación de sqlite3
Conn = sqlite3.connect('database03.db')
c = conn.cursor()
keyword1 = "ch01"
keyword2 = "ch02"
keyword3 = "ch03"
keyword4 = "ch04"
JK = 0
resolución = 3
autobús = SMBus(1)
analogDev = [0x68, 0x69, 0x6a, 0x6b] #address de la viruta de A/D
Chan = [0x18, 0x38, 0x58, 0x78] #channels utilizado en el chip
resultados =]
DEF dataEntry():
c.Execute ("INSERT INTO adresult1 (ID, datestamp, keyword1, ch1, ch2, ch3, ch4) valores (?,?,?,?,?,?,?)" \
, (jk,date,keyword1,results[0],results[1],results[2],results[3]))
c.Execute ("INSERT INTO adresult2 (ID, datestamp keyword2, ch1, ch2, ch3, ch4) valores (?,?,?,?,?,?,?)" \
, (jk,date,keyword1,results[4],results[5],results[6],results[7]))
c.Execute ("INSERT INTO adresult3 (ID, datestamp keyword3, ch1, ch2, ch3, ch4) valores (?,?,?,?,?,?,?)" \
, (jk,date,keyword1,results[8],results[9],results[10],results[11]))
c.Execute ("INSERT INTO adresult4 (ID, datestamp keyword4, ch1, ch2, ch3, ch4) valores (?,?,?,?,?,?,?)" \
, (jk,date,keyword1,results[12],results[13],results[14],results[15]))
Conn.Commit()
DEF getadreading(address,adcConfig):
adcreading = bus.read_i2c_block_data(address,adcConfig)
h = adcreading [0]
l = adcreading [1]
s = adcreading [2]
mientras (s & 128):
adcreading = bus.read_i2c_block_data(address,adcConfig)
h = adcreading [0]
l = adcreading [1]
s = adcreading [2]
t = (h << 8) | l
t = t / 16384.0
devolver t
DEF readValues():
para i en gama (0,len(analogDev)):
b en gama (0,len(chan)):
k = getadreading(analogDev[i],chan[b])
Results.Append ((k, resolución) de la Ronda)
fecha = str(datetime.datetime.fromtimestamp(int(time.time())) .strftime ('%Y - m %-%d % H: % m: %S'))
readValues()
h = 15 + (len(results)) * 20
w = 215 +(resolution * 10)
s = 22
ventana = Tk()
lienzo = lona (ventana, ancho = w, altura = h, bg = "gris")
Canvas.Pack()
b en el rango (0, len(results)):
Canvas.create_text (80, s+(b*20), texto = 'Analógica lectura')
Canvas.create_text (150, s+(b*20), texto = b)
Canvas.create_text (175, s+(b*20), texto = "=")
Window.Update()
resultados =]
Aunque la verdad:
ahora = time.time()
fecha = str(datetime.datetime.fromtimestamp(int(time.time())) .strftime ('%Y - m %-%d % H: % m: %S'))
readValues()
para i en gama (0,len(results)):
Canvas.create_rectangle (180, (s+(i*20)-10), w,s+((i*20)+10), contorno = "gris", fill = "gris")
Canvas.create_text (195, s+(i*20), ancla = W, texto = results[i])
Window.Update()
dataEntry()
JK = jk + 1
resultados =]
endTime = time.time()
Time.Sleep (5 - (Hora_fin - ahora))