adafruit / Adafruit_CircuitPython_AzureIoT

Access to Microsoft Azure IoT device, messaging, and job services from CircuitPython!
MIT License
20 stars 15 forks source link

Fix SSL and set_socket #63

Closed justmobilize closed 8 months ago

justmobilize commented 9 months ago

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

fixes: https://github.com/adafruit/Adafruit_CircuitPython_AzureIoT/issues/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)
tannewt commented 9 months ago

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

BlitzCityDIY commented 9 months ago

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

justmobilize commented 9 months ago

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