Integrating Chirpstack Network server with python

Dear Community members,

I have a following task. I should communicate between Chirpstack network server and Python. I have found that Python has a Paho library which can help me out here, but I am still not quite sure about the configuration I need here. My plan is to implement some kind of algorithm in Python for calculating references and then send those references (among some other things) as downlink messages to my end devices.
What is confusing me is the question do I also need to install Node red in order to integrate chirpstack with python or will the two without node red suffice? Any guidelines on this problem will be greatly appreciated.

I am using X-Logic XWG1 LoraWAN Gateway and STM32 Nucleo WL55JC1 as an end node. So far, I have successfully registered both of them on the network server and tested communication with sending some uplinks and downlinks.

Best regards,

fainlaip

There is no need.

You can use a MQTT client in Python to subscribe to events and enqueue downlinks. Or you could create a HTTP endpoint in Python and use the gRPC API for enqueuing downlinks.

1 Like

Dear @brocaar,

thank You for replying in such a short notice. What do You think is the better way to approach this problem? What do You recommend? Also, do You know where can I find some examples of this or?

Best regards,
fainlaip

Also, I am running Chirpstack v4 docker image on my windows 10 pro machine.

The best approach would be the one that fits your architecture best :slight_smile: Using the gRPC for downlink enqueue has the benefit that you get an immediate response back (e.g. OK response or error). With MQTT enqueue, there is no response.

With regards to handling HTTP events in Python, see:

With regards to use gRPC with Python, see:

1 Like

can you please share some links to read JSON data from application Event data object in python

Dear @brocaar,

after I have tried to enqueue downlink according to the links You have provided me, I ran into some kind of issues and was not able to find anything on the internet regarding that.

Traceback (most recent call last):
File “c:\Users\RENS\Desktop\Python\script.py”, line 35, in
resp = client.Enqueue(req, metadata=auth_token)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\RENS\AppData\Local\Programs\Python\Python312\Lib\site-packages\grpc_channel.py”, line 1181, in call
return _end_unary_response_blocking(state, call, False, None)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\RENS\AppData\Local\Programs\Python\Python312\Lib\site-packages\grpc_channel.py”, line 1006, in _end_unary_response_blocking
raise _InactiveRpcError(state) # pytype: disable=not-instantiable
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
status = StatusCode.UNAUTHENTICATED
details = “”
debug_error_string = “UNKNOWN:Error received from peer {created_time:“2024-05-13T11:23:53.0060217+00:00”, grpc_status:16, grpc_message:”“}”

Best regards,
fainlaip

From the line:

status = StatusCode.UNAUTHENTICATED

It looks like your API calls aren’t properly authenticating with the Chirpstack server. Are you sure your API token is correct? If thats not the problem can you share the code you used?

Dear @Liam_Philipp,

I have tried to generate new API key from my chirpstack server several times, but every time I get the same error. Do You think that the error might be coming from wrong Dev_EUI? Right now, I do not have any devices connected because I am waiting for the new equipment.

Best regards,
fainlaip

Dear @brocaar,

another question. I want to access API token from outer application (Python) and I am using this code, but unfortunately I keep getting the same error.

Code:

import requests

ChirpStack API endpoint

api_url = “http://localhost:8080/api”

Your ChirpStack username and password

username = “admin”
password = “admin”

Make the POST request to the login endpoint

login_url = f"{api_url}/internal/login"
payload = {
“username”: username,
“password”: password
}
headers = {“Content-Type”: “application/json”}

try:
response = requests.post(login_url, json=payload, headers=headers)
response.raise_for_status() # Raise an exception for non-2xx status codes
jwt_token = response.json()[“jwt”]
print(f"API Token: {jwt_token}“)
except requests.exceptions.RequestException as e:
print(f"Error obtaining API token: {e}”)
jwt_token = None

Error:

Error obtaining API token: 400 Client Error: Bad Request for url: http://localhost:8080/api/internal/login

Best regards,
fainlaip

It doesnt look right.
You should not access /api/internal…

Dear @IoTThinks,

thank You very much for Your reply. What do I need to access instead?

Best regards,
fainlaip

Regardless if you are using gRPC or the REST API, you should create an API token in the ChirpStack web-interface.

Dear @brocaar,

yes, that makes sense, but what I am trying to achieve with this code I pasted yesterday, is to create a python script for obtaining API token automatically, but unfortunately do not have any luck with that.

best regards,

fainlaip

That is not possible. You create a token once through the web-interface (or CLI, see chirpstack --help) and then use it in your scripts.

1 Like