1

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, it's fine if it has to reboot but with minimal downtime.

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)
  • Out of curiosity, how often (per second) is this going to read the wheather service for new data? I don't know the feedparser lib but maybe a multitude of requests is performing badly? – Ghanima Apr 19 '15 at 21:42
  • It has been checking continuously with no delay, because if i delay that it will also delay the outputs. – Melona380 Apr 19 '15 at 21:43
  • Have you looked in /var/log/syslog to see why the pi appears to "freeze"? – goldilocks Apr 20 '15 at 14:48
2

As @Ghanima implied, I don't believe you need to check the url flat out - that's not great for all the other internet users either. Maybe once every 5mins would be enough. You could use something like

if time.time() > next_check
    feed = feedparser.parse(url)
    # etc. all the url stuff here
    next_check = time.time() + 300

and carry on running the rest of your loop flat out.

  • To further emphasize this point, if I were a sys admin at alerts.weather.gov, I would temporarily blacklist IPs that continuously polled a service with no delay -- that is tantamount to a DoS attack, even if your machine alone can't pull it off. – goldilocks Apr 20 '15 at 14:47
  • Thank you, I have made my code better so that it calls to NWS less. Like in your suggestion. I haven't been able to test this on my Pi yet, but once I get home in an hour I will. – Melona380 Apr 20 '15 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.