Help Posting Values to SK

I think I must have misunderstood something major so I hope everyone can correct me. I am trying to update a value in SK. Here is a test value json string {"updates": [{"values": [{"path": "electrical.batteries.123.capacity.timeRemaining", "value": 3455}]}]} Dropped into the data fiddler it updates perfectly. I started writing python code to PUT this json via /signalk/v1/api/ having first successfully obtained a token via /signalk/v1/auth/login and added it to the header. I've tried this in Python / Circuit Python / MicroPython and the SK server responds with "SyntaxError: Unexpected token " in JSON at position 0" I've tried using Websockets in Python (on a PC) using the same Json and it posts fine to SK, but this is a bit tricky to port to Micropython. I've tried cutting out Python and using Postman and PUT and it returns "input is missing a value" (see picture) I've then tried posting via UDP port 8375 but I don't see the data appear in SK. Please can you point me in the right direction here? 1. Can I post data to SK via /signalk/v1/api/ or is this just for reading data ? 2. Is my Json example correct in the context of externally posting? 3. I assume I always need to login in and get a token to post, is this correct and can I add the token to the json string if I was trying to send via UDP. ? Thanks in advance for any help!
No description
9 Replies
AdrianP
AdrianPā€¢3mo ago
An HTTP PUT requires a plugin to handle the path, by default the server does not accept these requests. If you have security enabled then you will need to include credentials with the request. A simple way to test PUT requests is to enable Alarms in Freeboard-SK plugin config and then you can PUT /signalk/v2/api/notifications/mob which should raise a man over board alarm.
snipeytje
snipeytjeā€¢3mo ago
but to get data into the server it's easier to send them via websocket
PaddyB
PaddyBā€¢3mo ago
not sure it this helps but I use this to create sigk data in micropython, then send it UDP> def createSigKdata(self, path, value): try: _sigKdata = {"updates": [{"values":[]}]} _sigKdata["updates"][0]["values"].append( {"path":path,"value": value}) MESSAGE = (ujson.dumps(_sigKdata)) return MESSAGE except Exception as e: print("Create signalk error occurred = ",e)
Johnathan B
Johnathan Bā€¢3mo ago
thank you everyone Thanks Paddy. When sending over UDP how are you authenticating with SK?
Teppo Kurki
Teppo Kurkiā€¢3mo ago
There is no auth for UDP, but you need to configure the connection on the server, it is not there out of the box
Johnathan B
Johnathan Bā€¢3mo ago
Thanks. Is that done here?
No description
PaddyB
PaddyBā€¢3mo ago
I send from an ESP32, it connects to openplotter network then this sends the data to a connection set up in the server as you posted above > def insertIntoSigKdata(self, message): try: UDP_IP = "10.42.0.1" UDP_PORT = 10120 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(message, (UDP_IP, UDP_PORT)) sock.close() except Exception as e: print("Send signalk error = ",e) The ESP connection wifi function is a mess & won't reconnect if the Pi is down for a while so won't post that šŸ˜ Been running fine for must be years now though constantly.
Johnathan B
Johnathan Bā€¢3mo ago
thank you
Teppo Kurki
Teppo Kurkiā€¢3mo ago
Port is missing