Johnathan B
Johnathan B
SKSignal K
Created by Johnathan B on 5/10/2024 in #questions
Generate Notification from "True" "False"
Thank you
7 replies
SKSignal K
Created by Johnathan B on 3/26/2024 in #sensors
Johnathan B - HiCould someone point me in the ...
For anyone reading this in the future, it appears that what I am trying to do is not possible. And it is not possible to update a path's value via the rest API and it can only be done via a websockets / UDP connection.
11 replies
SKSignal K
Created by Johnathan B on 4/2/2024 in #questions
Help Posting Values to SK
thank you
11 replies
SKSignal K
Created by Johnathan B on 4/2/2024 in #questions
Help Posting Values to SK
No description
11 replies
SKSignal K
Created by Johnathan B on 4/2/2024 in #questions
Help Posting Values to SK
Thanks Paddy. When sending over UDP how are you authenticating with SK?
11 replies
SKSignal K
Created by Johnathan B on 4/2/2024 in #questions
Help Posting Values to SK
thank you everyone
11 replies
SKSignal K
Created by Johnathan B on 3/26/2024 in #sensors
Johnathan B - HiCould someone point me in the ...
I have tried this as well as sending it as a string - and I get the same result. This morning I rewrote the code in micropython and I get the same result as I do in Python and circuit Python. I really don't understand. I communicate with some of my Victron kit in exactly this way but I must be doing something wrong. In signalK is there a way of seeing the 'bad' inbound json string? I can only see log that says SyntaxError: Unexpected token " in JSON at position 0 at JSON.parse (<anonymous>) at createStrictSyntaxError I don't know where to go now. Any help from anyone would be appreciated. Below is the output of my code. With the headers, json and response printed to the screen. {'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ImFkbWluIiwiaWF0IjoxNzExOTY0NjI2LCJleHAiOjE3OTgzNjQ2MjZ9.uWqOLURCasix1-LsDRmJSpjC-bkdnlPLjpiMbFnjL90', 'Content-Type': 'application/json'} {"updates": [{"values": [{"path": "electrical.batteries.999.capacity.timeRemaining", "value": 1234}]}]} http://192.168.2.133:3000/signalk/v1/api/ Error writing to Signal K path: api/. Status code: 400 ** RESPONSE ** <html> <head> <meta charset='utf-8'> <title>SyntaxError: Unexpected token &quot; in JSON at position 0</title>
11 replies
SKSignal K
Created by Johnathan B on 3/26/2024 in #sensors
Johnathan B - HiCould someone point me in the ...
( import requests import json # Replace with your Signal K server URL and API token server_url = "http://192.168.2.133:3000" api_token = "" # Path to write to path = "api/" #----------------------Changed here value = 120 # Example value in minutes # Construct URL with path url = f"{server_url}/signalk/v1/{path}" # Function to obtain an authentication token def get_token(): login_url = "http://192.168.2.133:3000/signalk/v1/auth/login" credentials = {"username": "admin", "password": "******"} response = requests.post(login_url, json=credentials) if response.status_code == 200: return response.json().get("token") else: print("Failed to authenticate.") return None api_token=get_token() # Set headers with API token headers = { "Authorization": f"Bearer {api_token}", "Content-Type": "application/json", } # Data to send in PUT request #----------------------Changed here this json works fine int he data fiddler data = {"updates": [{"values": [{"path": "electrical.batteries.999.capacity.timeRemaining", "value": 1234}]}]} print(headers) print(json.dumps(data)) print(url) try: # Send PUT request response = requests.put(url, headers=headers, json=json.dumps(data)) # Check for successful response if response.status_code == 200: print("Value written successfully to Signal K path:", path) else: print(f"Error writing to Signal K path: {path}. Status code: {response.status_code}") print("********** RESPONSE **********") print(response.text) except requests.exceptions.RequestException as e: print(f"Error: {e}") )
11 replies
SKSignal K
Created by Johnathan B on 3/26/2024 in #sensors
Johnathan B - HiCould someone point me in the ...
Thanks for taking the time to reply Roger.
Here is is the same code but this time it includes getting a token when first run instead of using and access request. This gives a response of 400, but returns some HTML saying their is a problem with the Json. The output of my code looks like this. If I check the posted json in the datafiddler it validates post fine. {'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ImFkbWluIiwiaWF0IjoxNzExNTM4MjU0LCJleHAiOjE3OTc5MzgyNTR9.oo0FWBOFVRbzv-bA0TzEqzyG4ydAPZL5AkfJRmtF37Y', 'Content-Type': 'application/json'} {"updates": [{"values": [{"path": "electrical.batteries.999.capacity.timeRemaining", "value": 1234}]}]} http://192.168.2.133:3000/signalk/v1/api/ Error writing to Signal K path: api/. Status code: 400 html> <head> <meta charset='utf-8'> <title>SyntaxError: Unexpected token &quot; in JSON at position 0</title> ...... more HTML The stakctrace is: <ul id="stacktrace"><li> &nbsp; &nbsp;at JSON.parse (&lt;anonymous&gt;)</li><li> &nbsp; &nbsp;at createStrictSyntaxError (/usr/local/lib/node_modules/signalk-server/node_modules/body-parser/lib/types/json.js:169:10)</li><li> &nbsp; &nbsp;at parse (/usr/local/lib/node_modules/signalk-server/node_modules/body-parser/lib/types/json.js:86:15)</li><li> &nbsp; &nbsp;at /usr/local/lib/node_modules/signalk-server/node_modules/body-parser/lib/read.js:128:18</li><li> &nbsp; &nbsp;at AsyncResource.runInAsyncScope (node:async_hooks:203:9)</li><li> &nbsp; &nbsp;at invokeCallback (/usr/local/lib/node_modules/signalk-server/node_modules/raw-body/index.js:238:16)</li><li> &nbsp; &nbsp;at done (/usr/local/lib/node_modules/signalk-server/node_modules/raw-body/index.js:227:7)</li><li> &nbsp; &nbsp;at IncomingMessage.onEnd (/usr/local/lib/node_modules/signalk-server/node_modules/raw-body/index.js:287:7)</li> The odd thing is I can successfully post the same json using websockets in Python. My test code for the output above is:
11 replies
SKSignal K
Created by Johnathan B on 3/26/2024 in #sensors
Johnathan B - HiCould someone point me in the ...
Thanks for your help Roger I had tried the json previously. Here is the same code but no using the url /signalk/v1/api and some json that works fine in the data fiddler. Again I get this errror Error writing to Signal K path: api/. Status code: 401 You do not have permission to view this resource, <a href='/admin/#/login'>Please Login</a> Previously I have tried the same code but creating the token based on an u / p login - with the same issue. ( import requests # Replace with your Signal K server URL and API token server_url = "http://192.168.2.133:3000" api_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkZXZpY2UiOiJ5b3VyX2RldmljZV9pZCIsImlhdCI6MTcxMTQ2Nzg1N30.X2wxz-qzux0RWQyFq8zQ8v1La9jGBf9r1iL0xgjtliE" # Path to write to path = "api/" #----------------------Changed here # Value to write (replace with desired time remaining value) value = 120 # Example value in minutes # Construct URL with path url = f"{server_url}/signalk/v1/{path}" # Set headers with API token headers = { "Authorization": f"Bearer {api_token}", "Content-Type": "application/json", } # Data to send in PUT request #----------------------Changed here data = {"updates": [{"values": [{"path": "electrical.batteries.999.capacity.timeRemaining", "value": 1234}]}]} try: # Send PUT request response = requests.put(url, headers=headers, json=data) # Check for successful response if response.status_code == 200: print("Value written successfully to Signal K path:", path) else: print(f"Error writing to Signal K path: {path}. Status code: {response.status_code}") print(response.text) except requests.exceptions.RequestException as e: print(f"Error: {e}") )
11 replies