Python SDK adding devices


(Hayesey) #1

So I’m experimenting with the python sdk:

https://www.thethingsnetwork.org/docs/applications/python/api-reference.html

I have MQTTClient working OK, ApplicationClient, I’ve got as far as querying an existing device which is working fine.

I am struggling with creating/registering a new device though. Using the register_device function:

https://www.thethingsnetwork.org/docs/applications/python/api-reference.html#register_device

It tells me to supply a dictionary containing device information as per this format:

https://www.thethingsnetwork.org/docs/applications/python/api-reference.html#deviceobject

I’m unclear on which attributes are required and which are optional though, I’ve tried a few combinations such as this:

this_device = {                                                                                                                     
        "app_id": self.app_id,                                                                                                          
        "dev_id": device_id,                                                                                                            
        "lorawan_device": {                                                                                                             
            "app_eui": self.app_eui.encode('utf-8'),                                                                                    
            "dev_eui": dev_eui.encode('utf-8'),                                                                                         
            "app_key": self.app_key.encode('utf-8'),                                                                                    
            "disable_f_cnt_check": True,                                                                                                
        } 

Tried the EUIs as plain strings, encoding them to bytestrings etc…

I just get:

'Error when updating the', ' device: INVALID_ARGUMENT'

Back through the API.

Reading through the SDK source on github, I found an example usage of register_device which passes this dictionary:

devicetest = {
"description": "Description",
"appEui": "0011223344556677",
"devEui": "9988776655443322",
"devAddr": "11223344",
"nwkSKey": binascii.b2a_hex(os.urandom(16)).upper(),
"appSKey": binascii.b2a_hex(os.urandom(16)).upper(),
"appKey": binascii.b2a_hex(os.urandom(16)).upper(),
"fCntUp": 10,
"fCntDown": 11,
"latitude": 100,
"longitude": 200,
"altitude": 300,
"attributes": {
    "foo": "bar",
},
"disableFCntCheck": True,
"uses32BitFCnt": True,
}

Which seems very different to the API documentation but also doesn’t work for me (same error message back from the API).

Does anyone have a working example of the format of the device dictionary to use?


(Jmh) #2

Hello,
i am searching how automatically register devices in TTN, did you progress in your own research ?
Many thanks in advance for your update.


(Hayesey) #3

Hi,

I gave up with it originally but you asking the question has made me try it again and I’ve got it working.

I’ve used this format:

my_device = {
            "appEui": self.app_eui,
            "devEui": dev_eui,
            "appKey": self.app_key,
            "disableFCntCheck": True,
            "uses32BitFCnt": True,
        }
application_client.register_device(device_id, my_device)

And it has worked, it actually seemed like the reason I couldn’t get it to work last time was mostly down to me having uppercase characters in the device_id which I’d forgotten weren’t allowed! So a combination of a silly mistake by myself and not great documentation with the Python SDK. I see someone has created an issue saying similar too: