Troubleshooting AWS IoT MQTT

0 votes

I've been attempting a simple example for reading payload from AWS and publishing payload back to AWS. I'm, somewhat new to both AWS IoT and MQTT. Now, I'm able to read payload on my device but can't get it to publish. And, upon updating shadow state from AWS console, my raspberry pi receives the message but again nothing happens when I try publishing, like even the state on AWS console doesn't seem to change.

What could be happening or going wrong? Please advice. Following is my code:

def on_connect(mqttc, obj, flags, rc):
    if rc==0:
        print ("Subscriber Connection status code: "+str(rc)+" | Connection status: successful")
    elif rc==1:
        print ("Subscriber Connection status code: "+str(rc)+" | Connection status: Connection refused")

def on_subscribe(mqttc, obj, mid, granted_qos):
    print("Subscribed: "+str(mid)+" "+str(granted_qos)+"data"+str(obj))
    first_message()

def on_message(mqttc, obj, msg):
    print("Received message from topic: "+msg.topic+" | QoS: "+str(msg.qos)+" | Data Received: "+str(msg.payload))

def on_publish(client, userdata, mid):
    print("Message is published")

def first_message():
    data = {} data['r'] = 2 data['g'] = 255 data['b'] = 95 data2 = {} data2['color'] = data data3 = {} data3['reported'] = data2 data4 = {}
    data4['state'] = data3 json_data = json.dumps(data4) print(str(json_data))
    (rc, mid) = mqttc.publish("$aws/things/thirdthing/shadow/update/", str(json_data), 1)

mqttc = mqtt.Client(client_id="thirdthing1")
mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe
mqttc.on_message = on_message
mqttc.on_publish = on_publish
mqttc.tls_set("/home/pi/deviceSDK/root-CA.crt",
                certfile="/home/pi/deviceSDK/7391d7d21d-certificate.pem.crt",
                keyfile="/home/pi/deviceSDK/7391d7d21d-private.pem.key",
              tls_version=ssl.PROTOCOL_TLSv1_2,
              ciphers=None)
mqttc.connect("AYYCW0HM971XS.iot.us-west-2.amazonaws.com", port=8883) #AWS IoT service hostname and portno
mqttc.subscribe("$aws/things/thirdthing/shadow/update/#", qos=1) #The names of these topics start with $aws/things/thingName/shadow."
mqttc.loop_forever()
Dec 4, 2018 in IoT (Internet of Things) by Bharani
• 4,660 points
1,472 views

1 answer to this question.

0 votes

Took me little time, but resolved the issue. The error in above code seems to be in the JSON data formalization that you're sending. So, just replace that first_message() function with the following code and things should start working perfectly for you.

def first_message():
    payload = json.dumps({
        "state":{
            "reported":{
                "this_thing_is_alive":True,
                "color":{
                    "r":255,
                    "g":1,
                    "b":255
                }
            }
        }
    })
    mqttc.publish("$aws/things/thirdthing/shadow/update", payload)
answered Dec 4, 2018 by DataKing99
• 8,240 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
2,067 views
0 votes
1 answer
0 votes
1 answer

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

This fixed the problem: Browse to https://console.aws.amazon.com/iam/home?region=us-west-2#security_credentialand then if ...READ MORE

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

AWS-MQTT Connection

Link --> https://docs.aws.amazon.com/greengrass/latest/developerguide/module3-I.html This is Very Helpful to you.If ...READ MORE

answered Apr 3, 2019 in IoT (Internet of Things) by KanaguRaj
• 160 points
1,340 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP