Skip to content
This repository was archived by the owner on Dec 17, 2018. It is now read-only.

Commit 4eb119c

Browse files
committed
Initial Python client examples
1 parent f8934b4 commit 4eb119c

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

mqtt/python/arduino.cc.crt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIFHzCCBAegAwIBAgIJALWCaK9i4aK5MA0GCSqGSIb3DQEBCwUAMIG0MQswCQYD
3+
VQQGEwJVUzEQMA4GA1UECBMHQXJpem9uYTETMBEGA1UEBxMKU2NvdHRzZGFsZTEa
4+
MBgGA1UEChMRR29EYWRkeS5jb20sIEluYy4xLTArBgNVBAsTJGh0dHA6Ly9jZXJ0
5+
cy5nb2RhZGR5LmNvbS9yZXBvc2l0b3J5LzEzMDEGA1UEAxMqR28gRGFkZHkgU2Vj
6+
dXJlIENlcnRpZmljYXRlIEF1dGhvcml0eSAtIEcyMB4XDTE0MTIyNDExMjMzOFoX
7+
DTE2MTIyNDExMjMzOFowOjEhMB8GA1UECxMYRG9tYWluIENvbnRyb2wgVmFsaWRh
8+
dGVkMRUwEwYDVQQDDAwqLmFyZHVpbm8uY2MwggEiMA0GCSqGSIb3DQEBAQUAA4IB
9+
DwAwggEKAoIBAQCkvRVavhHiexjqfWbFm+2oGJdYQ/RJidJmL5x5p6xE7KWm5/Q7
10+
thuBEjAKgtweyO+sFuLHqqvP0y8OTDbuc1QeMhWFdrwfZwCeZvmLCf0+PbchJNPx
11+
P4Qp7N28vGs7iIZxgmDzrYXTyYl9n+QrGk8tN27ioAQpx5IrqSCWg9T5RFEkCy4I
12+
krlwuvZmRLLwDA+p7LADzII7XpzlFRyP9pfCTCziIxKMHEoX9IyzEJBQHAvwdHuN
13+
H+/CHDBcjhdapPMgBtgsbNaSg9bqxDlsxfoYak5BonlbbqCdB3KZAl57Gmu0NgMW
14+
MDwzVKBBSrIXWCtx0iKzWNIcRAA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t/python/publisher.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import sys
2+
import paho.mqtt.client as mqtt # https://eclipse.org/paho/clients/python/
3+
4+
server = "mqtt.arduino.cc"
5+
port = 8883
6+
7+
# use the values shown after logging into https://cloud.arduino.cc
8+
username = "username"
9+
password = "password"
10+
base_topic = "/" + username;
11+
topic = base_topic + "/001"
12+
13+
# default message value
14+
message = "0"
15+
16+
if len(sys.argv) > 1:
17+
# use command line argument as message
18+
message = sys.argv[1]
19+
20+
# The callback for when the client is connected to the broker
21+
def on_connect(client, userdata, flags, rc):
22+
print("Connected with result: " + mqtt.connack_string(rc))
23+
24+
# publish message to the topic
25+
client.publish(topic, message)
26+
27+
# The callback for when the client is disconnected from the broker
28+
def on_disconnect(client, userdata, rc):
29+
if rc != 0:
30+
print("Unexpected disconnection")
31+
32+
# The callback for when the client has published a message
33+
def on_publish(client, userdata, mid):
34+
print("Message published: " + message)
35+
36+
# message published, disconnect
37+
client.disconnect()
38+
39+
# create a new MQTT client
40+
client = mqtt.Client()
41+
42+
# configure CA cert for SSL connection
43+
client.tls_set("arduino.cc.crt")
44+
45+
# set username and password
46+
client.username_pw_set(username, password)
47+
48+
# assign callbacks
49+
client.on_connect = on_connect
50+
client.on_disconnect = on_disconnect
51+
client.on_publish = on_publish
52+
53+
# start connection
54+
client.connect(server, port)
55+
56+
# let the client loop (this is a blocking call), will unblock when the client disconnects
57+
client.loop_forever()

mqtt/python/subscriber.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import paho.mqtt.client as mqtt # https://eclipse.org/paho/clients/python/
2+
3+
server = "mqtt.arduino.cc"
4+
port = 8883
5+
6+
# use the values shown after logging into https://cloud.arduino.cc
7+
username = "username"
8+
password = "password"
9+
base_topic = "/" + username;
10+
topic = base_topic + "/001"
11+
12+
13+
# The callback for when the client is connected to the broker
14+
def on_connect(client, userdata, flags, rc):
15+
print("Connected with result: " + mqtt.connack_string(rc))
16+
17+
# subscribe to the topic
18+
client.subscribe(topic)
19+
20+
# The callback for when the client is disconnected from the broker
21+
def on_disconnect(client, userdata, rc):
22+
if rc != 0:
23+
print("Unexpected disconnection")
24+
25+
# The callback for when the client is subscribed to a topic
26+
def on_subscribe(client, userdata, mid, granted_qos):
27+
print("Subscribed to topic: " + topic)
28+
29+
# The callback for when a message for a subscribed topic is received from the broker
30+
def on_message(client, userdata, msg):
31+
print("Received message: " + msg.topic + " " + str(msg.payload))
32+
33+
# create a new MQTT client
34+
client = mqtt.Client()
35+
36+
# configure CA cert for SSL connection
37+
client.tls_set("arduino.cc.crt")
38+
39+
# set username and password
40+
client.username_pw_set(username, password)
41+
42+
# assign callbacks
43+
client.on_connect = on_connect
44+
client.on_disconnect = on_disconnect
45+
client.on_subscribe = on_subscribe
46+
client.on_message = on_message
47+
48+
# start connection
49+
client.connect(server, port)
50+
51+
# let the client loop forever (this is a blocking call)
52+
client.loop_forever()

0 commit comments

Comments
 (0)