【问题标题】:why I not receiving on_message callback from AWS iot core为什么我没有收到来自 AWS iot 核心的 on_message 回调
【发布时间】:2020-06-15 20:33:19
【问题描述】:

我试图从我在 aws 上订阅的主题接收消息,我能够发布到该主题,但我无法在我的 paho-mqtt 应用程序上接收消息。 在 AWS IOT 客户端中,它可以完美运行。

我的代码

import ssl

prefix = 'prefix'
uid = 'udi'
aws_region = 'us-west-2'
topic = 'node/test'

client_id = 'myNode'
device_cert = f'{prefix}-certificate.pem.crt'
device_privatekey = f'{prefix}-private.pem.key'
ca_cert = 'AmazonRootCA1.pem'
mqtt_url = f'{uid}.iot.{aws_region}.amazonaws.com'


def on_connect(client, userdata, flags, response_code):
    print(f"Connected with status: {response_code}")
    mqtt.subscribe(topic)

def on_disconnect(client, userdata, response_code):
    if response_code != 0:
        print(f"Unexpected disconnection. With Error code {response_code}")

def on_message(client, userdata, message):
    # print("Received message '" + str(message.payload) + "' on topic '"
    #     + message.topic + "' with QoS " + str(message.qos))
    print(message)

if __name__ == "__main__":
    print ("Loaded MQTT configuration information.")

    client = mqtt.Client(client_id=client_id, clean_session=True, userdata=None,
                        protocol=mqtt.MQTTv311, transport="tcp")

    client.tls_set(ca_cert,
                   certfile = device_cert,
                   keyfile = device_privatekey,
                   cert_reqs = ssl.CERT_REQUIRED,
                   tls_version = ssl.PROTOCOL_TLSv1_2,
                   ciphers = None)

    client.tls_insecure_set(False)
    client.on_connect = on_connect
    client.on_message = on_message

    print ("Connecting to AWS IoT Broker...")
    client.connect(mqtt_url, port = 8883, keepalive=60)
    client.loop_forever()

我的物联网策略: { “版本”:“2012-10-17”, “陈述”: [ { “效果”:“允许”, “行动”:“物联网:连接”, “资源”:“arn:aws:iot:us-west-2:account_id:client/myNode” }, { “效果”:“允许”, “行动”:“物联网:订阅”, “资源”:“arn:aws:iot:us-west-2:account_id:topicfilter/node/test” }, { “效果”:“允许”, “行动”:“物联网:接收”, “资源”:“arn:aws:iot:us-west-2:account_id:topic/node/test” } ] }

PD:出于安全原因,我没有显示我的 ACCOUNT_ID、前缀和 UID

【问题讨论】:

    标签: python amazon-web-services mqtt aws-iot


    【解决方案1】:

    on_connect() 回调中mqtt.subscribe(topic) 应该是client.subscribe(topic)

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 2021-11-08
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多