I run a simple publish message from a Raspberry Pi. It seems that my device is connected to AWS IoT, but the message is not publishing and the on_connect and on_publish methods are not invoking.

Can anyone help figure out why?

import paho.mqtt.client as mqtt       
import ssl  
import json,time

def on_connect(client, userdata, flags, rc):  
    print ("Subscriber Connection status code: "+str(rc))
def on_publish(client, userdata, mid):  
    print(client, userdata, mid)
#Connect to AWS IoT
print("starting aws client")  
awsclient = mqtt.Client(client_id="raspberry",protocol=mqtt.MQTTv311)  
awsclient.on_connect = on_connect  
awsclient.on_publish = on_publish  
awsclient.tls_set("./root-CA.pem",certfile="./575c6bc5b3-    certificate.pem.crt",keyfile="./575c6bc5b3-            private.pem.key",tls_version=ssl.PROTOCOL_SSLv23,ciphers=None)
awsclient.connect("a1e2evxrc4wz76.iot.us-east-1.amazonaws.com", 8883, 60)  

while rc == 0:  
   payload = json.dumps(data)  
   print("Payload: " + payload)  
   awsclient.publish("Rasp/data", payload, qos=1)   
print('rc: ' +str(rc))`
  • 1
    Are you sure that your certificate is correct? (BTW, probably not a good idea to make it public). I guess that you have seen examples like this one. Doesn't that awsclient.connect have a return value that you can check? For instance, CreateConnection does - both an HTTP response code and some JSON. It would be very strange if this one did not inform you of success or (reason for) failure. Jan 16, 2018 at 10:30

1 Answer 1


Your question is lacking a question, but assuming it is "what do I check next" then you need to look at the .connect function. It is a blocking call that waits for a CONNACK from the broker before calling on_connect with the result code.

If on_connect is not being called then the broker is not responding - maybe give it a bit more time. If execution is continuing after .connect then the on_connect reference is broken. Try stripping statements out until the on_connect function is successfully called. Only then can you start to debug your particular function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.