简体   繁体   English

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

[英]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.我目前正在使用 Timo 的库,并且能够从每个传感器中检索温度。 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.我是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.我尝试调整我的定义,输入语句的顺序,我已将语句的位置从 if 语句内部更改为外部。 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).我希望能够打印来自每个传感器的 C 和 F 读数以及其中两个(Fsensor 和 Rsensor)的平均值。 Eventually I will send the results out of the serial interface of the Pi to an HMI LCD.最终我会将结果从 Pi 的串行接口发送到 HMI LCD。

I think you have an issue with indentation:我认为你有缩进的问题:

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.

side note:边注:

  • Python allow to init variables as you go, but it is your responsibility that variables are initialized before accessed! Python 允许您随时初始化变量,但您有责任在访问之前初始化变量! Imaging that Rsensor was not available on "for" loop, your RtempC would never be initialized.想象 Rsensor 在“for”循环中不可用,您的 RtempC 将永远不会被初始化。 To address it - simply create variables ahead of time.为了解决它 - 只需提前创建变量。

You are absolutely correct, here is what I came up with.你是完全正确的,这是我想出的。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM