adafruit / Adafruit_CircuitPython_AdafruitIO

Adafruit IO for CircuitPython
http://io.adafruit.com
MIT License
49 stars 33 forks source link

mqtt subscribe to weather gives no updates, ever #106

Closed flavio-fernandes closed 1 year ago

flavio-fernandes commented 1 year ago

Ref script: https://github.com/flavio-fernandes/adaio/blob/master/scripts/mqtt_weather.py

The message callback never happens. I tried with both Dark sky and the new Apple WeatherKit ids. Waited 1hr+ w/out success.

(env) vagrant@adavm:~/scripts$ ./mqtt_weather.py
Connected to Adafruit IO!
Connected to Adafruit IO!  Listening to forecast: 2645...

zzzzzz ZZZ zzzzz

#!/usr/bin/env python3

"""
Example of using the Adafruit IO MQTT Client
for subscribing to the Adafruit IO Weather Service
Note: This feature is avaliable for IO Plus Subscribers ONLY

API Documentation: https://io.adafruit.com/services/weather

Author: Brent Rubell for Adafruit Industries
"""

# Import standard python modules.
import sys
import json
from os import environ as env

# Import Adafruit IO MQTT client.
from Adafruit_IO import MQTTClient

# Set to your Adafruit IO key.
# Remember, your key is a secret,
# so make sure not to publish it when you publish this code!
ADAFRUIT_IO_KEY = env['IO_KEY']

# Set to your Adafruit IO username.
# (go to https://accounts.adafruit.com to find your username)
ADAFRUIT_IO_USERNAME = env['IO_USERNAME']

# Set to ID of the forecast to subscribe to for updates
forecast_id = env['IO_HOME_WEATHER']

# Set to the ID of the feed to subscribe to for updates.
"""
Valid forecast types are:
current
forecast_minutes_5
forecast_minutes_30
forecast_hours_1
forecast_hours_2
forecast_hours_6
forecast_hours_24
forecast_days_1
forecast_days_2
forecast_days_5
"""
# Subscribe to the current forecast
forecast_today = 'current'
# Subscribe to tomorrow's forecast
forecast_two_days = 'forecast_days_2'
# Subscribe to forecast in 5 days
forecast_in_5_days = 'forecast_days_5'

# Define callback functions which will be called when certain events happen.
# pylint: disable=redefined-outer-name
def connected(client):
    # Connected function will be called when the client is connected to Adafruit IO.
    # This is a good place to subscribe to feed changes.  The client parameter
    # passed to this function is the Adafruit IO MQTT client so you can make
    # calls against it easily.
    print('Connected to Adafruit IO!  Listening to forecast: {0}...'.format(forecast_id))
    # Subscribe to changes on the current forecast.
    client.subscribe_weather(forecast_id, forecast_today)

    # Subscribe to changes on tomorrow's forecast.
    client.subscribe_weather(forecast_id, forecast_two_days)

    # Subscribe to changes on forecast in 5 days.
    client.subscribe_weather(forecast_id, forecast_in_5_days)

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

# pylint: disable=unused-argument
def message(client, topic, payload):
    """Message function will be called when any subscribed forecast has an update.
    Weather data is updated at most once every 20 minutes.
    """
    # forecast based on mqtt topic
    if topic == 'current':
        # Print out today's forecast
        today_forecast = payload
        print('\nCurrent Forecast')
        parseForecast(today_forecast)
    elif topic == 'forecast_days_2':
        # Print out tomorrow's forecast
        two_day_forecast = payload
        print('\nWeather in Two Days')
        parseForecast(two_day_forecast)
    elif topic == 'forecast_days_5':
        # Print out forecast in 5 days
        five_day_forecast = payload
        print('\nWeather in 5 Days')
        parseForecast(five_day_forecast)

def parseForecast(forecast_data):
    """Parses and prints incoming forecast data
    """
    # incoming data is a utf-8 string, encode it as a json object
    forecast = json.loads(forecast_data)
    # Print out the forecast
    try:
        print('It is {0} and {1}F.'.format(forecast['summary'], forecast['temperature']))
    except KeyError:
        # future weather forecasts return a high and low temperature, instead of 'temperature'
        print('It will be {0} with a high of {1}F and a low of {2}F.'.format(
            forecast['summary'], forecast['temperatureLow'], forecast['temperatureHigh']))
    print('with humidity of {0}%, wind speed of {1}mph, and {2}% chance of precipitation.'.format(
        forecast['humidity'], forecast['windSpeed'], forecast['precipProbability']))

