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 ESP32SPI #65

Merged
merged 2 commits into from
May 3, 2024
Merged

Fix ESP32SPI #65

merged 2 commits into from
May 3, 2024

Conversation

justmobilize
Copy link
Contributor

@justmobilize justmobilize commented May 1, 2024

Updates to adafruit_azureiot/device_registration.py and adafruit_azureiot/iot_mqtt.py to take passed in ssl_context. This adds support back for ESP32SPI.

Tested a few examples, this was my main testing routine (toggling USE_ONBOARD). Tested using a UM FeatherS3 + AirLift Featherwing (for ESP32SPI tests):

USE_ONBOARD = True

import json
import os
import rtc
import supervisor
import time

import adafruit_connection_manager
import adafruit_logging
import adafruit_ntp
from adafruit_azureiot import IoTCentralDevice


wifi_ssid = os.getenv("CIRCUITPY_WIFI_SSID")
wifi_password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
azure_id_scope = os.getenv("AZURE_ID_SCOPE")
azure_device_id = os.getenv("AZURE_DEVICE_ID")
azure_primary_key = os.getenv("AZURE_PRIMARY_KEY")

print("Connecting to WiFi...")
if USE_ONBOARD:
    print("Using on-board")
    import wifi

    radio = wifi.radio
    radio.connect(wifi_ssid, wifi_password)

else:
    print("Using esp32spi")
    import board
    import busio
    import digitalio
    from adafruit_esp32spi.adafruit_esp32spi import ESP_SPIcontrol

    esp32_cs = digitalio.DigitalInOut(board.D13)
    esp32_ready = digitalio.DigitalInOut(board.D11)
    esp32_reset = digitalio.DigitalInOut(board.D12)
    spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
    radio = ESP_SPIcontrol(spi_bus, esp32_cs, esp32_ready, esp32_reset)
    radio.connect_AP(wifi_ssid, wifi_password)
print("Connected to WiFi!")

pool = adafruit_connection_manager.get_radio_socketpool(radio)
ssl_context = adafruit_connection_manager.get_radio_ssl_context(radio)

def get_time():
    if USE_ONBOARD:
        ntp = adafruit_ntp.NTP(pool)
        rtc.RTC().datetime = ntp.datetime
    else:
        now = radio.get_time()
        now = time.localtime(now[0])
        rtc.RTC().datetime = now
    print(rtc.RTC().datetime)


if time.localtime().tm_year < 2022:
    print("Setting System Time in UTC")
    # asking for time right after connecting the esp32spi often fails
    time.sleep(5)
    get_time()
else:
    print("Year seems good, skipping set time.")

device = IoTCentralDevice(pool, ssl_context, azure_id_scope, azure_device_id, azure_primary_key)

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

azure_clock = 0
i = 0

while True:
    try:
        if azure_clock >= 10:
            i += 1
            print("getting ntp date/time")
            get_time()
            time.sleep(2)
            print("getting msg")
            message = {"value": i, "use_onboard": USE_ONBOARD}
            print(f"sending json: {message}")
            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)

fixes #55

@justmobilize justmobilize marked this pull request as ready for review May 1, 2024 03:02
@justmobilize
Copy link
Contributor Author

@dhalbert and @brentru. Updates as promised

@dhalbert dhalbert requested a review from brentru May 1, 2024 03:24
Copy link
Member

@brentru brentru left a comment

Choose a reason for hiding this comment

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

Test routine and associated code looks great, thanks! I have one packaging question.

@justmobilize
Copy link
Contributor Author

@dhalbert any changes you want?

@dhalbert dhalbert merged commit 077d539 into adafruit:main May 3, 2024
1 check passed
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request May 3, 2024
@justmobilize justmobilize deleted the fix-esp32spi branch May 21, 2024 17:53
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.

Can't use this module on PyPortal or Metro M4 Airlift Lite due to no SSL module
3 participants