我的东西连接到 AWS IoT 但它不发布消息

物联网 aws-iot aws
2021-06-23 07:33:42

这个问题是与此相关的一个,你帮我解决我的错误,我已经连接到Eclipse的经纪人后,它的工作就好了,连接和发布,然后我切换到AWS物联网经纪人与此代码

#!/user/bin/python3
import paho.mqtt.client as mqtt
import RPi.GPIO as GPIO
import time
import ssl
import _thread

GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN)

# Define Variables
MQTT_PORT = 8883
MQTT_KEEPALIVE_INTERVAL = 45
MQTT_TOPIC = "ldr"
MQTT_MSG = "there is a product"
#MQTT_HOST = "iot.eclipse.org"

MQTT_HOST = "xxxxxxx"
THING_NAME = "LDRsensor"
CLIENT_ID ="LDRsensor"
CA_ROOT_CERT_FILE = "xxxxxxx"
THING_CERT_FILE = "xxxxxxxxxxxx"
THING_PRIVATE_KEY = "xxxxxxxxxxx"


# Define on_publish event function
def on_publish(client, userdata, mid):
    print ("Message Published...")

# Initiate MQTT Client
mqttc = mqtt.Client()

# Register publish callback function
mqttc.on_publish = on_publish


# Configure TLS Set
mqttc.tls_set(CA_ROOT_CERT_FILE, certfile=THING_CERT_FILE, keyfile=THING_PRIVATE_KEY, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None)


# Connect with MQTT Broker
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
print ("Connected Successfully")
#mqttc.loop_forever()


def publishMessage(Variable):

    while (1):
        input_value = GPIO.input(4)
        if input_value == 1:
            mqttc.publish(MQTT_TOPIC,MQTT_MSG,qos=1)
            #print ("message published")
            time.sleep(1)
_thread.start_new_thread(publishMessage,("publishMessage",))
mqttc.loop_forever()

但我得到的是,我可以成功连接但无法发布消息。是因为永远循环还是有其他原因。我尝试了调试方法,在线_thread.start_new_thread(publishMessage,("publishMessage",))[Errno 32] Broken pipe知道这是什么意思以及如何修复它?

我应该安装 AWSIoTPythonSDK 吗?

1个回答

我终于知道我的错误是什么。在 ARN 政策资源中,我在政策资源行的末尾写了一个错误的主题。我写了 ldr 而不是 LDRsensor。