-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmqtt.py
More file actions
78 lines (61 loc) · 2.24 KB
/
mqtt.py
File metadata and controls
78 lines (61 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
# import logging
import time
import json
endpoint = "ENDPOINT"
rootCAPath = "<PATH>/rootCA.pem"
certificatePath = "<PATH>/certificate.pem.crt"
privateKeyPath = "<PATH>/private.pem.key"
clientId = 'irc_0'
topic = 'teste'
mode = 'both'
port = 8883
allowedActions = ['both', 'publish', 'subscribe']
if mode not in allowedActions:
print("Unknown --mode option %s. Must be one of %s" % (mode, str(allowedActions)))
exit(2)
if not certificatePath or not privateKeyPath:
print("Missing credentials for authentication.")
exit(2)
# Configure logging
# logger = logging.getLogger("AWSIoTPythonSDK.core")
# logger.setLevel(logging.DEBUG)
# streamHandler = logging.StreamHandler()
# formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# streamHandler.setFormatter(formatter)
# logger.addHandler(streamHandler)
# Init AWSIoTMQTTClient
myAWSIoTMQTTClient = None
myAWSIoTMQTTClient = AWSIoTMQTTClient(clientId)
myAWSIoTMQTTClient.configureEndpoint(endpoint, port)
myAWSIoTMQTTClient.configureCredentials(rootCAPath, privateKeyPath, certificatePath)
# AWSIoTMQTTClient connection configuration
myAWSIoTMQTTClient.configureAutoReconnectBackoffTime(1, 128, 20)
myAWSIoTMQTTClient.configureOfflinePublishQueueing(-1) # Infinite offline Publish queueing
myAWSIoTMQTTClient.configureDrainingFrequency(2) # Draining: 2 Hz
myAWSIoTMQTTClient.configureConnectDisconnectTimeout(10) # 10 sec
myAWSIoTMQTTClient.configureMQTTOperationTimeout(5) # 5 sec
myAWSIoTMQTTClient.connect()
def subscribe():
myAWSIoTMQTTClient.subscribe(topic, 1, customCallback)
def publish(messageToPublish):
print(topic)
print(messageToPublish)
return myAWSIoTMQTTClient.publish(topic, messageToPublish, 1)
# Custom MQTT message callback
def customCallback(client, userdata, message):
print("--------------\n\n")
print("Received a new message: ")
messageJson = json.loads(message.payload)
print(json.dumps(messageJson, indent=4))
# print(message.payload)
print("from topic: ")
print(message.topic)
print("--------------\n\n")
def main():
messageToPublish = 'Teste publish'
subscribe()
while True:
time.sleep(0.1)
if __name__ == '__main__':
main()