简体   繁体   English

如何解决mqtt和AWS iot问题

[英]How to troubleshoot mqtt and AWS iot

I am newbie to AWS IoT and MQTT as well. 我也是AWS IoT和MQTT的新手。 I am trying with simple example to read payload from AWS and publish payload to AWS. 我正在尝试使用简单示例从AWS读取有效负载并将有效负载发布到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. 当我从AWS控制台更新阴影状态时,我的覆盆子pi能够接收消息,但是当我发布时没有发生任何事情。 Not even state is changing on AWS console. 在AWS控制台上甚至没有状态发生变化。

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()

Ok I solved this on myself. 好的,我自己解决了这个问题。

Error in above code is json data formalization which I was sending. 上面代码中的错误是我发送的json数据形式化。 If I replace first_message() function with given code then this example works perfectly. 如果我用给定的代码替换first_message()函数,那么这个例子很有效。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM