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

How to send data to own AWS server using Cloud Client sample ?

HI All

Actually I need to send GPS data to own AWS server so from here I setup AWS server and use cloud client sample and follow the step 

like 

1) change the cloud backend to the AWS_IOT

2) Enable AWS IOT setting and added ID, HOSTNAME and SEC TAG.

3) Updated the modem certificates.

but get an error.

this my Kconfig 

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

menu "Cloud Client Sample Settings"

config CLOUD_BACKEND
	string "Selects the cloud backend to be used"
	default "AWS_IOT"
	help
	  Possible values are "NRF_CLOUD", "AWS_IOT", "AZURE_IOT_HUB".

config AWS_IOT_BROKER_HOST_NAME
	string "AWS IoT MQTT broker hostname"
	default "a6i4ulevbqzm-ats.iot.us-east-2.amazonaws.com"
	help
	  Default is set to be the nRF Cloud MQTT broker.	  

config AWS_IOT_CLIENT_ID_STATIC
	string "Client ID"
	default "nrf-352656101080789"
	
config AWS_IOT_SEC_TAG
	int "Security tag for TLS credentials"
	default 111
	
config MQTT_BROKER_PORT
	int "AWS IoT MQTT broker port"
	default 8883
	  
config CLOUD_MESSAGE
	string "Sets the custom message published periodically to cloud"
	default "{\"state\":{\"reported\":{\"message\":\"Hello Internet of Things!\"}}}"

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

config CLOUD_CONNECTION_RETRY_TIMEOUT_SECONDS
	int "Sets the number of seconds between each cloud connection retry"
	default 30

choice
	prompt "Cloud Publication Trigger"
	default CLOUD_PUBLICATION_BUTTON_PRESS

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

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

endchoice

config POWER_SAVING_MODE_ENABLE
	bool "Requests 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

this my prj.config

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

# Selected cloud backend
CONFIG_CLOUD_BACKEND="AWS_IOT"

# nRF Cloud
CONFIG_NRF_CLOUD=n
# CONFIG_NRF_CLOUD_CONNECTION_POLL_THREAD=y

# AWS IoT
CONFIG_AWS_IOT=y
CONFIG_AWS_IOT_CLIENT_ID_STATIC="nrf-352656101080789"
CONFIG_AWS_IOT_BROKER_HOST_NAME="a6i4ulevbqzm-ats.iot.us-east-2.amazonaws.com"
CONFIG_AWS_IOT_SEC_TAG=111
CONFIG_AWS_IOT_TOPIC_UPDATE_DELTA_SUBSCRIBE=y
CONFIG_AWS_IOT_CONNECTION_POLL_THREAD=y

# Azure IoT Hub
# CONFIG_AZURE_IOT_HUB=y
# CONFIG_AZURE_IOT_HUB_DEVICE_ID="my-device"
# CONFIG_AZURE_IOT_HUB_HOSTNAME="example.endpoint.com"
# CONFIG_AZURE_IOT_HUB_SEC_TAG=10

# 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

is there any i need to change?

Thanks & Regards

Iand Prj.config file

