Problem retrieving data using TTS.MQTT.Tab.py

Hi All

I have successfully migrated my nodes and gateway to the V3 stack and i can see data going in both directions in the console so no problems there
I wanted to try and make some use of the data using the MQTT service and here is where i am a bit stuck
I am using a python program TTS>MQTT.Tab.py to log int the MQTT service and retrieve the data . The program runs and i get the following

Python 3.7.3 (/usr/bin/python3)
%Run TTS.MQTT.Tab.py
TTS.MQTT.Tab.py 2021-05-24 v1.2
Imports:
Functions:
Body of program:
Init mqtt client
Assign callbacks
Connect
Subscribe
And run forever

Connect: rc = 0
.
Subscribe: 1 (0,)

Then as soon as i start the node application and ask it to send some data i get the following errors

Message: v3/my-iotlora-application-julianb-1@ttn/devices/eui-323833357plus some more numbers/join 0
Traceback (most recent call last):
  File "/home/pi/Desktop/TTS.MQTT.Tab.py", line 132, in <module>
    mqttc.loop(10) 	# seconds timeout / blocking time
  File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 1120, in loop
    return self._loop(timeout)
  File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 1164, in _loop
    rc = self.loop_read()
  File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 1556, in loop_read
    rc = self._packet_read()
  File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 2439, in _packet_read
    rc = self._packet_handle()
  File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 3033, in _packet_handle
    return self._handle_publish()
  File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 3327, in _handle_publish
    self._handle_on_message(message)
  File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 3570, in _handle_on_message
    on_message(self, self._userdata, message)
  File "/home/pi/Desktop/TTS.MQTT.Tab.py", line 85, in on_message
    saveToFile(parsedJSON)
  File "/home/pi/Desktop/TTS.MQTT.Tab.py", line 30, in saveToFile
    uplink_message = someJSON["uplink_message"];
KeyError: 'uplink_message'
KeyboardInterrupt

Any ideas what im doing wrong

Um, nothing, nothing at all.

The author keeps forgetting to put a note in about it only processing uplink messages as written - subscribing to the join message is somewhat of a once in a devices lifetime event and the author wanted to keep the starter code as simple as possible.

I shall have the author beaten. In the meanwhile, if you want to capture the join message, you’ll have to alter the code as it’s looking for the uplink_message which isn’t present in a join message.

Hi Nick

Thanks for the message

I’m pretty green with python code so i have found this quite difficult but can usually fumble my way through however trying to find some python code that will work with the MQTT service has been a real challenge

Any suggestions on code that i can use to both send and receive via the MQTT service?

Thanks again

That code works just fine with uplinks - you just need to subscribe to only uplinks rather than the anything and everything as it does on line 126

It’s deliberately kept as simple / vanilla as possible to be built on - so to answer your question it would be useful to know what you are trying to build - and why you went with the join message which, as outlined above, mostly only occurs once or twice in a device lifetime.

Maybe im a bit confused

Let me prove my ignorance!!

I have a very simple program on a node that sends out a message every 30 seconds
It simply sends a message that i can see in console as a bunch of hex numbers

As i understand it the MQTT service takes the hex string and sends it out to anyone who has subscribed to that particular node or application as i thinks its called

All i am trying to do is to subscribe to the application to receive the hex strings being sent from the node

At this stage i’m not trying to send any data back to the node but it would be great if that was doable too

Thanks again for reading and thanks for bearing with me

That is too often for the TTN fair access policy, you are allowed 30 seconds of airtime for a node each day in the public network.

Apart from the breach of the FUP, the code works absolutely fine out the box for an already running node - you appear to have altered it to subscribe to the JOIN message which it can’t handle. If you alter the subscribe to the UP message you’ll be fine.

Or alter the code to cope with any lack of uplink_message in the JSON.

Update on this - it’s actually functional, not just some random demo, so it’s not really as completely simple as possible. I’ll get the author to change the subscribe wild card whilst you change your uplink rate.

Hi All

thanks for the info The node code is just on a for loop so only repeats 10 times just to enable me to debug but thanks for reminding me anyway , a very valid point

I think im begging to understand what you mean regarding the" join message" . I just need to understand how to define the “Topic” or" up messages" and what that actually is

Thanks again , the help really is appreciated.

I only broke the speed limit until you stopped me officer …

Try putting it on a push button

The comprehensive documentation for the MQTT integration should enlighten. You did get as far as changing the code to subscribe to join requests, so you must have arrived at something somewhere to make that change.

Hi All

to try and understand the structure of the message i downloaded mosquitto application in windows and let it subscribe to the MQTT session

C:\Program Files\mosquitto>mosquitto_sub -h eu1.cloud.thethings.network -t “#” -u “my-tlora-application-@ttn” -P “NNSXS.RKX2ZKZFFxxxLOGNAIVXBTUIVHWxxxxxxxxxWYY.R3xxxxxxxxxxxxxxxxxZXXXXXXXXXXXXXXXX” -d

It happily does this and from that i was able to determine the structure of the message it needs
example is below
v3/my-iotlora-application-1@ttn/devices/eui-32xx333573xxxxx/up
To be fair this is on the TTN site if you dig a little
https://www.thethingsindustries.com/docs/integrations/mqtt/

however even if i use this in the TTS.MQTT.Tab.py program i still get a bunch of errors

as shown below

TTS.MQTT.Tab.py 2021-05-24 v1.2
Imports:
Functions:
Body of program:
Init mqtt client
Assign callbacks
Connect
Subscribe
And run forever
.
Connect: rc = 0
.
Subscribe: 1 (0,)

Message: v3/my-iotlora-application-@ttn/devices/eui-3xxx33853xxxxxxx05/up 0
Traceback (most recent call last):
File “/home/pi/Desktop/TTS.MQTT.Tab.py”, line 132, in
mqttc.loop(10) # seconds timeout / blocking time
File “/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py”, line 1120, in loop
return self._loop(timeout)
File “/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py”, line 1164, in _loop
rc = self.loop_read()
File “/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py”, line 1556, in loop_read
rc = self._packet_read()
File “/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py”, line 2439, in _packet_read
rc = self._packet_handle()
File “/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py”, line 3033, in _packet_handle
return self._handle_publish()
File “/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py”, line 3327, in _handle_publish
self._handle_on_message(message)
File “/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py”, line 3570, in _handle_on_message
on_message(self, self._userdata, message)
File “/home/pi/Desktop/TTS.MQTT.Tab.py”, line 85, in on_message
saveToFile(parsedJSON)
File “/home/pi/Desktop/TTS.MQTT.Tab.py”, line 36, in saveToFile
data_rate_index = uplink_message[“settings”][“data_rate_index”];
KeyError: ‘data_rate_index’

Any ideas where to go from here ?

It’s saying there isn’t an element in the JSON of data_rate_index. Hopefully you can start to see the info in the error messages as both of them are the same sort of problem.

I’m not sure what’s changed since this was last used by me for the TTN Summer Academy, I’ll have a look at the missing data_rata_index which would appear to breach the “don’t break the API” promise from TTI. However the whole philosophy of the repro is that it’s a starter code base for people to extend as they require, so there is an expectation of the ability to read & alter procedural/imperative code plus deal with data formats like JSON.

You could just comment out the offending line in the meanwhile, you’ll probably need to set the variable to some null value.

And you can please use the toolbar in the text entry to format your posts as per guidelines - you’ll have seen I’ve already done it for your first post.

Hi All

Finally after a bit of learning some basic python, i now have this working, I’m able to get messages from the node , via the MQTT service and rip them out of the received json file and thats all working a peach . So anyone trying to do the same with the TTS.MQTT program ;persist as it does work as previously stated

Does anyone know however if its possible to send data back to the node with the TTS.MQTT program as i can’t see an obvious way to do this

Thanks again in advance for all the help

Well yes, and er, no, because I haven’t included that code in the repro. The “TTS.MQTT program” is not a program. It is a code snippet to get people started & build upon, not a complete solution in its own right.

As you don’t appear to have done any investigation or research in to how to do this you should read the documentation you linked to above to figure out how to do downlinks via MQTT and then look at the Python Paho MQTT documentation to see what the appropriate function call would be, sort of like the opposite of subscribe.

The principal reason it’s not in the starter repro is so we (I) don’t have to contend with anyone hammering downlinks through in breach of the FUP which is 10 per day - the gateway can’t hear any other devices whilst transmitting, so we think of downlinks as a once a fortnight sort of situation.

If you are heading towards a real time command & control solution, please search the forum for prior responses to that so you are ready for any feedback that may arise.

slightly harsh dont you think?

How do you think the rest of us, all volunteers, learn this stuff?