Skip to content

Instantly share code, notes, and snippets.

@otms61
Created March 6, 2021 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save otms61/7af919e5295c3647431fa3132b3dbce6 to your computer and use it in GitHub Desktop.
Save otms61/7af919e5295c3647431fa3132b3dbce6 to your computer and use it in GitHub Desktop.
import time
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient, AWSIoTMQTTShadowClient
CLIENT_ID = "test1_Core"
IOT_HOST = "a3gfk65xrg4mo0-ats.iot.ap-northeast-1.amazonaws.com"
CERTIFICATE = "./gg_test1/certs/cert.pem"
PRIVATE_KEY = "./gg_test1/certs/private.key"
ROOT_CA = "./gg_test1/certs/root.ca.pem"
def create_mqtt_connection(client_id):
client = AWSIoTMQTTClient(client_id)
client.configureCredentials(ROOT_CA, PRIVATE_KEY, CERTIFICATE)
client.configureAutoReconnectBackoffTime(1, 32, 20)
client.configureOfflinePublishQueueing(-1)
client.configureDrainingFrequency(2)
client.configureConnectDisconnectTimeout(10)
client.configureMQTTOperationTimeout(5)
current_host = IOT_HOST
current_port = 8883
client.configureEndpoint(current_host, current_port)
client.connect(keepAliveIntervalSecond=10)
return client
def main():
CLIENT_ID = "test1_Core"
client = create_mqtt_connection(client_id=CLIENT_ID)
def subscribe_callback(client, userdata, message):
print(f"Received a new message: {message.payload.decode('utf-8')}")
client.subscribe(topic="hello/world", QoS=1, callback=subscribe_callback)
while True:
time.sleep(5)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment