TTN, Raspberry Pi and Python

I am trying to test the very simple python example to get data from a ttn applcation into a raspberry database.
It works fine and then after 5…6 data stream I get the following message on my ptyhon console :

  Traceback (most recent call last):
  File "getData.py", line 22, in <module>
    my_app = app_client.get()
  File "/home/xxx/.local/lib/python3.6/site-packages/ttn/application.py",
 line 64, in get
    " application: {}".format(err.code().name))
  RuntimeError: ('Error while getting the', ' application: PERMISSION_DENIED')

What is the problem ???

Thank you

import time
import ttn

app_id = "xxx"
access_key = "ttn-account-v2.x-xxx"

def uplink_callback(msg, client):
print("Received uplink from ", msg.dev_id)
print(msg)
handler = ttn.HandlerClient(app_id, access_key)

# using mqtt client
mqtt_client = handler.data()
mqtt_client.set_uplink_callback(uplink_callback)
mqtt_client.connect()
time.sleep(60)
mqtt_client.close()

# using application manager client
app_client = handler.application()
my_app = app_client.get()
print(my_app)
my_devices = app_client.devices()
print(my_devices)

I found the problem, the client close after 60 sec.
I shouldn’t close the client to allow the process for ever.

Also beware that the TTN Python SDK has been discontinued. See Python SDK discontinued, alternatives for application management? if you need the application management part of the SDK, or A Python program to listen to your devices with MQTT for the data (be sure to read up to the end of that topic).

I had exactly the same problem, but the error message is misleading because the code is not documented well.

If you look carefully you will see there are two examples of demo code. So, you should use this:

# using mqtt client
mqtt_client = handler.data()
mqtt_client.set_uplink_callback(uplink_callback)
mqtt_client.connect()
time.sleep(60)
mqtt_client.close()

or this

# using application manager client
app_client = handler.application()
my_app = app_client.get()
print(my_app)
my_devices = app_client.devices()
print(my_devices)

Fortunately only the second technique fails, so at least the first technique gets an opportunity to run (and it works). So, ignore the second technique (or fix it and tell everyone). You can sleep forever, or have an infinite loop, and deal with new data in the callback.

The documentation for that demo code should be improved. Everyone who tries it will see the same error, and wonder what is going on.

1 Like

yeah, it is often like that…
People publish their scripts but 50% of the time with always minimal explanation and minimal testing. When you try them it does not work.

I ended to remove this rubish second part keeping only the mqtt client.
I also removed the close instructrion as I want to collect my data for ever…

What documentation and demo code are you referring to?

https://www.thethingsnetwork.org/docs/applications/python/

As already mentioned:

The Python SDK has been discontinued and is no longer supported.
Therefore none of it’s (example) code and documentation will be further maintained, improved or corrected.

If you follow the link provided by @arjanvanb you will see the following message in the Python SDK GitHub repository’s readme:

:warning: This SDK has been discontinued and will not be further supported.

What is strange however, is that the Python SDK docs article does not make any mention that the Python SDK has been discontinued.
I will try to get this corrected and added to the docs article to prevent further confusion in the future.

Thanks.