I created a packet-forwarding script on my Raspberry Pi LoRa gateway to forward packets to a broker through MQTT using the Paho mqtt Python library. I managed to do the packet forwarding using my own payload values.
But now am failing to have a global payload variable from the on_rx_done/on_publish functions that have the self constructor that picks the payload values from LoRa. I would like to define the variable payload and give it self.read_payload in the publish function. But my approach is failing. In my current execution I am getting this error:
Traceback (most recent call last):
  File "/xxxx/xxxxx/xxxxxx/PythonxxxxxxPost.py", line 26, in <module>
    ret = client1.publish("xxxxxxxxxxx",payload) #topic
NameError: name 'payload' is not defined
Here is my code:
import paho.mqtt.client as paho             #mqtt library
import os
import json
import time
from datetime import datetime
from time import sleep
from xxxxx.LoRa import *
from xxxxx.board_config import BOARD
ACCESS_TOKEN='xxxxxx' #Token of your device
broker="xxx.xxx.xxx" #host name
port=xxxx
BOARD.setup()
class LoRaRcvCont(LoRa):
    def __init__(self, verbose=False):
        super(LoRaRcvCont, self).__init__(verbose)
        self.set_mode(MODE.SLEEP)
        self.set_dio_mapping([0] * 6)
    def start(self):
        self.reset_ptr_rx()
        self.set_mode(MODE.RXCONT)
        while True:
            sleep(.5)
            rssi_value = self.get_rssi_value()
            status = self.get_modem_status()
            sys.stdout.flush()
    def on_publish(self,client,userdata):
        global payload
        payload = self.read_payload(nocheck=True)
       #print(bytes(payload).decode("utf-8",'ignore'))
        print("data published to xxxxx \n")
        pass
    global client1
    client1= paho.Client("control1") #create client object
    client1.on_publish = on_publish #assign function to callback
    client1.username_pw_set(ACCESS_TOKEN) #access token 
    client1.connect(broker,port,keepalive=60)
   
    def send(self,client,userdata):      
        global payload
        payload = self.read_payload(nocheck=True)
        #payload = '{"id":"123456789","lat":48.596,"lon":-1.532}'
    ret = client1.publish("xxxxxxxxxxxx",payload) #topic
    print("Please check LATEST TELEMETRY field of your device")
    print(payload);
    time.sleep(5)
 
    