Gateway data retrieval over MQTT

Is it possible to receive the data as seen in the web console in a MQTT client?

The gateway data shown in the console are very useful to monitor activities over LoRa and provide information not always available in applications data. Unfortunately, the data are only transient and as the page regularly expires it is not possible to have long run data log.

I’ve tried to connect to the TTN MQTT broker, as with application, using the Gateway ID as user and the Gateway key, but the connection is refused.

Not possible.

you made me feel very unhappy now Jac. :disappointed_relieved:

I really thought I could retrieve this backend data over MQTT too
https://www.thethingsnetwork.org/docs/applications/mqtt/api.html#uplink-messages

This is Application/Device data, not gateway…

Gateway API is currently not published – strictly speaking you could reverse-engineer the console code, but I still hope the V3 stack will open that door…

tnx, yes confused… that’s exactly what I need for the handheld tool.
I have no TTN MQTT / Python experience, So it will be a challenge :sweat_smile:

Do you want to do something similar to this?

Captura_ttn

I did this with Python
@BoRRoZ @Mirmit

2 Likes

Not MQTT, and not decoded, but this will give you more insight.

sudo tcpdump -A -v port 1700 -i eth0

Too bad the TTN Gateway is not a Linux device.

exactly… :sunglasses:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#-----------------------------
#<-------- Libraries -------->
#-----------------------------
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import json
#import MySQLdb
import time
import datetime
import dateutil.parser
import base64
import requests


#-----------------------------
#<------ Configuration ------>
#-----------------------------
#TTN Configuration
TTN_appeui = ""
TTN_appid  = ""
TTN_password = ""
TTN_tls_path ='mqtt-ca.perm'

#-----------------------------
#<-------- Functions -------->
#-----------------------------

def on_connect(mqttc, mosq, obj, rc):
    print("Connected with result code:"+str(rc)+str(mqttc)+str(mosq)+str(obj))
    # subscribe to specific device in a specific app
    mqttc.subscribe('"')#app_ID/devices/+/up



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


def on_message(mqttc, obj, msg):
	
	try:
		#Formato Json
		x = json.loads(msg.payload.decode('utf-8'))
		print(x)
		data_enviar = x['payload_fields']['meteo']
		data_enviar2= json.dumps(data_enviar)
		print(data_enviar2)
		print("------------------------")
        
	except Exception as e:
		print(e)
		pass


#-----------------------------
#<---------- Main ----------->
#-----------------------------
mqttc= mqtt.Client()

# Assign event callbacks
mqttc.on_connect=on_connect
mqttc.on_message=on_message

mqttc.username_pw_set(TTN_appid, TTN_password)
mqttc.tls_set(TTN_tls_path)
mqttc.connect("us-west.thethings.network", 8883, 10)

mqttc.loop_forever()
3 Likes

tnx ! :sunglasses:

Actually mp_pkt_fwd and poly_pkt_fwd supports sending statistics via UDP packets. Just in global_conf.json add server with serv_type = gwtraf:

        "servers": [
            ...
            {
                "serv_port_up": 1688,
                "serv_type": "gwtraf",
                "serv_enabled": true,
                "server_address": "192.168.1.30",
                "serv_port_down": 1689
            }
        ],

Then the packet forwarder sends JSON via UDP to 192.168.1.30 port 1688 everytime it sends/receive any data from/to the air. For mp_pkt_fwd the JSON looks like:

{
  "type": "uplink",
  "gw": "B827xxxxxxxxxxxxx",
  "time": "2019-01-20T20:21:08Z",
  "tmst": 664801355,
  "chan": 2,
  "rfch": 1,
  "freq": 868.5,
  "stat": 1,
  "modu": "LORA",
  "datr": "SF7BW125",
  "codr": "4/5",
  "lsnr": 7,
  "rssi": -46,
  "size": 21,
  "mote": "01234567", // node ID
  "fcnt": 0
}

Note: poly_pkt_fwd use different JSON format and the first 12 bytes before JSON are binary data. So you have to strip them.

I use this to monitor in node-red how many unique devices use my GW. BTW @Charles use this way to display data on OLED screen in his GW, see oled.py source. So it isn’t hard to write python UDP server to forward required statistics to MQTT.

1 Like