Basic Python websockets example to retrieve current Tempest data

I’m relatively new to Python and wondered whether anyone could point me to a very basic example of retrieving current data from my Tempest using Python websockets. To get started, all I want to do is open a socket, get the current data from my Tempest in the console and close the socket. I’ve been trying to use the online documentation, but I haven’t been successful. There’s something missing in my understanding of the process and I’m hoping a simple example will get me over the the hump. Thanks for any help provided!

This is the simplest example I could come up with. It will connect to the Websocket, start listening for Tempest messages, and then disconnect once it has received a single data message:

from websocket import create_connection

personal_token = ' '
tempest_ID = ' '

print('Opening Websocket connection...')
ws = create_connection('wss://ws.weatherflow.com/swd/data?api_key=' + personal_token)
result =  ws.recv()
print("Received '%s'" % result)
print('')

print('Listening to Tempest endpoint...')
ws.send('{"type":"listen_start",'       + ' "device_id":' + tempest_ID + ',' + ' "id":"Tempest"}')
result =  ws.recv()
print("Received '%s'" % result)
print('')

print('Receiving Tempest data...')
result =  ws.recv()
print("Received '%s'" % result)
print('')

ws.close()

You need to install the Python websockets module with python3 -m pip install websocket-client (https://pypi.org/project/websocket_client/) and in the code above you need to enter a personal access token and device ID of your Tempest as strings. Hope this help

4 Likes

That’s a BIG help…thanks! I got it working just fine. Now that I see how it works, I think the online documentation will make more sense to me. I really appreciate the help!

2 Likes