Fix PyPlanter; update other catches of requests exceptions

This commit is contained in:
Dan Halbert 2022-07-26 18:20:19 -04:00
parent 24436a1ea7
commit 9eb5844d4c
40 changed files with 51 additions and 50 deletions

View file

@ -168,7 +168,7 @@ while True:
# Hourly reset # Hourly reset
if cur_time.tm_min == 0: if cur_time.tm_min == 0:
prv_mins = 0 prv_mins = 0
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Failed to fetch time, retrying\n", e) print("Failed to fetch time, retrying\n", e)
wifi.reset() wifi.reset()
wifi.connect() wifi.connect()
@ -200,7 +200,7 @@ while True:
io.send_data(feed_temperature["key"], str(temperature)) io.send_data(feed_temperature["key"], str(temperature))
io.send_data(feed_humidity["key"], str(humidity)) io.send_data(feed_humidity["key"], str(humidity))
print("Published!") print("Published!")
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Failed to send data to IO, retrying\n", e) print("Failed to send data to IO, retrying\n", e)
wifi.reset() wifi.reset()
wifi.connect() wifi.connect()

View file

@ -129,7 +129,7 @@ client.subscribe(feed_relay)
while True: while True:
try: # Poll for new messages on feed_relay try: # Poll for new messages on feed_relay
client.loop() client.loop()
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Failed to get data, retrying\n", e) print("Failed to get data, retrying\n", e)
wifi.reset() wifi.reset()
client.reconnect() client.reconnect()

View file

@ -169,7 +169,7 @@ while True:
print("Published!") print("Published!")
prv_sensor_value = sensor_value prv_sensor_value = sensor_value
start_time = now start_time = now
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Failed to get data, retrying\n", e) print("Failed to get data, retrying\n", e)
wifi.reset() wifi.reset()
client.reconnect() client.reconnect()

View file

@ -141,7 +141,7 @@ io.get("relay")
while True: while True:
try: try:
io.loop() io.loop()
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Failed to get data, retrying\n", e) print("Failed to get data, retrying\n", e)
wifi.reset() wifi.reset()
io.reconnect() io.reconnect()

View file

@ -51,7 +51,7 @@ while True:
try: try:
value = matrixportal.fetch() value = matrixportal.fetch()
print("Response is", value) print("Response is", value)
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Some error occured, retrying! -", e) print("Some error occured, retrying! -", e)
time.sleep(3 * 60) # wait 3 minutes time.sleep(3 * 60) # wait 3 minutes

View file

@ -216,8 +216,8 @@ while True:
device.loop() device.loop()
# if something disrupts the loop, reconnect # if something disrupts the loop, reconnect
# pylint: disable=broad-except # pylint: disable=broad-except
except (ValueError, RuntimeError, OSError, Exception) as e: except (ValueError, RuntimeError, OSError, ConnectionError) as e:
print("Connection error, reconnecting\n", str(e)) print("Network error, reconnecting\n", str(e))
supervisor.reload() supervisor.reload()
continue continue
# delay # delay

View file

@ -36,6 +36,6 @@ while True:
try: try:
value = magtag.fetch() value = magtag.fetch()
print("Response is", value) print("Response is", value)
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Some error occured, retrying! -", e) print("Some error occured, retrying! -", e)
timestamp = time.monotonic() timestamp = time.monotonic()

View file

@ -43,6 +43,6 @@ while True:
magtag.peripherals.neopixels.fill(color) magtag.peripherals.neopixels.fill(color)
external_pixels.fill(color) external_pixels.fill(color)
timestamp = time.monotonic() timestamp = time.monotonic()
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Some error occured, retrying! -", e) print("Some error occured, retrying! -", e)
time.sleep(1) time.sleep(1)

View file

@ -137,7 +137,7 @@ while True:
) )
timestamp = time.monotonic() timestamp = time.monotonic()
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
# Catch any random errors so the code will continue running. # Catch any random errors so the code will continue running.
print("Some error occured, retrying! -", e) print("Some error occured, retrying! -", e)
try: try:

View file

@ -60,7 +60,7 @@ while True:
seconds = int(datetime_str[17:19]) seconds = int(datetime_str[17:19])
rtc.RTC().datetime = time.struct_time((year, month, mday, hours, minutes, seconds, 0, 0, False)) rtc.RTC().datetime = time.struct_time((year, month, mday, hours, minutes, seconds, 0, 0, False))
lasttimefetch_stamp = time.monotonic() lasttimefetch_stamp = time.monotonic()
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Some error occured, retrying! -", e) print("Some error occured, retrying! -", e)
continue continue

View file

@ -95,7 +95,7 @@ try:
# OK we're done! # OK we're done!
magtag.peripherals.neopixels.fill(0x000F00) # greten magtag.peripherals.neopixels.fill(0x000F00) # greten
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Some error occured, trying again later -", e) print("Some error occured, trying again later -", e)
time.sleep(2) # let screen finish updating time.sleep(2) # let screen finish updating

View file

@ -112,7 +112,7 @@ try:
SECONDS_TO_SLEEP = 24 * 60 * 60 # Sleep for one day SECONDS_TO_SLEEP = 24 * 60 * 60 # Sleep for one day
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Some error occured, retrying in one hour! -", e) print("Some error occured, retrying in one hour! -", e)
seconds_to_sleep = 60 * 60 # Sleep for one hour seconds_to_sleep = 60 * 60 # Sleep for one hour

View file

@ -95,5 +95,5 @@ while True:
PAUSE = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 60 * 60) PAUSE = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 60 * 60)
alarm.exit_and_deep_sleep_until_alarms(PAUSE) alarm.exit_and_deep_sleep_until_alarms(PAUSE)
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Some error occured, retrying! -", e) print("Some error occured, retrying! -", e)

View file

@ -43,7 +43,7 @@ while True:
qn_idx = header.index("Queens") qn_idx = header.index("Queens")
si_idx = header.index("Staten Island") si_idx = header.index("Staten Island")
nyc_idx = header.index("Citywide") nyc_idx = header.index("Citywide")
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Some error occured, retrying! -", e) print("Some error occured, retrying! -", e)
continue continue

View file

@ -85,6 +85,6 @@ try:
progress_bar.progress = value[1] / 100.0 progress_bar.progress = value[1] / 100.0
magtag.refresh() magtag.refresh()
magtag.exit_and_deep_sleep(24 * 60 * 60) # one day magtag.exit_and_deep_sleep(24 * 60 * 60) # one day
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Some error occurred, retrying! -", e) print("Some error occurred, retrying! -", e)
magtag.exit_and_deep_sleep(60) # one minute magtag.exit_and_deep_sleep(60) # one minute

View file

@ -67,6 +67,6 @@ try:
print(now) print(now)
magtag.exit_and_deep_sleep(24 * 60 * 60) # one day magtag.exit_and_deep_sleep(24 * 60 * 60) # one day
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Some error occurred, retrying after 1 minute! -", e) print("Some error occurred, retrying after 1 minute! -", e)
magtag.exit_and_deep_sleep(60) # one minute magtag.exit_and_deep_sleep(60) # one minute

View file

@ -48,7 +48,7 @@ try:
magtag.network.connect() magtag.network.connect()
value = magtag.fetch() value = magtag.fetch()
print("Response is", value) print("Response is", value)
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
magtag.set_text(e) magtag.set_text(e)
print("Some error occured, retrying later -", e) print("Some error occured, retrying later -", e)
# wait 2 seconds for display to complete # wait 2 seconds for display to complete

View file

@ -50,7 +50,7 @@ try:
magtag.network.connect() magtag.network.connect()
value = magtag.fetch() value = magtag.fetch()
print("Response is", value) print("Response is", value)
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
magtag.set_text(e) magtag.set_text(e)
print("Some error occured, retrying! -", e) print("Some error occured, retrying! -", e)

View file

