Paso 10: Enregistrement vers la base de donnée
Pour sauvegarder les valeurs captés python script de la le de il faut à nouveau modificador
import _mysql from datetime import date, datetime, timedelta import os import sys import pexpect import time import urllib2 import urllib ble_addr="E5:AE:C1:71:96:E4" class Sensortag: def __init__(self,ble_addr): self.ble_addr=ble_addr self.child = pexpect.spawn('gatttool -t random -b ' + ble_addr + ' -I') self.child.expect('\[LE\]>') print("Try to connect to the board") self.child.sendline('connect') self.child.expect('Connection successful') print("Connected") return def getTemperature(self): print("try to update the temperature") self.child.sendline('char-write-req 0x0011 0100 -listen') self.child.expect('Characteristic value was written successfully') print("wait line") self.child.sendline('char-write-cmd 0x000e A00100') self.child.expect('Notification handle = 0x0010 value: 0b .*') rval = self.child.after.split() temperature = int(rval[7], 16) print(temperature) return temperature def getPressure(self): print("try to update the pressure") self.child.sendline('char-write-req 0x0011 0100 -listen') self.child.expect('Characteristic value was written successfully') self.child.sendline('char-write-cmd 0x000e A00300') self.child.expect('Notification handle = 0x0010 value: 0b .*') rval = self.child.after.split() pressure = "" + rval[6] + rval[7] print("pressure: " + str(int(pressure, 16))) return int(pressure, 16) def exit(self): self.child.sendline('exit'); return def main(): db=_mysql.connect(host="localhost",user="root", passwd="raspberry",db="iot") sensortag=Sensortag(ble_addr) i = 0 while i < 50: aux=("INSERT INTO Temperature(time, temp, pressure)" " VALUE (\""+datetime.now().strftime("%Y-%m-%d %H:%M:%S") +"\", "+str(sensortag.getTemperature())+ ", " +str(sensortag.getPressure()) + ")") print(aux) db.query(aux) time.sleep(10) i -= 1 sensortag.exit(); if __name__ == "__main__": main()