Infinite loop stops

248 Views Asked by At

I have googled this a lot and I can only find answers that relate to conditions within the loop being met. I want this loop to run infinitely (Hence while 1==1) and I'm testing it at the moment by just leaving it running in Thonny. It runs for variable lengths of time and then just stops. It doesn't exit the program or stop running, it just behaves as if it's waiting for something but there's nothing that I can see that it's waiting for. The shell doesn't report any errors or report that it has stopped running, it simply stops printing the string in the fourth line print statement.

I am very new to python and Linux and I have no idea how to debug this problem or where to look for the stopping point. Even running it in debug mode doesn't render any helpful information. Has anyone got any suggestions please?

The only other thing that I have tried outside of what I have said is I have tried running it on a fresh install of Raspberry Pi OS on three different Raspberry Pi 4 Model B computers. It behaves exactly the same on all of them.

while 1==1:
  time.sleep(1)
  cnt = 1
  print('One = One loop ' + str(datetime.today()) + ' CNT: ' + str(cnt))
   while Decimal(target_temperature()) - Decimal(0.3) >= Decimal(actual_temperature()) and switch_state() == 'currently not running':
    print('Before heating loop ' + str(datetime.today()))
    try:
        if cnt == 1:
            if Decimal(target_temperature()) - Decimal(0.3) >= Decimal(actual_temperature()) and switch_state() == 'currently not running':
                print('First heating loop ' + str(datetime.today()))
                requests.get('http://192.168.1.167/4/on')
                log_db('On', str(target_temperature()), str(actual_temperature()))
                time.sleep(225)
                requests.get('http://192.168.1.167/4/off')
                log_db('Off', str(target_temperature()), str(actual_temperature()))
                time.sleep(300)
                cnt = cnt + 1
        if(cnt != 1):
            if Decimal(target_temperature()) - Decimal(0.3) >= Decimal(actual_temperature()) and switch_state() == 'currently not running':
                print('Second heating loop ' + str(datetime.today()))
                requests.get('http://192.168.1.167/4/on')
                log_db('On', str(target_temperature()), str(actual_temperature()))
                time.sleep(180)
                requests.get('http://192.168.1.167/4/off')
                log_db('Off', str(target_temperature()), str(actual_temperature()))
                time.sleep(300)
    except Exception as e:
        print(e) 
2

There are 2 best solutions below

1
On

Bearing in mind i don't know anything about python i will try to help.

1 - The first thing i would do is put the whole program in a try catch block. That wasy if anything bad happens you should be told about it

try:
   <all your code>
except Exception as e2:
        print('The whole thing errored' + e2)

2 - The delays are in seconds? For testing i would change every sleep to (30) so you can see what is going on without getting too bored waiting, when you have it working then change the times back.

3 - I would add some more print('got here!') like where you have if(cnt == 1) add else print('first loop wasnt 1 it was ' + cnt)

4 - try and make the code easier for you to read, when it gets actually run it will be so optimized that it wont bear any relation to what you write. So write it in a way that is easiest for you

5 - You turn it on and then off, but if the off failed it would never be turned off, you should assume that it will go badly and that will be the day you get a big bill. Try and stop it if an error occurs by adding another check if actualTemp > targetTemp then turn it off?

6 - the http request might take ages, specify a time in seconds you are prepared to wait like , timeout=60

try:
  while 1==1:
    try:
      time.sleep(30)
  
      targetTemp = Decimal(target_temperature())
      actualTemp = Decimal(actual_temperature())
      switchState = switch_state() 

      print('Doing it at ' + str(datetime.now()) + ' target ' + str(targetTemp) + ' actual ' + str(actualTemp) + ' switch ' + switchState)

      if targetTemp - Decimal(0.3) >= actualTemp and switchState == 'currently not running'
        print('too cold turning it for a bit!')
        requests.get('http://192.168.1.167/4/on', timeout=60)
        log_db('On', targetTemp , actualTemp)
      else if actualTemp > targetTemp and switchState != 'currently not running'
        print('too hot turning it off!')
        requests.get('http://192.168.1.167/4/off', timeout=60)
        log_db('Off', targetTemp , actualTemp)
      else
        print('Not doing anything!')
    except Exception as e1:
      print('Loop errored -> ' + e1)     
except Exception as e2:
  print('Whole thing errored -> ' + e2) 
0
On

Thanks Billy the Kid. You were right. Sometimes the devices that the loop uses via HTTPRequests just don't reapond (the two functions use HTTPRequests) and sometimes they create errors that aren't caught in the loop. Putting the whole thing in a try/catch oddly did identify that. Problem solved.