melona380
Posts: 3
Joined: Sun Apr 19, 2015 9:28 pm

Raspberry Pi Freezes after running Python script

Sun Apr 19, 2015 9:32 pm

Hi, I am using the Pi to check for any weather statements and if it sees a tornado warning it uses a PiFace to set off an alarm. Program is in continuous loop but the problem is raspberry pi freezing every few hours(<24 hr) after starting the script. It will no longer accept input and I have to hard boot.

It is a Model B+ using Ethernet, USB power, 8 GB SD Card, Raspbian, and a PiFace on the GPIO pins using Pi's Power.

Please help, Its fine if it has to reboot but with minimal downtime.

Code: Select all

    import feedparser
    import pifacedigitalio as pfio
    import time
    import smtplib
    import datetime
    import os

    tornado = False
    horn = False
    strobe = False
    _alarm = False
    alarmack = False
    reset_ = False

    mailsent =False
    mailalreadysent = False
    pfio.init()



    while True:
        pfio.digital_write(7,1)
        '''print("------------------------------------------------------------------")'''
         #url = 'http://alerts.weather.gov/cap/us.php?x=1'
        url = 'http://alerts.weather.gov/cap/wwaatmget.php?x=NEC109&y=0'
        feed = feedparser.parse(url)
        '''print (feed.entries[0].title)'''
        if feed.entries[0].has_key("cap_event") is False:
            
                '''print('Alert: None')'''
                tornado = False
                
              
        else:
           
                '''print ('Alert:', feed.entries[0].cap_event)'''
                if feed.entries[0].cap_event == "Tornado Warning":
                        tornado = True
                        sendmail()
                        
                else:
                        tornado = False
                        
        localtime = time.asctime( time.localtime(time.time()) )
        
        '''print("Tornado Warning:",tornado)'''
        '''print ("Current Local Time:", localtime)'''
        '''print("------------------------------------------------------------------")'''
       
        
    #ALARM
        
        if tornado == True:
                _alarm = True
                horn = True
                strobe = True
                '''print("---------------------------------------------------------------------")
                'print("Tornado Warning Issued, TAKE SHELTER... TAKE SHELTER... TAKE SHELTER")
                'print("ALARM ACTIVE")
                'print("---------------------------------------------------------------------")'''
               
                pfio.digital_write(4,1)
                time.sleep(1)
                pfio.digital_write(4,0)
               
        else:
                _alarm = False
                horn = False
                strobe = False                
                '''print("---------------------------------------------------------------------")
                'print("No Tornado Warning Issued")
                'print("ALARM INACTIVE")
                'print("---------------------------------------------------------------------")'''
              
                
        
        if pfio.digital_read(0) == 1:
                alarmack= True
        else:
                alarmack = False
        if pfio.digital_read(1) == 1:
                reset_ = True
        else:
                reset_ = False
      
       
        #GPIO
        _rly1 = False
        _rly2 = False
        
        _reset = False
        #while True:
        '''print("---------------------------------------------------------------------")'''
        if reset_ == False:
                if horn == True and alarmack == False:
                        _rly2 = True
                        '''print("Horn: Active")'''
                elif alarmack == True and horn == True:
                        _rly2 = False
                        '''print("Horn: Inactive")'''
                        _rly2 = False
                elif alarmack== False and horn == False:
                        '''print("Horn: Inactive")'''
                        _rly2 = False                                
                if strobe == True:
                        _rly1 = True
                        '''print("Strobe: Active")'''
                else:
                        _rly1 = False
                        '''print("Strobe: Inactive")'''
        else:
                _reset = True
                _rly1 = False
                _rly2 = False
                chirp()
        '''print("---------------------------------------------------------------------")  ''' 
                
        
         
        if _rly1 == True:
                
                pfio.digital_write(0,1)
                
        else:
                
                pfio.digital_write(0,0)
                
                
        if _rly2 == True:
                
                pfio.digital_write(1,1)
                
        else:
                
                pfio.digital_write(1,0)
                
                
       
        '''print("---------------------STOP--------------------------")'''
        os.system('clear')
        

        
        def chirp():
            pfio.digital_write(5,1)
            time.sleep(1)
            pfio.digital_write(5,0)

User avatar
DougieLawson
Posts: 37074
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Raspberry Pi Freezes after running Python script

Sun Apr 19, 2015 11:17 pm

Is it a complete hard freeze? Does it respond to a ping? Can you ssh in? Or is it totally non-responsive?
Do you get an messages in /var/log/dmesg /var/log/dmesg.0 /var/log/messages or /var/log/syslog?
Is there anything displayed on your TV/monitor when it dies?

Have you looked at running free -lh periodically to see if memory usage is increasing?

Are your pifacecommon, pifacedigitialio and libmcp23s17 libraries up to date?
Is Raspbian up to date?

Your code is also running the CPU very busy because there's no timer delays in the main loop. Even when I take the piface calls out it's running 22.4% of my model B. I don't have a pifacedigitial, I may be able to get it running with my PifaceCAD for testing.

