0

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)
dda
  • 6,030
  • 2
  • 25
  • 34
  • You haven't declared your global variable payload at the beginning of the program. Check out this post: https://stackoverflow.com/questions/423379/using-global-variables-in-a-function – Luna Dec 22 '21 at 13:36
  • Is there a way i can assign it self.read_payload(nocheck=True) at the beginning of the program? – Banda Marvin Dec 22 '21 at 16:28

1 Answers1

0
    def on_publish(self,client,userdata):
        global payload

Where does payload come from? Asserting global payload means that you have it defined at the global level, hence the name. But it is not defined. So of course you get an error...

dda
  • 6,030
  • 2
  • 25
  • 34