diff --git a/adafruit_minimqtt.py b/adafruit_minimqtt.py index 5fa7b924..52c835a6 100644 --- a/adafruit_minimqtt.py +++ b/adafruit_minimqtt.py @@ -54,8 +54,6 @@ MQTT_TOPIC_LENGTH_LIMIT = const(65535) MQTT_TCP_PORT = const(1883) MQTT_TLS_PORT = const(8883) -TCP_MODE = const(0) -TLS_MODE = const(2) # MQTT Commands MQTT_PINGREQ = b"\xc0\0" @@ -128,11 +126,7 @@ def __init__( keep_alive=60, ): self._sock = None - # broker - try: # set broker IP - self.broker = _the_interface.unpretty_ip(broker) - except ValueError: # set broker URL - self.broker = broker + self.broker = broker # port/ssl self.port = MQTT_TCP_PORT if is_ssl: @@ -222,26 +216,7 @@ def connect(self, clean_session=True): :param bool clean_session: Establishes a persistent session. """ - try: - proto, dummy, broker, path = self.broker.split("/", 3) - # replace spaces in path - path = path.replace(" ", "%20") - except ValueError: - proto, dummy, broker = self.broker.split("/", 2) - path = "" - if proto == "http:": - self.port = MQTT_TCP_PORT - elif proto == "https:": - self.port = MQTT_TLS_PORT - else: - raise ValueError("Unsupported protocol: " + proto) - - if ":" in broker: - broker, port = broker.split(":", 1) - port = int(port) - - addr = _the_sock.getaddrinfo(broker, self.port, 0, _the_sock.SOCK_STREAM)[0] - self._sock = _the_sock.socket(addr[0], addr[1], addr[2]) + self._sock = _the_sock.socket() self._sock.settimeout(15) if self.port == 8883: try: @@ -249,7 +224,8 @@ def connect(self, clean_session=True): self.logger.debug( "Attempting to establish secure MQTT connection..." ) - self._sock.connect((broker, self.port), _the_interface.TLS_MODE) + conntype = _the_interface.TLS_MODE + self._sock.connect((self.broker, self.port), conntype) except RuntimeError as e: raise MMQTTException("Invalid broker address defined.", e) else: @@ -258,7 +234,10 @@ def connect(self, clean_session=True): self.logger.debug( "Attempting to establish insecure MQTT connection..." ) - self._sock.connect(addr[-1], TCP_MODE) + addr = _the_sock.getaddrinfo( + self.broker, self.port, 0, _the_sock.SOCK_STREAM + )[0] + self._sock.connect(addr[-1], _the_interface.TCP_MODE) except RuntimeError as e: raise MMQTTException("Invalid broker address defined.", e) diff --git a/examples/minimqtt_adafruitio_cellular.py b/examples/minimqtt_adafruitio_cellular.py index 8cae5909..a403822b 100755 --- a/examples/minimqtt_adafruitio_cellular.py +++ b/examples/minimqtt_adafruitio_cellular.py @@ -17,10 +17,8 @@ ### Cellular ### -# Create a serial connection for the FONA connection using 4800 baud. -# These are the defaults you should use for the FONA Shield. -# For other boards set RX = GPS module TX, and TX = GPS module RX pins. -uart = busio.UART(board.TX, board.RX, baudrate=4800) +# Create a serial connection for the FONA connection +uart = busio.UART(board.TX, board.RX) rst = digitalio.DigitalInOut(board.D4) # Initialize FONA fona = FONA(uart, rst) @@ -76,9 +74,10 @@ def message(client, topic, message): # Set up a MiniMQTT Client # NOTE: We'll need to connect insecurely for ethernet configurations. mqtt_client = MQTT.MQTT( - broker="http://io.adafruit.com", + broker="io.adafruit.com", username=secrets["aio_username"], password=secrets["aio_key"], + is_ssl=False, ) # Setup the callback methods above diff --git a/examples/minimqtt_adafruitio_eth.py b/examples/minimqtt_adafruitio_eth.py index f0e8b120..53e3b126 100755 --- a/examples/minimqtt_adafruitio_eth.py +++ b/examples/minimqtt_adafruitio_eth.py @@ -61,9 +61,10 @@ def message(client, topic, message): # Set up a MiniMQTT Client # NOTE: We'll need to connect insecurely for ethernet configurations. mqtt_client = MQTT.MQTT( - broker="http://io.adafruit.com", + broker="io.adafruit.com", username=secrets["aio_username"], password=secrets["aio_key"], + is_ssl=False, ) # Setup the callback methods above diff --git a/examples/minimqtt_adafruitio_wifi.py b/examples/minimqtt_adafruitio_wifi.py index 20d0ff07..07871d9e 100644 --- a/examples/minimqtt_adafruitio_wifi.py +++ b/examples/minimqtt_adafruitio_wifi.py @@ -89,7 +89,7 @@ def message(client, topic, message): # Set up a MiniMQTT Client mqtt_client = MQTT.MQTT( - broker="http://io.adafruit.com", + broker="io.adafruit.com", username=secrets["aio_username"], password=secrets["aio_key"], ) diff --git a/examples/minimqtt_simpletest_cellular.py b/examples/minimqtt_simpletest_cellular.py index 875caf1c..9ab72bd2 100644 --- a/examples/minimqtt_simpletest_cellular.py +++ b/examples/minimqtt_simpletest_cellular.py @@ -17,10 +17,8 @@ print("Cellular secrets are kept in secrets.py, please add them there!") raise -# Create a serial connection for the FONA connection using 4800 baud. -# These are the defaults you should use for the FONA Shield. -# For other boards set RX = GPS module TX, and TX = GPS module RX pins. -uart = busio.UART(board.TX, board.RX, baudrate=4800) +# Create a serial connection for the FONA connection +uart = busio.UART(board.TX, board.RX) rst = digitalio.DigitalInOut(board.D4) # Initialize FONA fona = FONA(uart, rst) @@ -86,7 +84,10 @@ def publish(client, userdata, topic, pid): # Set up a MiniMQTT Client client = MQTT.MQTT( - broker=secrets["broker"], username=secrets["user"], password=secrets["pass"] + broker=secrets["broker"], + username=secrets["user"], + password=secrets["pass"], + is_ssl=False, ) # Connect callback handlers to client