-
-
Save otms61/7af919e5295c3647431fa3132b3dbce6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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