Create device using gRPC

Hello,

I have a problem with chirpstack v4: I am trying to create a device using gRPC to an existing application that I created manually and it has an ID “application id: 47024c85-9559-46a0-ad0d-dbf1b16bde45”.

My python file is :

import os
import sys

import grpc
from chirpstack_api.as_pb.external import api

# Configuration.

# This must point to the API interface.
server = "localhost:8081"

# The API token (retrieved using the web-interface).
api_token = "...."

if __name__ == "__main__":
  # Connect without using TLS.
  channel = grpc.insecure_channel(server)

  client = api.DeviceServiceStub(channel)

  # Define the API key meta-data.
  auth_token = [("authorization", "Bearer %s" % api_token)]

  # Construct request.
  try:
  	req = api.CreateDeviceRequest()
  	req.device.dev_eui = 'f4fbcdb6545e5e3b'
  	req.device.name = 'device2'
  	req.device.description = 'A new device via grpc'
  	req.device.application_id = int('525548505299565345575353574552549748459710048100451009810249984954981001015253')
  	req.device.device_profile_id = 'Prof A'
  	req.device.skip_f_cnt_check = False
  	req.device.is_disabled = True
  	#req.device.variables = 'ras'
  	#req.device.variables.value = 'ras'
  	#req.device.tags = 'ras'
  	#req.device.tags.value = 'ras'
  	resp = client.Create(req, metadata=auth_token)
  except Exception as e:
  	print('<><><> Exception <><><> ' + str(e))

I have an error with the variable “req.device.application_id” that must be an integer.

Thank you in advance.

App id in v4 should be uuid instead?

Yes but I didn’t understand how I can get the UUID of an application that I created manually.

The UUID of any device, application, device profile, etc. are created automatically by ChirpStack. They will be included in the response when you create them, but you can also pull them via the API (list endpoint) or the web UI.

  1. So the UUID of this application is “4e07ce27-1d3e-4f51-8eb4-862e08dddfd5” ?

  1. If yes, when I’m trying to use it in my code :
import os
import sys

import grpc
from chirpstack_api.as_pb.external import api

# Configuration.

# This must point to the API interface.
server = "localhost:8081"

# The API token (retrieved using the web-interface).
api_token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJjaGlycHN0YWNrIiwiaXNzIjoiY2hpcnBzdGFjayIsInN1YiI6IjY1OTViODVjLWQ1ZTYtNGZhMi04YzQ2LWY0MWQ1Y2E2NjZhZSIsInR5cCI6ImtleSJ9.fyP9Pxm0zSEdjmRwn4x1_lnB97Bq2wcvibSEQLGQ__M"

if __name__ == "__main__":
  # Connect without using TLS.
  channel = grpc.insecure_channel(server)

  # Device-queue API client.
  client = api.DeviceServiceStub(channel)

  # Define the API key meta-data.
  auth_token = [("authorization", "Bearer %s" % api_token)]

  # Construct request.
  try:
  	req = api.CreateDeviceRequest()
  	req.device.dev_eui = 'f4fbcdb6545e5e3b'
  	req.device.name = 'device2'
  	req.device.description = 'A new device via grpc'
  	req.device.application_id = "4e07ce27-1d3e-4f51-8eb4-862e08dddfd5"
  	req.device.device_profile_id = 'Prof A'
  	req.device.skip_f_cnt_check = False
  	req.device.is_disabled = True
  	resp = client.Create(req, metadata=auth_token)
  except Exception as e:
  	print('<><><> Exception <><><> ' + str(e))

  
  # Print the downlink frame-counter value.
    #print(resp.device)
  print('------- End OF TEST')
  1. I’m getting this error :

Cannot set api.Device.application_id to ‘4e07ce27-1d3e-4f51-8eb4-862e08dddfd5’: ‘4e07ce27-1d3e-4f51-8eb4-862e08dddfd5’ has type <type ‘str’>, but expected one of: (<type ‘int’>, <type ‘long’>)

Your Chirpstack instance is v4, but the Python API you’re trying to use is v3 (which previously used integer application IDs). Take a look here:

https://www.chirpstack.io/docs/chirpstack/api/python-examples.html

I used the same example as https://www.chirpstack.io/docs/chirpstack/api/python-examples.html with a function for device creation.

Do you have an example for the function that I am looking for (create a device using Python API v4) ?