0
votes

I trying to receive a message from a topic I am subscribed to on aws, I am able to publish to the topic, but I unable to receive the message to on my paho-mqtt app. In AWS IOT client it work perfect.

my code

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()

My iot ploicy: { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "iot:Connect", "Resource": "arn:aws:iot:us-west-2:account_id:client/myNode" }, { "Effect": "Allow", "Action": "iot:Subscribe", "Resource": "arn:aws:iot:us-west-2:account_id:topicfilter/node/test" }, { "Effect": "Allow", "Action": "iot:Receive", "Resource": "arn:aws:iot:us-west-2:account_id:topic/node/test" } ] }

PD: FOR SECURITY REASON I NO SHOWING MY ACCOUNT_ID, PREFIX AND UID

1

1 Answers

0
votes

In the on_connect() callback mqtt.subscribe(topic) should probably be client.subscribe(topic)