Reputation: 624
i might be missing something while subscribing to a topic but not sure.Below is my iot python subscription code which works perfectly. however if i run the code without subscription code " myAWSIoTMQTTClient.subscribe("topic_1", 1, customCallback)" the topic_1 gets published but if try to access to subscribe the topic_1 from another console of python it just returns true and does not print message from the custom call back.only if keep both subscription and publish in the same python console it works, but i try it run subscription and publish in separate console then it throws an error. in other words how can i subscribe to a topic which is already created?
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
import time
import argparse
import json
host = "XXXXXXXX.iot.us-east-2.amazonaws.com"
rootCAPath = "root-CA.crt"
certificatePath = "XXXXX.cert.pem"
privateKeyPath = "XXXXX.private.key"
port = 8883
clientId = "sdk-java"
topic = "topic_1"
message_to_print="aws aws_preethi"
def customCallback(client, userdata, message):
print("Received a new message: ")
print(message.payload)
print("from topic: ")
print(message.topic)
print("--------------\n\n")
myAWSIoTMQTTClient = AWSIoTMQTTClient(clientId)
myAWSIoTMQTTClient.configureEndpoint(host, port)
myAWSIoTMQTTClient.configureCredentials(rootCAPath, privateKeyPath, certificatePath)
# AWSIoTMQTTClient connection configuration
myAWSIoTMQTTClient.configureAutoReconnectBackoffTime(1, 32, 20)
myAWSIoTMQTTClient.configureOfflinePublishQueueing(-1) # Infinite offline Publish queueing
myAWSIoTMQTTClient.configureDrainingFrequency(2) # Draining: 2 Hz
myAWSIoTMQTTClient.configureConnectDisconnectTimeout(10) # 10 sec
myAWSIoTMQTTClient.configureMQTTOperationTimeout(5) # 5 sec
# Connect and subscribe to AWS IoT
myAWSIoTMQTTClient.connect()
myAWSIoTMQTTClient.subscribe("topic_1", 1, customCallback)
# Publish to the same topic in a loop forever
loopCount = 0
while True:
message = {}
message['message'] = message_to_print
message['sequence'] = loopCount
messageJson = json.dumps(message)
myAWSIoTMQTTClient.publish(topic, messageJson, 1)
loopCount += 1
time.sleep(10)
Upvotes: 0
Views: 4592
Reputation: 21
do it like following:
# Connect and subscribe to AWS IoT
myAWSIoTMQTTClient.connect()
# Publish to the same topic in a loop forever
loopCount = 0
while True:
myAWSIoTMQTTClient.subscribe("topic_1", 1, customCallback)
message = {}
message['message'] = message_to_print
message['sequence'] = loopCount
messageJson = json.dumps(message)
myAWSIoTMQTTClient.publish(topic, messageJson, 1)
loopCount += 1
time.sleep(1)
Upvotes: 0
Reputation: 141
I believe you need to use a unique value for clientId for each running instance of the script, otherwise the second running instance will disconnect the first instance.
From this forum post: https://forums.aws.amazon.com/thread.jspa?threadID=219513
MQTT client IDs do not have an association with a Thing in AWS IoT. These client IDs are purely to uniquely identify the MQTT connection. One important thing to consider about MQTT client IDs is they need to be unique among devices across your AWS account. If you have a client connected as "client ID 1" and a second client connects with that same ID ("client ID 1") then first client's connection will be force disconnected. This is a feature of the MQTT spec that keeps a client with intermittent connectivity from spawning multiple MQTT sessions.
Upvotes: 3