Parents Reply Children
  • HI Oyvind

    Thanks you for your reply

    you able to solve last issue from the support case you linked to?

    I am Using AWS_IOT sample code Yes I am able to connect AWS server and received data on Link monitor like this 

    2020-12-25T04:05:22.610Z DEBUG modem << Next connection retry in 30 seconds
    2020-12-25T04:05:22.614Z DEBUG modem << AWS_IOT_EVT_CONNECTING
    2020-12-25T04:05:22.755Z DEBUG modem << RRC mode: Connected
    2020-12-25T04:05:26.959Z DEBUG modem << AWS_IOT_EVT_CONNECTED
    2020-12-25T04:05:27.091Z DEBUG modem << Publishing: {
    2020-12-25T04:05:27.094Z DEBUG modem << "state":{
    2020-12-25T04:05:27.095Z DEBUG modem << "reported":{
    2020-12-25T04:05:27.195Z DEBUG modem << "app_version":"v1.0.0",
    2020-12-25T04:05:27.198Z DEBUG modem << "batv":4410,
    2020-12-25T04:05:27.199Z DEBUG modem << "ts":943920514411
    2020-12-25T04:05:27.201Z DEBUG modem << }
    2020-12-25T04:05:27.202Z DEBUG modem << }
    2020-12-25T04:05:27.203Z DEBUG modem << } to AWS IoT broker
    2020-12-25T04:05:27.547Z DEBUG modem << AWS_IOT_EVT_READY
    2020-12-25T04:05:27.957Z DEBUG modem << AWS_IOT_EVT_DATA_RECEIVED
    2020-12-25T04:05:28.091Z DEBUG modem << Data received from AWS IoT console:
    2020-12-25T04:05:28.094Z DEBUG modem << Topic: $aws/things/nrf-352656101080789/shadow/get/accepted
    2020-12-25T04:05:28.096Z DEBUG modem << Message: {
    2020-12-25T04:05:28.098Z DEBUG modem << "state":{
    2020-12-25T04:05:28.101Z DEBUG modem << "desired":{
    2020-12-25T04:05:28.103Z DEBUG modem << "welcome":"aws-iot"
    2020-12-25T04:05:28.105Z DEBUG modem << },
    2020-12-25T04:05:28.107Z DEBUG modem << "reported":{
    2020-12-25T04:05:28.109Z DEBUG modem << "welcome":"aws-iot",
    2020-12-25T04:05:28.112Z DEBUG modem << "app_version":"v1.0.0",
    2020-12-25T04:05:28.115Z DEBUG modem << "batv":4410,
    2020-12-25T04:05:28.117Z DEBUG modem << "ts":943920514411
    2020-12-25T04:05:28.119Z DEBUG modem << }
    2020-12-25T04:05:28.121Z DEBUG modem << },
    2020-12-25T04:05:28.123Z DEBUG modem << "metadata":{
    2020-12-25T04:05:28.125Z DEBUG modem << "desired":{
    2020-12-25T04:05:28.127Z DEBUG modem << "welcome":{
    2020-12-25T04:05:28.129Z DEBUG modem << "timestamp":1608782408
    2020-12-25T04:05:28.131Z DEBUG modem << }
    2020-12-25T04:05:28.133Z DEBUG modem << },
    2020-12-25T04:05:28.135Z DEBUG modem << "reported":{
    2020-12-25T04:05:28.137Z DEBUG modem << "welcome":{
    2020-12-25T04:05:28.139Z DEBUG modem << "timestamp":1608782408
    2020-12-25T04:05:28.142Z DEBUG modem << },
    2020-12-25T04:05:28.144Z DEBUG modem << "app_version":{
    2020-12-25T04:05:28.146Z DEBUG modem << "timestamp":1608869128
    2020-12-25T04:05:28.148Z DEBUG modem << },
    2020-12-25T04:05:28.150Z DEBUG modem << "batv":{
    2020-12-25T04:05:28.152Z DEBUG modem << "timestamp":1608869128
    2020-12-25T04:05:28.154Z DEBUG modem << },
    2020-12-25T04:05:28.156Z DEBUG modem << "ts":{
    2020-12-25T04:05:28.158Z DEBUG modem << "timestamp":1608869128
    2020-12-25T04:05:28.161Z DEBUG modem << }
    2020-12-25T04:05:28.163Z DEBUG modem << }
    2020-12-25T04:05:28.165Z DEBUG modem << },
    2020-12-25T04:05:28.167Z DEBUG modem << "version":8,
    2020-12-25T04:05:28.169Z DEBUG modem << "timestamp":1608869128
    2020-12-25T04:05:28.171Z DEBUG modem << }
    2020-12-25T04:05:34.359Z DEBUG modem << RRC mode: Idle
    2020-12-25T04:06:06.537Z DEBUG modem << LTE cell changed: Cell ID: 33905667, Tracking area: 33794
    2020-12-25T04:06:26.977Z DEBUG modem << Publishing: {
    2020-12-25T04:06:26.982Z DEBUG modem << "state":{
    2020-12-25T04:06:26.986Z DEBUG modem << "reported":{
    2020-12-25T04:06:26.990Z DEBUG modem << "batv":4406,
    2020-12-25T04:06:27.154Z DEBUG modem << "ts":943920574411
    2020-12-25T04:06:27.158Z DEBUG modem << }
    2020-12-25T04:06:27.160Z DEBUG modem << }

    Now I want to send GPS data to my AWS server so there are 2 ways

    1) Is need to change in AGPS sample So question is where i need to changes in endpoint for AWS and certificate.

    2) Or Need to change in AWS_IOT sample who already connected to my AWS server So question is where can I put my GPS(Latitude, Longitude, Altitude) data for publish to AWS server. 

    And again Thank you for your valuable time.

    Regards
    Jaydip

  • Hi Jaydip, 

    Yes, you should be able to combine the AGPS and AWS_IOT sample. The AGPS sample uses a cloud back end which currently points to nRF Cloud. You will need to change this to AWS. Please see this ticket.

    Kind regards,
    Øyvind

  • Hi Oyvind

    Thanks for you valuable time and replay.

    I run normal GPS and AWS_IOT example separately and both work fine.

    Now merge GPS with AWS_IOT example. with this file

    main.c, prj.config, kconfig, sample yaml. Build successfully run

    Just need to know about modem configuration for this

    Try this step

    1 init_app --> Setup Modem --> At commads

    2 Modem configuration for lte 

    Error is could not send AT commands -8

    this is main.c

    /*
     * Copyright (c) 2019 Nordic Semiconductor ASA
     *
     * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
     */
    
    #include <zephyr.h>
    #include <nrf_socket.h>
    #include <net/socket.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #include <modem/lte_lc.h>
    #include <modem/bsdlib.h>
    #include <modem/at_cmd.h>
    #include <modem/at_notif.h>
    #include <modem/modem_info.h>
    #include <bsd.h>
    
    #include <net/aws_iot.h>
    #include <power/reboot.h>
    #include <date_time.h>
    #include <dfu/mcuboot.h>
    #include <cJSON.h>
    #include <cJSON_os.h>
    
    #ifdef CONFIG_SUPL_CLIENT_LIB
    #include <supl_os_client.h>
    #include <supl_session.h>
    #include "supl_support.h"
    #endif
    
    #define AT_XSYSTEMMODE      "AT\%XSYSTEMMODE=1,0,1,0"
    #define AT_ACTIVATE_GPS     "AT+CFUN=31"
    #define AT_ACTIVATE_LTE     "AT+CFUN=21"
    #define AT_DEACTIVATE_LTE   "AT+CFUN=20"
    
    #define GNSS_INIT_AND_START 1
    #define GNSS_STOP           2
    #define GNSS_RESTART        3
    
    #define AT_CMD_SIZE(x) (sizeof(x) - 1)
    
    #ifdef CONFIG_BOARD_NRF9160DK_NRF9160NS
    #define AT_MAGPIO      "AT\%XMAGPIO=1,0,0,1,1,1574,1577"
    #ifdef CONFIG_GPS_SAMPLE_ANTENNA_ONBOARD
    #define AT_COEX0       "AT\%XCOEX0=1,1,1565,1586"
    #elif CONFIG_GPS_SAMPLE_ANTENNA_EXTERNAL
    #define AT_COEX0       "AT\%XCOEX0"
    #endif
    #endif /* CONFIG_BOARD_NRF9160DK_NRF9160NS */
    
    #ifdef CONFIG_BOARD_THINGY91_NRF9160NS
    #define AT_MAGPIO      "AT\%XMAGPIO=1,1,1,7,1,746,803,2,698,748,2,1710,2200," \
    			"3,824,894,4,880,960,5,791,849,7,1565,1586"
    #ifdef CONFIG_GPS_SAMPLE_ANTENNA_ONBOARD
    #define AT_COEX0       "AT\%XCOEX0=1,1,1565,1586"
    #elif CONFIG_GPS_SAMPLE_ANTENNA_EXTERNAL
    #define AT_COEX0       "AT\%XCOEX0"
    #endif
    #endif /* CONFIG_BOARD_THINGY91_NRF9160NS */
    
    static const char update_indicator[] = {'\\', '|', '/', '-'};
    static const char *const at_commands[] = {
    	AT_XSYSTEMMODE,
    #if defined(CONFIG_BOARD_NRF9160DK_NRF9160NS) || \
    	defined(CONFIG_BOARD_THINGY91_NRF9160NS)
    	AT_MAGPIO,
    	AT_COEX0,
    #endif
    	AT_ACTIVATE_GPS
    };
    
    static int                   gnss_fd;
    static char                  nmea_strings[10][NRF_GNSS_NMEA_MAX_LEN];
    static uint32_t                 nmea_string_cnt;
    
    static bool                  got_fix;
    static uint64_t                 fix_timestamp;
    static nrf_gnss_data_frame_t last_pvt;
    
    K_SEM_DEFINE(lte_ready, 0, 1);
    
    BUILD_ASSERT(!IS_ENABLED(CONFIG_LTE_AUTO_INIT_AND_CONNECT),
    		"This sample does not support LTE auto-init and connect");
    
    #define APP_TOPICS_COUNT CONFIG_AWS_IOT_APP_SUBSCRIPTION_LIST_COUNT
    
    /* Timeout in seconds in which the application will wait for an initial event
     * from the date time library.
     */
    #define DATE_TIME_TIMEOUT_S 15
    
    static struct k_delayed_work shadow_update_work;
    static struct k_delayed_work connect_work;
    static struct k_delayed_work shadow_update_version_work;
    
    K_SEM_DEFINE(lte_connected, 0, 1);
    K_SEM_DEFINE(date_time_obtained, 0, 1);
    
    void bsd_recoverable_error_handler(uint32_t error)
    {
    	printf("Err: %lu\n", (unsigned long)error);
    }
    
    static int setup_modem(void)
    {
    	for (int i = 0; i < ARRAY_SIZE(at_commands); i++) {
    
    		if (at_cmd_write(at_commands[i], NULL, 0, NULL) != 0) {
    			return -1;
    		}
    	}
    
    	return 0;
    }
    
    #ifdef CONFIG_SUPL_CLIENT_LIB
    /* Accepted network statuses read from modem */
    static const char status1[] = "+CEREG: 1";
    static const char status2[] = "+CEREG:1";
    static const char status3[] = "+CEREG: 5";
    static const char status4[] = "+CEREG:5";
    
    static void wait_for_lte(void *context, const char *response)
    {
    	if (!memcmp(status1, response, AT_CMD_SIZE(status1)) ||
    		!memcmp(status2, response, AT_CMD_SIZE(status2)) ||
    		!memcmp(status3, response, AT_CMD_SIZE(status3)) ||
    		!memcmp(status4, response, AT_CMD_SIZE(status4))) {
    		k_sem_give(&lte_ready);
    	}
    }
    
    static int activate_lte(bool activate)
    {
    	if (activate) {
    		if (at_cmd_write(AT_ACTIVATE_LTE, NULL, 0, NULL) != 0) {
    			return -1;
    		}
    
    		at_notif_register_handler(NULL, wait_for_lte);
    		if (at_cmd_write("AT+CEREG=2", NULL, 0, NULL) != 0) {
    			return -1;
    		}
    
    		k_sem_take(&lte_ready, K_FOREVER);
    
    		at_notif_deregister_handler(NULL, wait_for_lte);
    		if (at_cmd_write("AT+CEREG=0", NULL, 0, NULL) != 0) {
    			return -1;
    		}
    	} else {
    		if (at_cmd_write(AT_DEACTIVATE_LTE, NULL, 0, NULL) != 0) {
    			return -1;
    		}
    	}
    
    	return 0;
    }
    #endif
    
    static int gnss_ctrl(uint32_t ctrl)
    {
    	int retval;
    
    	nrf_gnss_fix_retry_t    fix_retry    = 0;
    	nrf_gnss_fix_interval_t fix_interval = 1;
    	nrf_gnss_delete_mask_t	delete_mask  = 0;
    	nrf_gnss_nmea_mask_t	nmea_mask    = NRF_GNSS_NMEA_GSV_MASK |
    					       NRF_GNSS_NMEA_GSA_MASK |
    					       NRF_GNSS_NMEA_GLL_MASK |
    					       NRF_GNSS_NMEA_GGA_MASK |
    					       NRF_GNSS_NMEA_RMC_MASK;
    
    	if (ctrl == GNSS_INIT_AND_START) {
    		gnss_fd = nrf_socket(NRF_AF_LOCAL,
    				     NRF_SOCK_DGRAM,
    				     NRF_PROTO_GNSS);
    
    		if (gnss_fd >= 0) {
    			printk("GPS Socket created\n");
    		} else {
    			printk("Could not init socket (err: %d)\n", gnss_fd);
    			return -1;
    		}
    
    		retval = nrf_setsockopt(gnss_fd,
    					NRF_SOL_GNSS,
    					NRF_SO_GNSS_FIX_RETRY,
    					&fix_retry,
    					sizeof(fix_retry));
    		if (retval != 0) {
    			printk("Failed to set fix retry value\n");
    			return -1;
    		}
    
    		retval = nrf_setsockopt(gnss_fd,
    					NRF_SOL_GNSS,
    					NRF_SO_GNSS_FIX_INTERVAL,
    					&fix_interval,
    					sizeof(fix_interval));
    		if (retval != 0) {
    			printk("Failed to set fix interval value\n");
    			return -1;
    		}
    
    		retval = nrf_setsockopt(gnss_fd,
    					NRF_SOL_GNSS,
    					NRF_SO_GNSS_NMEA_MASK,
    					&nmea_mask,
    					sizeof(nmea_mask));
    		if (retval != 0) {
    			printk("Failed to set nmea mask\n");
    			return -1;
    		}
    	}
    
    	if ((ctrl == GNSS_INIT_AND_START) ||
    	    (ctrl == GNSS_RESTART)) {
    		retval = nrf_setsockopt(gnss_fd,
    					NRF_SOL_GNSS,
    					NRF_SO_GNSS_START,
    					&delete_mask,
    					sizeof(delete_mask));
    		if (retval != 0) {
    			printk("Failed to start GPS\n");
    			return -1;
    		}
    	}
    
    	if (ctrl == GNSS_STOP) {
    		retval = nrf_setsockopt(gnss_fd,
    					NRF_SOL_GNSS,
    					NRF_SO_GNSS_STOP,
    					&delete_mask,
    					sizeof(delete_mask));
    		if (retval != 0) {
    			printk("Failed to stop GPS\n");
    			return -1;
    		}
    	}
    
    	return 0;
    }
    
    static int init_app(void)
    {
    	int retval;
    
    
    	if (setup_modem() != 0) {
    		printk("Failed to initialize modem\n");
    		return -1;
    	}
    
    	retval = gnss_ctrl(GNSS_INIT_AND_START);
    
    	return retval;
    }
    
    static void print_satellite_stats(nrf_gnss_data_frame_t *pvt_data)
    {
    	uint8_t  tracked          = 0;
    	uint8_t  in_fix           = 0;
    	uint8_t  unhealthy        = 0;
    
    	for (int i = 0; i < NRF_GNSS_MAX_SATELLITES; ++i) {
    
    		if ((pvt_data->pvt.sv[i].sv > 0) &&
    		    (pvt_data->pvt.sv[i].sv < 33)) {
    
    			tracked++;
    
    			if (pvt_data->pvt.sv[i].flags &
    					NRF_GNSS_SV_FLAG_USED_IN_FIX) {
    				in_fix++;
    			}
    
    			if (pvt_data->pvt.sv[i].flags &
    					NRF_GNSS_SV_FLAG_UNHEALTHY) {
    				unhealthy++;
    			}
    		}
    	}
    
    	printk("Tracking: %d Using: %d Unhealthy: %d\n", tracked,
    							 in_fix,
    							 unhealthy);
    }
    
    static void print_gnss_stats(nrf_gnss_data_frame_t *pvt_data)
    {
    	if (pvt_data->pvt.flags & NRF_GNSS_PVT_FLAG_DEADLINE_MISSED) {
    		printk("GNSS notification deadline missed\n");
    	}
    	if (pvt_data->pvt.flags & NRF_GNSS_PVT_FLAG_NOT_ENOUGH_WINDOW_TIME) {
    		printk("GNSS operation blocked by insufficient time windows\n");
    	}
    }
    
    static void print_fix_data(nrf_gnss_data_frame_t *pvt_data)
    {
    	printf("Longitude:  %f\n", pvt_data->pvt.longitude);
    	printf("Latitude:   %f\n", pvt_data->pvt.latitude);
    	printf("Altitude:   %f\n", pvt_data->pvt.altitude);
    	printf("Speed:      %f\n", pvt_data->pvt.speed);
    	printf("Heading:    %f\n", pvt_data->pvt.heading);
    	printk("Date:       %02u-%02u-%02u\n", pvt_data->pvt.datetime.year,
    					       pvt_data->pvt.datetime.month,
    					       pvt_data->pvt.datetime.day);
    	printk("Time (UTC): %02u:%02u:%02u\n", pvt_data->pvt.datetime.hour,
    					       pvt_data->pvt.datetime.minute,
    					      pvt_data->pvt.datetime.seconds);
    }
    
    static void print_nmea_data(void)
    {
    	for (int i = 0; i < nmea_string_cnt; ++i) {
    		printk("%s", nmea_strings[i]);
    	}
    }
    
    int process_gps_data(nrf_gnss_data_frame_t *gps_data)
    {
    	int retval;
    
    	retval = nrf_recv(gnss_fd,
    			  gps_data,
    			  sizeof(nrf_gnss_data_frame_t),
    			  NRF_MSG_DONTWAIT);
    
    	if (retval > 0) {
    
    		switch (gps_data->data_id) {
    		case NRF_GNSS_PVT_DATA_ID:
    			memcpy(&last_pvt,
    			       gps_data,
    			       sizeof(nrf_gnss_data_frame_t));
    			nmea_string_cnt = 0;
    			got_fix = false;
    
    			if ((gps_data->pvt.flags &
    				NRF_GNSS_PVT_FLAG_FIX_VALID_BIT)
    				== NRF_GNSS_PVT_FLAG_FIX_VALID_BIT) {
    
    				got_fix = true;
    				fix_timestamp = k_uptime_get();
    			}
    			break;
    
    		case NRF_GNSS_NMEA_DATA_ID:
    			if (nmea_string_cnt < 10) {
    				memcpy(nmea_strings[nmea_string_cnt++],
    				       gps_data->nmea,
    				       retval);
    			}
    			break;
    
    		case NRF_GNSS_AGPS_DATA_ID:
    #ifdef CONFIG_SUPL_CLIENT_LIB
    			printk("\033[1;1H");
    			printk("\033[2J");
    			printk("New AGPS data requested, contacting SUPL server, flags %d\n",
    			       gps_data->agps.data_flags);
    			gnss_ctrl(GNSS_STOP);
    			activate_lte(true);
    			printk("Established LTE link\n");
    			if (open_supl_socket() == 0) {
    				printf("Starting SUPL session\n");
    				supl_session(&gps_data->agps);
    				printk("Done\n");
    				close_supl_socket();
    			}
    			activate_lte(false);
    			gnss_ctrl(GNSS_RESTART);
    			k_sleep(K_MSEC(2000));
    #endif
    			break;
    
    		default:
    			break;
    		}
    	}
    
    	return retval;
    }
    
    #ifdef CONFIG_SUPL_CLIENT_LIB
    int inject_agps_type(void *agps,
    		     size_t agps_size,
    		     nrf_gnss_agps_data_type_t type,
    		     void *user_data)
    {
    	ARG_UNUSED(user_data);
    	int retval = nrf_sendto(gnss_fd,
    				agps,
    				agps_size,
    				0,
    				&type,
    				sizeof(type));
    
    	if (retval != 0) {
    		printk("Failed to send AGNSS data, type: %d (err: %d)\n",
    		       type,
    		       errno);
    		return -1;
    	}
    
    	printk("Injected AGPS data, flags: %d, size: %d\n", type, agps_size);
    
    	return 0;
    }
    #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;
    	int16_t bat_voltage = 0;
    
    	err = date_time_now(&message_ts);
    	if (err) {
    		printk("date_time_now, error: %d\n", err);
    		return err;
    	}
    
    #if defined(CONFIG_BSD_LIBRARY)
    	/* Request battery voltage data from the modem. */
    	err = modem_info_short_get(MODEM_INFO_BATTERY, &bat_voltage);
    	if (err != sizeof(bat_voltage)) {
    		printk("modem_info_short_get, error: %d\n", 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, "app_version",
    				    CONFIG_APP_VERSION);
    	} else {
    		err = 0;
    	}
    
    	err += json_add_number(reported_obj, "batv", bat_voltage);
    	err += json_add_number(reported_obj, "ts", message_ts);
    	err += json_add_obj(state_obj, "reported", reported_obj);
    	err += json_add_obj(root_obj, "state", state_obj);
    
    	if (err) {
    		printk("json_add, error: %d\n", err);
    		goto cleanup;
    	}
    
    	message = cJSON_Print(root_obj);
    	if (message == NULL) {
    		printk("cJSON_Print, error: returned NULL\n");
    		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)
    	};
    
    	printk("Publishing: %s to AWS IoT broker\n", message);
    
    	err = aws_iot_send(&tx_data);
    	if (err) {
    		printk("aws_iot_send, error: %d\n", err);
    	}
    
    	cJSON_FreeString(message);
    
    cleanup:
    
    	cJSON_Delete(root_obj);
    
    	return err;
    }
    
    static void connect_work_fn(struct k_work *work)
    {
    	int err;
    
    	err = aws_iot_connect(NULL);
    	if (err) {
    		printk("aws_iot_connect, error: %d\n", err);
    	}
    
    	printk("Next connection retry in %d seconds\n",
    	       CONFIG_CONNECTION_RETRY_TIMEOUT_SECONDS);
    
    	k_delayed_work_submit(&connect_work,
    			K_SECONDS(CONFIG_CONNECTION_RETRY_TIMEOUT_SECONDS));
    }
    
    static void shadow_update_work_fn(struct k_work *work)
    {
    	int err;
    
    	err = shadow_update(false);
    	if (err) {
    		printk("shadow_update, error: %d\n", err);
    	}
    
    	printk("Next data publication in %d seconds\n",
    	       CONFIG_PUBLICATION_INTERVAL_SECONDS);
    
    	k_delayed_work_submit(&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) {
    		printk("shadow_update, error: %d\n", 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) {
    		printk("cJSON Parse failure");
    		return;
    	}
    
    	str = cJSON_Print(root_obj);
    	if (str == NULL) {
    		printk("Failed to print JSON object");
    		goto clean_exit;
    	}
    
    	printf("Data received from AWS IoT console:\nTopic: %.*s\nMessage: %s\n",
    	       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->type) {
    	case AWS_IOT_EVT_CONNECTING:
    		printk("AWS_IOT_EVT_CONNECTING\n");
    		break;
    	case AWS_IOT_EVT_CONNECTED:
    		printk("AWS_IOT_EVT_CONNECTED\n");
    
    		k_delayed_work_cancel(&connect_work);
    
    		if (evt->data.persistent_session) {
    			printk("Persistent session enabled\n");
    		}
    
    #if defined(CONFIG_BSD_LIBRARY)
    		/** 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_delayed_work_submit(&shadow_update_version_work, K_NO_WAIT);
    
    		/** Start sequential shadow data updates.
    		 */
    		k_delayed_work_submit(&shadow_update_work,
    				K_SECONDS(CONFIG_PUBLICATION_INTERVAL_SECONDS));
    
    #if defined(CONFIG_BSD_LIBRARY)
    		int err = lte_lc_psm_req(true);
    		if (err) {
    			printk("Requesting PSM failed, error: %d\n", err);
    		}
    #endif
    		break;
    	case AWS_IOT_EVT_READY:
    		printk("AWS_IOT_EVT_READY\n");
    		break;
    	case AWS_IOT_EVT_DISCONNECTED:
    		printk("AWS_IOT_EVT_DISCONNECTED\n");
    		k_delayed_work_cancel(&shadow_update_work);
    
    		if (k_delayed_work_pending(&connect_work)) {
    			break;
    		}
    
    		k_delayed_work_submit(&connect_work, K_NO_WAIT);
    		break;
    	case AWS_IOT_EVT_DATA_RECEIVED:
    		printk("AWS_IOT_EVT_DATA_RECEIVED\n");
    		print_received_data(evt->data.msg.ptr, evt->data.msg.topic.str,
    				    evt->data.msg.topic.len);
    		break;
    	case AWS_IOT_EVT_FOTA_START:
    		printk("AWS_IOT_EVT_FOTA_START\n");
    		break;
    	case AWS_IOT_EVT_FOTA_ERASE_PENDING:
    		printk("AWS_IOT_EVT_FOTA_ERASE_PENDING\n");
    		printk("Disconnect LTE link or reboot\n");
    #if defined(CONFIG_BSD_LIBRARY)
    		err = lte_lc_offline();
    		if (err) {
    			printk("Error disconnecting from LTE\n");
    		}
    #endif
    		break;
    	case AWS_IOT_EVT_FOTA_ERASE_DONE:
    		printk("AWS_FOTA_EVT_ERASE_DONE\n");
    		printk("Reconnecting the LTE link");
    #if defined(CONFIG_BSD_LIBRARY)
    		err = lte_lc_connect();
    		if (err) {
    			printk("Error connecting to LTE\n");
    		}
    #endif
    		break;
    	case AWS_IOT_EVT_FOTA_DONE:
    		printk("AWS_IOT_EVT_FOTA_DONE\n");
    		printk("FOTA done, rebooting device\n");
    		aws_iot_disconnect();
    		sys_reboot(0);
    		break;
    	case AWS_IOT_EVT_FOTA_DL_PROGRESS:
    		printk("AWS_IOT_EVT_FOTA_DL_PROGRESS, (%d%%)",
    		       evt->data.fota_progress);
    	case AWS_IOT_EVT_ERROR:
    		printk("AWS_IOT_EVT_ERROR, %d\n", evt->data.err);
    		break;
    	default:
    		printk("Unknown AWS IoT event type: %d\n", evt->type);
    		break;
    	}
    }
    
    static void work_init(void)
    {
    	k_delayed_work_init(&shadow_update_work, shadow_update_work_fn);
    	k_delayed_work_init(&connect_work, connect_work_fn);
    	k_delayed_work_init(&shadow_update_version_work,
    			    shadow_update_version_work_fn);
    }
    
    #if defined(CONFIG_BSD_LIBRARY)
    static void lte_handler(const struct lte_lc_evt *const evt)
    {
    	switch (evt->type) {
    	case LTE_LC_EVT_NW_REG_STATUS:
    		if ((evt->nw_reg_status != LTE_LC_NW_REG_REGISTERED_HOME) &&
    		     (evt->nw_reg_status != LTE_LC_NW_REG_REGISTERED_ROAMING)) {
    			break;
    		}
    
    		printk("Network registration status: %s\n",
    			evt->nw_reg_status == LTE_LC_NW_REG_REGISTERED_HOME ?
    			"Connected - home network" : "Connected - roaming");
    
    		k_sem_give(&lte_connected);
    		break;
    	case LTE_LC_EVT_PSM_UPDATE:
    		printk("PSM parameter update: TAU: %d, Active time: %d\n",
    			evt->psm_cfg.tau, evt->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),
    			       "eDRX parameter update: eDRX: %f, PTW: %f",
    			       evt->edrx_cfg.edrx, evt->edrx_cfg.ptw);
    		if (len > 0) {
    			printk("%s\n", log_buf);
    		}
    		break;
    	}
    	case LTE_LC_EVT_RRC_UPDATE:
    		printk("RRC mode: %s\n",
    			evt->rrc_mode == LTE_LC_RRC_MODE_CONNECTED ?
    			"Connected" : "Idle");
    		break;
    	case LTE_LC_EVT_CELL_UPDATE:
    		printk("LTE cell changed: Cell ID: %d, Tracking area: %d\n",
    			evt->cell.id, evt->cell.tac);
    		break;
    	default:
    		break;
    	}
    }
    
    static void modem_configure(void)
    {
    	int err = 0;
    
    	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);
    		err = lte_lc_psm_req(true);
    		if (err)
    		{
    			printk("Modem could not be configured, error: %d\n",err);
    			return;
    		}
    	}
    }
    
    static void at_configure(void)
    {
    	int err;
    
    	err = at_notif_init();
    	__ASSERT(err == 0, "AT Notify could not be initialized.");
    	err = at_cmd_init();
    	__ASSERT(err == 0, "AT CMD could not be established.");
    }
    
    static void bsd_lib_modem_dfu_handler(void)
    {
    	int err;
    
    	err = bsdlib_init();
    
    	switch (err) {
    	case MODEM_DFU_RESULT_OK:
    		printk("Modem update suceeded, reboot\n");
    		sys_reboot(SYS_REBOOT_COLD);
    		break;
    	case MODEM_DFU_RESULT_UUID_ERROR:
    	case MODEM_DFU_RESULT_AUTH_ERROR:
    		printk("Modem update failed, error: %d\n", err);
    		printk("Modem will use old firmware\n");
    		sys_reboot(SYS_REBOOT_COLD);
    		break;
    	case MODEM_DFU_RESULT_HARDWARE_ERROR:
    	case MODEM_DFU_RESULT_INTERNAL_ERROR:
    		printk("Modem update malfunction, error: %d, reboot\n", err);
    		sys_reboot(SYS_REBOOT_COLD);
    		break;
    	default:
    		break;
    	}
    
    	at_configure();
    }
    #endif
    
    static int app_topics_subscribe(void)
    {
    	int err;
    	static char custom_topic[75] = "$aws/things/nrf-352656101080789/shadow/update";
    	static char custom_topic_2[75] = "$aws/things/nrf-352656101080789/shadow/delta";
    
    	const struct aws_iot_topic_data topics_list[APP_TOPICS_COUNT] = {
    		[0].str = custom_topic,
    		[0].len = strlen(custom_topic),
    		[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) {
    		printk("aws_iot_subscription_topics_add, error: %d\n", err);
    	}
    
    	return err;
    }
    
    static void date_time_event_handler(const struct date_time_evt *evt)
    {
    	switch (evt->type) {
    	case DATE_TIME_OBTAINED_MODEM:
    		printk("DATE_TIME_OBTAINED_MODEM\n");
    		break;
    	case DATE_TIME_OBTAINED_NTP:
    		printk("DATE_TIME_OBTAINED_NTP\n");
    		break;
    	case DATE_TIME_OBTAINED_EXT:
    		printk("DATE_TIME_OBTAINED_EXT\n");
    		break;
    	case DATE_TIME_NOT_OBTAINED:
    		printk("DATE_TIME_NOT_OBTAINED\n");
    		break;
    	default:
    		break;
    	}
    
    	/** Do not depend on obtained time, continue upon any event from the
    	 *  date time library.
    	 */
    	k_sem_give(&date_time_obtained);
    }
    
    void aws_iot1(void)
    {
    	int err;
           
    	printk("The AWS IoT sample started, version: %s\n", CONFIG_APP_VERSION);
    
    	cJSON_Init();
    
    #if defined(CONFIG_BSD_LIBRARY)
    	bsd_lib_modem_dfu_handler();
    #endif
    
    	err = aws_iot_init(NULL, aws_iot_event_handler);
    	if (err) {
    		printk("AWS IoT library could not be initialized, error: %d\n",
    		       err);
    	}
    
    	/** Subscribe to customizable non-shadow specific topics
    	 *  to AWS IoT backend.
    	 */
    	err = app_topics_subscribe();
    	if (err) {
    		printk("Adding application specific topics failed, error: %d\n",
    			err);
    	}
    
    	work_init();
    	
    #if defined(CONFIG_BSD_LIBRARY)
    	modem_configure();
    
    	err = modem_info_init();
    	if (err) {
    		printk("Failed initializing modem info module, error: %d\n",
    			err);
    	}
    
    	k_sem_take(&lte_connected, K_FOREVER);
    #endif
    
    
    	date_time_update_async(date_time_event_handler);
    
    	err = k_sem_take(&date_time_obtained, K_SECONDS(DATE_TIME_TIMEOUT_S));
    	if (err) {
    		printk("Date time, no callback event within %d seconds\n",
    			DATE_TIME_TIMEOUT_S);
    	}
    
    	k_delayed_work_submit(&connect_work, K_NO_WAIT);
    }
    
    int main(void)
    {
    	aws_iot1();
    	k_sleep(K_MSEC(10000));
    	
    	nrf_gnss_data_frame_t gps_data;
    	uint8_t		      cnt = 0;
    
    #ifdef CONFIG_SUPL_CLIENT_LIB
    	static struct supl_api supl_api = {
    		.read       = supl_read,
    		.write      = supl_write,
    		.handler    = inject_agps_type,
    		.logger     = supl_logger,
    		.counter_ms = k_uptime_get
    	};
    #endif
    
    	printk("Starting GPS application\n");
    
    	if (init_app() != 0) {
    		return -1;
    	}
    	
    	modem_configure();
    
    	err = modem_info_init();
    	if (err) {
    		printk("Failed initializing modem info module, error: %d\n",
    			err);
    	}
    
    	k_sem_take(&lte_connected, K_FOREVER);
    	
    	
    #ifdef CONFIG_SUPL_CLIENT_LIB
    	int rc = supl_init(&supl_api);
    
    	if (rc != 0) {
    		return rc;
    	}
    #endif
    
    	printk("Getting GPS data...\n");
    
    	while (1) {
    
    		do {
    			/* Loop until we don't have more
    			 * data to read
    			 */
    		} while (process_gps_data(&gps_data) > 0);
    
    		if (IS_ENABLED(CONFIG_GPS_SAMPLE_NMEA_ONLY)) {
    			print_nmea_data();
    			nmea_string_cnt = 0;
    		} else {
    			printk("\033[1;1H");
    			printk("\033[2J");
    			print_satellite_stats(&last_pvt);
    			print_gnss_stats(&last_pvt);
    			printk("---------------------------------\n");
    
    			if (!got_fix) {
    				printk("Seconds since last fix: %lld\n",
    				       (k_uptime_get() - fix_timestamp) / 1000);
    				cnt++;
    				printk("Searching [%c]\n",
    				       update_indicator[cnt%4]);
    			} else {
    				print_fix_data(&last_pvt);
    				
    			}
    
    			printk("\nNMEA strings:\n\n");
    			print_nmea_data();
    			printk("---------------------------------");
    			
    			
    		}
    
    		k_sleep(K_MSEC(500));
    	}
    
    	return 0;
    }

    and this is prg.config

    #GPS
    # Copyright (c) 2019 Nordic Semiconductor ASA
    #
    # SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
    #
    CONFIG_BSD_LIBRARY=y
    CONFIG_STDOUT_CONSOLE=y
    CONFIG_UART_INTERRUPT_DRIVEN=y
    CONFIG_NEWLIB_LIBC=y
    CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y
    CONFIG_AT_CMD=y
    CONFIG_AT_NOTIF=y
    
    # Enable SUPL client support
    CONFIG_SUPL_CLIENT_LIB=n
    
    # Networking
    CONFIG_NETWORKING=y
    CONFIG_NET_SOCKETS_OFFLOAD=y
    CONFIG_NET_SOCKETS=y
    CONFIG_NET_SOCKETS_POSIX_NAMES=y
    CONFIG_NET_SOCKETS_SOCKOPT_TLS=y
    
    # Disable native network stack to save some memory
    CONFIG_NET_NATIVE=n
    
    # Main thread
    CONFIG_HEAP_MEM_POOL_SIZE=4096
    CONFIG_MAIN_STACK_SIZE=4096
    CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048
    
    # Application
    CONFIG_GPS_SAMPLE_NMEA_ONLY=n
    
    # General config
    CONFIG_NCS_SAMPLES_DEFAULTS=y
    CONFIG_REBOOT=y
    
    # LTE link control
    CONFIG_LTE_LINK_CONTROL=y
    CONFIG_LTE_NETWORK_MODE_LTE_M_GPS=y
    CONFIG_LTE_AUTO_INIT_AND_CONNECT=n
    CONFIG_LTE_PSM_REQ_RPTAU="00011111"
    CONFIG_LTE_PSM_REQ_RAT="00000010"
    CONFIG_LTE_POWER_SAVING_MODE=y
    
    # Download client (needed by AWS FOTA)
    CONFIG_DOWNLOAD_CLIENT=y
    CONFIG_DOWNLOAD_CLIENT_STACK_SIZE=4096
    
    # 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
    
    # Date Time library
    CONFIG_DATE_TIME=y
    
    # Modem information
    CONFIG_MODEM_INFO=y
    
    
    # BSD library
    CONFIG_BSD_LIBRARY_SYS_INIT=y
    
    # AT Host
    CONFIG_AT_HOST_LIBRARY=y
    
    # AWS IoT library
    CONFIG_AWS_IOT=y
    CONFIG_AWS_IOT_CLIENT_ID_STATIC="nrf-352656101080789"
    CONFIG_AWS_IOT_BROKER_HOST_NAME="a6i4ulevbqzm-ats.iot.us-east-2.amazonaws.com"
    CONFIG_AWS_IOT_SEC_TAG=201
    CONFIG_AWS_IOT_APP_SUBSCRIPTION_LIST_COUNT=2
    CONFIG_AWS_IOT_TOPIC_UPDATE_DELTA_SUBSCRIBE=y
    CONFIG_AWS_IOT_CONNECTION_POLL_THREAD=y
    CONFIG_AWS_IOT_LAST_WILL=y
    CONFIG_AWS_IOT_TOPIC_GET_ACCEPTED_SUBSCRIBE=y
    CONFIG_AWS_IOT_TOPIC_GET_REJECTED_SUBSCRIBE=y

    Thanks in Advance for your time

  • Hi, 

    Do you know what AT command is failing? Can you provide the full log from your application? 

    I will try you application here as well. 

    Kind regards,
    Øyvind

  • I'm not able to compile. Can you please share full project?

Related