Transfer data with MQTT and python

Hello,
I have a tracker device that I have migrated to V3. Data are correctly arriving from my device into the TTN network as I can see them into the live data.
I wish to transfer the data into a data base located in a linux device and for this, in V2 I was using MQTT and a pyhton script which is shown down here. This code was working perfect with V2
In V3, I have created a new MQTT integration and change the APPEUI, APPID and PSW in the python code.
I can see that my code is connecting to the server but the data are not arriving.
What is wrong ?

# https://www.thethingsnetwork.org/forum/t/a-python-program-to-listen-to-your-devices-with-mqtt/9036/6
# Get data from MQTT server
# Run this with python 3, install paho.mqtt prior to use

import paho.mqtt.client as mqtt
import json
import base64
import haversine
from datetime import datetime as dt
#from haversine import haversine
from geojson import Feature, Point, dump

longitudep = 0
latitudep = 0
timep = dt.now()

APPEUI = "70b3d57ed0049e1c"
APPID  = "dynamitrontracker@ttn"
#PSW    = 'NNSXS.NLD6E3TOZYKFK6VU2XG7YJVCM3TCQB4BQFYJQEI.WSQX2B557JYQMRZ2BGHPWU7RDK3XVCUCKXZBSPXPBLGVZHXYQODA'
PSW    = 'NNSXS.NLD6E3TOZYKFK6VU2XG7YJVCM3CQB4BQFYJQEI.WSQX2B557JYQMRZ2BGHPWU7RDK3XVCUCKXZBSPXPBLGVZHXYQODA'

#Call back functions

# gives connection message
def on_connect(mqttc, mosq, obj,rc):
    print("Connected with result code:"+str(rc))
    # subscribe for all devices of user
    mqttc.subscribe('+/devices/+/up')

# gives message from device
def on_message(mqttc,obj,msg):
    try:
        #print(msg.payload)
        x = json.loads(msg.payload.decode('utf-8'))
        device = x["dev_id"]
        counter = x["counter"]
        payload_raw = x["payload_raw"]
        payload_fields = x["payload_fields"]
        datetime = x["metadata"]["time"]
        gateways = x["metadata"]["gateways"]
        SF = x["metadata"]["data_rate"]
        y =  json.dumps(payload_fields)
        y_dict = json.loads(y)
        latitude = y_dict["latitude"]
        longitude = y_dict["longitude"]
        dist = round((haversine((5.626525,4.44700),(latitude,longitude,))),3)  
        
        global longitudep
        global latitudep
        global timep       
             

        distp = round((haversine((latitudep,longitudep),(latitude,longitude))),3)
        time_delta = dt.now()-timep
        speed = distp /((time_delta.total_seconds())/3600)
        #print (longitudep)
        #print (latitudep)
        
        longitudep = longitude
        latitudep = latitude
        timep = dt.now()
        
        #print (longitudep)
        #print (latitudep)
        #print (distp)
        #print (speed)
        print (counter)
        #print ("\n\r")
      
        for gw in gateways:
            gateway_id = gw["gtw_id"]
            rssi = gw["rssi"]
            snr =  gw["snr"]
            
        #print(datetime + ", " + device + ", " + str(counter) + ", "+ gateway_id + ", "+ str(rssi) + ", " + str(snr) + ", "+ str(payload_fields))
        #print(datetime + ", " + gateway_id + ", "+ str(rssi) + ", " + str(snr) + ", "+ str(SF)+ ", "+ str(round(latitude,6)) + ", "+ str(round(longitude,6)) + ", " + str(dist) + ", " +str(round(speed,1)))
        my_point = Point((longitude,latitude))
        my_feature = json.dumps(Feature(geometry=my_point, properties= {"datetime": str(datetime),"gateway_id": str (gateway_id),"rssi": str (rssi),"snr": str (snr),"SF": str (SF),"dist": str (dist),"speed":str (round(speed,1))}))
        #print (my_feature)
        if ((latitude!=0) and (longitude!=0) and (distp>0.015)):
            with open("track.geojson","a") as outfile:
                outfile.write (my_feature + '\n')
            outfile.close()
    except Exception as e:
        print(e)
        pass

def on_publish(mosq, obj, mid):
    print("mid: " + str(mid))

def on_subscribe(mosq, obj, mid, granted_qos):
    print("Subscribed: " + str(mid) + " " + str(granted_qos))

def on_log(mqttc,obj,level,buf):
    print("message:" + str(buf))
    print("userdata:" + str(obj))

mqttc= mqtt.Client()
# Assign event callbacks
mqttc.on_connect=on_connect
mqttc.on_message=on_message

mqttc.username_pw_set(APPID, PSW)
mqttc.connect("eu1.cloud.thethings.network",1883,60)

# and listen to server
run = True
while run:
    mqttc.loop()

The message structure changed a bit in V3. I think u can find it here:
https://www.thethingsindustries.com/docs/reference/data-formats/

Also the subscribe string should be different:

It should be mqttc.subscribe(‘v3/+/devices/+/up’)

Try this as a starter / reference:

Thank you, this is the solution. :ok_hand: :+1:
You are right, I had to rewrite the code to cope with the new V3 data format.

Nice !!!
My tracker is back in line after migration to V3

image

You are a few km/h off time travel :innocent:
image

I kown, still a bug to solve… :joy:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.