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

Fix SSL and set_socket #63

merged 2 commits into from
Feb 27, 2024

Conversation

justmobilize
Copy link
Contributor

@justmobilize justmobilize commented Feb 25, 2024

Fix SSL and update for removal of MiniMQTT legacy set_socket removal.

fixes: #60

Tested with which is a stripped down version of the learn guide: Getting Started with Microsoft Azure and CircuitPython:

import time
import json
import supervisor
import rtc
import socketpool
import wifi
import adafruit_ntp
from adafruit_azureiot import IoTCentralDevice
import adafruit_logging

logger = adafruit_logging.Logger("log", 0)

try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise

print("Connecting to WiFi...")
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to WiFi!")

pool = socketpool.SocketPool(wifi.radio)
ntp = adafruit_ntp.NTP(pool, tz_offset=-4)
rtc.RTC().datetime = ntp.datetime

if time.localtime().tm_year < 2022:
    print("Setting System Time in UTC")
    rtc.RTC().datetime = ntp.datetime

else:
    print("Year seems good, skipping set time.")

esp = None
pool = socketpool.SocketPool(wifi.radio)
device = IoTCentralDevice(
    pool, esp, secrets["id_scope"], secrets["device_id"], secrets["device_primary_key"], logger=logger,
)

print("Connecting to Azure IoT Central...")
device.connect()
print("Connected to Azure IoT Central!")

azure_clock = 10
i = 0

while True:
    try:
        if azure_clock >= 10:
            i += 1
            print("getting ntp date/time")
            cal = ntp.datetime
            time.sleep(2)
            print("getting msg")
            message = {"value": i}
            print("sending json")
            device.send_telemetry(json.dumps(message))
            print("data sent")
            azure_clock = 0
        else:
            azure_clock += 1
        device.loop()
    except (ValueError, RuntimeError, OSError, ConnectionError) as e:
        print("Network error, reconnecting\n", str(e))
        supervisor.reload()
        continue

    time.sleep(1)
    print(azure_clock)

@@ -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

@@ -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

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

@justmobilize justmobilize marked this pull request as ready for review February 25, 2024 18:23
@tannewt
Copy link
Member

tannewt commented Feb 26, 2024

@BlitzCityDIY Mind looking through this? It looks like you did the learn guide.

@BlitzCityDIY
Copy link
Contributor

sure, i can test. @justmobilize this would require the other updated libraries you've been working on as well?

@justmobilize
Copy link
Contributor Author

@BlitzCityDIY it does not. It's purely a change from the legacy method to the current ones

@BlitzCityDIY BlitzCityDIY merged commit 5421ff8 into adafruit:main Feb 27, 2024
1 check passed
@justmobilize justmobilize deleted the fix-ssl-and-set-socket branch February 29, 2024 14:19
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Mar 5, 2024
Updating https://github.com/adafruit/Adafruit_CircuitPython_FONA to 3.0.4 from 3.0.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_FONA#32 from justmobilize/update-legacy-set-socket-examples

Updating https://github.com/adafruit/Adafruit_CircuitPython_PyPortal to 6.3.3 from 6.3.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_PyPortal#133 from justmobilize/fix-get-radio-ssl-context
  > Merge pull request adafruit/Adafruit_CircuitPython_PyPortal#132 from justmobilize/update-legacy-set-socket-examples

Updating https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k to 5.0.7 from 5.0.6:
  > Merge pull request adafruit/Adafruit_CircuitPython_Wiznet5k#147 from justmobilize/fix-get-radio-ssl-context
  > Merge pull request adafruit/Adafruit_CircuitPython_Wiznet5k#146 from justmobilize/update-legacy-set-socket-examples

Updating https://github.com/adafruit/Adafruit_CircuitPython_AWS_IOT to 2.0.18 from 2.0.17:
  > Merge pull request adafruit/Adafruit_CircuitPython_AWS_IOT#26 from justmobilize/fix-get-radio-ssl-context
  > Merge pull request adafruit/Adafruit_CircuitPython_AWS_IOT#25 from justmobilize/update-legacy-set-socket-examples
  > Merge pull request adafruit/Adafruit_CircuitPython_AWS_IOT#24 from justmobilize/fix-readme-requirements

Updating https://github.com/adafruit/Adafruit_CircuitPython_GC_IOT_Core to 3.2.11 from 3.2.10:
  > Merge pull request adafruit/Adafruit_CircuitPython_GC_IOT_Core#29 from justmobilize/fix-get-radio-ssl-context
  > Merge pull request adafruit/Adafruit_CircuitPython_GC_IOT_Core#28 from justmobilize/update-legacy-set-socket-examples
  > Merge pull request adafruit/Adafruit_CircuitPython_GC_IOT_Core#27 from justmobilize/fix-readme-requirements

Updating https://github.com/adafruit/Adafruit_CircuitPython_AdafruitIO to 5.8.2 from 5.8.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_AdafruitIO#118 from justmobilize/fix-get-radio-ssl-context
  > Merge pull request adafruit/Adafruit_CircuitPython_AdafruitIO#117 from justmobilize/update-legacy-set-socket-examples
  > Merge pull request adafruit/Adafruit_CircuitPython_AdafruitIO#116 from justmobilize/fix-readme-requirements

Updating https://github.com/adafruit/Adafruit_CircuitPython_AzureIoT to 2.5.18 from 2.5.17:
  > Merge pull request adafruit/Adafruit_CircuitPython_AzureIoT#64 from justmobilize/fix-readme-requirements
  > Merge pull request adafruit/Adafruit_CircuitPython_AzureIoT#63 from justmobilize/fix-ssl-and-set-socket

Updating https://github.com/adafruit/Adafruit_CircuitPython_BLE to 10.0.6 from 10.0.5:
  > Merge pull request adafruit/Adafruit_CircuitPython_BLE#193 from dhalbert/deinit-characteristic-buffers

Updating https://github.com/adafruit/Adafruit_CircuitPython_Dash_Dis to 2.1.2 from 2.1.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_Dash_Dis#18 from justmobilize/fix-get-radio-ssl-context
  > Merge pull request adafruit/Adafruit_CircuitPython_Dash_Dis#17 from justmobilize/update-legacy-set-socket-examples

Updating https://github.com/adafruit/Adafruit_CircuitPython_JSON_Stream to 0.8.3 from 0.8.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_JSON_Stream#3 from bablokb/main

Updating https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT to 7.6.3 from 7.6.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#208 from justmobilize/change-free-to-close
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#205 from LelandSindt/main
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#206 from justmobilize/fix-get-radio-ssl-context

Updating https://github.com/adafruit/Adafruit_CircuitPython_OAuth2 to 1.0.17 from 1.0.16:
  > Merge pull request adafruit/Adafruit_CircuitPython_OAuth2#12 from justmobilize/fix-get-radio-ssl-context
  > Merge pull request adafruit/Adafruit_CircuitPython_OAuth2#11 from justmobilize/update-legacy-set-socket-examples
  > Merge pull request adafruit/Adafruit_CircuitPython_OAuth2#10 from justmobilize/fix-readme-requirements

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Updated download stats for the libraries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

IOT Hub requires TLS
3 participants