0

The following script is to send email alerts, for some reason it keeps bypassing the flag I set..

############################################################
##                       MAIN                             ##
############################################################

Open_serial()

while True:

    line = ser.readline()
    if line:
        tmp = line.split()
        if 'tank' in line:
            tank_temp = tmp[1]
            print 'Tank temperature:',tank_temp
            if tank_temp >= 27:
                flag = raise_flag()
                if flag:
                    send_email('Tank temperature overheat',tank_temp)
                    flag = False
            elif tank_temp <= 23:
                flag = raise_flag()
                if flag:
                    send_email('Tank temperature too low',tank_temp)
                    flag = False

        if 'led' in line:
            led_temp = tmp[1]
            print 'Lights temperature:',led_temp
            if led_temp >= 55:
                flag = raise_flag()
                if flag:
                    send_email('Lights temperature Overheat',led_temp)
                    flag = False

        if 'White' in line:

            white_pwm = tmp[1]
            print 'White lights PWM value:',white_pwm
            white_per = white_percent(white_pwm)
            print 'White lights % value:',white_per

        if 'Blue' in line:
            blue_pwm = tmp[1]
            print 'Blue lights PWM:',blue_pwm
            blue_per = blue_percent(blue_pwm)
            print 'Blue lights % value:',blue_per

And here is the flag function:

def raise_flag():
    global start
    interval = 900
    if start > interval:
        start = 0
        flag = True
        return flag
    else:
        flag = False
        start = start + 1
        time.sleep(1)
        return flag

the script keeps sending emails when the temp is whithin range i.e. 25°C, I dont know why it keeps sending emails? I placed the flag so when the condition is met it doesnt send 1000 emails, only 1 every 15minutes...

pes502
  • 1,597
  • 3
  • 17
  • 32
celamb
  • 68
  • 5
  • example of the email sent: Warning error occurred!! Tank temperature overheat: 25.31... As you can see the temp was OK and it should not have raised the flag to send the email... – celamb Sep 08 '13 at 18:45

1 Answers1

1

readline() passed into line.split() will generate a list of strings. So you are comparing a string (your data) with an integer (27). This is not a good idea. see this explanation of how sting/int comparison works.

To fix it, simply cast your data to a float like this before you manipulate it:

tank_temp = float(tmp[1])

or compare strings like this:

if tank_temp >= '27':

I would opt for the former as your data should be floats so it makes sense to convert them to floats.

Community
  • 1
  • 1
Graeme Stuart
  • 5,837
  • 2
  • 26
  • 46
  • Thanks! I modified the script, will keep it running but your answer was quit clear and makes sense... The temp variable is float so I went with option 1 as you stated.. Thanks! – celamb Sep 08 '13 at 19:48