1
1
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2
2
# SPDX-License-Identifier: MIT
3
3
4
+ import ssl
4
5
import socket
5
6
import adafruit_minimqtt .adafruit_minimqtt as MQTT
6
7
7
- ### Secrets File Setup ###
8
-
8
+ # Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
9
+ # "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
10
+ # source control.
11
+ # pylint: disable=no-name-in-module,wrong-import-order
9
12
try :
10
13
from secrets import secrets
11
14
except ImportError :
12
- print ("Connection secrets are kept in secrets.py, please add them there!" )
15
+ print ("WiFi secrets are kept in secrets.py, please add them there!" )
13
16
raise
14
17
15
18
### Topic Setup ###
16
19
17
20
# MQTT Topic
18
21
# Use this topic if you'd like to connect to a standard MQTT broker
19
- # mqtt_topic = "test/topic"
22
+ mqtt_topic = "test/topic"
20
23
21
24
# Adafruit IO-style Topic
22
25
# Use this topic if you'd like to connect to io.adafruit.com
23
- mqtt_topic = secrets ["aio_username" ] + "/feeds/temperature"
26
+ # mqtt_topic = secrets["aio_username"] + "/feeds/temperature"
24
27
25
28
### Code ###
26
-
27
- # Keep track of client connection state
28
- disconnect_client = False
29
-
30
29
# Define callback methods which are called when events occur
31
30
# pylint: disable=unused-argument, redefined-outer-name
32
31
def connect (mqtt_client , userdata , flags , rc ):
@@ -35,40 +34,34 @@ def connect(mqtt_client, userdata, flags, rc):
35
34
print ("Connected to MQTT Broker!" )
36
35
print ("Flags: {0}\n RC: {1}" .format (flags , rc ))
37
36
38
-
39
37
def disconnect (mqtt_client , userdata , rc ):
40
38
# This method is called when the mqtt_client disconnects
41
39
# from the broker.
42
40
print ("Disconnected from MQTT Broker!" )
43
41
44
-
45
42
def subscribe (mqtt_client , userdata , topic , granted_qos ):
46
43
# This method is called when the mqtt_client subscribes to a new feed.
47
44
print ("Subscribed to {0} with QOS level {1}" .format (topic , granted_qos ))
48
45
49
-
50
46
def unsubscribe (mqtt_client , userdata , topic , pid ):
51
47
# This method is called when the mqtt_client unsubscribes from a feed.
52
48
print ("Unsubscribed from {0} with PID {1}" .format (topic , pid ))
53
49
54
-
55
50
def publish (mqtt_client , userdata , topic , pid ):
56
51
# This method is called when the mqtt_client publishes data to a feed.
57
52
print ("Published to {0} with PID {1}" .format (topic , pid ))
58
53
59
-
60
54
def message (client , topic , message ):
61
55
# Method callled when a client's subscribed feed has a new value.
62
56
print ("New message on topic {0}: {1}" .format (topic , message ))
63
57
64
-
65
58
# Set up a MiniMQTT Client
66
59
mqtt_client = MQTT .MQTT (
67
60
broker = secrets ["broker" ],
68
- port = 1883 ,
69
61
username = secrets ["aio_username" ],
70
62
password = secrets ["aio_key" ],
71
63
socket_pool = socket ,
64
+ ssl_context = ssl .create_default_context ()
72
65
)
73
66
74
67
# Connect callback handlers to mqtt_client
0 commit comments