This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
OpenPlotter data to Python
#11
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
Reply
#12
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


Reply
#13
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
Reply
#14
Maybe your second script will work too but you forgot the SK port in the url
Reply
#15
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
Reply
#16
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
Reply
#17
(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'] )
Reply
#18
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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)