4

I am newbie to AWS IoT and MQTT as well. I am trying with simple example to read payload from AWS and publish payload to AWS. I am able to read payload on device but not able to publish. When I update shadow state from AWS console my raspberry pi able to receive the message but when I am publishing nothing happening. Not even state is changing on AWS console.

Code is attached. Please suggest.

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()
2
  • Please provide code as text; not an image. Commented Apr 19, 2016 at 14:22
  • Enable CloudWatch and check log. Don't forget to attach proper policy to your product Commented Jan 23, 2018 at 6:07

1 Answer 1

1

Ok I solved this on myself.

Error in above code is json data formalization which I was sending. If I replace first_message() function with given code then this example works perfectly.

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.