Signal KSK
Signal K2y ago
10 replies
Johnathan B

Johnathan B - HiCould someone point me in the ...

Hi
Could someone point me in the right direction please as I am now going around in circles.
I'm trying to write some very basic Python code to post a value to a path. I ultimately want to port it to CircuitPython. I have code to get an auth token and post a path value successfully via steaming websockets running in Python, but there are some issue porting this to circuit python. I have therefore been trying to write code in Python (on a PC) using a straightforward PUT , but whatever I try and can not get it to work.
I have separate code to successfully make and access request and return the device token (manually hard coded int his example below) .
In this simple test example below I am returning code 401 "You do not have permission to view this resource, <a href='/admin/#/login'>Please Login</a>"
Having spent most of the day on this trying many approaches I am clearly getting something fundamentally wrong. I would be grateful of any pointers - thank you.


( import requests server_url = "http://192.168.2.133:3000" # Token hard coded from previously authorized access request api_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkZXZpY2UiOiJ5b3VyX2RldmljZV9pZCIsImlhdCI6MTcxMTQ2Nzg1N30.X2wxz-qzux0RWQyFq8zQ8v1La9jGBf9r1iL0xgjtliE" path = "electrical/batteries/999/capacity/timeRemaining" value = 120 # 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 data = {"value": value} 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}") )
Was this page helpful?