Skip to content
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

Fix SSL and set_socket #63

Merged
merged 2 commits into from
Feb 27, 2024
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
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are added because MiniMQTT will throw when the loop timeout is less then the socket_timeout, which defaults to 1 here


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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed since MiniMQTT is loosing the legacy set_socket


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(),
)
Comment on lines 197 to 207
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • params sorted based on order in MiniMQTT
  • added missing needed is_ssl
  • added socket_pool since there is no more set_socket


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
Loading