# the below is an example of how to subscribe to a WIS2 topic # and download the data from the URL provided in the WIS2 data notifcations import json import paho.mqtt.client as mqtt_client import requests import uuid import os import sys BROKER_HOST = 'wis2box-training.wis2dev.io' WEBSOCKETS_PATH = '/mqtt' topics_to_subscribe = [ 'origin/a/wis2/maaike-test/data/recommended/weather/experimental/wis2box-training' ] # function to download data from a given URL and save it to a local directory def download_data(url): # create a directory to save the downloaded data if it doesn't exist download_dir = 'downloaded_data' if not os.path.exists(download_dir): os.makedirs(download_dir) print(f"Downloading data from {url}") # make a GET request to the URL to download the data response = requests.get(url) if response.status_code == 200: filename = url.split('/')[-1] with open(download_dir+'/'+filename, 'wb') as f: f.write(response.content) print(f"Data downloaded and saved as {filename}") else: print(f"Failed to download data from {url}. Status code: {response.status_code}") # function to be called whenever a message is received on a subscribed topic def on_message(client, userdata, message): j = json.loads(message.payload.decode('utf-8')) for link in j['links']: if link['rel'] == 'canonical' or link['rel'] == 'update': download_data(link['href']) # function to be called when connection is made def on_connect(client, userdata, flags, reason_code, properties=None): if reason_code != 0: print(f"Connection failed. reason_code={reason_code}") # function to be called when connection to the MQTT broker is stopped def on_disconnect(client, userdata, reason_code, properties=None): print(f"Disconnected. reason_code={reason_code}") if reason_code != 0: print("Unexpected disconnect. Check TLS/WebSocket path/credentials/network.") sys.exit(1) # generate a unique client ID for the MQTT client client_id = f"python-mqtt-{str(uuid.uuid4().int)[-5:]}" client = mqtt_client.Client(client_id, transport='websockets') # In WIS2 the default READONLY broker-credentials are "everyone" for both username and password client.username_pw_set("everyone", "everyone") # set TLS encrypted (secure connection, e.g. HTTPS instead of HTTP)) client.tls_set() client.ws_set_options(path=WEBSOCKETS_PATH) # add the on_message callback to the MQTT client, this will be called whenever a message is received on a subscribed topic client.on_message = on_message # add the on_connect and on_disconnect callbacks to the MQTT client client.on_connect = on_connect client.on_disconnect = on_disconnect # use port 443, which is the default port for HTTPS print(f"Connecting client_id={client_id} to MQTT broker at {BROKER_HOST} on port 443 with TLS encryption") client.connect(BROKER_HOST, port=443, keepalive=60) # subscribe to the topics defined in the "topics_to_subscribe" list print(f"Subscribing to topics: {topics_to_subscribe}") for topic in topics_to_subscribe: client.subscribe(topic) # "loop_forever" starts the MQTT client loop to listen for messages # this will get the python script running while waiting for messages to arrive # if you stop the script, it will stop listening for messages and you won't get any data downloaded # you can stop the script by pressing Ctrl+C in the terminal print("MQTT client loop started, waiting for messages... (press Ctrl+C to stop)") client.loop_forever()