I have been trying to the Pahos Python client to send a downlink message to a device I have built. I have successfully done this through the Chirpstack enqueue interface as well as the node-red application. However, I have found that all the examples I have found to do this in python do not work for me and actually have competing syntax:
They almost always use Port:1883 The node-red application uses 8080.
Node-red uses an API key and none of the examples show how to use this information.
They all vary with using quotation marks, topic structure ( /command/down vs /Device ID/down), if the message has to be json encoded, etc.
I have read all the mosquito documentation,2 books on the pathos client and mosquito, Chirpstack, TTN, Semtech chats, and somehow I have missed how to do this seemly easy thing to do. The documentation there but no real examples…particularly on API Key from what I found.
I can successfully subscribe to all the device events and I can see that the node-red message gets sent successfully, but I can not see the actual message it is sending which I understand is not python.
Does anyone have a small python stub on how to do this that they can point me to?
Then this is the python script that I made to work on my previous test.
import os
import sys
import grpc
from chirpstack_api.as_pb.external import api
# Configuration.
# This must point to the API interface.
server = "192.168.1.20:8080"
# The DevEUI for which you want to enqueue the downlink.
dev_eui = bytes([0x1D, 0xFB, 0x41, 0xD3, 0x79, 0x77, 0x2C, 0x52])
# The API token (retrieved using the web-interface).
api_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcGlfa2V5X2lkIjoiYzA3ZTBjYzktM2E1OC00NTVlLWFhYjItNGYxNjdiOTgwN2VkIiwiYXVkIjoiYXMiLCJpc3MiOiJhcyIsIm5iZiI6MTYzMDg5NDk1Nywic3ViIjoiYXBpX2tleSJ9.-aKVGILqrPmKLA__RApxxXUre3mt8kY2slzCG899jBI"
if __name__ == "__main__":
# Connect without using TLS.
channel = grpc.insecure_channel(server)
# Device-queue API client.
client = api.DeviceQueueServiceStub(channel)
# Define the API key meta-data.
auth_token = [("authorization", "Bearer %s" % api_token)]
# Construct request.
req = api.EnqueueDeviceQueueItemRequest()
req.device_queue_item.confirmed = False
req.device_queue_item.data = bytes([0x01, 0x02, 0x03])
req.device_queue_item.dev_eui = dev_eui.hex()
req.device_queue_item.f_port = 10
resp = client.Enqueue(req, metadata=auth_token)
# Print the downlink frame-counter value.
print(resp.f_cnt)
Excellent response! Thank you so much… I have not seen any of this code before using the grpc library. You addressed all my issues… really thank so much.
I will give it go and hopefully solve this problem.