Étape 8: Le programme complet (lecture A/D et magasin de résultats de base de données)
smbus importons SMBus
de Tkinter import *
importer des mathématiques
moment de l’importation
importation de datetime
importation de sqlite3
Conn = sqlite3.connect('database03.db')
c = conn.cursor()
motcle1 = « ch01 »
keyword2 = « ch02 »
keyword3 = « ch03 »
keyword4 = « ch04 »
JK = 0
résolution = 3
bus = SMBus(1)
analogDev = [0x68, 0x69, 0x6a, 0x6b] #address de puce A/D
Chan = [0 x 18, 0 x 38, 0 x 58, 0 x 78] #channels utilisé sur puce
résultats =]
def dataEntry() :
c.Execute ("INSERT INTO adresult1 (ID, datestamp, motcle1, ch1, ch2, ch3, ch4) valeurs (?,?,?,?,?,?,?)" \
, (jk,date,keyword1,results[0],results[1],results[2],results[3]))
c.Execute ("INSERT INTO adresult2 (ID, datestamp, keyword2, ch1, ch2, ch3, ch4) valeurs (?,?,?,?,?,?,?)" \
, (jk,date,keyword1,results[4],results[5],results[6],results[7]))
c.Execute ("INSERT INTO adresult3 (ID, datestamp, keyword3, ch1, ch2, ch3, ch4) valeurs (?,?,?,?,?,?,?)" \
, (jk,date,keyword1,results[8],results[9],results[10],results[11]))
c.Execute ("INSERT INTO adresult4 (ID, datestamp, keyword4, ch1, ch2, ch3, ch4) valeurs (?,?,?,?,?,?,?)" \
, (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]
tandis que (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
retour t
def readValues() :
car moi en éventail (0,len(analogDev)) :
b dans la gamme (0,len(chan)) :
k = getadreading(analogDev[i],chan[b])
Results.Append (ronde (k, résolution))
Date = str(datetime.datetime.fromtimestamp(int(time.time())) .strftime ('%A-%m-%j % H: % m: %S"))
readValues()
h = 15 + (len(results)) * 20
w = 215 +(resolution * 10)
s = 22
fenêtre = Tk()
toile = toile (fenêtre, largeur = w, hauteur = h, bg = « gris »)
Canvas.Pack()
b dans l’intervalle (0, len(results)) :
Canvas.create_text (80, s+(b*20), text = « Lecture analogique »)
Canvas.create_text (150, s+(b*20), texte = b)
Canvas.create_text (175, s+(b*20), text = « = »)
Window.Update()
résultats =]
While True :
maintenant = time.time()
Date = str(datetime.datetime.fromtimestamp(int(time.time())) .strftime ('%A-%m-%j % H: % m: %S"))
readValues()
car moi en éventail (0,len(results)) :
Canvas.create_rectangle (180, (s+(i*20)-10), w,s+((i*20)+10), contour = « gris », fill = « gris »)
Canvas.create_text (195, s+(i*20), Ancre = W, texte = results[i])
Window.Update()
dataEntry()
JK = jk + 1
résultats =]
endTime = time.time()
Time.Sleep (5 - (endTime - maintenant))