OpenMarine

Full Version: OpenPlotter data to Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I subscribe tkurki advice
OpenPlotter is using websockets and json to get and send data to SK server. We will replace old UDP connections by webcoskets too.
pypilot is following this course too.
I recommend you this active and well maintained module: https://github.com/websocket-client/websocket-client
Sorry for such basic questions but obviously my knowledge of both SignalK and Python is limited Sad.
 I have managed to connect to SignalK with the following:

import websocket
ws = websocket.WebSocket()
ws.connect("ws://localhost:3000/signalk/v1/stream")
print(ws.recv())
ws.close()

This returns:
{"name":"signalk-server","version":"1.37.6","self":"vessels.urn:mrnConfusedignalk:uuid:1a221f54-3677-4260-ab71-cbd99552f5a4","roles":["master","main"],"timestamp":"2021-02-10T18:33:13.731Z"}

My next problem - What is the correct url to get all the data streaming?

When readings the docs on signalK it says 
  • ws://hostname/signalk/«version»/stream?subscribe=all
However when I try

import websocket

ws = websocket.WebSocket()
ws.connect("ws://localhost/signalk/v1/stream?subscribe=all")
print(ws.recv())
ws.close()

I get errors as below:
raceback (most recent call last):

  File "/home/pi/Python/Test2.py", line 4, in <module>
    ws.connect("ws://localhost/signalk/v1/stream?subscribe=all")
  File "/usr/lib/python3/dist-packages/websocket/_core.py", line 219, in connect
    options.pop('socket', None))
  File "/usr/lib/python3/dist-packages/websocket/_http.py", line 120, in connect
    sock = _open_socket(addrinfo_list, options.sockopt, options.timeout)
  File "/usr/lib/python3/dist-packages/websocket/_http.py", line 190, in _open_socket
    raise err
  File "/usr/lib/python3/dist-packages/websocket/_http.py", line 170, in _open_socket
    sock.connect(address)
ConnectionRefusedError: [Errno 111] Connection refused

I seem to have an error in      ws.connect("ws://localhost/signalk/v1/stream?subscribe=all")

What is the correct format?

Thanks, Jodel


From SK docs: "By default a Signal K server will provide a new WebSocket client with a delta stream of the vessels.self record, as updates are received from sources"
so, your first script is ok but you do not get more data because you are closing the client. Try this "long-lived" connection: https://github.com/websocket-client/webs...connection
Maybe your second script will work too but you forgot the SK port in the url
Brilliant. With hindsight, I suppose if I close a socket it is hard to expect more data from it!
 I corrected the last two lines to:

while True:
    print(ws.recv())

That gave me a stream of all the data.

You were also correct in relation to the second url - when I added the port and stopped closing the socket I got the same data stream.

I really appreciate the help.
Thanks,
Jodel
Hi Folks I found this thread because I am trying to input the GPS lat long position from signalK into my Python Program. Can anyone give me some pointers as my Python is not that great and I am a bit stuck. Thanks
(2023-12-18, 07:58 PM)Hillzzz Wrote: [ -> ]Hi Folks I found this thread because I am trying to input the GPS lat long position from signalK into my Python Program. Can anyone give me some pointers as my Python is not that great and I am a bit stuck. Thanks

Try this _
>>> import requests
>>> url = "http://10.10.10.1:3000/signalk/v1/api/vessels/urn:mrn:imo:mmsi:***********/navigation/position"
(put your mmsi here > *********)
>>> response = requests.get(url)
>>> print(response.text)
>>> import json
>>> res = json.loads(response.text)
>>> print("Lat = ", res['value']['latitude'], "Long = ", res['value']['longitude'] )
Or use SignalK API like: curl 'http://localhost:3000/signalk/v1/api/' and next grep/format JSON info?
I did like that in separated thread to send some data to OLED screen
Pages: 1 2