<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Dynamically assign MQTT subscription topic on AWS</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/98182/dynamically-assign-mqtt-subscription-topic-on-aws</link><description>I try to subscribe to a MQTT topic on AWS using the following code which is used to compose a dynamic MQTT topic based on IMEI 
 
 The output is this (*** for security reasons) 
 
 
 If I now publish a message using AWS MQTT Client the message will not</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 04 Apr 2023 07:27:55 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/98182/dynamically-assign-mqtt-subscription-topic-on-aws" /><item><title>RE: Dynamically assign MQTT subscription topic on AWS</title><link>https://devzone.nordicsemi.com/thread/418948?ContentTypeID=1</link><pubDate>Tue, 04 Apr 2023 07:27:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:af8a856c-c054-4806-8d8e-1b1cdd6fc7b1</guid><dc:creator>&amp;#216;yvind</dc:creator><description>&lt;p&gt;Happy to hear that you got it working!&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Dynamically assign MQTT subscription topic on AWS</title><link>https://devzone.nordicsemi.com/thread/418911?ContentTypeID=1</link><pubDate>Mon, 03 Apr 2023 22:02:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a5ed1e33-60d3-4704-8a16-9823e46151df</guid><dc:creator>thijsfranssen</dc:creator><description>&lt;p&gt;I updated to SDK 2.3, now it works!&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Dynamically assign MQTT subscription topic on AWS</title><link>https://devzone.nordicsemi.com/thread/418702?ContentTypeID=1</link><pubDate>Sat, 01 Apr 2023 18:07:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:179aa051-8ce9-4ece-b00e-312cd25cafef</guid><dc:creator>thijsfranssen</dc:creator><description>&lt;p&gt;Thanks for your effort! Do you know if this problem still exists in later sdk versions?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Dynamically assign MQTT subscription topic on AWS</title><link>https://devzone.nordicsemi.com/thread/418295?ContentTypeID=1</link><pubDate>Thu, 30 Mar 2023 12:19:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b3c3f318-4832-4182-9646-165d59abb7b7</guid><dc:creator>&amp;#216;yvind</dc:creator><description>&lt;p&gt;Thanks for sharing. I am able to reproduce with nRF Connect SDK v2.1.2. Had to rearrange some of the calls, i.e. moved the following to after reading IMEI&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/** Subscribe to customizable non-shadow specific topics
 *  to AWS IoT backend.
 */
