Skip to content

Commit 5e840a4

Browse files
authored
Merge pull request adafruit#33 from brentru/fix-hostname
Fix Hostname Issue (adafruit#25)
2 parents fe70374 + 964144c commit 5e840a4

5 files changed

+21
-41
lines changed

adafruit_minimqtt.py

+8-29
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@
5454
MQTT_TOPIC_LENGTH_LIMIT = const(65535)
5555
MQTT_TCP_PORT = const(1883)
5656
MQTT_TLS_PORT = const(8883)
57-
TCP_MODE = const(0)
58-
TLS_MODE = const(2)
5957

6058
# MQTT Commands
6159
MQTT_PINGREQ = b"\xc0\0"
@@ -128,11 +126,7 @@ def __init__(
128126
keep_alive=60,
129127
):
130128
self._sock = None
131-
# broker
132-
try: # set broker IP
133-
self.broker = _the_interface.unpretty_ip(broker)
134-
except ValueError: # set broker URL
135-
self.broker = broker
129+
self.broker = broker
136130
# port/ssl
137131
self.port = MQTT_TCP_PORT
138132
if is_ssl:
@@ -222,34 +216,16 @@ def connect(self, clean_session=True):
222216
:param bool clean_session: Establishes a persistent session.
223217
224218
"""
225-
try:
226-
proto, dummy, broker, path = self.broker.split("/", 3)
227-
# replace spaces in path
228-
path = path.replace(" ", "%20")
229-
except ValueError:
230-
proto, dummy, broker = self.broker.split("/", 2)
231-
path = ""
232-
if proto == "http:":
233-
self.port = MQTT_TCP_PORT
234-
elif proto == "https:":
235-
self.port = MQTT_TLS_PORT
236-
else:
237-
raise ValueError("Unsupported protocol: " + proto)
238-
239-
if ":" in broker:
240-
broker, port = broker.split(":", 1)
241-
port = int(port)
242-
243-
addr = _the_sock.getaddrinfo(broker, self.port, 0, _the_sock.SOCK_STREAM)[0]
244-
self._sock = _the_sock.socket(addr[0], addr[1], addr[2])
219+
self._sock = _the_sock.socket()
245220
self._sock.settimeout(15)
246221
if self.port == 8883:
247222
try:
248223
if self.logger is not None:
249224
self.logger.debug(
250225
"Attempting to establish secure MQTT connection..."
251226
)
252-
self._sock.connect((broker, self.port), _the_interface.TLS_MODE)
227+
conntype = _the_interface.TLS_MODE
228+
self._sock.connect((self.broker, self.port), conntype)
253229
except RuntimeError as e:
254230
raise MMQTTException("Invalid broker address defined.", e)
255231
else:
@@ -258,7 +234,10 @@ def connect(self, clean_session=True):
258234
self.logger.debug(
259235
"Attempting to establish insecure MQTT connection..."
260236
)
261-
self._sock.connect(addr[-1], TCP_MODE)
237+
addr = _the_sock.getaddrinfo(
238+
self.broker, self.port, 0, _the_sock.SOCK_STREAM
239+
)[0]
240+
self._sock.connect(addr[-1], _the_interface.TCP_MODE)
262241
except RuntimeError as e:
263242
raise MMQTTException("Invalid broker address defined.", e)
264243

examples/minimqtt_adafruitio_cellular.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717

1818
### Cellular ###
1919

20-
# Create a serial connection for the FONA connection using 4800 baud.
21-
# These are the defaults you should use for the FONA Shield.
22-
# For other boards set RX = GPS module TX, and TX = GPS module RX pins.
23-
uart = busio.UART(board.TX, board.RX, baudrate=4800)
20+
# Create a serial connection for the FONA connection
21+
uart = busio.UART(board.TX, board.RX)
2422
rst = digitalio.DigitalInOut(board.D4)
2523
# Initialize FONA
2624
fona = FONA(uart, rst)
@@ -76,9 +74,10 @@ def message(client, topic, message):
7674
# Set up a MiniMQTT Client
7775
# NOTE: We'll need to connect insecurely for ethernet configurations.
7876
mqtt_client = MQTT.MQTT(
79-
broker="http://io.adafruit.com",
77+
broker="io.adafruit.com",
8078
username=secrets["aio_username"],
8179
password=secrets["aio_key"],
80+
is_ssl=False,
8281
)
8382

8483
# Setup the callback methods above

examples/minimqtt_adafruitio_eth.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ def message(client, topic, message):
6161
# Set up a MiniMQTT Client
6262
# NOTE: We'll need to connect insecurely for ethernet configurations.
6363
mqtt_client = MQTT.MQTT(
64-
broker="http://io.adafruit.com",
64+
broker="io.adafruit.com",
6565
username=secrets["aio_username"],
6666
password=secrets["aio_key"],
67+
is_ssl=False,
6768
)
6869

6970
# Setup the callback methods above

examples/minimqtt_adafruitio_wifi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def message(client, topic, message):
8989

9090
# Set up a MiniMQTT Client
9191
mqtt_client = MQTT.MQTT(
92-
broker="http://io.adafruit.com",
92+
broker="io.adafruit.com",
9393
username=secrets["aio_username"],
9494
password=secrets["aio_key"],
9595
)

examples/minimqtt_simpletest_cellular.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717
print("Cellular secrets are kept in secrets.py, please add them there!")
1818
raise
1919

20-
# Create a serial connection for the FONA connection using 4800 baud.
21-
# These are the defaults you should use for the FONA Shield.
22-
# For other boards set RX = GPS module TX, and TX = GPS module RX pins.
23-
uart = busio.UART(board.TX, board.RX, baudrate=4800)
20+
# Create a serial connection for the FONA connection
21+
uart = busio.UART(board.TX, board.RX)
2422
rst = digitalio.DigitalInOut(board.D4)
2523
# Initialize FONA
2624
fona = FONA(uart, rst)
@@ -86,7 +84,10 @@ def publish(client, userdata, topic, pid):
8684

8785
# Set up a MiniMQTT Client
8886
client = MQTT.MQTT(
89-
broker=secrets["broker"], username=secrets["user"], password=secrets["pass"]
87+
broker=secrets["broker"],
88+
username=secrets["user"],
89+
password=secrets["pass"],
90+
is_ssl=False,
9091
)
9192

9293
# Connect callback handlers to client

0 commit comments

Comments
 (0)