@ -84,7 +84,7 @@ magtag.add_text(
magtag.add_text( magtag.add_text(
text_font=terminalio.FONT, text_font=terminalio.FONT,
text_position=(10, 94), text_position=(10, 94),
line_spacing=0.8, line_spacing=0.8,
text_wrap=47, # wrap text at this count text_wrap=47, # wrap text at this count
text_transform=details_transform text_transform=details_transform
) )
@ -95,7 +95,7 @@ try:
# This statement gets the JSON data and displays it automagically # This statement gets the JSON data and displays it automagically
value = magtag.fetch() value = magtag.fetch()
print("Response is", value) print("Response is", value)
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Some error occured, retrying! -", e) print("Some error occured, retrying! -", e)
# wait 2 seconds for display to complete # wait 2 seconds for display to complete

View file

@ -68,7 +68,7 @@ magtag.preload_font()
try: try:
value = magtag.fetch() value = magtag.fetch()
print("Response is", value) print("Response is", value)
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Some error occured, retrying! -", e) print("Some error occured, retrying! -", e)
time.sleep(2) time.sleep(2)

View file

@ -172,6 +172,6 @@ while True:
# Reset timer # Reset timer
initial = now initial = now
aws_iot.loop() aws_iot.loop()
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Failed to get data, retrying", e) print("Failed to get data, retrying", e)
wifi.reset() wifi.reset()

View file

@ -88,7 +88,7 @@ while True:
io.send_data(light_feed['key'], light_value) io.send_data(light_feed['key'], light_value)
io.send_data(temperature_feed['key'], temperature, precision=2) io.send_data(temperature_feed['key'], temperature, precision=2)
print('Sent to Adafruit IO!') print('Sent to Adafruit IO!')
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Failed to get data, retrying\n", e) print("Failed to get data, retrying\n", e)
wifi.reset() wifi.reset()
continue continue

View file

@ -97,7 +97,7 @@ while True:
gfx.display_azure_status("Data sent!") gfx.display_azure_status("Data sent!")
print("Data sent!") print("Data sent!")
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Failed to get data, retrying\n", e) print("Failed to get data, retrying\n", e)
wifi.reset() wifi.reset()
wifi.connect() wifi.connect()

View file

@ -42,7 +42,7 @@ while True:
try: try:
value = pyportal.fetch() value = pyportal.fetch()
print("Response is", value) print("Response is", value)
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Some error occured, retrying! -", e) print("Some error occured, retrying! -", e)
time.sleep(3*60) # wait 3 minutes time.sleep(3*60) # wait 3 minutes

View file

@ -46,7 +46,7 @@ while True:
try: try:
value = pyportal.fetch() value = pyportal.fetch()
print("Response is", value) print("Response is", value)
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Some error occured, retrying! -", e) print("Some error occured, retrying! -", e)
time.sleep(3*60) # wait 3 minutes time.sleep(3*60) # wait 3 minutes

View file

@ -189,7 +189,7 @@ while True:
# Reset timer # Reset timer
initial = now initial = now
google_mqtt.loop() google_mqtt.loop()
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, OSError, ConnectionError) as e:
print("Failed to get data, retrying", e) print("Failed to get data, retrying", e)
wifi.reset() wifi.reset()
google_mqtt.reconnect() google_mqtt.reconnect()

View file

@ -51,7 +51,7 @@ while True:
print("New star!") print("New star!")
pyportal.play_file(cwd+"/coin.wav") pyportal.play_file(cwd+"/coin.wav")
last_value = value last_value = value
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Some error occured, retrying! -", e) print("Some error occured, retrying! -", e)
time.sleep(60) # wait a minute before getting again time.sleep(60) # wait a minute before getting again

View file

@ -104,7 +104,7 @@ while True:
text_label.text = 'sending...' text_label.text = 'sending...'
# send data to Adafruit IO (rounded to one decimal place) # send data to Adafruit IO (rounded to one decimal place)
io.send_data(weight_feed['key'], round(reading.weight, 1)) io.send_data(weight_feed['key'], round(reading.weight, 1))
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("failed to send data..retrying...") print("failed to send data..retrying...")
wifi.reset() wifi.reset()
continue continue

View file

@ -52,7 +52,7 @@ while True:
print("New level!") print("New level!")
pyportal.play_file(cwd+"/triode_low_fade.wav") pyportal.play_file(cwd+"/triode_low_fade.wav")
last_value = value last_value = value
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Some error occurred, retrying! -", e) print("Some error occurred, retrying! -", e)
#check again in two minutes #check again in two minutes
time.sleep(60*2) time.sleep(60*2)

View file

@ -90,7 +90,7 @@ while True:
refresh_time = time.monotonic() refresh_time = time.monotonic()
# set the_time # set the_time
the_time = time.localtime() the_time = time.localtime()
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Failed to get data, retrying\n", e) print("Failed to get data, retrying\n", e)
esp.reset() esp.reset()
continue continue

View file

@ -119,7 +119,7 @@ while True:
refresh_time = time.monotonic() refresh_time = time.monotonic()
# set the_time # set the_time
the_time = time.localtime() the_time = time.localtime()
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Failed to get data, retrying\n", e) print("Failed to get data, retrying\n", e)
esp.reset() esp.reset()
continue continue

View file

@ -34,6 +34,6 @@ while True:
try: try:
value = pyportal.fetch() value = pyportal.fetch()
print("Response is", value) print("Response is", value)
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Some error occured, retrying! -", e) print("Some error occured, retrying! -", e)
time.sleep(60) time.sleep(60)

View file

@ -42,7 +42,7 @@ while True:
print("New subscriber!") print("New subscriber!")
pyportal.play_file(cwd+"/coin.wav") pyportal.play_file(cwd+"/coin.wav")
last_value = value last_value = value
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Some error occured, retrying! -", e) print("Some error occured, retrying! -", e)
time.sleep(60) time.sleep(60)

View file

