0
votes

Recently I tried to publish and subscribe message through AWS IOT service.

Here is how I processed it:

  • Subscribe to topic "Sensor/+/reading" (Subscribe.py)
  • Publish the data to topic "Sensor/livingroom/reading" (Publish.py)

However, every time I published the data to AWS IOT MQTT server, my script for subscribing reconnected to the MQTT server and received no data from remote.

Here are my Subscribe.py script:

import sys                                 
import ssl
import paho.mqtt.client as mqtt 
import time

def on_connect(mqttc, obj, flags, rc):
    if rc==0:
        print ("Connection status: successful")
    elif rc==1:
        print ("Connection status: Connection refused")
    mqttc.subscribe("Sensor/+/reading", qos=1)

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

def on_message(mqttc, obj, msg):
    print("TOPIC: "+msg.topic+"| Data Received: "+str(msg.payload))


mqttc = mqtt.Client(client_id="Yun")

mqttc.on_connect = on_connect 
mqttc.on_subscribe = on_subscribe

mqttc.tls_set("root-CA.crt",
                certfile="64bc41eab8-certificate.pem.crt",
                keyfile="64bc41eab8-private.pem.key",
                tls_version=ssl.PROTOCOL_TLSv1_2, 
                ciphers=None)

mqttc.connect("AWS Thing endpoint",port = 8883)

mqttc.loop_forever()     

And here is my Publish.py script:

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

jsonstr = '{"DeviceId":0,"Readings":[{"label":"Light","data":223},{"label":"Motion","data":245}]}'

jsonReading = json.loads(jsonstr)

payloadJson = {}
payloadJson['state'] = {}
payloadJson['state']['desired'] = jsonReading

print payloadJson

def on_publish(mosq, obj, mid):
    print("mid: "+str(mid))

def on_connect(mqttc, obj, flags, rc):
    if rc==0:
        print ("Connection status: successful")
    elif rc==1:
        print ("Connection refused")
    return

def on_disconnect(client, userdata, rc):
    print("disonnected")

mqttc = mqtt.Client(client_id="Yun")


mqttc.on_publish = on_publish
mqttc.on_connect = on_connect
mqttc.on_disconnect = on_disconnect

mqttc.tls_set("root-CA.crt",
                certfile="64bc41eab8-certificate.pem.crt",
                keyfile="64bc41eab8-private.pem.key",
                tls_version=ssl.PROTOCOL_TLSv1_2, 
                ciphers=None)

mqttc.connect("AWS iot things endpoint",8883)

mqttc.loop_start()
while True:
    mqttc.publish("Sensor/livingroom/reading", json.dumps(payloadJson) , qos = 1)
    time.sleep(10)
2

2 Answers

1
votes

Solved! One AWS iot Thing cannot connect with two things(connections). Just adding one more thing in AWS iot for extract connection then the scripts will works well.

0
votes

MQTT Client Ids have to be unique. You are using the Client Id of Yun` for both clients.

The MQTT spec says the broker must disconnect the first client when a second connects with the same client id.