4

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?

#!/usr/bin/python2.7
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.tls_insecure_set(True)
awsclient.connect("a1e2evxrc4wz76.iot.us-east-1.amazonaws.com", 8883, 60)
awsclient.loop_start()

rc=0
while rc == 0:
data={}
data['temp']='64'
data['humid']='65'
print(data)
payload = json.dumps(data)
print("Payload: " + payload)
awsclient.publish("Rasp/data", payload, qos=1)
time.sleep(10)
print('rc: ' +str(rc))`

Bence Kaulics
  • 7,843
  • 8
  • 42
  • 90
amit banik
  • 41
  • 1

1 Answers1

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.

Heath Raftery
  • 683
  • 3
  • 7