@ -88,7 +88,7 @@ def getchannels():
channel_dict[name] = chan_id channel_dict[name] = chan_id
response.close() response.close()
return channel_dict return channel_dict
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Failed to get data\n", e) print("Failed to get data\n", e)
wifi.reset() wifi.reset()
return None return None
@ -104,7 +104,7 @@ def sendkey(key):
if response: if response:
response.close() response.close()
print("OK") print("OK")
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Failed to get data\n", e) print("Failed to get data\n", e)
wifi.reset() wifi.reset()
return return
@ -119,7 +119,7 @@ def sendletter(letter):
if response: if response:
response.close() response.close()
print("OK") print("OK")
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Failed to get data\n", e) print("Failed to get data\n", e)
wifi.reset() wifi.reset()
return return
@ -135,8 +135,8 @@ def openchannel(channel):
response.close() response.close()
print("OK") print("OK")
response = None response = None
except (ValueError, RuntimeError): except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Probably worked") print("Probably worked, but got error\n", e)
wifi.reset() wifi.reset()
response = None response = None

View file

@ -105,7 +105,7 @@ while True:
gfx.display_io_status('Data sent!') gfx.display_io_status('Data sent!')
except AdafruitIO_RequestError as e: except AdafruitIO_RequestError as e:
raise AdafruitIO_RequestError('IO Error: ', e) raise AdafruitIO_RequestError('IO Error: ', e)
except (ValueError, RuntimeError) as e: # WiFi Connection Failure except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Failed to get data, retrying\n", e) print("Failed to get data, retrying\n", e)
wifi.reset() wifi.reset()
continue continue

View file

@ -45,7 +45,7 @@ while True:
print("New follower!") print("New follower!")
pyportal.play_file(cwd+"/coin.wav") # uncomment make a noise! pyportal.play_file(cwd+"/coin.wav") # uncomment make a noise!
last_value = value last_value = value
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Some error occured, retrying! -", e) print("Some error occured, retrying! -", e)
time.sleep(60) # wait a minute before getting again time.sleep(60) # wait a minute before getting again

View file

@ -82,7 +82,7 @@ while True:
try: try:
json_payload = pyportal.fetch() json_payload = pyportal.fetch()
raw_data = json.loads(json_payload) raw_data = json.loads(json_payload)
except (ValueError, RuntimeError) as ex: except (ValueError, RuntimeError, ConnectionError, OSError) as ex:
print('Error: ', ex) print('Error: ', ex)
if isinstance(ex, ValueError): if isinstance(ex, ValueError):
print('JSON:', json_payload) print('JSON:', json_payload)

View file

@ -439,7 +439,7 @@ while True:
else: else:
pass pass
gc.collect() gc.collect()
except (ValueError, RuntimeError) as err: except (ValueError, RuntimeError, ConnectionError, OSError) as err:
print("Failed to get data, retrying\n", err) print("Failed to get data, retrying\n", err)
wifi.reset() wifi.reset()
mqtt_client.reconnect() mqtt_client.reconnect()

View file

@ -23,7 +23,8 @@ from simpleio import map_range
#---| User Config |--------------- #---| User Config |---------------
# How often to poll the soil sensor, in seconds # How often to poll the soil sensor, in seconds
DELAY_SENSOR = 30 # Polling every 30 seconds or more may cause connection timeouts
DELAY_SENSOR = 15
# How often to send data to adafruit.io, in minutes # How often to send data to adafruit.io, in minutes
DELAY_PUBLISH = 5 DELAY_PUBLISH = 5
@ -179,7 +180,7 @@ label_status.text = "Connecting..."
while not esp.is_connected: while not esp.is_connected:
try: try:
wifi.connect() wifi.connect()
except RuntimeError as e: except (RuntimeError, ConnectionError) as e:
print("could not connect to AP, retrying: ",e) print("could not connect to AP, retrying: ",e)
wifi.reset() wifi.reset()
continue continue
@ -269,7 +270,7 @@ while True:
# to keep the connection active # to keep the connection active
try: try:
io.loop() io.loop()
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Failed to get data, retrying...\n", e) print("Failed to get data, retrying...\n", e)
wifi.reset() wifi.reset()
continue continue
@ -314,7 +315,7 @@ while True:
# reset timer # reset timer
initial = now initial = now
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
label_status.text = "ERROR!" label_status.text = "ERROR!"
print("Failed to get data, retrying...\n", e) print("Failed to get data, retrying...\n", e)
wifi.reset() wifi.reset()

View file

@ -136,7 +136,7 @@ while True:
print('Data sent!') print('Data sent!')
except AdafruitIO_RequestError as e: except AdafruitIO_RequestError as e:
raise AdafruitIO_RequestError('IO Error: ', e) raise AdafruitIO_RequestError('IO Error: ', e)
except (ValueError, RuntimeError) as e: except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Failed to get data, retrying\n", e) print("Failed to get data, retrying\n", e)
wifi.reset() wifi.reset()
continue continue