Skip to content

Commit

Permalink
Merge pull request #63 from justmobilize/fix-ssl-and-set-socket
Browse files Browse the repository at this point in the history
Fix SSL and set_socket
  • Loading branch information
BlitzCityDIY authored Feb 27, 2024
2 parents 99e3d34 + d36a351 commit 5421ff8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ To create an Azure IoT Hub instance or an Azure IoT Central app, you will need a
ESP32 AirLift Networking
========================

*NOTE* currently the ESP32 AirLift is not supported due to the requirment of `ssl`, which is only on boards with native WiFi.

To use this library, you will need to create an ESP32_SPI WifiManager, connected to WiFi. You will also need to set the current time, as this is used to generate time-based authentication keys. One way to do this is with the following code:

.. code-block:: python
Expand Down
14 changes: 7 additions & 7 deletions adafruit_azureiot/device_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def _connect_to_mqtt(self) -> None:
" - device_registration :: connect :: created mqtt client. connecting.."
)
while not self._auth_response_received:
self._mqtt.loop()
self._mqtt.loop(2)

self._logger.info(
" - device_registration :: connect :: on_connect must be fired. Connected ?"
Expand All @@ -139,7 +139,7 @@ def _start_registration(self) -> None:
while self._operation_id is None and retry < 10:
time.sleep(1)
retry += 1
self._mqtt.loop()
self._mqtt.loop(2)

if self._operation_id is None:
raise DeviceRegistrationError(
Expand All @@ -159,7 +159,7 @@ def _wait_for_operation(self) -> None:
while self._hostname is None and retry < 10:
time.sleep(1)
retry += 1
self._mqtt.loop()
self._mqtt.loop(2)

if self._hostname is None:
raise DeviceRegistrationError(
Expand Down Expand Up @@ -194,15 +194,15 @@ def register_device(self, expiry: int) -> str:
"&skn=registration"
)

MQTT.set_socket(self._socket, self._iface)

self._mqtt = MQTT.MQTT(
broker=constants.DPS_END_POINT,
port=8883,
username=username,
password=auth_string,
port=8883,
keep_alive=120,
client_id=self._device_id,
is_ssl=True,
keep_alive=120,
socket_pool=self._socket,
ssl_context=ssl.create_default_context(),
)

Expand Down
10 changes: 5 additions & 5 deletions adafruit_azureiot/iot_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ def _gen_sas_token(self) -> str:
)

def _create_mqtt_client(self) -> None:
MQTT.set_socket(self._socket, self._iface)

log_text = (
f"- iot_mqtt :: _on_connect :: username = {self._username}, password = "
+ f"{self._passwd}"
Expand All @@ -135,11 +133,13 @@ def _create_mqtt_client(self) -> None:

self._mqtts = MQTT.MQTT(
broker=self._hostname,
port=8883,
username=self._username,
password=self._passwd,
port=8883,
keep_alive=120,
client_id=self._device_id,
is_ssl=True,
keep_alive=120,
socket_pool=self._socket,
ssl_context=ssl.create_default_context(),
)

Expand Down Expand Up @@ -457,7 +457,7 @@ def loop(self) -> None:
if not self.is_connected():
return

self._mqtts.loop()
self._mqtts.loop(2)
gc.collect()

def send_device_to_cloud_message(
Expand Down

0 comments on commit 5421ff8

Please sign in to comment.