# Create an MQTT client instance.
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY, secure=True)

# Setup the callback functions defined above.
client.on_connect    = connected
client.on_disconnect = disconnected
client.on_message    = message

def main():
    # Connect to the Adafruit IO server.
    client.connect()

    # Start a message loop that blocks forever waiting for MQTT messages to be
    # received.  Note there are other options for running the event loop like doing
    # so in a background thread--see the mqtt_client.py example to learn more.
    client.loop_blocking()

if __name__ == "__main__":
    main()
flavio-fernandes commented 1 year ago

heh, nevermind... it parses differently now. Sorry for the noise.

(env) vagrant@adavm:~/scripts$ ./mqtt_weather.py
Connected to Adafruit IO!
Connected to Adafruit IO!  Listening to forecast: 2645...

Weather in 5 Days
FORECAST {'forecastStart': '2023-06-02T04:00:00Z', 'forecastEnd': '2023-06-03T04:00:00Z', 'conditionCode': 'MostlyClear', 'maxUvIndex': 8, 'moonPhase': 'waxingGibbous', 'moonrise': '2023-06-02T23:00:54Z', 'moonset': '2023-06-02T07:51:37Z', 'precipitationAmount': 0.0, 'precipitationAmountByType': {}, 'precipitationChance': 0.0, 'precipitationType': 'clear', 'snowfallAmount': 0.0, 'solarMidnight': '2023-06-02T04:43:36Z', 'solarNoon': '2023-06-02T16:43:49Z', 'sunrise': '2023-06-02T09:10:19Z', 'sunriseCivil': '2023-06-02T08:36:16Z', 'sunriseNautical': '2023-06-02T07:52:23Z', 'sunriseAstronomical': '2023-06-02T06:59:51Z', 'sunset': '2023-06-03T00:17:49Z', 'sunsetCivil': '2023-06-03T00:52:04Z', 'sunsetNautical': '2023-06-03T01:36:12Z', 'sunsetAstronomical': '2023-06-03T02:28:59Z', 'temperatureMax': 33.01, 'temperatureMin': 15.41, 'daytimeForecast': {'forecastStart': '2023-06-02T11:00:00Z', 'forecastEnd': '2023-06-02T23:00:00Z', 'cloudCover': 0.26, 'conditionCode': 'MostlyClear', 'humidity': 0.44, 'precipitationAmount': 0.0, 'precipitationAmountByType': {}, 'precipitationChance': 0.0, 'precipitationType': 'clear', 'snowfallAmount': 0.0, 'windDirection': 65, 'windSpeed': 6.04}, 'overnightForecast': {'forecastStart': '2023-06-02T23:00:00Z', 'forecastEnd': '2023-06-03T11:00:00Z', 'cloudCover': 0.5, 'conditionCode': 'PartlyCloudy', 'humidity': 0.74, 'precipitationAmount': 0.0, 'precipitationAmountByType': {}, 'precipitationChance': 0.0, 'precipitationType': 'clear', 'snowfallAmount': 0.0, 'windDirection': 52, 'windSpeed': 10.17}}

