Skip to content

Commit 52f03a1

Browse files
author
Andrei Birjukov
committed
Improve Paho MQTT examples for Sync for IoT.
- Fix client tls_set call - Fix MyFirstDocument name - Add comments regarding trusted CA roots location - Add example for publishing document updates
1 parent ee6128d commit 52f03a1

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import paho.mqtt.client as mqtt
2+
import json
3+
from datetime import datetime
4+
5+
#
6+
# Use the actual location of your downloaded certificate and key.
7+
#
8+
pem_location = './CYXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.pem'
9+
key_location = './CYXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.key.decrypted'
10+
11+
client = mqtt.Client(client_id="bob", clean_session=False)
12+
13+
#
14+
# Initialize TLS, specify trusted CA roots, client certifiate and key file locations.
15+
# You may need to adjust the location of trusted Certificate Authorities depending on your OS.
16+
# Typical locations are /etc/ssl/cert.pem and /usr/local/ssl/cert.pem
17+
#
18+
client.tls_set("/usr/local/ssl/cert.pem",
19+
certfile=pem_location, keyfile=key_location)
20+
21+
#
22+
# Print out log messages.
23+
#
24+
def on_log(mqttc, obj, level, string):
25+
print(string)
26+
27+
client.on_log = on_log
28+
29+
#
30+
# Publish current date/time to a new document, use QoS 1 to confirm delivery.
31+
#
32+
client.connect('mqtt-sync.us1.twilio.com', 8883, 60)
33+
client.publish('sync/docs/HelloWorld',
34+
json.dumps({"hello": datetime.now().isoformat()}), qos=1)

deployed-devices/quickstarts/paho-python/subscribe-and-print.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,34 @@
33
#
44
# Use the actual location of your downloaded certificate and key.
55
#
6-
pem_location = '/…/CY499a5cbd774f4970a9ab51e2e8c4fb57.pem'
7-
key_location = '/…/CY499a5cbd774f4970a9ab51e2e8c4fb57.key.decrypted'
6+
pem_location = './CYXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.pem'
7+
key_location = './CYXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.key.decrypted'
88

99
client = mqtt.Client(client_id="bob", clean_session=False)
10-
client.set_tls(None, pem_location, key_location)
1110

11+
#
12+
# Initialize TLS, specify trusted CA roots, client certifiate and key file locations.
13+
# You may need to adjust the location of trusted Certificate Authorities depending on your OS.
14+
# Typical locations are /etc/ssl/cert.pem and /usr/local/ssl/cert.pem
15+
#
16+
client.tls_set("/usr/local/ssl/cert.pem",
17+
certfile=pem_location, keyfile=key_location)
1218

1319
#
14-
# Print out any updates.
20+
# Print out log messages and topic updates.
1521
#
16-
def print_message(client, userdata, msg):
17-
print(msg.topic + ' ' + str(msg.payload))
22+
def on_log(mqttc, obj, level, string):
23+
print(string)
1824

25+
def on_message(client, userdata, msg):
26+
print(msg.topic + ' ' + str(msg.payload))
1927

20-
client.on_message = print_message
28+
client.on_log = on_log
29+
client.on_message = on_message
2130

2231
#
2332
# Use qos=1 to get your device caught up right away.
2433
#
2534
client.connect('mqtt-sync.us1.twilio.com', 8883, 60)
26-
client.subscribe('sync/docs/MyDoc', qos=1)
35+
client.subscribe('sync/docs/MyFirstDocument', qos=1)
2736
client.loop_forever()

0 commit comments

Comments
 (0)