2017-01-31 5 views
0

저는 python을 처음 사용합니다. 내 고객을 중개인과 연결하려고합니다. 그러나 "전역 이름 'mqttClient'가 정의되지 않았습니다."라는 오류가 발생합니다. 누구든지 내 코드 문제에 도움을 줄 수 있습니까?글로벌 이름 'mqttClient'가 정의되지 않았습니다.

다음

내 코드는,

Test.py

#!/usr/bin/env python 

import time, threading 
import mqttConnector 


class UtilsThread(object): 
    def __init__(self): 
     thread = threading.Thread(target=self.run, args=()) 
     thread.daemon = True # Daemonize thread 
     thread.start() # Start the execution 


class SubscribeToMQTTQueue(object): 
    def __init__(self): 
     thread = threading.Thread(target=self.run, args=()) 
     thread.daemon = True # Daemonize thread 
     thread.start() # Start the execution 

    def run(self): 
     mqttConnector.main() 


def connectAndPushData(): 
    PUSH_DATA = "xxx" 
    mqttConnector.publish(PUSH_DATA) 


def main(): 
    SubscribeToMQTTQueue() # connects and subscribes to an MQTT Queue that receives MQTT commands from the server 
    LAST_TEMP = 25 

    try: 
     if LAST_TEMP > 0: 
      connectAndPushData() 
      time.sleep(5000) 
    except (KeyboardInterrupt, Exception) as e: 
     print "Exception in RaspberryAgentThread (either KeyboardInterrupt or Other)" 
     print ("STATS: " + str(e)) 
     pass 


if __name__ == "__main__": 
    main() 

mqttConnector.py 나는이납니다

#!/usr/bin/env python 

import time 
import paho.mqtt.client as mqtt 

def on_connect(client, userdata, flags, rc): 
    print("MQTT_LISTENER: Connected with result code " + str(rc)) 


def on_message(client, userdata, msg): 
    print 'MQTT_LISTENER: Message Received by Device' 


def on_publish(client, userdata, mid): 
    print 'Temperature Data Published Succesfully' 


def publish(msg): 
    # global mqttClient 
    mqttClient.publish(TOPIC_TO_PUBLISH, msg) 


def main(): 

    MQTT_IP = "IP" 
    MQTT_PORT = "port" 

    global TOPIC_TO_PUBLISH 
    TOPIC_TO_PUBLISH = "xxx/laptop-management/001/data" 

    global mqttClient 
    mqttClient = mqtt.Client() 
    mqttClient.on_connect = on_connect 
    mqttClient.on_message = on_message 
    mqttClient.on_publish = on_publish 

    while True: 
     try: 
      mqttClient.connect(MQTT_IP, MQTT_PORT, 180) 
      mqttClient.loop_forever() 

     except (KeyboardInterrupt, Exception) as e: 
      print "MQTT_LISTENER: Exception in MQTTServerThread (either KeyboardInterrupt or Other)" 
      print ("MQTT_LISTENER: " + str(e)) 

      mqttClient.disconnect() 
      print "MQTT_LISTENER: " + time.asctime(), "Connection to Broker closed - %s:%s" % (MQTT_IP, MQTT_PORT) 



if __name__ == '__main__': 
    main() 

,

Exception in RaspberryAgentThread (either KeyboardInterrupt or Other) 
STATS: global name 'mqttClient' is not defined 
,
+0

변경'글로벌 mqttClient''에 # 글로벌 mqttClient'을 변경합니다. –

답변

0

mqttClient를 전역으로 정의하지 않았습니다.

다음은

import time 
import paho.mqtt.client as mqtt 

def on_connect(client, userdata, flags, rc): 
    print("MQTT_LISTENER: Connected with result code " + str(rc)) 


def on_message(client, userdata, msg): 
    print 'MQTT_LISTENER: Message Received by Device' 


def on_publish(client, userdata, mid): 
    print 'Temperature Data Published Succesfully' 


def publish(msg): 
    global mqttClient 
    mqttClient.publish(TOPIC_TO_PUBLISH, msg) 


def main(): 

    MQTT_IP = "IP" 
    MQTT_PORT = "port" 

    global TOPIC_TO_PUBLISH 
    TOPIC_TO_PUBLISH = "xxx/laptop-management/001/data" 

    global mqttClient 
    mqttClient.on_connect = on_connect 
    mqttClient.on_message = on_message 
    mqttClient.on_publish = on_publish 

    while True: 
     try: 
      mqttClient.connect(MQTT_IP, MQTT_PORT, 180) 
      mqttClient.loop_forever() 

     except (KeyboardInterrupt, Exception) as e: 
      print "MQTT_LISTENER: Exception in MQTTServerThread (either KeyboardInterrupt or Other)" 
      print ("MQTT_LISTENER: " + str(e)) 

      mqttClient.disconnect() 
      print "MQTT_LISTENER: " + time.asctime(), "Connection to Broker closed - %s:%s" % (MQTT_IP, MQTT_PORT) 


mqttClient = mqtt.Client() 
if __name__ == '__main__': 
    main() 
+0

고마워이 작품은 나를 위해 :) – AnjuT

관련 문제