是否有更好的方法来平均两个温度传感器输入?

时间:2019-03-23 01:49:39

标签: python function if-statement average sensor

我正在为我的汽车建造一个顶置控制台。我想使用两个温度传感器的平均值显示外部温度。我目前正在使用Timo的资料库,并且能够从每个传感器检索温度。当我尝试平均或转换温度时,运行代码时,除了闪烁的光标外,什么都没有发生。我是python的新手。

我尝试调整我的定义,输入语句的顺序,并且将语句的位置从if语句内部更改为外部。希望它是我错过的东西。如果我的问题格式不正确,我会提前道歉。

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)

我希望能够打印每个传感器的C和F读数,以及两个传感器(Fsensor和Rsensor)的平均值。最终,我会将结果从Pi的串行接口发送到HMI LCD。

2 个答案:

答案 0 :(得分:0)

我认为您在缩进方面遇到了问题

while True:
    for sensor in W1ThermSensor.get_available_sensors():
        if some_condition:
            pass
        elif some_other_condition:
            pass
        else:  # unreal case just to handle all possible cases
            print('this is not printed when everything is ok')

            calculcate1()  # this function is called only if above is printed!

    calculate2()  # <-- move all calculation to the same level as "for" loop.

旁注:

  • Python允许您随便初始化变量,但是在访问变量之前初始化变量是您的责任!想象到Rsensor在“ for”循环上不可用,您的RtempC将永远不会初始化。要解决这个问题-只需提前创建变量即可。

答案 1 :(得分:0)

你是完全正确的,这就是我的想法。

while True:

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

        if CurSensorID == Fsensor:
            FtempC = CurTempC
            FtempF = CurTempF
#            print("F-Temp = " + str(FtempC) + " C" + ", " + str(FtempF) + " F")
        elif CurSensorID == Rsensor:
            RtempC = CurTempC
            RtempF = CurTempF
#            print("R-Temp = " + str(RtempC) + " C" + ", " + str(RtempF) + " F")
        elif CurSensorID == Isensor:
            ItempC = CurTempC
            ItempF = round(float(CurTempF))
#            print("I-Temp = " + str(ItempC) + " C" + ", " + str(ItempF) + " F")
        else:
            print("Unexpected Sensor = " + CurSensorID)





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

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

#-----------------------------------------------------------------
#  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(60.0)