Weather in Two Days
FORECAST {'forecastStart': '2023-05-30T04:00:00Z', 'forecastEnd': '2023-05-31T04:00:00Z', 'conditionCode': 'Clear', 'maxUvIndex': 8, 'moonPhase': 'waxingGibbous', 'moonrise': '2023-05-30T19:27:25Z', 'moonset': '2023-05-30T06:44:26Z', 'precipitationAmount': 0.0, 'precipitationAmountByType': {}, 'precipitationChance': 0.0, 'precipitationType': 'clear', 'snowfallAmount': 0.0, 'solarMidnight': '2023-05-30T04:43:08Z', 'solarNoon': '2023-05-30T16:43:23Z', 'sunrise': '2023-05-30T09:11:50Z', 'sunriseCivil': '2023-05-30T08:38:03Z', 'sunriseNautical': '2023-05-30T07:54:37Z', 'sunriseAstronomical': '2023-05-30T07:03:10Z', 'sunset': '2023-05-31T00:15:26Z', 'sunsetCivil': '2023-05-31T00:49:28Z', 'sunsetNautical': '2023-05-31T01:33:06Z', 'sunsetAstronomical': '2023-05-31T02:24:53Z', 'temperatureMax': 23.08, 'temperatureMin': 5.97, 'daytimeForecast': {'forecastStart': '2023-05-30T11:00:00Z', 'forecastEnd': '2023-05-30T23:00:00Z', 'cloudCover': 0.05, 'conditionCode': 'Clear', 'humidity': 0.46, 'precipitationAmount': 0.0, 'precipitationAmountByType': {}, 'precipitationChance': 0.0, 'precipitationType': 'clear', 'snowfallAmount': 0.0, 'windDirection': 133, 'windSpeed': 7.15}, 'overnightForecast': {'forecastStart': '2023-05-30T23:00:00Z', 'forecastEnd': '2023-05-31T11:00:00Z', 'cloudCover': 0.02, 'conditionCode': 'Clear', 'humidity': 0.74, 'precipitationAmount': 0.0, 'precipitationAmountByType': {}, 'precipitationChance': 0.0, 'precipitationType': 'clear', 'snowfallAmount': 0.0, 'windDirection': 184, 'windSpeed': 7.05}}

Current Forecast
FORECAST {'name': 'CurrentWeather', 'metadata': {'attributionURL': 'https://developer.apple.com/weatherkit/data-source-attribution/', 'expireTime': '2023-05-28T19:16:06Z', 'latitude': 42.642, 'longitude': -71.436, 'readTime': '2023-05-28T19:11:06Z', 'reportedTime': '2023-05-28T18:18:17Z', 'units': 'm', 'version': 1}, 'asOf': '2023-05-28T19:11:06Z', 'cloudCover': 0.0, 'cloudCoverLowAltPct': 0.0, 'cloudCoverMidAltPct': 0.0, 'cloudCoverHighAltPct': 0.0, 'conditionCode': 'Clear', 'daylight': True, 'humidity': 0.31, 'precipitationIntensity': 0.0, 'pressure': 1014.83, 'pressureTrend': 'falling', 'temperature': 31.09, 'temperatureApparent': 30.54, 'temperatureDewPoint': 11.87, 'uvIndex': 6, 'visibility': 42638.24, 'windDirection': 250, 'windGust': 25.49, 'windSpeed': 13.44}

Weather in 5 Days
FORECAST {'forecastStart': '2023-06-02T04:00:00Z', 'forecastEnd': '2023-06-03T04:00:00Z', 'conditionCode': 'MostlyClear', 'maxUvIndex': 8, 'moonPhase': 'waxingGibbous', 'moonrise': '2023-06-02T23:00:54Z', 'moonset': '2023-06-02T07:51:37Z', 'precipitationAmount': 0.0, 'precipitationAmountByType': {}, 'precipitationChance': 0.0, 'precipitationType': 'clear', 'snowfallAmount': 0.0, 'solarMidnight': '2023-06-02T04:43:36Z', 'solarNoon': '2023-06-02T16:43:49Z', 'sunrise': '2023-06-02T09:10:19Z', 'sunriseCivil': '2023-06-02T08:36:16Z', 'sunriseNautical': '2023-06-02T07:52:23Z', 'sunriseAstronomical': '2023-06-02T06:59:51Z', 'sunset': '2023-06-03T00:17:49Z', 'sunsetCivil': '2023-06-03T00:52:04Z', 'sunsetNautical': '2023-06-03T01:36:12Z', 'sunsetAstronomical': '2023-06-03T02:28:59Z', 'temperatureMax': 33.01, 'temperatureMin': 15.41, 'daytimeForecast': {'forecastStart': '2023-06-02T11:00:00Z', 'forecastEnd': '2023-06-02T23:00:00Z', 'cloudCover': 0.26, 'conditionCode': 'MostlyClear', 'humidity': 0.44, 'precipitationAmount': 0.0, 'precipitationAmountByType': {}, 'precipitationChance': 0.0, 'precipitationType': 'clear', 'snowfallAmount': 0.0, 'windDirection': 65, 'windSpeed': 6.04}, 'overnightForecast': {'forecastStart': '2023-06-02T23:00:00Z', 'forecastEnd': '2023-06-03T11:00:00Z', 'cloudCover': 0.5, 'conditionCode': 'PartlyCloudy', 'humidity': 0.74, 'precipitationAmount': 0.0, 'precipitationAmountByType': {}, 'precipitationChance': 0.0, 'precipitationType': 'clear', 'snowfallAmount': 0.0, 'windDirection': 52, 'windSpeed': 10.17}}

