Skip to content

Commit 7c3752d

Browse files
author
brentru
committed
trim unused wifi methods out of library
1 parent 60b8a7d commit 7c3752d

File tree

2 files changed

+25
-102
lines changed

2 files changed

+25
-102
lines changed

adafruit_minimqtt.py

Lines changed: 11 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -224,23 +224,20 @@ def connect(self, clean_session=True):
224224
self.broker, port = self.broker.split(":", 1)
225225
port = int(port)
226226

227+
addr = _the_sock.getaddrinfo(self.broker, self.port)[0]
228+
self._sock = _the_sock.socket(addr[0], _the_sock.SOCK_STREAM, addr[2])
229+
self._sock.settimeout(15)
227230
if self.port == 8883:
228231
try:
229232
if self.logger is not None:
230233
self.logger.debug('Attempting to establish secure MQTT connection...')
231-
self._sock.connect((self.broker, self.port), TLS_MODE)
232-
except RuntimeError:
233-
raise MMQTTException("Invalid broker address defined.")
234+
self._sock.connect(addr[-1], TLS_MODE)
235+
except RuntimeError as e:
236+
raise MMQTTException("Invalid broker address defined.", e)
234237
else:
235-
if isinstance(self.broker, str):
236-
addr = _the_sock.getaddrinfo(self.broker, self.port)[0]
237-
else:
238-
addr = (self.broker, 0x21, self.port)
239238
try:
240239
if self.logger is not None:
241240
self.logger.debug('Attempting to establish insecure MQTT connection...')
242-
self._sock = _the_sock.socket(addr[0], 0x21, addr[2])
243-
self._sock.settimeout(15)
244241
self._sock.connect(addr[-1], TCP_MODE)
245242
except RuntimeError as e:
246243
raise MMQTTException("Invalid broker address defined.", e)
@@ -571,49 +568,6 @@ def unsubscribe(self, topic):
571568
self._subscribed_topics.remove(t)
572569
return
573570

574-
@property
575-
def is_wifi_connected(self):
576-
"""Returns if the ESP module is connected to
577-
an access point, resets module if False"""
578-
if self._wifi:
579-
return self._wifi.esp.is_connected
580-
raise MMQTTException("MiniMQTT Client does not use a WiFi NetworkManager.")
581-
582-
# pylint: disable=line-too-long, protected-access
583-
@property
584-
def is_sock_connected(self):
585-
"""Returns if the socket is connected."""
586-
return self.is_wifi_connected and self._sock and self._wifi.esp.socket_connected(self._sock._socknum)
587-
588-
def reconnect_socket(self):
589-
"""Re-establishes the socket's connection with the MQTT broker.
590-
"""
591-
try:
592-
if self.logger is not None:
593-
self.logger.debug("Attempting to reconnect with MQTT Broker...")
594-
self.reconnect()
595-
except RuntimeError as err:
596-
if self.logger is not None:
597-
self.logger.debug('Failed to reconnect with MQTT Broker, retrying...', err)
598-
time.sleep(1)
599-
self.reconnect_socket()
600-
601-
def reconnect_wifi(self):
602-
"""Reconnects to WiFi Access Point and socket, if disconnected.
603-
"""
604-
while not self.is_wifi_connected:
605-
try:
606-
if self.logger is not None:
607-
self.logger.debug('Connecting to WiFi AP...')
608-
self._wifi.connect()
609-
except (RuntimeError, ValueError):
610-
if self.logger is not None:
611-
self.logger.debug('Failed to reset WiFi module, retrying...')
612-
time.sleep(1)
613-
# we just reconnected, is the socket still connected?
614-
if not self.is_sock_connected:
615-
self.reconnect_socket()
616-
617571
def reconnect(self, resub_topics=True):
618572
"""Attempts to reconnect to the MQTT broker.
619573
:param bool resub_topics: Resubscribe to previously subscribed topics.
@@ -634,27 +588,19 @@ def loop_forever(self):
634588
"""Starts a blocking message loop. Use this
635589
method if you want to run a program forever.
636590
Code below a call to this method will NOT execute.
637-
Network reconnection is handled within this call.
591+
592+
NOTE: Network reconnection is not handled within this call and
593+
must be handled by your code for each interface.
638594
639595
"""
640596
while True:
641-
# Check WiFi and socket status
642-
if self.is_sock_connected:
643-
try:
644-
self.loop()
645-
except (RuntimeError, ValueError):
646-
if self._wifi:
647-
# Reconnect the WiFi module and the socket
648-
self.reconnect_wifi()
649-
continue
597+
if self._sock.connected:
598+
self.loop()
650599

651600
def loop(self):
652601
"""Non-blocking message loop. Use this method to
653602
check incoming subscription messages.
654603
655-
This method does NOT handle networking or
656-
network hardware management, use loop_forever
657-
or handle in code instead.
658604
"""
659605
if self._timestamp == 0:
660606
self._timestamp = time.monotonic()
@@ -674,7 +620,6 @@ def _wait_for_msg(self, timeout=30):
674620
"""
675621
res = self._sock.recv(1)
676622
self._sock.settimeout(timeout)
677-
print("RES: ", res)
678623
if res in [None, b""]:
679624
return None
680625
if res == MQTT_PINGRESP:

examples/minimqtt_adafruitio_wifi.py

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,19 @@
1111
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
1212
from adafruit_minimqtt import MQTT
1313

14-
### WiFi ###
1514

16-
# Get wifi details and more from a secrets.py file
15+
# Get Adafruit IO details and more from a secrets.py file
1716
try:
1817
from secrets import secrets
1918
except ImportError:
20-
print("WiFi secrets are kept in secrets.py, please add them there!")
19+
print("Adafruit IO secrets are kept in secrets.py, please add them there!")
2120
raise
2221

23-
# If you are using a board with pre-defined ESP32 Pins:
24-
esp32_cs = DigitalInOut(board.ESP_CS)
25-
esp32_ready = DigitalInOut(board.ESP_BUSY)
26-
esp32_reset = DigitalInOut(board.ESP_RESET)
27-
28-
# If you have an externally connected ESP32:
29-
# esp32_cs = DigitalInOut(board.D9)
30-
# esp32_ready = DigitalInOut(board.D10)
31-
# esp32_reset = DigitalInOut(board.D5)
32-
33-
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
34-
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
35-
"""Use below for Most Boards"""
36-
status_light = neopixel.NeoPixel(
37-
board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
38-
"""Uncomment below for ItsyBitsy M4"""
39-
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
40-
# Uncomment below for an externally defined RGB LED
41-
# import adafruit_rgbled
42-
# from adafruit_esp32spi import PWMOut
43-
# RED_LED = PWMOut.PWMOut(esp, 26)
44-
# GREEN_LED = PWMOut.PWMOut(esp, 27)
45-
# BLUE_LED = PWMOut.PWMOut(esp, 25)
46-
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
47-
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(
48-
esp, secrets, status_light)
22+
cs = DigitalInOut(board.D10)
23+
spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
24+
25+
# Initialize ethernet interface with DHCP
26+
eth = WIZNET5K(spi_bus, cs)
4927

5028
### Feeds ###
5129

@@ -78,15 +56,15 @@ def message(client, topic, message):
7856
print('New message on topic {0}: {1}'.format(topic, message))
7957

8058

81-
# Connect to WiFi
82-
wifi.connect()
59+
# Initialize MQTT interface with the ethernet interface
60+
MQTT.set_socket
8361

8462
# Set up a MiniMQTT Client
85-
mqtt_client = MQTT(socket,
86-
broker='io.adafruit.com',
87-
username=secrets['aio_username'],
88-
password=secrets['aio_key'],
89-
network_manager=wifi)
63+
# NOTE: We'll need to connect insecurely for ethernet configurations.
64+
mqtt_client = MQTT.MQTT(broker = 'io.adafruit.com',
65+
username = secrets['aio_username'],
66+
password = secrets['aio_key'],
67+
is_ssl = False)
9068

9169
# Setup the callback methods above
9270
mqtt_client.on_connect = connected

0 commit comments

Comments
 (0)