Skip to content

Fix Hostname Issue (#25) #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 8 additions & 29 deletions adafruit_minimqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -222,34 +216,16 @@ 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:
if self.logger is not None:
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:
Expand All @@ -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)

Expand Down
9 changes: 4 additions & 5 deletions examples/minimqtt_adafruitio_cellular.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion examples/minimqtt_adafruitio_eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/minimqtt_adafruitio_wifi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)
Expand Down
11 changes: 6 additions & 5 deletions examples/minimqtt_simpletest_cellular.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down