adafruit / Adafruit_CircuitPython_MiniMQTT

MQTT Client Library for CircuitPython
Other
72 stars 50 forks source link

M4 Matrix Portal ESP32 set_private_key set_certificate BrokenPipe #220

Closed cashhollister2u closed 1 month ago

cashhollister2u commented 1 month ago

Hey,

I am trying to connect my M4 Matrix Portal via MQTT over port 8883 but I receive a BokenPipeError when setting the private key and certificate. If I remove the functions to set the certificate and key and connect over port 1883 I am able to connect via wifi and mqtt.

import board
import busio
from digitalio import DigitalInOut
import adafruit_connection_manager
import adafruit_requests
from adafruit_esp32spi import adafruit_esp32spi

# conventional 
from os import getenv

# Get wifi details and more from a settings.toml file
# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD
secrets = {
    "ssid": getenv("CIRCUITPY_WIFI_SSID"),
    "password": getenv("CIRCUITPY_WIFI_PASSWORD"),
}

print("ESP32 SPI webclient test")

# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)

# Secondary (SCK1) SPI used to connect to WiFi board on Arduino Nano Connect RP2040
if "SCK1" in dir(board):
    spi = busio.SPI(board.SCK1, board.MOSI1, board.MISO1)
else:
    spi = busio.SPI(board.SCK, board.MOSI, board.MISO)

esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)

# Open and read the .pem file
with open('device_cert.pem', 'r') as pem_file:
    device_cert = pem_file.read()

# Print the contents of the .pem file
print(type(device_cert))

# Open and read the .pem file
with open('device_key.pem', 'r') as pem_file:
    device_key = pem_file.read()

# Print the contents of the .pem file
print(type(device_cert))

try:
    # Set Device Certificate
    esp.set_certificate(device_cert)
    print('cert worked')

except Exception as e:
    print(e)

# Set Private Key
esp.set_private_key(device_key)
-----BEGIN RSA PRIVATE KEY-----

key data ...

-----END RSA PRIVATE KEY-----

-----BEGIN CERTIFICATE-----

cert data ...

-----END CERTIFICATE-----
Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
ESP32 SPI webclient test
<class 'str'>
<class 'str'>
Error response to command
Traceback (most recent call last):
  File "code.py", line 5, in <module>
  File "components/mqtt_connect.py", line 10, in <module>
  File "components/network_connect.py", line 57, in <module>
  File "adafruit_esp32spi/adafruit_esp32spi.py", line 1014, in set_private_key
  File "adafruit_esp32spi/adafruit_esp32spi.py", line 341, in _send_command_get_response
  File "adafruit_esp32spi/adafruit_esp32spi.py", line 308, in _wait_response_cmd
  File "adafruit_esp32spi/adafruit_esp32spi.py", line 283, in _wait_spi_char
BrokenPipeError: Error response to command

Code done running.
justmobilize commented 1 month ago

@cashhollister2u, what version of Circuit Python are you running? And what firmware is running on the WiFi Coprocessor (the example here prints it out to the serial console)?

cashhollister2u commented 1 month ago

@cashhollister2u, what version of Circuit Python are you running? And what firmware is running on the WiFi Coprocessor (the example here prints it out to the serial console)?

@justmobilize, thanks for the quick response here is both my circuit python version and the firmware running on the wifi coprocessor:

3.4.0; CircuitPython 9.0.4 on 2024-04-16
Firmware version: 1.2.2
justmobilize commented 1 month ago

Quite welcome.

The first thing I would recommend is updating the Nina firmware, guide here

cashhollister2u commented 1 month ago

Quite welcome.

The first thing I would recommend is updating the Nina firmware, guide here

@justmobilize Thanks, updating the firmware to latest fixed the issue I am able to get past the BrokenPipeError now.

justmobilize commented 1 month ago

@cashhollister2u awesome! Good to close this issue?

cashhollister2u commented 1 month ago

@cashhollister2u awesome! Good to close this issue?

@justmobilize Yes thanks again!

justmobilize commented 1 month ago

Anytime!