Python: Update Sensor Data to Variable -


i have temperature , pressure gauge want use track temperature on time with. since may end multiple sensors on time, want able reference bmp085 sensor tp. in other words, call tp.temp or tp.pressure obtain current temperature, etc. problem tp.temp or .pressure not updating each time call it. suggestions?

#!/usr/bin/env python #temperature logger bmp085 temperature , pressure sensor on raspberry pi  adafruit_bmp085 import bmp085 time import sleep import pickle, sys, os  class tps():     def __init__(self):         #temperature/pressure sensor setup         self.bmp = bmp085(0x77)         self.temp = self.bmp.readtemperature()*1.8+32         self.pressure = self.bmp.readpressure()*0.0002953  class data():     def __init__(self):         self.tp = tps()         self.savedata()       def savedata(self): #        if os.path.exists("data.dat")==true: #            if os.path.isfile("data.dat")==true: #                filehandle = open ( 'data.dat' ) #                olddata = pickle.load ( filehandle ) #                filehandle.close()          print self.tp.temp, self.tp.pressure         sleep(4)         print self.tp.temp, self.tp.pressure  #        newdata = [self.tp.temp, self.tp.pressure] #        self.datadump = [olddata] #        self.datadump.append(newdata) #        filehandle = open ( 'data.dat', 'w' ) #        pickle.dump ( self.datadump, filehandle ) #        filehandle.close()               data() 

that's because called bmp.readtemperature() , bmp.readpressure() functions once - in tps.__init__. in print statements @ end, you're twice reading values functions returned, rather getting updated values.

here's example of how updated values:

class tps():     def __init__(self):         #temperature/pressure sensor setup         self.bmp = bmp085(0x77)         self.temp = none         self.pressure = none #       if want initialize tps object sensor data, can call updater method here.         self.updatetempandpressure()  #   here's function can call whenever want updated data sensor     def updatetempandpressure(self):         self.temp = self.bmp.readtemperature()*1.8+32         self.pressure = self.bmp.readpressure()*0.0002953  class data():     def __init__(self):         self.tp = tps()         self.savedata()      def savedata(self): #       call method gets updated data sensor         self.tp.updatetempandpressure()         print self.tp.temp, self.tp.pressure         sleep(4) #       call update method again         self.tp.updatetempandpressure()         print self.tp.temp, self.tp.pressure  data() 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -