Skip to content

Commit dc54320

Browse files
author
brentru
committed
update weather, remove broken example
1 parent abad94d commit dc54320

File tree

2 files changed

+29
-54
lines changed

2 files changed

+29
-54
lines changed

examples/basics/darksky.py

-29
This file was deleted.

examples/mqtt/mqtt_weather.py

+29-25
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
forecast_in_5_days = 'forecast_days_5'
5050

5151
# Define callback functions which will be called when certain events happen.
52+
# pylint: disable=redefined-outer-name
5253
def connected(client):
5354
# Connected function will be called when the client is connected to Adafruit IO.
5455
# This is a good place to subscribe to feed changes. The client parameter
@@ -64,45 +65,48 @@ def connected(client):
6465
# Subscribe to changes on forecast in 5 days.
6566
client.subscribe_weather(forecast_id, forecast_in_5_days)
6667

68+
# pylint: disable=unused-argument
6769
def disconnected(client):
6870
# Disconnected function will be called when the client disconnects.
6971
print('Disconnected from Adafruit IO!')
7072
sys.exit(1)
7173

74+
# pylint: disable=unused-argument
7275
def message(client, topic, payload):
7376
"""Message function will be called when any subscribed forecast has an update.
7477
Weather data is updated at most once every 20 minutes.
7578
"""
7679
# forecast based on mqtt topic
7780
if topic == 'current':
78-
# Print out today's forecast
79-
today_forecast = payload
80-
print('\nCurrent Forecast')
81-
parseForecast(today_forecast)
81+
# Print out today's forecast
82+
today_forecast = payload
83+
print('\nCurrent Forecast')
84+
parseForecast(today_forecast)
8285
elif topic == 'forecast_days_2':
83-
# Print out tomorrow's forecast
84-
two_day_forecast = payload
85-
print('\nWeather in Two Days')
86-
parseForecast(two_day_forecast)
86+
# Print out tomorrow's forecast
87+
two_day_forecast = payload
88+
print('\nWeather in Two Days')
89+
parseForecast(two_day_forecast)
8790
elif topic == 'forecast_days_5':
88-
# Print out forecast in 5 days
89-
five_day_forecast = payload
90-
print('\nWeather in 5 Days')
91-
parseForecast(five_day_forecast)
91+
# Print out forecast in 5 days
92+
five_day_forecast = payload
93+
print('\nWeather in 5 Days')
94+
parseForecast(five_day_forecast)
9295

9396
def parseForecast(forecast_data):
94-
"""Parses and prints incoming forecast data
95-
"""
96-
# incoming data is a utf-8 string, encode it as a json object
97-
forecast = json.loads(forecast_data)
98-
99-
# Print out the forecast
100-
try:
101-
print('It is {0} and {1}F.'.format(forecast['summary'], forecast['temperature']))
102-
except KeyError:
103-
# future weather forecasts return a high and low temperature, instead of 'temperature'
104-
print('It will be {0} with a high of {1}F and a low of {2}F.'.format(forecast['summary'], forecast['temperatureLow'], forecast['temperatureHigh']))
105-
print('with a humidity of {0}%, a wind speed of {1}mph, and a {2}% chance of precipitation.'.format(forecast['humidity'], forecast['windSpeed'], forecast['precipProbability']))
97+
"""Parses and prints incoming forecast data
98+
"""
99+
# incoming data is a utf-8 string, encode it as a json object
100+
forecast = json.loads(forecast_data)
101+
# Print out the forecast
102+
try:
103+
print('It is {0} and {1}F.'.format(forecast['summary'], forecast['temperature']))
104+
except KeyError:
105+
# future weather forecasts return a high and low temperature, instead of 'temperature'
106+
print('It will be {0} with a high of {1}F and a low of {2}F.'.format(
107+
forecast['summary'], forecast['temperatureLow'], forecast['temperatureHigh']))
108+
print('with humidity of {0}%, wind speed of {1}mph, and {2}% chance of precipitation.'.format(
109+
forecast['humidity'], forecast['windSpeed'], forecast['precipProbability']))
106110

107111
# Create an MQTT client instance.
108112
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
@@ -118,4 +122,4 @@ def parseForecast(forecast_data):
118122
# Start a message loop that blocks forever waiting for MQTT messages to be
119123
# received. Note there are other options for running the event loop like doing
120124
# so in a background thread--see the mqtt_client.py example to learn more.
121-
client.loop_blocking()
125+
client.loop_blocking()

0 commit comments

Comments
 (0)