Connection and publishing operations using paho-mqtt not working on AWS IoT

0 votes

Attempted to work with AWS IoT, the following code was working earlier:

import paho.mqtt.client as mqtt
import ssl, random
from time import sleep

mqtt_url = ""
root_ca = './certs/iotRootCA.pem'
public_crt = './certs/deviceCert.crt'
private_key = './certs/deviceCert.key'

connflag = False

def on_connect(client, userdata, flags, response_code):
    global connflag
    connflag = True
    print("Connected with status: {0}".format(response_code))

def on_publish(client, userdata, mid):
    print userdata + " -- " + mid

if __name__ == "__main__":
    print "Loaded MQTT configuration information."
    print "Endpoint URL: " + mqtt_url
    print "Root Cert: " + root_ca
    print "Device Cert: " + public_crt
    print "Private Key: " + private_key

    client = mqtt.Client()
                   certfile = public_crt,
                   keyfile = private_key,
                   cert_reqs = ssl.CERT_REQUIRED,
                   tls_version = ssl.PROTOCOL_TLSv1_2,
                   ciphers = None)

    client.on_connect = on_connect
#    client.on_publish = on_publish

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

    while 1==1:
        print connflag
        if connflag == True:
            print "Publishing..."
            ap_measurement = random.uniform(25.0, 150.0)
            client.publish("ActivePower", ap_measurement, qos=1)
            print("ActivePower published: " + "%.2f" % ap_measurement )
            print "waiting for connection..."

Later, I received the following error(there is no connection):

Loaded MQTT configuration information.
Endpoint URL:
Root Cert: ./certs/iotRootCA.pem
Device Cert: ./certs/deviceCert.crt
Private Key: ./certs/deviceCert.key
Connecting to AWS IoT Broker... False
waiting for connection...
waiting for connection...
waiting for connection... False

What should i do about this?

Mar 11, 2019 in IoT (Internet of Things) by Shubham
• 13,480 points

1 answer to this question.

0 votes

I'd say your issue is mainly that your certificate's policy lacks permissions to connect. If it's unspecified, paho generates a random client_id. You should set the client_id. You also need a policy allowing your certificate to connect using that client id.

  "Effect": "Allow",
  "Action": "iot:Connect",

You can also set the resource in your policy to * and then connect with any client_id:

  "Effect": "Allow",
  "Action": "iot:Connect",
answered Mar 11, 2019 by Upasana
• 8,620 points

Related Questions In IoT (Internet of Things)

0 votes
1 answer

AWS IoT login from android MQTT client using IAM is not working

Seeing your comments and questions. I had ...READ MORE

answered Jul 24, 2018 in IoT (Internet of Things) by anonymous2
• 4,280 points
0 votes
1 answer

Publishing commands to device in IBM IoT using MQTT in Java

If you are publishing from an application, ...READ MORE

answered Jul 27, 2018 in IoT (Internet of Things) by anonymous2
• 4,280 points
0 votes
1 answer

aws iot describe-endpoint::You must specify a region

This fixed the problem: Browse to then if ...READ MORE

answered Jul 11, 2018 in IoT (Internet of Things) by anonymous2
• 4,280 points
0 votes
1 answer
0 votes
1 answer

Azure IoT Hub : Sending messages using Python via mqtt

The thing is that IoT Hub is not ...READ MORE

answered Jan 4, 2019 in IoT (Internet of Things) by Upasana
• 8,620 points
0 votes
1 answer

Invalid MQTT publish topic on Google Cloud IoT

It's pretty simple. You have the incorrect ...READ MORE

answered Feb 26, 2019 in IoT (Internet of Things) by Upasana
• 8,620 points