# No data in message

**URL:** <https://www.thethingsnetwork.org/forum/t/no-data-in-message/40561>\
**Category:** End Devices (Nodes)\
**Created:** [October 14, 2020, 10:50am UTC](https://www.thethingsnetwork.org/forum/t/no-data-in-message/40561 "2020-10-14T10:50:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cfosvang](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/cfosvang/32/38707_2.png) [@Cfosvang](https://www.thethingsnetwork.org/forum/u/Cfosvang)\
**Post date:** [October 14, 2020, 10:50am UTC](https://www.thethingsnetwork.org/forum/t/no-data-in-message/40561/1 "2020-10-14T10:50:29Z")

</div>

The problem is that i get a payload from the device but it is not readable in my API.  
my message is ok:

The MQTT is:

```python
import binascii
import paho.mqtt.client as mqtt
import json
import pybase64
import requests

# Call back functions
APPEUI = "70B3D5E75xxxxxxxxx"
APPID = "xxxxxxxxxxxx"
PSW = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

# 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):
    global app, deveui, counter
    try:
        x = json.loads(msg.payload.decode('utf-8'))
        # metadata
        app = x["app_id"]
        device = x["dev_id"]
        deveui = x["hardware_serial"]
        port = x["port"]
        confirmed = x["confirmed"]
        counter = x["counter"]
        global payload_fields
        payload_fields = x["payload_raw"]
        datetime = x["metadata"]["time"]
        frequency = x["metadata"]["frequency"]
        modulation = x["metadata"]["modulation"]
        data_rate = x["metadata"]["data_rate"]
        air_time = x["metadata"]["airtime"]
        coding_rate = x["metadata"]["coding_rate"]
        gateways = x["metadata"]["gateways"]

        for gw in gateways:
            global rssi, snr
            gateway_id = gw["gtw_id"]
            timestamp = gw["timestamp"]
            time = gw["time"]
            channel = gw["channel"]
            rssi = gw["rssi"]
            snr = gw["snr"]

    except Exception as e:
        print(e)
        pass

    #decoding the payload_field
    def decoder():
        global byte
        payload_r = (pybase64.b64decode(payload_fields + "=")) # decoding the Payload_r to Byte-Ascii string
        payload_h = binascii.hexlify(payload_r) # Bin to b'hex
        node_data = (payload_h.decode("utf-8)")) # b'hex to pretty
        byte = payload_r
        print(byte)
    decoder()
("here comes the code cracker and after that comes" )

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.on_log = on_log

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

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

```

---

<div class="post-metadata">

**Author:** ![descartes](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/descartes/32/47220_2.png) [@descartes](https://www.thethingsnetwork.org/forum/u/descartes)\
**Post date:** [October 14, 2020, 11:19am UTC](https://www.thethingsnetwork.org/forum/t/no-data-in-message/40561/2 "2020-10-14T11:19:41Z")

</div>

It’s not clear from your opening paragraph what the problem is.

Does this receive the uplink messages OK?

If it does, which bit of the code is causing issue?

---

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [October 14, 2020, 11:34am UTC](https://www.thethingsnetwork.org/forum/t/no-data-in-message/40561/3 "2020-10-14T11:34:39Z")

</div>

I’m with @descartes here as for: what’s the problem? But maybe it’s below:

> [@Cfosvang](#):
>
> ```python
> payload_fields = x["payload_raw"]
> 
> ```

That doesn’t look right. See also [A Python program to listen to your devices with MQTT](https://www.thethingsnetwork.org/forum/t/a-python-program-to-listen-to-your-devices-with-mqtt/9036) which uses:

> [@A Python program to listen to your devices with MQTT](https://www.thethingsnetwork.org/forum/t/a-python-program-to-listen-to-your-devices-with-mqtt/9036/8):
>
> ```python
> payload_raw = x["payload_raw"]
> payload_fields = x["payload_fields"]
> 
> ```

…and:

> [@A Python program to listen to your devices with MQTT](https://www.thethingsnetwork.org/forum/t/a-python-program-to-listen-to-your-devices-with-mqtt/9036/6):
>
> ```python
> payload_raw = x["payload_raw"]
> payload_plain = base64.b64decode(payload_raw)
> 
> ```

(Where hopefully `payload_plain` is not plain text.)

See also [What to do with the raw payload via MQTT?](https://www.thethingsnetwork.org/forum/t/what-to-do-with-the-raw-payload-via-mqtt/4889)

(And please see also [How do I format my forum post? [HowTo]](https://www.thethingsnetwork.org/forum/t/how-do-i-format-my-forum-post-howto/8735))

---

<div class="post-metadata">

**Author:** ![descartes](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/descartes/32/47220_2.png) [@descartes](https://www.thethingsnetwork.org/forum/u/descartes)\
**Post date:** [October 14, 2020, 12:18pm UTC](https://www.thethingsnetwork.org/forum/t/no-data-in-message/40561/4 "2020-10-14T12:18:24Z")

</div>

Additionally, MQTT has been seen recently to time out on the JavaScript decoder on occasion so you may not get any `payload_fields`.

And Python isn’t overly helpful in what it returns when you try to access an entry that doesn’t exist:

> `#` Technique 1, use get()  
> missing1 = theJSON.get(“missing1”)  
> `#` But if it’s missing, it has a value of NoneType which can’t be readily printed  
> try:  
> print("missing1 = " + missing1)  
> except:  
> pass  
> `#` so we can add an if missing, use this value situation  
> missing1 = theJSON.get(“missing1”, “”)  
> print("missing1 = " + missing1)
> 
> `#` Technique 2, check if the field is there  
> if ‘missing2’ in theJSON:  
> missing2 = theJSON[“missing2”]  
> else:  
> missing2 = “Couldn’t find it at all”  
> print("missing2 = " + missing2)
> 
> `#` BUT that’s a bit of a pain as you may as well do  
> missing2 = theJSON.get(“missing2”, “Couldn’t find it at all”)  
> print("missing2 = " + missing2)  
> `#` unless you want to do other processing when a field is missing  
> `#` or you are heading off handling a list (see below) at the pass
> 
> `#` Technique 3, use an error handler  
> try:  
> missing1 = theJSON[“missing1”]  
> missing2 = theJSON[“missing2”]  
> except KeyError:  
> print(“Something is missing but I’m not sure what”)  
> `#` so you either have to wrap each dictionary lookup in a try / error handler  
> `#` or just not do it this way

And just to add an extra level of annoyance, # is a heading so Python comments have to be turned in to pre-formatted text.

---

<div class="post-metadata">

**Author:** ![Cfosvang](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/cfosvang/32/38707_2.png) [@Cfosvang](https://www.thethingsnetwork.org/forum/u/Cfosvang)\
**Post date:** [October 15, 2020, 12:10pm UTC](https://www.thethingsnetwork.org/forum/t/no-data-in-message/40561/5 "2020-10-15T12:10:25Z")

</div>

The API receives the uplink as it should.  
The problem is in the on\_message function . I do have other devices running on the same setup with the same json load.  
But with this setup i get nothing in this part, where i actually should get the data from TTN:

```
def on_message(mqttc, obj, msg):
    global app, deveui, counter
    try:
        x = json.loads(msg.payload.decode('utf-8'))
        # metadata
        app = x["app_id"]
        device = x["dev_id"]
        deveui = x["hardware_serial"]
        port = x["port"]
        confirmed = x["confirmed"]
        counter = x["counter"]
        global payload_fields
        payload_fields = x["payload_raw"]
        datetime = x["metadata"]["time"]
        frequency = x["metadata"]["frequency"]
        modulation = x["metadata"]["modulation"]
        data_rate = x["metadata"]["data_rate"]
        air_time = x["metadata"]["airtime"]
        coding_rate = x["metadata"]["coding_rate"]
        gateways = x["metadata"]["gateways"]

        for gw in gateways:
            global rssi, snr
            gateway_id = gw["gtw_id"]
            timestamp = gw["timestamp"]
            time = gw["time"]
            channel = gw["channel"]
            rssi = gw["rssi"]
            snr = gw["snr"]

    except Exception as e:
        print(e)
        pass
```

---

<div class="post-metadata">

**Author:** ![descartes](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/descartes/32/47220_2.png) [@descartes](https://www.thethingsnetwork.org/forum/u/descartes)\
**Post date:** [October 15, 2020, 12:13pm UTC](https://www.thethingsnetwork.org/forum/t/no-data-in-message/40561/6 "2020-10-15T12:13:28Z")

</div>

How do you know?

Can you dump `x` and also `msg.payload` and `msg.payload.decode` and post here.

---

<div class="post-metadata">

**Author:** ![Cfosvang](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/cfosvang/32/38707_2.png) [@Cfosvang](https://www.thethingsnetwork.org/forum/u/Cfosvang)\
**Post date:** [October 16, 2020, 1:59pm UTC](https://www.thethingsnetwork.org/forum/t/no-data-in-message/40561/7 "2020-10-16T13:59:40Z")

</div>

There was an error, json do not accept empty attributes,despite exceptions.  
After removing all unnessersary attributes in message the problem was solved.
