This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Connecting to AWS IoT through MQTT

Hello guys,

I have nRF9160DK board here with me and the nRF Connect SDK v1.3.0 environment installed. I want to connect to AWS IoT Thing through the MQTT connection.

I used cloud_client example and your documentation from here as a starting point. My AWS IoT device is properly created on the AWS account and certificates are downloaded into nRF0160DK. In addition to that, here is how I modified my prj.conf and Kconfig files of the cloud_client example.

prj.conf:

#
# Copyright (c) 2020 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
#

# General config
CONFIG_NEWLIB_LIBC=y
CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y
CONFIG_RESET_ON_FATAL_ERROR=n
CONFIG_NCS_SAMPLES_DEFAULTS=y

# Log level
# For more verbose and detailed log output, set the log level to
# CONFIG_CLOUD_CLIENT_LOG_LEVEL_DBG=y instead.
CONFIG_CLOUD_CLIENT_LOG_LEVEL_INF=y

# Network
CONFIG_NETWORKING=y
CONFIG_NET_NATIVE=n
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_OFFLOAD=y

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

# BSD library
CONFIG_BSD_LIBRARY=y

# DK
CONFIG_DK_LIBRARY=y

# AT commands interface
CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_AT_HOST_LIBRARY=y

# Generic cloud API
CONFIG_CLOUD_API=y

# nRF Cloud
CONFIG_NRF_CLOUD=n

# AWS IoT
CONFIG_AWS_IOT=y

# Heap and stacks
# Extended memory heap size needed for encoding nRF Cloud messages to JSON
CONFIG_HEAP_MEM_POOL_SIZE=8192
CONFIG_MAIN_STACK_SIZE=4096
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048

Kconfig:

#
# Copyright (c) 2020 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
#

menu "Cloud Client Sample Settings"

config CLOUD_BACKEND
	string "String that selects the cloud backend to be used"
	default "AWS_IOT"

config AWS_IOT_BROKER_HOST_NAME
	string "AWS IoT MQTT broker hostname"
	default "xxxxxxxxxx.amazonaws.com"
	help
	  Default is set to be the nRF Cloud MQTT broker.

config AWS_IOT_CLIENT_ID_STATIC
	string "Client ID"
	default "nrf-IMEI"

config AWS_IOT_SEC_TAG
	int "Security tag for TLS credentials"
	default 168xxxxx

config MQTT_BROKER_PORT
	int "AWS IoT MQTT broker port"
	default 8883	

config CLOUD_MESSAGE
	string "Custom message published periodically to cloud"
	default "{\"state\":{\"reported\":{\"message\":\"Hello Internet of Things!\"}}}"

config CLOUD_MESSAGE_PUBLICATION_INTERVAL
	int "How often the custom message should be published to cloud, in seconds"
	default 10

choice
	prompt "Cloud Publication Trigger"
	default CLOUD_PUBLICATION_BUTTON_PRESS

config CLOUD_PUBLICATION_BUTTON_PRESS
	bool "Trigger publication to cloud upon a button press"

config CLOUD_PUBLICATION_SEQUENTIAL
	bool "Sequential publication to cloud set by the CONFIG_MESSAGE_PUBLICATION_INTERVAL option"

endchoice

config POWER_SAVING_MODE_ENABLE
	bool "Request PSM from cellular network"

endmenu

module = CLOUD_CLIENT
module-str = Cloud client
source "${ZEPHYR_BASE}/subsys/logging/Kconfig.template.log_config"

menu "Zephyr Kernel"
source "Kconfig.zephyr"
endmenu

However, when I try to connect to the cloud I get the following error messages:

E: mqtt_connect, error: -116
E: Failed to connect to cloud, error: -10

Is there anything I am missing here?

An interesting thing is that a few months ago, with ncs v1.1.0 or v1.2.0 I was able to connect to the AWS IoT Cloud by using the same approach (see this thread).

Thanks in advance for your time and efforts.

Sincerely,

Bojan.

  • Hello Bojan,

    E: Failed to connect to cloud, error: -10

    is probably just a following error from the previous:

    E: mqtt_connect, error: -116,

    so let's focus on that one for now.

    116 probably refers to ETIMEDOUT. I asked a colleague of mine, and he said that he had seen that a couple of times if the SIM card was out of data. Since this is fairly easy to check, can you either try to refill the SIM card, or to try another SIM that you know has more data left?

    Best regards,

    Edvin

  • Hey @Edvin!

    Thanks for sharing that info with me! That also was on my TODO list ! :-)

    I am using a SIM card that came with nRF9160DK board (iBASIS eSIM). Where can I actually check how much data is left and how can I recharge the card?

    Regards,

    Bojan.

  • Hello Bojan,

    If you are using the iBasis Sim card you can check it on nRFCloud. Log in with the same account that you used to activate the sim card. 

    Then go to the SIM CARDS tab to the left and you should see the status of your sim cards. Click refresh beneath the SIM card you are using to make sure the information is up to date. You can use the "+ Buy Data" option to refill the SIM Card:

    NB: I am not sure whether it is still like this, but it was at some point in time.

    If you buy 10MB for an iBasis SIM card, you top it up to 10MB. This means that if you have 5MB left, and buy 10MB, you will only actually receive 5 new MB, and the new remaining data will be 10MB in total. I am not sure if this has changed or not, but if you are all out, there is no loss in buying 10MB :-) 

    BR,

    Edvin

  • Just checked, ... My SIM card was indeed out of data!

    I recharged the card and voila! I was able to communicate with the Thing on AWS IoT Cloud!

    Thanks once again for your help!

    Cheers,

    Bojan.

Related