Weather in Two Days
FORECAST {'forecastStart': '2023-05-30T04:00:00Z', 'forecastEnd': '2023-05-31T04:00:00Z', 'conditionCode': 'Clear', 'maxUvIndex': 8, 'moonPhase': 'waxingGibbous', 'moonrise': '2023-05-30T19:27:25Z', 'moonset': '2023-05-30T06:44:26Z', 'precipitationAmount': 0.0, 'precipitationAmountByType': {}, 'precipitationChance': 0.0, 'precipitationType': 'clear', 'snowfallAmount': 0.0, 'solarMidnight': '2023-05-30T04:43:08Z', 'solarNoon': '2023-05-30T16:43:23Z', 'sunrise': '2023-05-30T09:11:50Z', 'sunriseCivil': '2023-05-30T08:38:03Z', 'sunriseNautical': '2023-05-30T07:54:37Z', 'sunriseAstronomical': '2023-05-30T07:03:10Z', 'sunset': '2023-05-31T00:15:26Z', 'sunsetCivil': '2023-05-31T00:49:28Z', 'sunsetNautical': '2023-05-31T01:33:06Z', 'sunsetAstronomical': '2023-05-31T02:24:53Z', 'temperatureMax': 23.08, 'temperatureMin': 5.97, 'daytimeForecast': {'forecastStart': '2023-05-30T11:00:00Z', 'forecastEnd': '2023-05-30T23:00:00Z', 'cloudCover': 0.05, 'conditionCode': 'Clear', 'humidity': 0.46, 'precipitationAmount': 0.0, 'precipitationAmountByType': {}, 'precipitationChance': 0.0, 'precipitationType': 'clear', 'snowfallAmount': 0.0, 'windDirection': 133, 'windSpeed': 7.15}, 'overnightForecast': {'forecastStart': '2023-05-30T23:00:00Z', 'forecastEnd': '2023-05-31T11:00:00Z', 'cloudCover': 0.02, 'conditionCode': 'Clear', 'humidity': 0.74, 'precipitationAmount': 0.0, 'precipitationAmountByType': {}, 'precipitationChance': 0.0, 'precipitationType': 'clear', 'snowfallAmount': 0.0, 'windDirection': 184, 'windSpeed': 7.05}}

Current Forecast
FORECAST {'name': 'CurrentWeather', 'metadata': {'attributionURL': 'https://developer.apple.com/weatherkit/data-source-attribution/', 'expireTime': '2023-05-28T19:16:06Z', 'latitude': 42.642, 'longitude': -71.436, 'readTime': '2023-05-28T19:11:06Z', 'reportedTime': '2023-05-28T18:18:17Z', 'units': 'm', 'version': 1}, 'asOf': '2023-05-28T19:11:06Z', 'cloudCover': 0.0, 'cloudCoverLowAltPct': 0.0, 'cloudCoverMidAltPct': 0.0, 'cloudCoverHighAltPct': 0.0, 'conditionCode': 'Clear', 'daylight': True, 'humidity': 0.31, 'precipitationIntensity': 0.0, 'pressure': 1014.83, 'pressureTrend': 'falling', 'temperature': 31.09, 'temperatureApparent': 30.54, 'temperatureDewPoint': 11.87, 'uvIndex': 6, 'visibility': 42638.24, 'windDirection': 250, 'windGust': 25.49, 'windSpeed': 13.44}