How often does http://alerts.weather.gov/cap/wwaatmget ... NEC109&y=0 publish new data? Because you're fetching that RSS feed every time round the loop. How long can you wait before a tornado warning becomes critical for you?
alerts.weather.gov wrote:***PULL***
The major advantage of the Atom feeds and the pull is that it is easy and inexpensive to implement with little or no resources. The disadvantage is the inherent delay introduced by a pull service like alerts.weather.gov. The NWS CAP v1.2 pull has been designed and engineered to reduce the delay as much as possible. It should be understood that it is only as fast as the client is configured to pull it - every 2, 5, 10, even 15 minutes - based on the consumer’s own technical needs and constraints. It should be understood that some alerts (ex. Tornado Warnings) are extremely time sensitive.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

melona380
Posts: 3
Joined: Sun Apr 19, 2015 9:28 pm

Re: Raspberry Pi Freezes after running Python script

Mon Apr 20, 2015 7:53 pm

Thank you for the fast reply, I am at school but as soon as i get home I will try to update everything and check messages in /var/log/dmesg /var/log/dmesg.0 /var/log/messages or /var/log/syslog, and check memory usage. It is a complete hard freeze, it will not ping, the ssh and VNC connections will not connect after this point. 100% unresponsive, however i did notice that since my code does listen for button presses that if you press a button after this point all the lights and relays shut off on the piface, then become unresponsive. The screen is just frozen at the last point before it went unresponsive.

Note I have changed the code to check every 2 minuets.

Here is the updated version of the code:

Code: Select all

import feedparser
import pifacedigitalio as pfio
import time
import smtplib
import datetime

tornado = False
horn = False
strobe = False
_alarm = False
alarmack = False
reset_ = False
checked = True
next_check = time.time()
mailalreadysent = False
pfio.init()


def chirp():
        pfio.digital_write(5,1)
        time.sleep(1)
        pfio.digital_write(5,0)
        

        
while True:      

        if time.time() > next_check:
                url = 'http://alerts.weather.gov/cap/wwaatmget.php?x=NEC109&y=0'
                feed = feedparser.parse(url)
                
                if feed.entries[0].has_key("cap_event") is False:
                            
                        tornado = False
                                                     
                else:
                           
                        if feed.entries[0].cap_event == "Tornado Warning":
                                tornado = True
                        
                        
                        else:
                                tornado = False
                
                next_check = time.time() + 120
        
        if tornado == True:
                _alarm = True
                horn = True
                strobe = True
                
        else:
                        
                _alarm = False
                horn = False
                strobe = False                
               
              
                
        
        if pfio.digital_read(0) == 1:
                alarmack= True
        else:
                alarmack = False
        if pfio.digital_read(1) == 1:
                reset_ = True
        else:
                reset_ = False
      
       
        #GPIO
        _rly1 = False
        _rly2 = False
        
        _reset = False
        
        
        if reset_ == False:
                if horn == True and alarmack == False:
                        _rly2 = True
                        
                elif alarmack == True and horn == True:
                        _rly2 = False
                        
                        _rly2 = False
                elif alarmack== False and horn == False:
                       
                        _rly2 = False                                
                if strobe == True:
                        _rly1 = True
                        
                else:
                        _rly1 = False
                        
        else:
                _reset = True
                _rly1 = False
                _rly2 = False
                chirp()
        
       
        if _rly1 == True:
                pfio.digital_write(0,1)
        else:
                pfio.digital_write(0,0)
        if _rly2 == True:
                pfio.digital_write(1,1)
        else:
                pfio.digital_write(1,0)
                
       

       
                                
       
        

bulletmark
Posts: 121
Joined: Wed Oct 17, 2012 10:10 pm
Location: Brisbane Australia

Re: Raspberry Pi Freezes after running Python script

Mon Apr 20, 2015 11:31 pm

melona380 wrote: Note I have changed the code to check every 2 minuets.
Yes you have but the overall while loop is still run flat out on the CPU. You need a small time.sleep() in there.

melona380
Posts: 3
Joined: Sun Apr 19, 2015 9:28 pm

Re: Raspberry Pi Freezes after running Python script

Tue Apr 21, 2015 12:34 am

Yes you have but the overall while loop is still run flat out on the CPU. You need a small time.sleep() in there.
@bulletmark

How long of a sleep, would 5-10 sec work? I need to minimize main loop down time.

bulletmark
Posts: 121
Joined: Wed Oct 17, 2012 10:10 pm
Location: Brisbane Australia

Re: Raspberry Pi Freezes after running Python script

Tue Apr 21, 2015 12:42 am

melona380 wrote: How long of a sleep, would 5-10 sec work? I need to minimize main loop down time.
No, that would be too slow to see your button presses. You need to think about and understand your code better. Also, use 4 spaces instead of 8 and get rid of redundant code and variables.

ghp
Posts: 1456
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Raspberry Pi Freezes after running Python script

Tue Apr 21, 2015 4:13 am

Some other reason for system freezes is overclocking. Use raspi-config to reset overclock and give it a try.

Return to “Python”