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

Parents
  • 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 

Reply
  • 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 

Children
  • 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