Skip to content

Commit 1d5602d

Browse files
committed
feat(micorpython): add support for disabling SSL verification in MQTT connection
1 parent d7d7cd1 commit 1d5602d

File tree

5 files changed

+117
-12
lines changed

5 files changed

+117
-12
lines changed

mqtt-client-Micropython/v1.23_and_above/pub.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
$ micropython pub_sub_tls.py
77
"""
88
import time
9+
910
from umqtt.simple import MQTTClient
1011

12+
1113
# Define the connection information for the pub client
1214
server = "broker.emqx.io"
1315
ClientID = f'raspberry-pub-{time.time_ns()}'
@@ -16,19 +18,22 @@
1618
topic = "raspberry/mqtt"
1719
msg = b'{"msg":"hello"}'
1820

21+
1922
# Create a connection, parameters are client ID, broker address, broker port number, authentication information
2023
def connect():
2124
print('Connected to MQTT Broker "%s"' % (server))
2225
client = MQTTClient(ClientID, server, 1883, user, password)
2326
client.connect()
2427
return client
2528

29+
2630
def reconnect():
2731
# If unable to connect to the broker, print a message to notify that the connection failed and wait 5 seconds to reconnect
2832
print('Failed to connect to MQTT broker, Reconnecting...' % (server))
2933
time.sleep(5)
3034
client.reconnect()
3135

36+
3237
# If able to connect to the broker, call connect(), otherwise call reconnect()
3338
try:
3439
client = connect()

mqtt-client-Micropython/v1.23_and_above/pub_sub_tcp.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,37 @@
88
import json
99
import random
1010
import time
11-
import wifi
1211

1312
from umqtt.simple import MQTTClient
1413

14+
import wifi
15+
16+
1517
SERVER = "broker.emqx.io"
1618
PORT = 1883
17-
CLIENT_ID = 'micropython-client-{id}'.format(id = random.getrandbits(8))
19+
CLIENT_ID = 'micropython-client-{id}'.format(id=random.getrandbits(8))
1820
USERNAME = 'emqx'
1921
PASSWORD = 'public'
2022
TOPIC = "raspberry/mqtt"
2123

24+
2225
def on_message(topic, msg):
2326
print("Received '{payload}' from topic '{topic}'\n".format(
24-
payload = msg.decode(), topic = topic.decode()))
27+
payload=msg.decode(), topic=topic.decode()))
28+
2529

2630
def connect():
2731
client = MQTTClient(CLIENT_ID, SERVER, PORT, USERNAME, PASSWORD)
2832
client.connect()
29-
print('Connected to MQTT Broker "{server}"'.format(server = SERVER))
33+
print('Connected to MQTT Broker "{server}"'.format(server=SERVER))
3034
return client
3135

36+
3237
def subscribe(client):
3338
client.set_callback(on_message)
3439
client.subscribe(TOPIC)
3540

41+
3642
def loop_publish(client):
3743
msg_count = 0
3844
while True:
@@ -41,16 +47,18 @@ def loop_publish(client):
4147
}
4248
msg = json.dumps(msg_dict)
4349
result = client.publish(TOPIC, msg)
44-
print("Send '{msg}' to topic '{topic}'".format(msg = msg, topic = TOPIC))
50+
print("Send '{msg}' to topic '{topic}'".format(msg=msg, topic=TOPIC))
4551
client.wait_msg()
4652
msg_count += 1
4753
time.sleep(1)
4854

55+
4956
def run():
5057
wifi.connect()
5158
client = connect()
5259
subscribe(client)
5360
loop_publish(client)
5461

62+
5563
if __name__ == "__main__":
5664
run()

mqtt-client-Micropython/v1.23_and_above/pub_sub_tls.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,47 @@
1717
import random
1818
import ssl
1919
import time
20-
import wifi
2120

2221
from umqtt.simple import MQTTClient
2322

23+
import wifi
24+
2425

2526
SERVER = "broker.emqx.io"
2627
PORT = 8883
27-
CLIENT_ID = 'micropython-client-{id}'.format(id = random.getrandbits(8))
28+
CLIENT_ID = 'micropython-client-{id}'.format(id=random.getrandbits(8))
2829
USERNAME = 'emqx'
2930
PASSWORD = 'public'
3031
TOPIC = "raspberry/mqtt"
31-
CA_CERTS_PATH = "./certs/broker.emqx.io-ca.der" # use the public broker CA
32+
CA_CERTS_PATH = "./certs/broker.emqx.io-ca.der" # use the public broker CA
33+
3234

3335
def on_message(topic, msg):
3436
print("Received '{payload}' from topic '{topic}'\n".format(
35-
payload = msg.decode(), topic = topic.decode()))
37+
payload=msg.decode(), topic=topic.decode()))
38+
3639

3740
def create_ssl_context():
3841
# Create an SSL context
3942
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
4043
ssl_context.load_verify_locations(CA_CERTS_PATH)
4144
return ssl_context
4245

46+
4347
def connect():
4448
ssl_context = create_ssl_context()
4549

46-
client = MQTTClient(CLIENT_ID, SERVER, PORT, USERNAME, PASSWORD, ssl = ssl_context)
50+
client = MQTTClient(CLIENT_ID, SERVER, PORT, USERNAME, PASSWORD, ssl=ssl_context)
4751
client.connect()
48-
print('Connected to MQTT Broker "{server}"'.format(server = SERVER))
52+
print('Connected to MQTT Broker "{server}"'.format(server=SERVER))
4953
return client
5054

55+
5156
def subscribe(client):
5257
client.set_callback(on_message)
5358
client.subscribe(TOPIC)
5459

60+
5561
def loop_publish(client):
5662
msg_count = 0
5763
while True:
@@ -60,16 +66,18 @@ def loop_publish(client):
6066
}
6167
msg = json.dumps(msg_dict)
6268
result = client.publish(TOPIC, msg)
63-
print("Send '{msg}' to topic '{topic}'".format(msg = msg, topic = TOPIC))
69+
print("Send '{msg}' to topic '{topic}'".format(msg=msg, topic=TOPIC))
6470
client.wait_msg()
6571
msg_count += 1
6672
time.sleep(1)
6773

74+
6875
def run():
6976
wifi.connect()
7077
client = connect()
7178
subscribe(client)
7279
loop_publish(client)
7380

81+
7482
if __name__ == "__main__":
7583
run()
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"""
2+
This script code is only available for MicroPython version 1.23 and above.
3+
4+
Install umqtt.simple library:
5+
$ micropython -m mip install umqtt.simple
6+
7+
This script demonstrates how to publish and subscribe to an MQTT broker using no secure connection, don't use this in production.
8+
9+
To run this script:
10+
$ micropython pub_sub_tls.py
11+
"""
12+
13+
import json
14+
import random
15+
import ssl
16+
import time
17+
18+
from umqtt.simple import MQTTClient
19+
20+
import wifi
21+
22+
23+
SERVER = "broker.emqx.io"
24+
PORT = 8883
25+
CLIENT_ID = 'micropython-client-{id}'.format(id=random.getrandbits(8))
26+
USERNAME = 'emqx'
27+
PASSWORD = 'public'
28+
TOPIC = "raspberry/mqtt"
29+
30+
31+
def on_message(topic, msg):
32+
print("Received '{payload}' from topic '{topic}'\n".format(
33+
payload=msg.decode(), topic=topic.decode()))
34+
35+
36+
def create_ssl_context():
37+
# Create an SSL context
38+
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
39+
ssl_context.verify_mode = ssl.CERT_NONE
40+
return ssl_context
41+
42+
43+
def connect():
44+
ssl_context = create_ssl_context()
45+
46+
client = MQTTClient(CLIENT_ID, SERVER, PORT, USERNAME, PASSWORD, ssl=ssl_context)
47+
client.connect()
48+
print('Connected to MQTT Broker "{server}"'.format(server=SERVER))
49+
return client
50+
51+
52+
def subscribe(client):
53+
client.set_callback(on_message)
54+
client.subscribe(TOPIC)
55+
56+
57+
def loop_publish(client):
58+
msg_count = 0
59+
while True:
60+
msg_dict = {
61+
'msg': msg_count
62+
}
63+
msg = json.dumps(msg_dict)
64+
result = client.publish(TOPIC, msg)
65+
print("Send '{msg}' to topic '{topic}'".format(msg=msg, topic=TOPIC))
66+
client.wait_msg()
67+
msg_count += 1
68+
time.sleep(1)
69+
70+
71+
def run():
72+
wifi.connect()
73+
client = connect()
74+
subscribe(client)
75+
loop_publish(client)
76+
77+
78+
if __name__ == "__main__":
79+
run()

mqtt-client-Micropython/v1.23_and_above/sub.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
$ micropython sub.py
77
"""
88
import time
9+
910
from umqtt.simple import MQTTClient
1011

12+
1113
# Define the connection information for the sub client
1214
SERVER = "broker.emqx.io"
1315
ClientID = f'raspberry-sub-{time.time_ns()}'
@@ -16,10 +18,12 @@
1618
topic = "raspberry/mqtt"
1719
msg = b'{"msg":"hello"}'
1820

21+
1922
def sub(topic, msg):
2023
# Print the topic and message in the callback function
2124
print('received message %s on topic %s' % (msg, topic))
2225

26+
2327
def main(server=SERVER):
2428
# Create a connection, parameters are client ID, broker address, broker port number, authentication information
2529
client = MQTTClient(ClientID, server, 1883, user, password)
@@ -35,5 +39,6 @@ def main(server=SERVER):
3539
client.check_msg()
3640
time.sleep(1)
3741

42+
3843
if __name__ == "__main__":
3944
main()

0 commit comments

Comments
 (0)