Skip to content

Commit 25538f9

Browse files
author
brentru
committed
show all 3 forecasts nicely
1 parent 8914f59 commit 25538f9

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

examples/mqtt/mqtt_weather.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
for subscribing to the Adafruit IO Weather Service
44
Note: This feature is avaliable for IO Plus Subscribers ONLY
55
6+
API Documentation: https://io.adafruit.com/services/weather
7+
68
Author: Brent Rubell for Adafruit Industries
79
"""
810

@@ -93,10 +95,13 @@ def parseForecast(forecast_data):
9395
"""
9496
# incoming data is a utf-8 string, encode it as a json object
9597
forecast = json.loads(forecast_data)
96-
print(forecast)
97-
print('It is {0} and {1} F.'.format(forecast['summary'], forecast['temperature']))
98-
print('with a humidity of {0}% and a wind speed of {1}mph.'.format(forecast['humidity'], forecast['windSpeed']))
99-
print('There is a {0}% chance of precipitation. It feels like {1} F'.format(forecast['precipProbability'], forecast['apparentTemperature']))
98+
try:
99+
print('It is {0} and of {1}F.'.format(forecast['summary'], forecast['temperature']))
100+
except KeyError:
101+
# future weather forecasts return a high and low temperature, instead of 'temperature'
102+
print('It will be {0} with a high of {1}F and a low of {2}F.'.format(forecast['summary'], forecast['temperatureLow'], forecast['temperatureHigh']))
103+
print('with a humidity of {0}% and a wind speed of {1}mph.'.format(forecast['humidity'], forecast['windSpeed']))
104+
print('There is a {0}% chance of precipitation.'.format(forecast['precipProbability']))
100105

101106
# Create an MQTT client instance.
102107
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

0 commit comments

Comments
 (0)