11# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22# SPDX-License-Identifier: MIT
33
4+ import ssl
45import socket
56import adafruit_minimqtt .adafruit_minimqtt as MQTT
67
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
912try :
1013 from secrets import secrets
1114except 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!" )
1316 raise
1417
1518### Topic Setup ###
1619
1720# MQTT Topic
1821# Use this topic if you'd like to connect to a standard MQTT broker
19- # mqtt_topic = "test/topic"
22+ mqtt_topic = "test/topic"
2023
2124# Adafruit IO-style Topic
2225# 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"
2427
2528### Code ###
26-
27- # Keep track of client connection state
28- disconnect_client = False
29-
3029# Define callback methods which are called when events occur
3130# pylint: disable=unused-argument, redefined-outer-name
3231def connect (mqtt_client , userdata , flags , rc ):
@@ -35,40 +34,34 @@ def connect(mqtt_client, userdata, flags, rc):
3534 print ("Connected to MQTT Broker!" )
3635 print ("Flags: {0}\n RC: {1}" .format (flags , rc ))
3736
38-
3937def disconnect (mqtt_client , userdata , rc ):
4038 # This method is called when the mqtt_client disconnects
4139 # from the broker.
4240 print ("Disconnected from MQTT Broker!" )
4341
44-
4542def subscribe (mqtt_client , userdata , topic , granted_qos ):
4643 # This method is called when the mqtt_client subscribes to a new feed.
4744 print ("Subscribed to {0} with QOS level {1}" .format (topic , granted_qos ))
4845
49-
5046def unsubscribe (mqtt_client , userdata , topic , pid ):
5147 # This method is called when the mqtt_client unsubscribes from a feed.
5248 print ("Unsubscribed from {0} with PID {1}" .format (topic , pid ))
5349
54-
5550def publish (mqtt_client , userdata , topic , pid ):
5651 # This method is called when the mqtt_client publishes data to a feed.
5752 print ("Published to {0} with PID {1}" .format (topic , pid ))
5853
59-
6054def message (client , topic , message ):
6155 # Method callled when a client's subscribed feed has a new value.
6256 print ("New message on topic {0}: {1}" .format (topic , message ))
6357
64-
6558# Set up a MiniMQTT Client
6659mqtt_client = MQTT .MQTT (
6760 broker = secrets ["broker" ],
68- port = 1883 ,
6961 username = secrets ["aio_username" ],
7062 password = secrets ["aio_key" ],
7163 socket_pool = socket ,
64+ ssl_context = ssl .create_default_context ()
7265)
7366
7467# Connect callback handlers to mqtt_client
0 commit comments