我正在使用paho mqtt经纪人,我在AWS(亚马逊网络服务)上托管了我的经纪人, 当我尝试连接到AWS上的mqtt时,它会多次显示意外断开连接。它连接多次并立即断开连接。有时它成功连接到服务器但没有向代理发布任何消息。我在客户端使用python。
import paho.mqtt.client as MQTT
import serial
import time
import sqlite3
import json
import picamera
from ast import literal_eval
DEBUG = True
MQTT_HOST = "192.168.0.19"
MQTT_PORT = 1883
MQTT_USERNAME = ""
MQTT_PASSWORD = ""
MQTT_CLIENT_ID = "publisher"
MQTT_TOPIC = "test/sd/getdata"
MQTT_QOS = 0
MQTT_RETAIN = False
MQTT_CLEAN_SESSION = True
# MQTT_LWT = "paho/test/hello"
def on_connect(client, userdata, flags, rc):
#print("Connected with result code "+str(rc))
if rc == 0:
print "Connected to %s:%s" % (MQTT_HOST, MQTT_PORT)
#logging.info("Connected to %s:%s" % (MQTT_HOST, MQTT_PORT))
# Subscribe to our incoming topic
client.subscribe(MQTT_TOPIC, qos=MQTT_QOS
elif rc == 1:
print "Connection refused - unacceptable protocol version"
elif rc == 2:
#logging.info("Connection refused - identifier rejected")
print "Connection refused - identifier rejected"
elif rc == 3:
#logging.info("Connection refused - server unavailable")
print "Connection refused - server unavailable"
elif rc == 4:
#logging.info("Connection refused - bad user name or password")
print "Connection refused - bad user name or password"
elif rc == 5:
#logging.info("Connection refused - not authorised")
print "Connection refused - not authorised"
else:
print "Connection failed - result code %d" % (rc)
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
#print msg
print(msg.topic+" : "+str(msg.payload))
def on_disconnect(client, userdata, rc):
if rc != 0:
print("Unexpected disconnection.")
def on_publish(client, userdata, mid):
"""
What to do when a message is published
"""
print "published data"
def on_subscribe(client, userdata, mid, granted_qos):
"""
What to do in the event of subscribing to a topic"
"""
def on_unsubscribe(client, userdata, mid):
"""
What to do in the event of unsubscribing from a topic
"""
client = mqtt.Client(MQTT_CLIENT_ID, clean_session=MQTT_CLEAN_SESSION)
client.on_connect = on_connect
client.on_message = on_message
client.on_disconnect = on_disconnect
client.on_subscribe = on_subscribe
client.on_unsubscribe = on_unsubscribe
client.on_publish = on_publish
# Set the login details
if MQTT_USERNAME:
client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)
# Set the Last Will and Testament (LWT) *before* connecting
# client.will_set(MQTT_LWT, payload="this is my last will", qos=0, retain=True)
# print ""
# Attempt to connect
print "Connecting to %s:%d..." % (MQTT_HOST, MQTT_PORT)
try:
client.connect(MQTT_HOST, MQTT_PORT, 60)
except Exception, e:
print "Error connecting to %s:%d: %s" % (MQTT_HOST, MQTT_PORT, str(e))
sys.exit(2)
# Let the connection run forever
client.loop_start()
time.sleep(3)
result = a.start()
if result:
fo = open(a.fileName, "r")
x= fo.read()
# bytearr = bytearray(x)
data = bytearray(x)
# print a.savingTemp
# fingerdata = {'id':a.savingTemp,'data': data}
client.publish(MQTT_TOPIC,data)
# client.publish(MQTT_TOPIC,json.dumps(fingerdata))
while True:
pass