err = app_topics_subscribe();
if (err) {
	LOG_ERR(&amp;quot;Adding application specific topics failed, error: %d&amp;quot;, err);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Here is my output:&lt;br /&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;*** Booting Zephyr OS build v3.2.99-ncs2 ***
I: Starting bootloader
I: Primary image: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
I: Secondary image: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
I: Boot source: none
I: Swap type: none
I: Bootloader chainload address offset: 0x10000
I: Jumping to the first image slot
*** Booting Zephyr OS build v3.2.99-ncs2 ***
I: The AWS IoT sample started, version: v1.0.0

I: Modem IMEI: 350457791991456

+CEREG: 2,&amp;quot;08FF&amp;quot;,&amp;quot;0212410C&amp;quot;,7
I: LTE cell changed: Cell ID: 34750732, Tracking area: 2303
+CSCON: 1

I: RRC mode: Connected
+CEREG: 1,&amp;quot;08FF&amp;quot;,&amp;quot;0212410C&amp;quot;,7,,,&amp;quot;11100000&amp;quot;,&amp;quot;11100000&amp;quot;
I: Network registration status: Connected - home network
I: SI: PSM parameter update: TAU: 3240, Active time: -1
ent 350457791991456/settings
I: Sent 350457791991456
I: Sen%XTIME: &amp;quot;80&amp;quot;,&amp;quot;32300321716380&amp;quot;,&amp;quot;01&amp;quot;
t /settings
I: Sent my-custom-topic/example_2
I: Date time obtained
I: Next connection retry in 30 seconds
I: AWS_IOT_EVT_CONNECTING
I: AWS_IOT_EVT_CONNECTED

I: Persistent session enabled
I: Publishing: {
&amp;quot;state&amp;quot;:{
&amp;quot;reported&amp;quot;:{
&amp;quot;app_version&amp;quot;:&amp;quot;v1.0.0&amp;quot;,
&amp;quot;batv&amp;quot;:5117,
&amp;quot;ts&amp;quot;:1680178660424
}
}
} to AWS IoT broker
I: AWS_IOT_EVT_READY
+CEREG: 1,&amp;quot;08FF&amp;quot;,&amp;quot;0212410C&amp;quot;,7,,,&amp;quot;00100001&amp;quot;,&amp;quot;00000110&amp;quot;

I: PSM parameter update: TAU: 3600, Active time: 60
I: AWS_IOT_EVT_DATA_RECEIVED

I: Data received from AWS IoT console: Topic: $aws/things/350457791991456/shadow/get/accepted Message: {
&amp;quot;state&amp;quot;:{
&amp;quot;reported&amp;quot;:{
&amp;quot;app_version&amp;quot;:&amp;quot;v1.0.0&amp;quot;,
&amp;quot;batv&amp;quot;:5117,
&amp;quot;ts&amp;quot;:1680178660424
}
},

&amp;quot;metadata&amp;quot;:{
&amp;quot;reported&amp;quot;:{
&amp;quot;app_version&amp;quot;:{
&amp;quot;timestamp&amp;quot;:1680178660
},
&amp;quot;batv&amp;quot;:{
&amp;quot;timestamp&amp;quot;:1680178660
},
&amp;quot;ts&amp;quot;:{
&amp;quot;timestamp&amp;quot;:1680178660
}
}
},
&amp;quot;version&amp;quot;:272,
&amp;quot;timestamp&amp;quot;:1680178660
}
I: AWS_IOT_EVT_PUBACK, message ID: 22170
+CSCON: 0

I: RRC mode: Idle
+CEREG: 1,&amp;quot;08FF&amp;quot;,&amp;quot;0209E216&amp;quot;,7,,,&amp;quot;00100001&amp;quot;,&amp;quot;00000110&amp;quot;
I: LTE cell changed: Cell ID: 34202134, Tracking area: 2303
+CSCON: 1
I: RRC mode: Connected
I: AWS_IOT_EVT_DATA_RECEIVED
I: Data received from AWS IoT console: Topic: my-custom-topic/example_2 Message: {
&amp;quot;message&amp;quot;:&amp;quot;Hello from AWS IoT console - my-custom-topic/example_2&amp;quot;
}
+CSCON: 0
I: RRC mode: Idle
&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;I am able to subscribe to custom_topic_2, but not topicString&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Dynamically assign MQTT subscription topic on AWS</title><link>https://devzone.nordicsemi.com/thread/418075?ContentTypeID=1</link><pubDate>Wed, 29 Mar 2023 11:04:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9bd8fac5-e5b6-4628-8d53-70cddb8fc50a</guid><dc:creator>thijsfranssen</dc:creator><description>&lt;p&gt;Below is the aws_iot sample as provided with minimal changes.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;main.c&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;/*
 * Copyright (c) 2020 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
 */

#include &amp;lt;zephyr/kernel.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#if defined(CONFIG_NRF_MODEM_LIB)
#include &amp;lt;modem/lte_lc.h&amp;gt;
#include &amp;lt;modem/nrf_modem_lib.h&amp;gt;
#include &amp;lt;modem/modem_info.h&amp;gt;
#include &amp;lt;nrf_modem.h&amp;gt;
#endif
#include &amp;lt;net/aws_iot.h&amp;gt;
#include &amp;lt;zephyr/sys/reboot.h&amp;gt;
#include &amp;lt;date_time.h&amp;gt;
#include &amp;lt;zephyr/dfu/mcuboot.h&amp;gt;
#include &amp;lt;cJSON.h&amp;gt;
#include &amp;lt;cJSON_os.h&amp;gt;
#include &amp;lt;zephyr/logging/log.h&amp;gt;

char imei[32] = {
	&amp;#39;\0&amp;#39;,
};

LOG_MODULE_REGISTER(aws_iot_sample, CONFIG_AWS_IOT_SAMPLE_LOG_LEVEL);

BUILD_ASSERT(!IS_ENABLED(CONFIG_LTE_AUTO_INIT_AND_CONNECT),
	     &amp;quot;This sample does not support LTE auto-init and connect&amp;quot;);

#define APP_TOPICS_COUNT CONFIG_AWS_IOT_APP_SUBSCRIPTION_LIST_COUNT

static struct k_work_delayable shadow_update_work;
static struct k_work_delayable connect_work;
static struct k_work shadow_update_version_work;

static bool cloud_connected;

static K_SEM_DEFINE(lte_connected, 0, 1);
static K_SEM_DEFINE(date_time_obtained, 0, 1);

#if defined(CONFIG_NRF_MODEM_LIB)
NRF_MODEM_LIB_ON_INIT(aws_iot_init_hook, on_modem_lib_init, NULL);

/* Initialized to value different than success (0) */
static int modem_lib_init_result = -1;

static void on_modem_lib_init(int ret, void *ctx)
{
	modem_lib_init_result = ret;
}
#endif

static int json_add_obj(cJSON *parent, const char *str, cJSON *item)
{
	cJSON_AddItemToObject(parent, str, item);

	return 0;
}

static int json_add_str(cJSON *parent, const char *str, const char *item)
{
	cJSON *json_str;

	json_str = cJSON_CreateString(item);
	if (json_str == NULL) {
		return -ENOMEM;
	}

	return json_add_obj(parent, str, json_str);
}

static int json_add_number(cJSON *parent, const char *str, double item)
{
	cJSON *json_num;

	json_num = cJSON_CreateNumber(item);
	if (json_num == NULL) {
		return -ENOMEM;
	}

	return json_add_obj(parent, str, json_num);
}

static int shadow_update(bool version_number_include)
{
	int err;
	char *message;
	int64_t message_ts = 0;
	int16_t bat_voltage = 0;

	err = date_time_now(&amp;amp;message_ts);
	if (err) {
		LOG_ERR(&amp;quot;date_time_now, error: %d&amp;quot;, err);
		return err;
	}

#if defined(CONFIG_NRF_MODEM_LIB)
	/* Request battery voltage data from the modem. */
	err = modem_info_short_get(MODEM_INFO_BATTERY, &amp;amp;bat_voltage);
	if (err != sizeof(bat_voltage)) {
		LOG_ERR(&amp;quot;modem_info_short_get, error: %d&amp;quot;, err);
		return err;
	}
#endif

	cJSON *root_obj = cJSON_CreateObject();
	cJSON *state_obj = cJSON_CreateObject();
	cJSON *reported_obj = cJSON_CreateObject();

	if (root_obj == NULL || state_obj == NULL || reported_obj == NULL) {
		cJSON_Delete(root_obj);
		cJSON_Delete(state_obj);
		cJSON_Delete(reported_obj);
		err = -ENOMEM;
		return err;
	}

	if (version_number_include) {
		err = json_add_str(reported_obj, &amp;quot;app_version&amp;quot;, CONFIG_APP_VERSION);
	} else {
		err = 0;
	}

	err += json_add_number(reported_obj, &amp;quot;batv&amp;quot;, bat_voltage);
	err += json_add_number(reported_obj, &amp;quot;ts&amp;quot;, message_ts);
	err += json_add_obj(state_obj, &amp;quot;reported&amp;quot;, reported_obj);
	err += json_add_obj(root_obj, &amp;quot;state&amp;quot;, state_obj);

	if (err) {
		LOG_ERR(&amp;quot;json_add, error: %d&amp;quot;, err);
		goto cleanup;
	}

	message = cJSON_Print(root_obj);
	if (message == NULL) {
		LOG_ERR(&amp;quot;cJSON_Print, error: returned NULL&amp;quot;);
		err = -ENOMEM;
		goto cleanup;
	}

	struct aws_iot_data tx_data = { .qos = MQTT_QOS_0_AT_MOST_ONCE,
					.topic.type = AWS_IOT_SHADOW_TOPIC_UPDATE,
					.ptr = message,
					.len = strlen(message) };

	LOG_INF(&amp;quot;Publishing: %s to AWS IoT broker&amp;quot;, message);

	err = aws_iot_send(&amp;amp;tx_data);
	if (err) {
		LOG_ERR(&amp;quot;aws_iot_send, error: %d&amp;quot;, err);
	}

	cJSON_FreeString(message);

cleanup:

	cJSON_Delete(root_obj);

	return err;
}

static void connect_work_fn(struct k_work *work)
{
	int err;

	if (cloud_connected) {
		return;
	}

	err = aws_iot_connect(NULL);
	if (err) {
		LOG_ERR(&amp;quot;aws_iot_connect, error: %d&amp;quot;, err);
	}

	LOG_INF(&amp;quot;Next connection retry in %d seconds&amp;quot;, CONFIG_CONNECTION_RETRY_TIMEOUT_SECONDS);

	k_work_schedule(&amp;amp;connect_work, K_SECONDS(CONFIG_CONNECTION_RETRY_TIMEOUT_SECONDS));
}

static void shadow_update_work_fn(struct k_work *work)
{
	int err;

	if (!cloud_connected) {
		return;
	}

	err = shadow_update(false);
	if (err) {
		LOG_ERR(&amp;quot;shadow_update, error: %d&amp;quot;, err);
	}

	LOG_INF(&amp;quot;Next data publication in %d seconds&amp;quot;, CONFIG_PUBLICATION_INTERVAL_SECONDS);

	k_work_schedule(&amp;amp;shadow_update_work, K_SECONDS(CONFIG_PUBLICATION_INTERVAL_SECONDS));
}

static void shadow_update_version_work_fn(struct k_work *work)
{
	int err;

	err = shadow_update(true);
	if (err) {
		LOG_ERR(&amp;quot;shadow_update, error: %d&amp;quot;, err);
	}
}

static void print_received_data(const char *buf, const char *topic, size_t topic_len)
{
	char *str = NULL;
	cJSON *root_obj = NULL;

	root_obj = cJSON_Parse(buf);
	if (root_obj == NULL) {
		LOG_ERR(&amp;quot;cJSON Parse failure&amp;quot;);
		return;
	}

	str = cJSON_Print(root_obj);
	if (str == NULL) {
		LOG_ERR(&amp;quot;Failed to print JSON object&amp;quot;);
		goto clean_exit;
	}

	LOG_INF(&amp;quot;Data received from AWS IoT console: Topic: %.*s Message: %s&amp;quot;, topic_len, topic,
		str);

	cJSON_FreeString(str);

clean_exit:
	cJSON_Delete(root_obj);
}

void aws_iot_event_handler(const struct aws_iot_evt *const evt)
{
	switch (evt-&amp;gt;type) {
	case AWS_IOT_EVT_CONNECTING:
		LOG_INF(&amp;quot;AWS_IOT_EVT_CONNECTING&amp;quot;);
		break;
	case AWS_IOT_EVT_CONNECTED:
		LOG_INF(&amp;quot;AWS_IOT_EVT_CONNECTED&amp;quot;);

		cloud_connected = true;
		/* This may fail if the work item is already being processed,
		 * but in such case, the next time the work handler is executed,
		 * it will exit after checking the above flag and the work will
		 * not be scheduled again.
		 */
		(void)k_work_cancel_delayable(&amp;amp;connect_work);

		if (evt-&amp;gt;data.persistent_session) {
			LOG_INF(&amp;quot;Persistent session enabled&amp;quot;);
		}

#if defined(CONFIG_NRF_MODEM_LIB)
		/** Successfully connected to AWS IoT broker, mark image as
		 *  working to avoid reverting to the former image upon reboot.
		 */
		boot_write_img_confirmed();
#endif

		/** Send version number to AWS IoT broker to verify that the
		 *  FOTA update worked.
		 */
		k_work_submit(&amp;amp;shadow_update_version_work);

		/** Start sequential shadow data updates.
		 */
		k_work_schedule(&amp;amp;shadow_update_work,
				K_SECONDS(CONFIG_PUBLICATION_INTERVAL_SECONDS));

#if defined(CONFIG_NRF_MODEM_LIB)
		int err = lte_lc_psm_req(true);
		if (err) {
			LOG_ERR(&amp;quot;Requesting PSM failed, error: %d&amp;quot;, err);
		}
#endif
		break;
	case AWS_IOT_EVT_READY:
		LOG_INF(&amp;quot;AWS_IOT_EVT_READY&amp;quot;);
		break;
	case AWS_IOT_EVT_DISCONNECTED:
		LOG_INF(&amp;quot;AWS_IOT_EVT_DISCONNECTED&amp;quot;);
		cloud_connected = false;
		/* This may fail if the work item is already being processed,
		 * but in such case, the next time the work handler is executed,
		 * it will exit after checking the above flag and the work will
		 * not be scheduled again.
		 */
		(void)k_work_cancel_delayable(&amp;amp;shadow_update_work);
		k_work_schedule(&amp;amp;connect_work, K_NO_WAIT);
		break;
	case AWS_IOT_EVT_DATA_RECEIVED:
		LOG_INF(&amp;quot;AWS_IOT_EVT_DATA_RECEIVED&amp;quot;);
		print_received_data(evt-&amp;gt;data.msg.ptr, evt-&amp;gt;data.msg.topic.str,
				    evt-&amp;gt;data.msg.topic.len);
		break;
	case AWS_IOT_EVT_PUBACK:
		LOG_INF(&amp;quot;AWS_IOT_EVT_PUBACK, message ID: %d&amp;quot;, evt-&amp;gt;data.message_id);
		break;
	case AWS_IOT_EVT_FOTA_START:
		LOG_INF(&amp;quot;AWS_IOT_EVT_FOTA_START&amp;quot;);
		break;
	case AWS_IOT_EVT_FOTA_ERASE_PENDING:
		LOG_INF(&amp;quot;AWS_IOT_EVT_FOTA_ERASE_PENDING&amp;quot;);
		LOG_INF(&amp;quot;Disconnect LTE link or reboot&amp;quot;);
#if defined(CONFIG_NRF_MODEM_LIB)
		err = lte_lc_offline();
		if (err) {
			LOG_ERR(&amp;quot;Error disconnecting from LTE&amp;quot;);
		}
#endif
		break;
	case AWS_IOT_EVT_FOTA_ERASE_DONE:
		LOG_INF(&amp;quot;AWS_FOTA_EVT_ERASE_DONE&amp;quot;);
		LOG_INF(&amp;quot;Reconnecting the LTE link&amp;quot;);
#if defined(CONFIG_NRF_MODEM_LIB)
		err = lte_lc_connect();
		if (err) {
			LOG_ERR(&amp;quot;Error connecting to LTE&amp;quot;);
		}
#endif
		break;
	case AWS_IOT_EVT_FOTA_DONE:
		LOG_INF(&amp;quot;AWS_IOT_EVT_FOTA_DONE&amp;quot;);
		LOG_INF(&amp;quot;FOTA done, rebooting device&amp;quot;);
		aws_iot_disconnect();
		sys_reboot(0);
		break;
	case AWS_IOT_EVT_FOTA_DL_PROGRESS:
		LOG_INF(&amp;quot;AWS_IOT_EVT_FOTA_DL_PROGRESS, (%d%%)&amp;quot;, evt-&amp;gt;data.fota_progress);
	case AWS_IOT_EVT_ERROR:
		LOG_INF(&amp;quot;AWS_IOT_EVT_ERROR, %d&amp;quot;, evt-&amp;gt;data.err);
		break;
	case AWS_IOT_EVT_FOTA_ERROR:
		LOG_INF(&amp;quot;AWS_IOT_EVT_FOTA_ERROR&amp;quot;);
		break;
	default:
		LOG_WRN(&amp;quot;Unknown AWS IoT event type: %d&amp;quot;, evt-&amp;gt;type);
		break;
	}
}

static void work_init(void)
{
	k_work_init_delayable(&amp;amp;shadow_update_work, shadow_update_work_fn);
	k_work_init_delayable(&amp;amp;connect_work, connect_work_fn);
	k_work_init(&amp;amp;shadow_update_version_work, shadow_update_version_work_fn);
}

#if defined(CONFIG_NRF_MODEM_LIB)
static void lte_handler(const struct lte_lc_evt *const evt)
{
	switch (evt-&amp;gt;type) {
	case LTE_LC_EVT_NW_REG_STATUS:
		if ((evt-&amp;gt;nw_reg_status != LTE_LC_NW_REG_REGISTERED_HOME) &amp;amp;&amp;amp;
		    (evt-&amp;gt;nw_reg_status != LTE_LC_NW_REG_REGISTERED_ROAMING)) {
			break;
		}

		LOG_INF(&amp;quot;Network registration status: %s&amp;quot;,
			evt-&amp;gt;nw_reg_status == LTE_LC_NW_REG_REGISTERED_HOME ?
				&amp;quot;Connected - home network&amp;quot; :
				&amp;quot;Connected - roaming&amp;quot;);

		k_sem_give(&amp;amp;lte_connected);
		break;
	case LTE_LC_EVT_PSM_UPDATE:
		LOG_INF(&amp;quot;PSM parameter update: TAU: %d, Active time: %d&amp;quot;, evt-&amp;gt;psm_cfg.tau,
			evt-&amp;gt;psm_cfg.active_time);
		break;
	case LTE_LC_EVT_EDRX_UPDATE: {
		char log_buf[60];
		ssize_t len;

		len = snprintf(log_buf, sizeof(log_buf), &amp;quot;eDRX parameter update: eDRX: %f, PTW: %f&amp;quot;,
			       evt-&amp;gt;edrx_cfg.edrx, evt-&amp;gt;edrx_cfg.ptw);
		if (len &amp;gt; 0) {
			LOG_INF(&amp;quot;%s&amp;quot;, log_buf);
		}
		break;
	}
	case LTE_LC_EVT_RRC_UPDATE:
		LOG_INF(&amp;quot;RRC mode: %s&amp;quot;,
			evt-&amp;gt;rrc_mode == LTE_LC_RRC_MODE_CONNECTED ? &amp;quot;Connected&amp;quot; : &amp;quot;Idle&amp;quot;);
		break;
	case LTE_LC_EVT_CELL_UPDATE:
		LOG_INF(&amp;quot;LTE cell changed: Cell ID: %d, Tracking area: %d&amp;quot;, evt-&amp;gt;cell.id,
			evt-&amp;gt;cell.tac);
		break;
	default:
		break;
	}
}

static void modem_configure(void)
{
	int err;

	if (IS_ENABLED(CONFIG_LTE_AUTO_INIT_AND_CONNECT)) {
		/* Do nothing, modem is already configured and LTE connected. */
	} else {
		err = lte_lc_init_and_connect_async(lte_handler);
		if (err) {
			LOG_ERR(&amp;quot;Modem could not be configured, error: %d&amp;quot;, err);
			return;
		}
	}
}

static void nrf_modem_lib_dfu_handler(void)
{
	int err;

	err = modem_lib_init_result;

	switch (err)
	{
	case MODEM_DFU_RESULT_OK:
		printk(&amp;quot;Modem update suceeded, reboot\n&amp;quot;);
		sys_reboot(SYS_REBOOT_COLD);
		break;
	case MODEM_DFU_RESULT_UUID_ERROR:
	case MODEM_DFU_RESULT_AUTH_ERROR:
		printk(&amp;quot;Modem update failed, error: %d\n&amp;quot;, err);
		printk(&amp;quot;Modem will use old firmware\n&amp;quot;);
		sys_reboot(SYS_REBOOT_COLD);
		break;
	case MODEM_DFU_RESULT_HARDWARE_ERROR:
	case MODEM_DFU_RESULT_INTERNAL_ERROR:
		printk(&amp;quot;Modem update malfunction, error: %d, reboot\n&amp;quot;, err);
		sys_reboot(SYS_REBOOT_COLD);
		break;
	default:
		break;
	}
}
#endif

static int app_topics_subscribe(void)
{
	int err;
	static char settingsString[20] = &amp;quot;/settings&amp;quot;;
	static char topicString[75];
	strcpy(topicString, imei);
	strcat(topicString, settingsString);
	static char custom_topic_2[75] = &amp;quot;my-custom-topic/example_2&amp;quot;;

	const struct aws_iot_topic_data topics_list[APP_TOPICS_COUNT] = {
		[0].str = topicString,
		[0].len = strlen(topicString),
		[1].str = custom_topic_2,
		[1].len = strlen(custom_topic_2)
	};

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

	return err;
}

static void date_time_event_handler(const struct date_time_evt *evt)
{
	switch (evt-&amp;gt;type) {
	case DATE_TIME_OBTAINED_MODEM:
		/* Fall through */
	case DATE_TIME_OBTAINED_NTP:
		/* Fall through */
	case DATE_TIME_OBTAINED_EXT:
		LOG_INF(&amp;quot;Date time obtained&amp;quot;);
		k_sem_give(&amp;amp;date_time_obtained);

		/* De-register handler. At this point the sample will have
		 * date time to depend on indefinitely until a reboot occurs.
		 */
		date_time_register_handler(NULL);
		break;
	case DATE_TIME_NOT_OBTAINED:
		LOG_INF(&amp;quot;DATE_TIME_NOT_OBTAINED&amp;quot;);
		break;
	default:
		LOG_ERR(&amp;quot;Unknown event: %d&amp;quot;, evt-&amp;gt;type);
		break;
	}
}

void main(void)
{
	int err;

	LOG_INF(&amp;quot;The AWS IoT sample started, version: %s&amp;quot;, CONFIG_APP_VERSION);

	cJSON_Init();

#if defined(CONFIG_NRF_MODEM_LIB)
	nrf_modem_lib_dfu_handler();
#endif

	err = aws_iot_init(NULL, aws_iot_event_handler);
	if (err) {
		LOG_ERR(&amp;quot;AWS IoT library could not be initialized, error: %d&amp;quot;, err);
	}

	/** Subscribe to customizable non-shadow specific topics
	 *  to AWS IoT backend.
	 */
	err = app_topics_subscribe();
	if (err) {
		LOG_ERR(&amp;quot;Adding application specific topics failed, error: %d&amp;quot;, err);
	}

	work_init();
#if defined(CONFIG_NRF_MODEM_LIB)
	modem_configure();

	err = modem_info_init();
	if (err) {
		LOG_ERR(&amp;quot;Failed initializing modem info module, error: %d&amp;quot;, err);
	}
	modem_info_string_get(MODEM_INFO_IMEI, imei, sizeof(imei));
	printk(&amp;quot;Modem IMEI: %s \n&amp;quot;, imei);

	k_sem_take(&amp;amp;lte_connected, K_FOREVER);
#endif

	/* Trigger a date time update. The date_time API is used to timestamp data that is sent
	 * to AWS IoT.
	 */
	date_time_update_async(date_time_event_handler);

	/* Postpone connecting to AWS IoT until date time has been obtained. */
	k_sem_take(&amp;amp;date_time_obtained, K_FOREVER);
	k_work_schedule(&amp;amp;connect_work, K_NO_WAIT);
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;prj.conf (*** changed)&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#
# Copyright (c) 2020 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#
# General config
CONFIG_NCS_SAMPLES_DEFAULTS=y
CONFIG_REBOOT=y

# NEWLIB C
CONFIG_NEWLIB_LIBC=y
CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y

# Log
CONFIG_LOG=y

# Network
CONFIG_NETWORKING=y
CONFIG_NET_NATIVE=n

# LTE link control
CONFIG_LTE_LINK_CONTROL=y
CONFIG_LTE_NETWORK_MODE_LTE_M=y
CONFIG_LTE_AUTO_INIT_AND_CONNECT=n

# Modem library
CONFIG_NRF_MODEM_LIB=y

# AT Host
CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_AT_HOST_LIBRARY=n

# AWS IoT library
CONFIG_AWS_IOT=y
CONFIG_AWS_IOT_CLIENT_ID_STATIC=&amp;quot;351358815046***&amp;quot;
CONFIG_AWS_IOT_BROKER_HOST_NAME=&amp;quot;***.iot.eu-central-1.amazonaws.com&amp;quot;
CONFIG_AWS_IOT_SEC_TAG=***
CONFIG_AWS_IOT_APP_SUBSCRIPTION_LIST_COUNT=2
CONFIG_AWS_IOT_TOPIC_UPDATE_DELTA_SUBSCRIBE=y
CONFIG_AWS_IOT_LAST_WILL=y
CONFIG_AWS_IOT_TOPIC_GET_ACCEPTED_SUBSCRIBE=y
CONFIG_AWS_IOT_TOPIC_GET_REJECTED_SUBSCRIBE=y

# MQTT - Maximum MQTT keepalive timeout specified by AWS IoT Core
CONFIG_MQTT_KEEPALIVE=1200

# Date Time library
CONFIG_DATE_TIME=y

# Modem information
CONFIG_MODEM_INFO=y

# Heap and stacks
CONFIG_HEAP_MEM_POOL_SIZE=8192
CONFIG_MAIN_STACK_SIZE=4096
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048

# MCUBOOT
CONFIG_BOOTLOADER_MCUBOOT=y
CONFIG_MCUBOOT_IMG_MANAGER=y

# Image manager
CONFIG_IMG_MANAGER=y
CONFIG_FLASH=y
CONFIG_IMG_ERASE_PROGRESSIVELY=y

# AWS FOTA
CONFIG_AWS_FOTA=y
CONFIG_FOTA_DOWNLOAD=y
CONFIG_DFU_TARGET=y

# CJSON
CONFIG_CJSON_LIB=y

# Download client (needed by AWS FOTA)
CONFIG_DOWNLOAD_CLIENT=y
CONFIG_DOWNLOAD_CLIENT_STACK_SIZE=4096

## PSM
CONFIG_LTE_PSM_REQ_RPTAU=&amp;quot;00100001&amp;quot;
CONFIG_LTE_PSM_REQ_RAT=&amp;quot;00000000&amp;quot;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Seems not to work for me. This is the output:&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;13:02:12:301 -&amp;gt; *** Booting Zephyr OS build v3.1.99-ncs1-1  ***
13:02:12:306 -&amp;gt; I: The AWS IoT sample started, version: v1.0.0
13:02:12:354 -&amp;gt; Modem IMEI: 351358815046***
13:02:39:360 -&amp;gt; I: LTE cell changed: Cell ID: 6712589, Tracking area: 34101
13:02:39:408 -&amp;gt; I: RRC mode: Connected
13:02:41:089 -&amp;gt; I: Network registration status: Connected - roaming
13:02:41:096 -&amp;gt; I: PSM parameter update: TAU: 3240, Active time: -1
13:02:41:101 -&amp;gt; I: Date time obtained
13:02:41:101 -&amp;gt; I: Next connection retry in 30 seconds
13:02:41:116 -&amp;gt; I: AWS_IOT_EVT_CONNECTING
13:02:44:647 -&amp;gt; I: AWS_IOT_EVT_CONNECTED
13:02:44:647 -&amp;gt; I: Persistent session enabled
13:02:44:647 -&amp;gt; I: Publishing: {
13:02:44:652 -&amp;gt;         &amp;quot;state&amp;quot;:        {
13:02:44:652 -&amp;gt;                 &amp;quot;reported&amp;quot;:     {
13:02:44:652 -&amp;gt;                         &amp;quot;app_version&amp;quot;:  &amp;quot;v1.0.0&amp;quot;,
13:02:44:658 -&amp;gt;                         &amp;quot;batv&amp;quot;: 3316,
13:02:44:658 -&amp;gt;                         &amp;quot;ts&amp;quot;:   1680087764074
13:02:44:658 -&amp;gt;                 }
13:02:44:658 -&amp;gt;         }
13:02:44:658 -&amp;gt; } to AWS IoT broker
13:02:44:673 -&amp;gt; I: AWS_IOT_EVT_READY
13:02:44:977 -&amp;gt; I: AWS_IOT_EVT_DATA_RECEIVED
13:02:45:005 -&amp;gt; E: Failed to print JSON object
13:02:45:005 -&amp;gt; I: AWS_IOT_EVT_PUBACK, message ID: 30604
13:02:57:683 -&amp;gt; I: RRC mode: Idle
13:03:44:651 -&amp;gt; I: Publishing: {
13:03:44:657 -&amp;gt;         &amp;quot;state&amp;quot;:        {
13:03:44:657 -&amp;gt;                 &amp;quot;reported&amp;quot;:     {
13:03:44:657 -&amp;gt;                         &amp;quot;batv&amp;quot;: 3312,
13:03:44:657 -&amp;gt;                         &amp;quot;ts&amp;quot;:   1680087824074
13:03:44:669 -&amp;gt;                 }
13:03:44:669 -&amp;gt;         }
13:03:44:669 -&amp;gt; } to AWS IoT broker
13:03:44:669 -&amp;gt; I: Next data publication in 60 seconds
13:03:44:752 -&amp;gt; I: RRC mode: Connected&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;This is the AWS console MQTT page&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1680087969990v1.png" /&gt;&lt;/p&gt;
&lt;p&gt;To be clear, the other (default) topic works fine:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;13:40:33:455 -&amp;gt; I: RRC mode: Connected
13:40:33:597 -&amp;gt; I: AWS_IOT_EVT_DATA_RECEIVED
13:40:33:597 -&amp;gt; I: Data received from AWS IoT console: Topic: my-custom-topic/example_2 Message: {
13:40:33:603 -&amp;gt;         &amp;quot;message&amp;quot;:      &amp;quot;Hello from AWS IoT console&amp;quot;
13:40:33:618 -&amp;gt; }&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Dynamically assign MQTT subscription topic on AWS</title><link>https://devzone.nordicsemi.com/thread/418027?ContentTypeID=1</link><pubDate>Wed, 29 Mar 2023 08:14:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:17d7bb2b-0f30-4326-b316-524de3ef3f62</guid><dc:creator>&amp;#216;yvind</dc:creator><description>&lt;p&gt;Are you able to provide me the project you are working on, in order to reproduce the issue?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Dynamically assign MQTT subscription topic on AWS</title><link>https://devzone.nordicsemi.com/thread/417829?ContentTypeID=1</link><pubDate>Tue, 28 Mar 2023 08:36:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:55510c64-d839-446a-b9c7-c4d5746a6ac3</guid><dc:creator>thijsfranssen</dc:creator><description>&lt;p&gt;I tried with and without the &amp;quot;\0&amp;quot;, result is the same.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I am using&amp;nbsp;&amp;nbsp;2.1.2&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Dynamically assign MQTT subscription topic on AW</title><link>https://devzone.nordicsemi.com/thread/417802?ContentTypeID=1</link><pubDate>Tue, 28 Mar 2023 07:00:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:569dbe87-956e-4e10-a2fe-6f68ec533454</guid><dc:creator>&amp;#216;yvind</dc:creator><description>&lt;p&gt;Hello,&lt;br /&gt;&lt;br /&gt;What version of the nRF Connect SDK are you using? Could there be an issue with &amp;quot;&lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;\0&amp;quot; &lt;/span&gt;added to end of settingsString?&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;br /&gt;Øyvind&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>