somewhereinusa
Posts: 10
Joined: Thu Dec 10, 2015 1:59 pm
Location: Andrews,IN
Contact: Website

Limit output to two decimals

Sat Jan 09, 2016 10:51 pm

I have finally gotten this to read two DS18B20 sensors. How do I limit the display to two decimals?

Code: Select all

#works on 3
import threading
import time

DEVICESDIR = "/sys/bus/w1/devices/"

#class for holding temperature values
class Temperature():
    def __init__(self, rawData):
        self.rawData = rawData
    @property
    def C(self):
        return float(self.rawData) / 1000
    @property
    def F(self):
        return self.C * 9.0 / 5.0 + 32.0

#class for controlling the temperature sensor
class TempSensorController(threading.Thread):
    def __init__(self, sensorId, timeToSleep):
        threading.Thread.__init__(self)
       
        #persist the file location
        self.tempSensorFile = DEVICESDIR + sensorId + "/w1_slave"

        #persist properties
        self.sensorId = sensorId
        self.timeToSleep = timeToSleep

         #update the temperature
        self.updateTemp()
       
        #set to not running
        self.running = False
       
    def run(self):
        #loop until its set to stopped
        self.running = True
        while(self.running):
            #update temperature
            self.updateTemp()
            #sleep
            time.sleep(self.timeToSleep)
        self.running = False
       
    def stopController(self):
        self.running = False

    def readFile(self):
        sensorFile = open(self.tempSensorFile, "r")
        lines = sensorFile.readlines()
        sensorFile.close()
        return lines

    def updateTemp(self):
        data = self.readFile()
        
        #has a YES been returned?
        if data[0].strip()[-3:] == "YES":
            #can I find a temperature (t=)
            equals_pos = data[1].find("t=")
            if equals_pos != -1:
                tempData = data[1][equals_pos+2:]
                #update temperature
                self.temperature = Temperature(tempData)
                #update success status
                self.updateSuccess = True
            else:
                self.updateSuccess = False
        else:
            self.updateSuccess = False
       
if __name__ == "__main__":

    #create temp sensor controller, put your controller Id here
    # look in "/sys/bus/w1/devices/" after running
    #  sudo modprobe w1-gpio
    #  sudo modprobe w1-therm
    #Engine
    tempcontrol = TempSensorController("28-011561b31cff", 1)
    #Outside
    tempcontrol2 = TempSensorController("28-021561a10dff", 1)

    try:
        #print("Starting temp sensor controller")
        #start up temp sensor controller
        tempcontrol.start()
        tempcontrol2.start()
        #loop forever, wait for Ctrl C
        while(True):
            #print tempcontrol.temperature.C
            print ("Engine temp")               
            print (tempcontrol.temperature.F)
            print ("Outside temp")
            print (tempcontrol2.temperature.F)
            time.sleep(1)
    #Ctrl C
    except KeyboardInterrupt:
        print ("Cancelled")
   
    #Error
    except:
        print ("Unexpected error:"), sys.exc_info()[0]
        raise

    #if it finishes or Ctrl C, shut it down
    finally:
        print ("Stopping temp sensor controller")
        #stop the controller
        tempcontrol.stopController()
        #wait for the tread to finish if it hasn't already
        tempcontrol.join()
       
    #print "Done"

User avatar
kusti8
Posts: 3439
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

Re: Limit output to two decimals

Sat Jan 09, 2016 11:16 pm

There are 10 types of people: those who understand binary and those who don't.

somewhereinusa
Posts: 10
Joined: Thu Dec 10, 2015 1:59 pm
Location: Andrews,IN
Contact: Website

Re: Limit output to two decimals

Sun Jan 10, 2016 4:44 am

I have looked at that post many times over the last week. There are at least four answers, which seem to be dealing with money. I think my biggest problem is I don't know where to put it.

ghp
Posts: 1518
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Limit output to two decimals

Sun Jan 10, 2016 7:38 am

Hello,

Code: Select all

            print ("Engine temp")               
            print (tempcontrol.temperature.F)
            print ("Outside temp")
            print (tempcontrol2.temperature.F)
could go to

Code: Select all

            print ("Engine temp {et:.2f} Outside temp {ot:.2f}". format( et= tempcontrol.temperature.F, ot=tempcontrol2.temperature.F))
(untested).

Regards,
Gerhard

somewhereinusa
Posts: 10
Joined: Thu Dec 10, 2015 1:59 pm
Location: Andrews,IN
Contact: Website

Re: Limit output to two decimals

Sun Jan 10, 2016 2:33 pm

Thank you,thank you,thank you.
That is exactly what I needed.

Dick

Return to “Python”