简体   繁体   中英

Python loop print change of state only once

I'm pretty new to Python and I just start to understand the basics. I'm trying to run a script in a loop to check the temperatures and if the outside temp getting higher than inside or the opposite, the function should print it once and continue to check every 5 seconds, for changed state. I found a similar questions what was very helpful but if I execute the code it print the outside temp is higher, next I heat up the inside sensor and it prints that it is inside higher, all good except that it doesn't continue, the loop works but it doesn't recognize the next change of state. .

import RPi.GPIO as GPIO
import time      
sensor_name_0 = "test"
printed_out = False
printed_in = False  

try:
    while True:   
    if sensor_name_0:
        sensor_0 = open('/sys/devices/w1_bus_master1/w1_master_slaves','r').read().split('\n')[0]
        sensor_1 = open('/sys/devices/w1_bus_master1/w1_master_slaves','r').read().split('\n')[1]

        sensorpath = "/sys/bus/w1/devices/"                   
        sensorfile = "/w1_slave"                                          

    def callsensor_0(sensor_0): 
        f = open(sensorpath + sensor_0 + sensorfile, 'r')     
        lines = f.readlines()                                                 
        f.close()                                                                
        temp_line = lines[1].find('t=')
        temp_output = lines[1].strip() [temp_line+2:] 
        temp_celsius = float(temp_output) / 1000          
        return temp_celsius

    def callsensor_1(sensor_1):         
        f = open(sensorpath + sensor_1 + sensorfile, 'r')     
        lines = f.readlines()                                                 
        f.close()                                                                 
        temp_line = lines[1].find('t=')
        temp_output = lines[1].strip() [temp_line+2:]  
        temp_celsius = float(temp_output) / 1000          
        return temp_celsius


    outside = (str('%.1f' % float(callsensor_0(sensor_0))).rstrip('0').rstrip('.'))  
    inside = (str('%.1f' % float(callsensor_1(sensor_1))).rstrip('0').rstrip('.'))  
    print "loop"

    if outside > inside and not printed_out:                         
        printed_out = True
        print "outside is higher then inside"
        print  outside


    if outside < inside and not printed_in:
        printed_in = True
        print "inside is higher then outside"
        print  inside

    time.sleep(5)    

except KeyboardInterrupt:
print('interrupted!')

Both the flags printed_in and printed_out are set to True after printing the corresponding messages. You are not setting them back to False because of which the if condition will never get satisified and the messages will never print again. You should set printed_out to False in the if block where you are printing that the inside temperature is higher and similarly set printed_in to False in the if block where you are printing outside temperature is higher

    if outside > inside and not printed_out:                         
        printed_out = True
        print "outside is higher then inside"
        print  outside
        printed_in = False  

    if outside < inside and not printed_in:
        printed_in = True
        print "inside is higher then outside"
        print  inside
        printed_out = False
    time.sleep(5)    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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