aws_iot mqtt_connect and client_connect failing

Hi, I am working on using nrf7002dk to connect to AWS over WiFi. So far I have been able to connect to WiFi and send a HTTP get request through DHCPV4. When I continue to aws_iot library, I have been able to call init, subscribe to topics, and provisioning certificates work (or not return error when they return). But whenever I call aws_iot_connect, I get
<err> aws_iot: mqtt_connect, error: -22 and
<wrn> aws_iot: connect_client failed, error: -7
Looking up the error numbers, I find that -22 stands for Invalid argument, and -7 stands for bad private key. I have tried to format my certificates header according to the library’s example and some posts here on nordic so I am pretty sure my certificates are not wrong. It would be great if I can get some help on the issue! Thank you for reading, and looking forward to any help that may come. Attached are a rough outline of my aws_iot function calls, my certificate header, and my prj.conf file. I have taken out sensitive parts in the code, please let me know if more info is needed!

// Standard library
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <zephyr/kernel.h>

// Zephyr network library
#include <zephyr/net/net_if.h>
#include <zephyr/net/wifi_mgmt.h>
#include <zephyr/net/net_event.h>
#include <zephyr/logging/log.h>
#include <zephyr/net/ethernet.h>
#include <zephyr/net/ethernet_mgmt.h>
#include <zephyr/net/socket.h>
#include <zephyr/net/net_context.h>

// HTTP Module
#include <zephyr/net/http/client.h>

// AWS Module
#include <net/aws_iot.h>
#include "certs.h"

// ---AWS MODULE BEGIN---
static int aws_iot_certificates_provision(void)
{
	static bool certs_added;
	int err;

	if (!IS_ENABLED(CONFIG_NET_SOCKETS_SOCKOPT_TLS) || certs_added) {
		return 0;
	}

	err = tls_credential_add(CONFIG_AWS_IOT_SEC_TAG,
				 TLS_CREDENTIAL_CA_CERTIFICATE,
				 ca_certificate,
				 sizeof(ca_certificate));
	if (err < 0) {
		LOG_ERR("Failed to register CA certificate: %d",
			err);
		return err;
	}

	err = tls_credential_add(CONFIG_AWS_IOT_SEC_TAG,
				 TLS_CREDENTIAL_PRIVATE_KEY,
				 private_key,
				 sizeof(private_key));
	if (err < 0) {
		LOG_ERR("Failed to register private key: %d", err);
		return err;
	}

	err = tls_credential_add(CONFIG_AWS_IOT_SEC_TAG,
				 TLS_CREDENTIAL_SERVER_CERTIFICATE,
				 device_certificate,
				 sizeof(device_certificate));
	if (err < 0) {
		LOG_ERR("Failed to register public certificate: %d", err);
		return err;
	}

	certs_added = true;

	return 0;
}

static bool cloud_connected;

void aws_iot_event_handler(const struct aws_iot_evt *const evt)
{
	switch (evt->type) {
	case AWS_IOT_EVT_CONNECTING:
		LOG_INF("AWS_IOT_EVT_CONNECTING");
		break;
	case AWS_IOT_EVT_CONNECTED:
		LOG_INF("AWS_IOT_EVT_CONNECTED");

		cloud_connected = true;

		if (evt->data.persistent_session) {
			LOG_INF("Persistent session enabled");
		}
		break;
	case AWS_IOT_EVT_READY:
		LOG_INF("AWS_IOT_EVT_READY");
		break;
	case AWS_IOT_EVT_DISCONNECTED:
		LOG_INF("AWS_IOT_EVT_DISCONNECTED");
		cloud_connected = false;
		break;
	case AWS_IOT_EVT_DATA_RECEIVED:
		LOG_INF("AWS_IOT_EVT_DATA_RECEIVED");
		// TODO: Handle received data
		// print_received_data(evt->data.msg.ptr, evt->data.msg.topic.str,
		// 		    evt->data.msg.topic.len);
		break;
	case AWS_IOT_EVT_PUBACK:
		LOG_INF("AWS_IOT_EVT_PUBACK, message ID: %d", evt->data.message_id);
		break;
	case AWS_IOT_EVT_FOTA_START:
		LOG_INF("AWS_IOT_EVT_FOTA_START");
		break;
	case AWS_IOT_EVT_FOTA_ERASE_PENDING:
		LOG_INF("AWS_IOT_EVT_FOTA_ERASE_PENDING");
		LOG_INF("Disconnect LTE link or reboot");
		break;
	case AWS_IOT_EVT_FOTA_ERASE_DONE:
		LOG_INF("AWS_FOTA_EVT_ERASE_DONE");
		LOG_INF("Reconnecting the LTE link");
		break;
	case AWS_IOT_EVT_FOTA_DONE:
		LOG_INF("AWS_IOT_EVT_FOTA_DONE");
		LOG_INF("FOTA done, rebooting device");
		aws_iot_disconnect();
		sys_reboot(0);
		break;
	case AWS_IOT_EVT_FOTA_DL_PROGRESS:
		LOG_INF("AWS_IOT_EVT_FOTA_DL_PROGRESS, (%d%%)", evt->data.fota_progress);
	case AWS_IOT_EVT_ERROR:
		LOG_INF("AWS_IOT_EVT_ERROR, %d", evt->data.err);
		break;
	case AWS_IOT_EVT_FOTA_ERROR:
		LOG_INF("AWS_IOT_EVT_FOTA_ERROR");
		break;
	default:
		LOG_WRN("Unknown AWS IoT event type: %d", evt->type);
		break;
	}
}

static int app_topics_subscribe(char *topic)
{
	int err;

	// Just subscribe to 1 topic for now...
	const struct aws_iot_topic_data topics_list[CONFIG_AWS_IOT_APP_SUBSCRIPTION_LIST_COUNT] = {
		[0].str = topic,
		[0].len = strlen(topic),
	};

	err = aws_iot_subscription_topics_add(topics_list, ARRAY_SIZE(topics_list));
	if (err) {
		LOG_ERR("aws_iot_subscription_topics_add, error: %d", err);
	}

	return err;
}
// ---AWS MODULE END---

int main(void)
{
	LOG_INF("Hello World! %s\n", CONFIG_BOARD);

	// ---AWS MODULE BEGIN---
	LOG_INF("AWS MODULE BEGIN");
	int error = 0;
	error = aws_iot_certificates_provision();
	if (error < 0)
	{
		LOG_ERR("Error while provisioning AWS certificates! err %d", error);
	}

	struct aws_iot_config config = {
		// AWS Configs
		// socket is left blank as it will be set in connect_client(), in aws_iot_connect()
	};

	LOG_INF("AWS IOT initializing");
	error = aws_iot_init(&config, aws_iot_event_handler);
	if (error < 0)
	{
		LOG_ERR("Error while initializing AWS IOT class! err %d", error);
	}

	LOG_INF("Subscribing to one AWS topic!");
	error = app_topics_subscribe("nrf72/6969");
	if (error < 0)
	{
		LOG_ERR("Error subscribing to AWS topic! err %d", error);
	}

	error = aws_iot_connect(&config);
	if (error < 0)
	{
		LOG_ERR("Error connectoing to aws iot! err %d", error);
	}

}
certs.h6740.prj.conf
-Eric

Parents Reply Children
No Data
Related