Skip to content

Instantly share code, notes, and snippets.

@grfiv
Last active April 5, 2021 13:15
Show Gist options
  • Select an option

  • Save grfiv/b92970891211c55f4b9e1faba50ba3a6 to your computer and use it in GitHub Desktop.

Select an option

Save grfiv/b92970891211c55f4b9e1faba50ba3a6 to your computer and use it in GitHub Desktop.

Revisions

  1. grfiv revised this gist Apr 5, 2021. 1 changed file with 8 additions and 14 deletions.
    22 changes: 8 additions & 14 deletions weatherlink.py
    Original file line number Diff line number Diff line change
    @@ -82,17 +82,16 @@

    pd.DataFrame(data=item_data,index=[pd.Timestamp(datetime.datetime.today())])

    ## --------------------
    ## Read historical data
    ## --------------------

    end_timestamp = int(time.time())
    start_timestamp = end_timestamp - 86400 # maximum: 24 hours

    t = str(int(time.time()))
    message_to_hash = 'api-key'+APIKeyv2+\
    'end-timestamp'+str(end_timestamp)+\
    'start-timestamp'+str(start_timestamp)+\
    'station-id'+str(station_id)+\
    't'+t
    t = int(time.time())
    message_to_hash = "api-key{}end-timestamp{}start-timestamp{}station-id{}t{}"\
    .format(APIKeyv2, end_timestamp, start_timestamp, station_id, t)
    #print(message_to_hash)

    apiSignature = hmac.new(
    @@ -102,13 +101,9 @@
    ).hexdigest()
    #print(apiSignature)

    historic_url = 'https://api.weatherlink.com/v2/historic/'+str(station_id)+\
    '?api-key='+APIKeyv2+\
    '&t='+t+\
    '&start-timestamp='+str(start_timestamp)+\
    '&end-timestamp='+str(end_timestamp)+\
    '&api-signature='+apiSignature
    print(historic_url,'\n')
    historic_url = "https://api.weatherlink.com/v2/historic/{}?api-key={}&t={}&start-timestamp={}&end-timestamp={}&api-signature={}"\
    .format(station_id, APIKeyv2, t, start_timestamp, end_timestamp, apiSignature)
    #print(historic_url,'\n')

    with urllib.request.urlopen(historic_url) as url:
    data = json.loads(url.read().decode())
    @@ -118,7 +113,6 @@

    local_timezone = tzlocal.get_localzone()

    df['date'] = [pd.Timestamp(datetime.datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')) for ts in df.ts]
    df['date'] = [pd.Timestamp(datetime.datetime.fromtimestamp(ts, local_timezone).strftime("%Y-%m-%d %H:%M:%S.%f%z (%Z)")) for ts in df.ts]
    df.set_index('date',inplace=True)
    df.sort_index(inplace=True)
  2. grfiv revised this gist Apr 5, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions weatherlink.py
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,7 @@
    import datetime
    import time
    import tzlocal
    import matplotlib.pyplot as plt

    import hashlib
    import hmac
  3. grfiv revised this gist Apr 5, 2021. 1 changed file with 11 additions and 10 deletions.
    21 changes: 11 additions & 10 deletions weatherlink.py
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,9 @@
    APISecret = '... and this'


    ## ----------------------
    ## Read the stations data
    ## ----------------------

    t = int(time.time())
    message_to_hash = "api-key{}t{}"\
    @@ -47,25 +49,24 @@
    station_name = data['stations'][0]['station_name']
    print("\nstation_id: {}\nstation_name: {}".format(station_id,station_name))


    ## --------------------------------
    ## Read the current conditions data
    ## --------------------------------

    t = str(int(time.time()))
    message_to_hash = 'api-key'+APIKeyv2+'station-id'+str(station_id)+'t'+t
    print(message_to_hash)
    t = int(time.time())
    message_to_hash = "api-key{}station-id{}t{}"\
    .format(APIKeyv2, station_id, t)
    #print(message_to_hash)

    apiSignature = hmac.new(
    APISecret.encode('utf-8'),
    message_to_hash.encode('utf-8'),
    hashlib.sha256
    ).hexdigest()


    current_url = 'https://api.weatherlink.com/v2/current/'+str(station_id)+\
    '?api-key='+APIKeyv2+\
    '&t='+t+\
    '&api-signature='+apiSignature
    print(current_url,'\n')
    current_url = "https://api.weatherlink.com/v2/current/{}?api-key={}&t={}&api-signature={}"\
    .format(station_id, APIKeyv2, t, apiSignature)
    #print(current_url,'\n')

    with urllib.request.urlopen(current_url) as url:
    data = json.loads(url.read().decode())
  4. grfiv revised this gist Apr 5, 2021. 1 changed file with 10 additions and 8 deletions.
    18 changes: 10 additions & 8 deletions weatherlink.py
    Original file line number Diff line number Diff line change
    @@ -20,32 +20,34 @@
    APIKeyv2 = 'get this for your device'
    APISecret = '... and this'


    ## Read the stations data

    t = str(int(time.time()))
    message_to_hash = 'api-key'+APIKeyv2+'t'+t
    t = int(time.time())
    message_to_hash = "api-key{}t{}"\
    .format(APIKeyv2, t)

    apiSignature = hmac.new(
    APISecret.encode('utf-8'),
    message_to_hash.encode('utf-8'),
    hashlib.sha256
    ).hexdigest()

    stations_url = 'https://api.weatherlink.com/v2/stations?'+\
    'api-key='+APIKeyv2+\
    '&t='+t+\
    '&api-signature='+apiSignature
    print(stations_url,'\n')

    stations_url = "https://api.weatherlink.com/v2/stations?api-key={}&t={}&api-signature={}"\
    .format(APIKeyv2, t, apiSignature)
    #print(stations_url,'\n')

    with urllib.request.urlopen(stations_url) as url:
    data = json.loads(url.read().decode())
    print(json.dumps(data, indent=4, sort_keys=False))

    # in my case, there is only one station
    # modifications are required if more than one
    station_id = data['stations'][0]['station_id']
    station_name = data['stations'][0]['station_name']
    print("\nstation_id: {}\nstation_name: {}".format(station_id,station_name))


    ## Read the current conditions data

    t = str(int(time.time()))
  5. grfiv revised this gist Apr 5, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion weatherlink.py
    Original file line number Diff line number Diff line change
    @@ -110,7 +110,7 @@
    data = json.loads(url.read().decode())


    df = pd.DataFrame(data=data['sensors'][0]['data']) #.strftime('%Y-%m-%d %H:%M:%S')
    df = pd.DataFrame(data=data['sensors'][0]['data'])

    local_timezone = tzlocal.get_localzone()

  6. grfiv revised this gist Apr 5, 2021. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions weatherlink.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    # WeatherLink is a sensor that detects particulate matter in the air
    # see https://www.davisinstruments.com

    # It is very useful for determining the air quality in your home or office
    # The tutorial (https://weatherlink.github.io/v2-api/tutorial) is very good but insufficient
    # The code I found online was mostly incomplete, to say the least
  7. grfiv created this gist Apr 5, 2021.
    126 changes: 126 additions & 0 deletions weatherlink.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,126 @@
    # WeatherLink is a sensor that detects particulate matter in the air
    # It is very useful for determining the air quality in your home or office
    # The tutorial (https://weatherlink.github.io/v2-api/tutorial) is very good but insufficient
    # The code I found online was mostly incomplete, to say the least

    import numpy as np
    import pandas as pd
    import datetime
    import time
    import tzlocal

    import hashlib
    import hmac
    import time
    import urllib.request
    import json

    APIKeyv2 = 'get this for your device'
    APISecret = '... and this'

    ## Read the stations data

    t = str(int(time.time()))
    message_to_hash = 'api-key'+APIKeyv2+'t'+t

    apiSignature = hmac.new(
    APISecret.encode('utf-8'),
    message_to_hash.encode('utf-8'),
    hashlib.sha256
    ).hexdigest()

    stations_url = 'https://api.weatherlink.com/v2/stations?'+\
    'api-key='+APIKeyv2+\
    '&t='+t+\
    '&api-signature='+apiSignature
    print(stations_url,'\n')


    with urllib.request.urlopen(stations_url) as url:
    data = json.loads(url.read().decode())
    print(json.dumps(data, indent=4, sort_keys=False))

    station_id = data['stations'][0]['station_id']
    station_name = data['stations'][0]['station_name']
    print("\nstation_id: {}\nstation_name: {}".format(station_id,station_name))

    ## Read the current conditions data

    t = str(int(time.time()))
    message_to_hash = 'api-key'+APIKeyv2+'station-id'+str(station_id)+'t'+t
    print(message_to_hash)

    apiSignature = hmac.new(
    APISecret.encode('utf-8'),
    message_to_hash.encode('utf-8'),
    hashlib.sha256
    ).hexdigest()


    current_url = 'https://api.weatherlink.com/v2/current/'+str(station_id)+\
    '?api-key='+APIKeyv2+\
    '&t='+t+\
    '&api-signature='+apiSignature
    print(current_url,'\n')

    with urllib.request.urlopen(current_url) as url:
    data = json.loads(url.read().decode())
    #print(json.dumps(data, indent=4, sort_keys=True))

    current_data = data['sensors'][0]['data'][0]
    current_items = ["hum","temp","aqi_val","pm_1","pm_2p5","pm_10"]

    item_data = {}
    for item in current_items:
    item_data[item] = int(current_data[item])

    pd.DataFrame(data=item_data,index=[pd.Timestamp(datetime.datetime.today())])

    ## Read historical data

    end_timestamp = int(time.time())
    start_timestamp = end_timestamp - 86400 # maximum: 24 hours

    t = str(int(time.time()))
    message_to_hash = 'api-key'+APIKeyv2+\
    'end-timestamp'+str(end_timestamp)+\
    'start-timestamp'+str(start_timestamp)+\
    'station-id'+str(station_id)+\
    't'+t
    #print(message_to_hash)

    apiSignature = hmac.new(
    APISecret.encode('utf-8'),
    message_to_hash.encode('utf-8'),
    hashlib.sha256
    ).hexdigest()
    #print(apiSignature)

    historic_url = 'https://api.weatherlink.com/v2/historic/'+str(station_id)+\
    '?api-key='+APIKeyv2+\
    '&t='+t+\
    '&start-timestamp='+str(start_timestamp)+\
    '&end-timestamp='+str(end_timestamp)+\
    '&api-signature='+apiSignature
    print(historic_url,'\n')

    with urllib.request.urlopen(historic_url) as url:
    data = json.loads(url.read().decode())


    df = pd.DataFrame(data=data['sensors'][0]['data']) #.strftime('%Y-%m-%d %H:%M:%S')

    local_timezone = tzlocal.get_localzone()

    df['date'] = [pd.Timestamp(datetime.datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')) for ts in df.ts]
    df['date'] = [pd.Timestamp(datetime.datetime.fromtimestamp(ts, local_timezone).strftime("%Y-%m-%d %H:%M:%S.%f%z (%Z)")) for ts in df.ts]
    df.set_index('date',inplace=True)
    df.sort_index(inplace=True)
    display(df)

    ## Plot 24 hours of particulate matter

    df.loc[:,['pm_1_avg','pm_2p5_avg','pm_10_avg']].plot(figsize=(20,20))
    plt.grid()
    plt.axhline(y=12) # PM2.5 max healthy level
    plt.show()