-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathawspubsub.py
More file actions
44 lines (33 loc) · 877 Bytes
/
awspubsub.py
File metadata and controls
44 lines (33 loc) · 877 Bytes
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
import machine
import network
import time
from umqtt.robust import MQTTClient
def cb(topic,msg):
print(msg)
HOST = b'endpoint.iot.us-east-1.amazonaws.com' #el host debe ser reemplazado por uno propio
TOPIC = bytes('TopicName', 'utf-8')
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("SSID","PASS")
time.sleep(4)
cert="/cert/cert.pem.crt"
key="/cert/private.pem.key"
root="/cert/root_ca.pem"
with open(cert, 'rb') as f:
certf = f.read()
print(certf)
with open(key, 'rb') as f:
keyf = f.read()
print(keyf)
with open(root, 'rb') as f:
rootf = f.read()
print(rootf)
time.sleep(1)
conn = MQTTClient(client_id=TOPIC, server=HOST, port=8883, ssl=True, ssl_params={"cert":certf, "key":keyf})
conn.set_callback(cb)
conn.connect()
conn.subscribe(TOPIC)
msg='mensaje'
while True:
connection.publish(topic=TOPIC, msg=msg, qos=0)
sleep(2)