1

I am running a program for an alarm clock displaying weather data from wunderground, but sometimes I get the urllib2 error message:

URLError: <urlopen error [Errno -2] Name or service not known>

and the raspberry stops, so next morning I will not wake up.

Code:

while True:
    if  time.strftime("%M") == "50":
        lcd.clear()
        f = urllib2.urlopen('http://api.wunderground.com/api/my_API/conditions/lang:BR/q/pws:IPORTUGA71.json')
        json_string = f.read()
        parsed_json = json.loads(json_string)
        tempo = parsed_json ['current_observation']['weather']
        vento = parsed_json ['current_observation']['wind_kph']
        vento_dir = parsed_json ['current_observation']['wind_dir']
        chuva = parsed_json ['current_observation']['precip_today_metric']
        print ("tempo: %s  vento: %s km/h" %(tempo, vento))
        lcd.cursor_pos = (2, 0)
        lcd.write_string("%s    %s mm/h" %(tempo, chuva))
        lcd.cursor_pos =(3,0)
        lcd.write_string("vento %skm/h %s" %(vento, vento_dir))
        f.close()
        time.sleep(30)
    curr_time = int(time.strftime("%H%M"))
    humidade, temperatura = Adafruit_DHT.read_retry(11, 4)
    if humidade == None: humidade = 0
    if temperatura == None: temperatura = 0
    lcd.cursor_pos = (0, 0)
    lcd.write_string("%d'C" % temperatura)
    lcd.cursor_pos = (0, 5)
    lcd.write_string("%d%%" % humidade)
    lcd.cursor_pos = (0,10)
    lcd.write_string("Wake %s" % (hh + ":" + mm))
    lcd.cursor_pos = (1, 0)
    lcd.write_string("%s" % time.strftime("%H:%M"))
    lcd.cursor_pos = (1, 10)
    lcd.write_string("%s" % time.strftime("%d/%m/%Y"))
    if GPIO.input(on_off) == 1:    

I am thinking to include an if to check error writing something instead just to get working the program but I don't know how; I feel that error comes from a weather station stop or internet fail

I will be grateful for a help

PC

1

I am thinking to include an if to check error writing something instead just to get working the program

That is a good idea to check for potential errors while writing a good code.

but I don't know how; I feel that error comes from a weather station stop or internet fail

The whole error would show the stacktrace from where you would be able to determine the actual line of the error in your code. But I believe the error comes from the line 3, where you call urlopen(). In python, it is common to write the try: except blocks to catch potential errors:

try:
    f = urllib2.urlopen('http://api.wunderground.com/api/my_API/conditions/lang:BR/q/pws:IPORTUGA71.json')
    # the rest of handling f
except URLError:
    print("Error fetching data from wunderground")
    pass
  • 1
    Looks fairly sensible to me - catching the error is definitely an improvement over a crash. It might be worth mentioning that catching the error doesn't mean you'll get the result you're looking for - the program should probably be rejigged a little to ensure that a caught exception results in a retry, or the required data won't be fetched until the next hour. – goobering May 24 '17 at 7:51
  • try...except without catching the error solves the endless while loop to run always when api data from pws is down occasionaly, In that case I set a space to data string to lcd.write_string. – Pedro Costa May 29 '17 at 21:39

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.