используя переменную из json .load в другой функции - PullRequest
0 голосов
/ 28 мая 2020

Я использую API для получения данных TTN с устройства. Я создал функцию «def on_message (mqtt c, obj, msg):», используя json .loads (msg.payload.decode ('utf-8')) для получения данных mqtt. Я хочу выбрать переменную «node_data» и использовать ее в def devdata (). но, похоже, я не получаю ничего, кроме None.

import paho.mqtt.client as mqtt
import json
import pybase64
import binascii


APPEUI = "0018B24441524632"
APPID = "adeunis_fieldtester"
PSW = "ttn-account-v2.vuQczD1bmPoghhaKjlIHR-iHovHIbYMpfWSKosPAGaU"

# 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:
        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"]
        payload_fields = x["payload_raw"]
        datetime = x["metadata"]["time"]
        gateways = x["metadata"]["gateways"]
        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"]

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

            # decoding the payload_field
        payload_r = (pybase64.b64decode(payload_fields + "="))
        # decoding the Payload_r to Byte-Ascii string
        payload_h = binascii.hexlify(payload_r)
        # Byte to tekst
        node_data = (payload_h.decode("utf-8)"))        #this is the variable i would like to use in devdata()
        # Printing data, + str(payload_fields) + ", "

        print(str(app) + ", " + str(device) + ", " + str(deveui) + ", " + str(port) + ", " + str(counter) + ", "
          + str(node_data) + ", " + str(modulation) + ", " + str(datetime) + ", " + str(frequency) + ", "
          + str(confirmed) + ", " + str(data_rate) + ", " + str(air_time) + ", " + str(coding_rate) + ", "
          + str(gateway_id) + ", " + str(timestamp) + ", " + str(time) + "," + str(channel) + ", "
          + str(gwrssi) + ", " + str(gwsnr))

        return node_data    #return data for use in devdata()

    except Exception as e:
        print(e)
        pass

def devdata():
    node_data = on_message()            # trying to use this as the Variable
    temperatur = int(node_data[2:4], 16)
    print("temperatur =", temperatur)
`
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...