http_client network error on thingy91

Hello,

I am trying to use http requests on Thingy91 and therefore I tried with the sample from zephyr/samples/net/sockets/http_client.

I can build the project without any problem but when I try to run it, I get the following error when I open a putty terminal:

I did not change anything in the code, and I was supposing it was not a network problem since I activated my SIM card and I can run the asset_tracker and see the data in nrFcloud without any issue.

I am using the nrfConnectSDK 2.0.0, and the SIM is from twilio.

Best regards,

C

  • Hello,

    Thanks for the answer.

    Here is when it works:

    2022-07-15T08:03:48.867Z DEBUG modem << Waiting for network.. 
    2022-07-15T08:03:49.875Z DEBUG modem << [00:00:52.217,071] [0m<dbg> lte_lc: init_and_config: The library is already initialized and configured[0m
    2022-07-15T08:07:02.474Z DEBUG modem << [00:04:04.805,206] [0m<dbg> lte_lc: at_handler_cereg: +CEREG notification: +CEREG: 0
    2022-07-15T08:07:02.477Z DEBUG modem << [0m
    2022-07-15T08:07:02.479Z DEBUG modem << [00:04:04.805,328] [0m<dbg> lte_lc_helpers: parse_cereg: Network registration status: 0[0m
    2022-07-15T08:07:02.481Z DEBUG modem << [00:04:04.805,358] [0m<dbg> lte_lc_helpers: parse_cereg: LTE mode not found, error code: -22[0m
    2022-07-15T08:07:07.405Z DEBUG modem << [00:04:09.730,438] [0m<dbg> lte_lc: at_handler_cereg: +CEREG notification: +CEREG: 2,"0FC8","0114E209",7
    2022-07-15T08:07:07.414Z DEBUG modem << [0m
    2022-07-15T08:07:07.416Z DEBUG modem << [00:04:09.730,651] [0m<dbg> lte_lc_helpers: parse_cereg: Network registration status: 2[0m
    2022-07-15T08:07:07.417Z DEBUG modem << [00:04:09.730,682] [0m<dbg> lte_lc_helpers: parse_cereg: LTE mode: 7[0m
    2022-07-15T08:07:07.418Z DEBUG modem << [00:04:09.833,801] [0m<dbg> lte_lc: at_handler_cscon: +CSCON notification[0m
    2022-07-15T08:07:12.024Z DEBUG modem << OK
    2022-07-15T08:07:13.051Z DEBUG modem << [00:04:15.380,981] [0m<dbg> lte_lc: at_handler_cereg: +CEREG notification: +CEREG: 5,"0FC8","0114E209",7,,,"11100000","11100000"
    2022-07-15T08:07:13.054Z DEBUG modem << [0m
    2022-07-15T08:07:13.056Z DEBUG modem << [00:04:15.381,256] [0m<dbg> lte_lc_helpers: parse_cereg: Network registration status: 5[0m
    2022-07-15T08:07:13.058Z DEBUG modem << [00:04:15.381,286] [0m<dbg> lte_lc_helpers: parse_cereg: LTE mode: 7[0m
    2022-07-15T08:07:17.367Z DEBUG modem << [00:04:19.683,319] [0m<dbg> lte_lc: at_handler_cscon: +CSCON notification[0m
    2022-07-15T08:07:17.369Z DEBUG modem << [00:04:19.774,993] [0m<dbg> lte_lc: at_handler_cereg: +CEREG notification: +CEREG: 5,"0FC8","01046C04",7,,,"11100000","11100000"
    2022-07-15T08:07:17.370Z DEBUG modem << [0m
    2022-07-15T08:07:17.378Z DEBUG modem << [00:04:19.775,238] [0m<dbg> lte_lc_helpers: parse_cereg: Network registration status: 5[0m
    2022-07-15T08:07:17.380Z DEBUG modem << [00:04:19.775,268] [0m<dbg> lte_lc_helpers: parse_cereg: LTE mode: 7[0m
    2022-07-15T08:07:17.381Z DEBUG modem << [00:04:20.294,799] [0m<dbg> lte_lc: at_handler_cscon: +CSCON notification[0m
    2022-07-15T08:07:17.570Z DEBUG modem << Connecting to google.com
    2022-07-15T08:07:17.859Z DEBUG modem << Sent 60 bytes
    2022-07-15T08:07:18.227Z DEBUG modem << bytes are 0 
    2022-07-15T08:07:18.232Z DEBUG modem << Received 0 bytes
    2022-07-15T08:07:18.234Z DEBUG modem << Finished, closing socket.

    And here is what I get when it does not work with error 115:

    2022-07-15T08:03:07.244Z DEBUG modem << Waiting for network.. 
    2022-07-15T08:03:08.254Z DEBUG modem << [00:00:10.595,550] [0m<dbg> lte_lc: init_and_config: System mode (1) and preference (0) are already configured[0m
    2022-07-15T08:03:41.923Z DEBUG modem << [00:00:44.243,011] [0m<dbg> lte_lc: at_handler_cereg: +CEREG notification: +CEREG: 2,"0FC8","0114E209",7
    2022-07-15T08:03:41.927Z DEBUG modem << [0m
    2022-07-15T08:03:41.930Z DEBUG modem << [00:00:44.243,225] [0m<dbg> lte_lc_helpers: parse_cereg: Network registration status: 2[0m
    2022-07-15T08:03:41.931Z DEBUG modem << [00:00:44.243,255] [0m<dbg> lte_lc_helpers: parse_cereg: LTE mode: 7[0m
    2022-07-15T08:03:41.932Z DEBUG modem << [00:00:44.338,775] [0m<dbg> lte_lc: at_handler_cscon: +CSCON notification[0m
    2022-07-15T08:03:45.005Z DEBUG modem << OK
    2022-07-15T08:03:46.055Z DEBUG modem << [00:00:48.357,788] [0m<dbg> lte_lc: at_handler_cereg: +CEREG notification: +CEREG: 5,"0FC8","0114E209",7,,,"11100000","11100000"
    2022-07-15T08:03:46.057Z DEBUG modem << [0m
    2022-07-15T08:03:46.064Z DEBUG modem << [00:00:48.358,032] [0m<dbg> lte_lc_helpers: parse_cereg: Network registration status: 5[0m
    2022-07-15T08:03:46.066Z DEBUG modem << [00:00:48.358,062] [0m<dbg> lte_lc_helpers: parse_cereg: LTE mode: 7[0m
    2022-07-15T08:03:46.071Z DEBUG modem << [00:00:48.414,428] [0m<dbg> lte_lc: at_handler_cereg: +CEREG notification: +CEREG: 2,"0FC8","0114E209",7
    2022-07-15T08:03:46.072Z DEBUG modem << [0m
    2022-07-15T08:03:46.076Z DEBUG modem << [00:00:48.414,611] [0m<dbg> lte_lc_helpers: parse_cereg: Network registration status: 2[0m
    2022-07-15T08:03:46.077Z DEBUG modem << [00:00:48.414,642] [0m<dbg> lte_lc_helpers: parse_cereg: LTE mode: 7[0m
    2022-07-15T08:03:47.435Z DEBUG modem << [00:00:49.755,401] [0m<dbg> lte_lc: at_handler_cscon: +CSCON notification[0m
    2022-07-15T08:03:47.438Z DEBUG modem << [00:00:49.864,868] [0m<dbg> lte_lc: at_handler_cereg: +CEREG notification: +CEREG: 2
    2022-07-15T08:03:47.441Z DEBUG modem << [0m
    2022-07-15T08:03:47.444Z DEBUG modem << [00:00:49.864,990] [0m<dbg> lte_lc_helpers: parse_cereg: Network registration status: 2[0m
    2022-07-15T08:03:47.445Z DEBUG modem << [00:00:49.865,020] [0m<dbg> lte_lc_helpers: parse_cereg: LTE mode not found, error code: -22[0m
    2022-07-15T08:03:48.759Z DEBUG modem << getaddrinfo() failed, err 115

    And here with error 116:

    2022-07-15T08:25:01.226Z DEBUG modem << Waiting for network.. 
    2022-07-15T08:25:01.854Z DEBUG modem << [00:22:04.205,078] [0m<dbg> lte_lc: at_handler_cscon: +CSCON notification[0m
    2022-07-15T08:25:01.858Z DEBUG modem << [00:22:04.205,261] [0m<dbg> lte_lc: at_handler_cereg: +CEREG notification: +CEREG: 0
    2022-07-15T08:25:01.861Z DEBUG modem << [0m
    2022-07-15T08:25:01.864Z DEBUG modem << [00:22:04.205,383] [0m<dbg> lte_lc_helpers: parse_cereg: Network registration status: 0[0m
    2022-07-15T08:25:01.869Z DEBUG modem << [00:22:04.205,383] [0m<dbg> lte_lc_helpers: parse_cereg: LTE mode not found, error code: -22[0m
    2022-07-15T08:25:01.871Z DEBUG modem << [00:22:04.605,346] [0m<dbg> lte_lc: init_and_config: The library is already initialized and configured[0m
    2022-07-15T08:26:13.851Z DEBUG modem << [00:23:16.201,904] [0m<dbg> lte_lc: at_handler_cereg: +CEREG notification: +CEREG: 2,"0FC8","0114E209",7
    2022-07-15T08:26:13.854Z DEBUG modem << [0m
    2022-07-15T08:26:13.860Z DEBUG modem << [00:23:16.202,117] [0m<dbg> lte_lc_helpers: parse_cereg: Network registration status: 2[0m
    2022-07-15T08:26:13.862Z DEBUG modem << [00:23:16.202,148] [0m<dbg> lte_lc_helpers: parse_cereg: LTE mode: 7[0m
    2022-07-15T08:26:13.866Z DEBUG modem << [00:23:16.300,689] [0m<dbg> lte_lc: at_handler_cscon: +CSCON notification[0m
    2022-07-15T08:26:17.076Z DEBUG modem << OK
    2022-07-15T08:26:17.078Z DEBUG modem << Connecting to google.com
    2022-07-15T08:26:17.785Z DEBUG modem << connect() failed, err: 116

    I am not really familiar with all that, and did not find much help online.It seems like it might be an unstable connection during the call to getAddrInfo() but what I don't really get is why it is still working half of the time.I'm using a Twilio SIM in Switzerland and I though it was supposed to have great connection.

    I know I am also having a 0 answer from recv() which I'm still trying to figure out but I'm not sure it is coming from this since it works some of the time.

    Thanks!

    Carole

  • Thanks for the logs, Carole :-)

    rupcar said:
    I am not really familiar with all that, and did not find much help online.It seems like it might be an unstable connection during the call to getAddrInfo() but what I don't really get is why it is still working half of the time.I'm using a Twilio SIM in Switzerland and I though it was supposed to have great connection.

    The nRF9160 has two cores: the application core and the modem core. The application core is where the HTTPS Client is located, while the modem core has its own firmware image which kind of acts as a “black box”, meaning that it is not accessible from the outside.  

    However, there is an API available that enables both cores communicating with each other. Referring to the modem library architecture documentation in case you are interested. In general, the application and modem talk to each other using AT commandsIn case of HTTPS Client sample, it uses the LTE link controller library instead, which basically is a wrapper for the most commonly used AT commands. Using this library increases readability during development, but AT commands can also be used directly if preferred. 

    rupcar said:
    Here is when it works:

    What I can see from the first log is that the modem initially establishes a network connection: 

    2022-07-15T08:07:07.405Z DEBUG modem << [00:04:09.730,438] [0m<dbg> lte_lc: at_handler_cereg: +CEREG notification: +CEREG: 2,"0FC8","0114E209",7 

    Which it successfully does a couple of seconds later:

    2022-07-15T08:07:13.051Z DEBUG modem << [00:04:15.380,981] [0m<dbg> lte_lc: at_handler_cereg: +CEREG notification: +CEREG: 5,"0FC8","0114E209",7,,,"11100000","11100000" 

    Referring to the network registration status notification AT command for more insights.

    rupcar said:
    And here is what I get when it does not work with error 115:

    In second log, modem does exactly the same thing: 

    2022-07-15T08:03:41.923Z DEBUG modem << [00:00:44.243,011] [0m<dbg> lte_lc: at_handler_cereg: +CEREG notification: +CEREG: 2,"0FC8","0114E209",7 
    
    2022-07-15T08:03:46.055Z DEBUG modem << [00:00:48.357,788] [0m<dbg> lte_lc: at_handler_cereg: +CEREG notification: +CEREG: 5,"0FC8","0114E209",7,,,"11100000","11100000" 

    But loses the connection almost right away: 

    2022-07-15T08:03:46.071Z DEBUG modem << [00:00:48.414,428] [0m<dbg> lte_lc: at_handler_cereg: +CEREG notification: +CEREG: 2,"0FC8","0114E209",7 

    Why that happens, is hard to tell without any more input. But my first thought here would be that the signal quality might be insufficient. The Thingy:91 does not have the same signal power capacity compared to the nRF9160 DK, due to its antenna design. This makes it even more relevant for the Thingy:91 to be placed in an environment where good signal coverage can be expected. How does the environment your Thingy:91 is placed in look like? 

    rupcar said:
    And here with error 116:

    Regarding the third log, there I assume that you have restarted the application? Because it looks like that the modem did not shut down as it is supposed to, which might have created this warning: 

    2022-07-15T08:25:01.871Z DEBUG modem << [00:22:04.605,346] [0m<dbg> lte_lc: init_and_config: The library is already initialized and configured[0m 

    However, modem still tries to establish a network connection: 

    2022-07-15T08:26:13.851Z DEBUG modem << [00:23:16.201,904] [0m<dbg> lte_lc: at_handler_cereg: +CEREG notification: +CEREG: 2,"0FC8","0114E209",7 

    But the application itself just moves on with the DNS lookup before modem had a chance to finish the connection attempt. That is in a way is expected, since the sample had a valid reason to leave the blocking lte_lc_init_and_connect() function (already initialised). Now even though the behaviour is correct, the outcome itself will be wrong ;-)

    Regards, 

    Markus 

  • Thanks a lot for the explanation! It makes more sense to me now.

    I tried at home and at work and I had similar results. At home it's a new building with pretty thick walls, but I tried outside and it did not change much.

    For the 3rd log, it surprised me as well because I did not touch it at all during the call, so I did not restart it. My code is in a while loop so I try connection several time and the error 116 always comes in that form, even though I am just letting it run without doing anything on it.

    Do you hava any clue how to be sure it is not already initialize to avoid this error ?

    Carole

  • Hello Carole, 

    rupcar said:
    For the 3rd log, it surprised me as well because I did not touch it at all during the call, so I did not restart it. My code is in a while loop so I try connection several time and the error 116 always comes in that form, even though I am just letting it run without doing anything on it.

    I guess that might explain the issue. 

    rupcar said:
    My code is in a while loop so I try connection several time

    Would you mind elaborating how you are doing that? 

    rupcar said:
    I tried at home and at work and I had similar results. At home it's a new building with pretty thick walls, but I tried outside and it did not change much.

    We might need to cross-check the signal quality here. The simplest way of doing so is by using the AT Client and running the following commands: 

    [2022-07-08 17:43:01] *** Booting Zephyr OS build v3.0.99-ncs1  *** 
    
    [2022-07-08 17:43:01] The AT host sample started 
    
    [2022-07-08 17:43:04] AT+CGMR 
    
    [2022-07-08 17:43:05] mfw_nrf9160_1.3.2 
    
    [2022-07-08 17:43:05] OK 
    
    [2022-07-08 17:43:10] AT%MDMEV=1 
    
    [2022-07-08 17:43:10] OK 
    
    [2022-07-08 17:43:14] AT%XMODEMSLEEP=1,1000,60000 
    
    [2022-07-08 17:43:14] O%XMODEMSLEEP: 4 
    
    [2022-07-08 17:43:14] K 
    
    [2022-07-08 17:43:18] AT+CEREG=5 
    
    [2022-07-08 17:43:19] OK 
    
    [2022-07-08 17:43:22] AT%CESQ=1  
    
    [2022-07-08 17:43:23] OK 
    
    [2022-07-08 17:43:27] AT%XSYSTEMMODE? 
    
    [2022-07-08 17:43:28] %XSYSTEMMODE: 1,0,0,0 
    
    [2022-07-08 17:43:28] OK 
    
    [2022-07-08 17:43:31] AT+CFUN=1 
    
    [2022-07-08 17:43:32] %XMODEMSLEEP: 4,0 
    
    [2022-07-08 17:43:32] OK 
    
    [2022-07-08 17:43:33] %CESQ: 46,2,18,2 
    
    [2022-07-08 17:43:33] +CEREG: 2,"7AA9","01444000",7 
    
    [2022-07-08 17:43:34] %MDMEV: SEARCH STATUS 2 
    
    [2022-07-08 17:43:34] +CEREG: 1,"7AA9","01444000",7,,,"00001010","11000001" 
    
    [2022-07-08 17:43:42] %CESQ: 47,2,23,3 
    
    [2022-07-08 17:43:57] %CESQ: 46,2,18,2 
    
    [2022-07-08 17:44:08] %CESQ: 255,0,255,0 
    
    [2022-07-08 17:44:08] %XMODEMSLEEP: 1,1151979975 

    It would be great if you could share your log output :-)

    Cheers, 

    Markus 

  • Would you mind elaborating how you are doing that? 

    Actually, for now I just edited the https_client so that what was in the main() is in a new function called network_connection(). I commented the certificate part and changed the website to access from "example.com" to "google.com" for now. My point was to enable several connections trials without having to disconnect/reconnect the thingy every time. Do you mean it could be coming from there as if the clean_up part was not taken into account ?

    If its easier, here is the code:

    /*
     * Copyright (c) 2020 Nordic Semiconductor ASA
     *
     * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
     */
    
    
    #include <string.h>
    #include <zephyr/kernel.h>
    #include <stdlib.h>
    #include <zephyr/net/socket.h>
    #include <modem/nrf_modem_lib.h>
    #include <zephyr/net/tls_credentials.h>
    #include <modem/lte_lc.h>
    #include <modem/modem_key_mgmt.h>
    #include <sys/printk.h>
    
    #include <zephyr.h>
    #include <device.h>
    #include <devicetree.h>
    #include <drivers/gpio.h>
    
    #define HTTPS_PORT 443 // https port 
    #define HTTPS_HOSTNAME "google.com"
    #define HTTP_HEAD                                                              \
    	"HEAD / HTTP/1.1\r\n"                                                  \
    	"Host: " HTTPS_HOSTNAME ":443\r\n"                                     \
    	"Connection: close\r\n\r\n"
    #define HTTP_HEAD_LEN (sizeof(HTTP_HEAD) - 1)
    #define HTTP_HDR_END "\r\n\r\n"
    #define RECV_BUF_SIZE 2048
    #define TLS_SEC_TAG 42
    
    static const char send_buf[] = HTTP_HEAD;
    static char recv_buf[RECV_BUF_SIZE];
    
    /* Certificate for `example.com` */
    static const char cert[] = {
    	#include "../cert/DigiCertGlobalRootCA.pem"
    };
    
    BUILD_ASSERT(sizeof(cert) < KB(4), "Certificate too large");
    
    /* Provision certificate to modem */
    int cert_provision(void)
    {
    	int err;
    	bool exists;
    	int mismatch;
    
    	/* It may be sufficient for you application to check whether the correct
    	 * certificate is provisioned with a given tag directly using modem_key_mgmt_cmp().
    	 * Here, for the sake of the completeness, we check that a certificate exists
    	 * before comparing it with what we expect it to be.
    	 */
    	err = modem_key_mgmt_exists(TLS_SEC_TAG, MODEM_KEY_MGMT_CRED_TYPE_CA_CHAIN, &exists);
    	if (err) {
    		printk("Failed to check for certificates err %d\n", err);
    		return err;
    	}
    
    	if (exists) {
    		mismatch = modem_key_mgmt_cmp(TLS_SEC_TAG,
    					      MODEM_KEY_MGMT_CRED_TYPE_CA_CHAIN,
    					      cert, strlen(cert));
    		if (!mismatch) {
    			printk("Certificate match\n");
    			return 0;
    		}
    
    		printk("Certificate mismatch\n");
    		err = modem_key_mgmt_delete(TLS_SEC_TAG, MODEM_KEY_MGMT_CRED_TYPE_CA_CHAIN);
    		if (err) {
    			printk("Failed to delete existing certificate, err %d\n", err);
    		}
    	}
    
    	printk("Provisioning certificate\n");
    
    	/*  Provision certificate to the modem */
    	err = modem_key_mgmt_write(TLS_SEC_TAG,
    				   MODEM_KEY_MGMT_CRED_TYPE_CA_CHAIN,
    				   cert, sizeof(cert) - 1);
    	if (err) {
    		printk("Failed to provision certificate, err %d\n", err);
    		return err;
    	}
    
    	return 0;
    }
    
    /* Setup TLS options on a given socket */
    int tls_setup(int fd)
    {
    	int err;
    	int verify;
    
    	/* Security tag that we have provisioned the certificate with */
    	const sec_tag_t tls_sec_tag[] = {
    		TLS_SEC_TAG,
    	};
    
    #if defined(CONFIG_SAMPLE_TFM_MBEDTLS)
    	err = tls_credential_add(tls_sec_tag[0], TLS_CREDENTIAL_CA_CERTIFICATE, cert, sizeof(cert));
    	if (err) {
    		return err;
    	}
    #endif
    
    	/* Set up TLS peer verification */
    	enum {
    		NONE = 0,
    		OPTIONAL = 1,
    		REQUIRED = 2,
    	};
    
    	verify = REQUIRED;
    
    	err = setsockopt(fd, SOL_TLS, TLS_PEER_VERIFY, &verify, sizeof(verify));
    	if (err) {
    		printk("Failed to setup peer verification, err %d\n", errno);
    		return err;
    	}
    
    	/* Associate the socket with the security tag
    	 * we have provisioned the certificate with.
    	 */
    	err = setsockopt(fd, SOL_TLS, TLS_SEC_TAG_LIST, tls_sec_tag,
    			 sizeof(tls_sec_tag));
    	if (err) {
    		printk("Failed to setup TLS sec tag, err %d\n", errno);
    		return err;
    	}
    
    	err = setsockopt(fd, SOL_TLS, TLS_HOSTNAME, HTTPS_HOSTNAME, sizeof(HTTPS_HOSTNAME) - 1);
    	if (err) {
    		printk("Failed to setup TLS hostname, err %d\n", errno);
    		return err;
    	}
    	return 0;
    }
    
    void network_connection(){
    
    	int err;
    	int fd; // socket descriptor
    	char *p;
    	int bytes;
    	size_t off;
    	struct addrinfo *res; // pointer to the results
    
    	// first thing to call when making a connection: to prep socket structure address for subseq uses
    	struct addrinfo hints = {
    		.ai_family = AF_INET, // force to use IPV4
    		.ai_socktype = SOCK_STREAM, // type of socket that we're using
    	};
    
    	printk("HTTPS client sample started\n\r");
    
    	/*
    
    #if !defined(CONFIG_SAMPLE_TFM_MBEDTLS)
    	/* Provision certificates before connecting to the LTE network 
    	err = cert_provision();
    	if (err) {
    		return;
    	}
    #endif
    	*/
    	printk("Waiting for network.. \n");
    	err = lte_lc_init_and_connect();
    	if (err) {
    		printk("Failed to connect to the LTE network, err %d\n", err);
    		return;
    	}
    
    	printk("OK\n");
    
    	err = getaddrinfo(HTTPS_HOSTNAME, NULL, &hints, &res);
    	if (err) {
    		printk("getaddrinfo() failed, err %d\n", errno);
    		return;
    	}
    
    	((struct sockaddr_in *)res->ai_addr)->sin_port = htons(HTTPS_PORT);
    
    	if (IS_ENABLED(CONFIG_SAMPLE_TFM_MBEDTLS)) {
    		fd = socket(AF_INET, SOCK_STREAM | SOCK_NATIVE_TLS, IPPROTO_TCP);
    	} else {
    		fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    	}
    	if (fd == -1) {
    		printk("Failed to open socket!\n");
    		goto clean_up;
    	}
    
    	/* Setup TLS socket options */
    	/*err = tls_setup(fd);
    	if (err) {
    		goto clean_up;
    	}*/
    
    	printk("Connecting to %s\n", HTTPS_HOSTNAME);
    	err = connect(fd, res->ai_addr, sizeof(struct sockaddr_in));
    	if (err) {
    		printk("connect() failed, err: %d\n", errno);
    		goto clean_up;
    	}
    
    	off = 0;
    	do {
    		bytes = send(fd, &send_buf[off], HTTP_HEAD_LEN - off, 0);
    		if (bytes < 0) {
    			printk("send() failed, err %d\n", errno);
    			goto clean_up;
    		}
    		off += bytes;
    	} while (off < HTTP_HEAD_LEN); // to be sure that everything is sent 
    
    	printk("Sent %d bytes\n", off);
    
    	off = 0;
    	do {
    		bytes = recv(fd, &recv_buf[off], RECV_BUF_SIZE - off, 0);
    		printk("bytes are %d \n", bytes);
    		if (bytes < 0) {
    			printk("recv() failed, err %d\n", errno);
    			goto clean_up;
    		}
    		off += bytes;
    	} while (bytes != 0 /* peer closed connection */);
    
    	printk("Received %d bytes\n", off);
    
    	/* Make sure recv_buf is NULL terminated (for safe use with strstr) */
    	if (off < sizeof(recv_buf)) {
    		recv_buf[off] = '\0';
    	} else {
    		recv_buf[sizeof(recv_buf) - 1] = '\0';
    	}
    
    	/* Print HTTP response */
    	p = strstr(recv_buf, "\r\n");
    	if (p) {
    		off = p - recv_buf;
    		recv_buf[off + 1] = '\0';
    		printk("\n>\t %s\n\n", recv_buf);
    	}
    
    	printk("Finished, closing socket.\n");
    
    clean_up:
    	freeaddrinfo(res);
    	(void)close(fd);
    
    	lte_lc_power_off();
    }
    
    void main(void)
    {
    	int i = 0;
    	k_msleep(1000);
    
    	while(1){
    		printk("i=%d \n", i);
    		network_connection();
    		i+=1;
    	}
    }
    

    Here is also my output for the log:

    2022-07-15T13:09:07.315Z DEBUG modem >> AT+CGMR
    2022-07-15T13:09:07.331Z DEBUG modem << mfw_nrf9160_1.3.2
    2022-07-15T13:09:07.334Z DEBUG modem << OK
    2022-07-15T13:09:18.891Z DEBUG modem >> AT%MDMEV=1
    2022-07-15T13:09:18.906Z DEBUG modem << OK
    2022-07-15T13:09:25.299Z DEBUG modem >> AT%XMODEMSLEEP=1,1000,60000
    2022-07-15T13:09:25.318Z DEBUG modem << %XMODEMSLEEP: 4
    2022-07-15T13:09:25.324Z DEBUG modem << OK
    2022-07-15T13:09:33.364Z DEBUG modem >> AT+CEREG=5
    2022-07-15T13:09:33.380Z DEBUG modem << OK
    2022-07-15T13:09:40.795Z DEBUG modem >> AT%CESQ=1
    2022-07-15T13:09:40.811Z DEBUG modem << OK
    2022-07-15T13:09:47.675Z DEBUG modem >> AT%XSYSTEMMODE?
    2022-07-15T13:09:47.693Z DEBUG modem << %XSYSTEMMODE: 1,0,0,0
    2022-07-15T13:09:47.695Z DEBUG modem << OK
    2022-07-15T13:09:55.704Z DEBUG modem >> AT+CFUN=1
    2022-07-15T13:09:55.749Z DEBUG modem << %XMODEMSLEEP: 4,0
    2022-07-15T13:09:55.752Z DEBUG modem << OK
    2022-07-15T13:10:03.665Z DEBUG modem << %MDMEV: SEARCH STATUS 1
    2022-07-15T13:11:04.761Z DEBUG modem << %CESQ: 31,1,10,1
    2022-07-15T13:11:04.778Z DEBUG modem << +CEREG: 2,"0FC8","0114E209",7
    2022-07-15T13:11:09.753Z DEBUG modem << %MDMEV: SEARCH STATUS 2
    2022-07-15T13:11:09.757Z DEBUG modem << +CEREG: 5,"0FC8","0114E209",7,,,"11100000","11100000"
    2022-07-15T13:11:09.776Z DEBUG modem >> AT+COPS=3,2
    2022-07-15T13:11:09.783Z DEBUG modem << OK
    2022-07-15T13:11:09.787Z DEBUG modem >> AT+COPS?
    2022-07-15T13:11:09.804Z DEBUG modem << +COPS: 0,2,"22801",7
    2022-07-15T13:11:09.806Z DEBUG modem << OK
    2022-07-15T13:11:09.824Z DEBUG modem >> AT%XCBAND
    2022-07-15T13:11:09.827Z DEBUG modem << +CEREG: 2,"0FC8","0114E209",7
    2022-07-15T13:11:09.834Z DEBUG modem << %XCBAND: 20
    2022-07-15T13:11:09.836Z DEBUG modem << OK
    2022-07-15T13:11:09.843Z DEBUG modem >> AT+CGDCONT?
    2022-07-15T13:11:09.849Z DEBUG modem << OK
    2022-07-15T13:11:09.854Z DEBUG modem >> AT+CGACT?
    2022-07-15T13:11:09.862Z DEBUG modem << OK
    2022-07-15T13:11:11.261Z DEBUG modem << %CESQ: 255,0,255,0
    2022-07-15T13:11:11.275Z DEBUG modem << +CEREG: 2
    2022-07-15T13:11:13.460Z DEBUG modem << %CESQ: 31,1,11,1
    2022-07-15T13:11:13.828Z DEBUG modem << %CESQ: 23,1,9,1
    2022-07-15T13:11:17.027Z DEBUG modem << %CESQ: 23,1,4,0
    2022-07-15T13:11:38.158Z DEBUG modem << %CESQ: 18,0,0,0
    2022-07-15T13:13:03.588Z DEBUG modem << %CESQ: 23,1,4,0
    2022-07-15T13:13:37.828Z DEBUG modem << %CESQ: 26,1,9,1
    2022-07-15T13:13:53.507Z DEBUG modem << %CESQ: 25,1,4,0
    2022-07-15T13:14:08.483Z DEBUG modem << +CEREG: 0
    2022-07-15T13:14:08.495Z DEBUG modem << %CESQ: 255,0,255,0
    2022-07-15T13:14:08.507Z DEBUG modem << %XMODEMSLEEP: 3
    2022-07-15T13:14:09.330Z DEBUG modem << %XMODEMSLEEP: 3,0

    Thank you again Slight smile

    Carole

Related