vendredi 22 mars 2019

Is there a better way to average two temperature sensor inputs?

I'm building an overhead console for my car. I want to display the temperature outside using the average reading from two temp sensors. I am currently using Timo's Library and am able to retrieve temperatures from each sensor. When I try to average or convert the temps, nothing happens when I run the code except for a blinking cursor. I am new to python.

I have tried adjusting my definitions, the order in which statements are entered, I have changed the locations of the statements from inside the if statements to outside. Hoping its something that I missed. I apologize ahead of time if my question is not formatted correctly.

import os
import time
import serial
from w1thermsensor import W1ThermSensor

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

#---------------------------------------------------------
#  Define the function to convert Celsius to Fahrenheit
#---------------------------------------------------------

def CelsiusToFahrenheit(TempCelsius):
 TempFahrenheit = ((TempCelsius*9)/5) + 32
 return TempFahrenheit

#----------------------------------------------------------
#  Define function to Average Outside Temperature in Fahrenheit
#----------------------------------------------------------

def OutsideTempAvg(A, B):
  OutsideTemp = (A + B)/2
  return OutsideTemp

#----------------------------------------------------------
#  Set Pointer to W1ThermSensor object
#----------------------------------------------------------

TmpSensor = W1ThermSensor()

#----------------------------------------------------------
#  Define Sensors by ID
#----------------------------------------------------------

Fsensor = '03079779c963'
Rsensor = '03159779fe5a'
Isensor = '030797799182'

#----------------------------------------------------------
#  Loop Through Available Sensors Identify and Assign 
#----------------------------------------------------------
while True:

    for TmpSensor in W1ThermSensor.get_available_sensors():
        CurSensorID = TmpSensor.id
        CurTempC = round(float(TmpSensor.get_temperature()),1)

        if CurSensorID == Fsensor:
            FtempC = CurTempC
        elif CurSensorID == Rsensor:
            RtempC = CurTempC
        elif CurSensorID == Isensor:
            ItempC = CurTempC
        else:
            print("Unexpected Sensor = " + CurSensorID)
            print("Temp = " + str(FtempC))
            print("Temp = " + str(RtempC))
            print("Temp = " + str(ItempC))

#----------------------------------------------------------------
#  Average and Convert Celsius to Fahrenheit
#----------------------------------------------------------------

            OtempC = round(float(OutsideTempAvg(FtempC, RtempC)))
            OtempF = round(float(CelsiusToFahrenheit(OtempC)))
            FtempF = round(float(CelsiusToFahrenheit(FtempC)))
            RtempF = round(float(CelsiusToFahrenheit(RtempC)))
            ItempF = round(float(CelsiusToFahrenheit(ItempC)))

#-----------------------------------------------------------------
#  Print for testing - Final will be sent to HMI display over Serial
#------------------------------------------------------------------     

            print("Outside Temperature = " + str(OtempF) + " Fahrenheit")
            print("Inside Temperature = " + str(ItempF) + " Fahrenheit")

#---------------------------------------------------------------------
#  Reset time - Final will be 5 minutes
#----------------------------------------------------------------------

    time.sleep(5.0)

I want to be able to print the readings, in both C and F, from each sensor and the average from two of them (Fsensor and Rsensor). Eventually I will send the results out of the serial interface of the Pi to an HMI LCD.

Aucun commentaire:

Enregistrer un commentaire