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

nRF9160 DK HTTP POST to my Webserver

Dear , 

I want to use LTE_M of nRF9160 DK to POST to my webserver

Do you have any suggestions?

Thanks, 

Regards

hng

Parents Reply Children
  • It works, just fixed a little bit: 

    /******************************************"**************************************/
    #define MAXLINE 4096
    #define HTTP_HOST "142.xx.xx.192"  //my webserver
    #define HTTP_PORT 80
    #define HTTP_PATH "/"
    #define TEST_STRING  "Hello from nRF9160 DK"
    
    #define POST_TEMPLATE "POST %s? HTTP/1.1\r\n"\
    		"Host: %s\r\n"\
    		"Connection: keep-alive\r\n"\
    	        "Content-type: application/x-www-form-urlencoded\r\n"\
    		"Content-length: %d\r\n\r\n"\
    		"%s"
    /************************************************************************/
    void app_http_post()
    {  
    	//int err; 
        struct sockaddr_in local_addr;
    	struct addrinfo *res;
    
    	local_addr.sin_family = AF_INET;
    	local_addr.sin_port = htons(0);
    	local_addr.sin_addr.s_addr = 0;
    
    	      
    	int err = getaddrinfo(HTTP_HOST, NULL, NULL, &res);
    
    	printk("getaddrinfo err: %d\n\r", err);
    	((struct sockaddr_in *)res->ai_addr)->sin_port = htons(HTTP_PORT);
            
            char send_buf[MAXLINE + 1];
    	
    	int send_data_len_post = snprintf(send_buf,
    				         500, /*total length should not exceed MTU size*/
    				         POST_TEMPLATE, HTTP_PATH,
    				         HTTP_HOST, strlen(TEST_STRING),
    				         TEST_STRING);
    
    	printk("send err: %d\n\r", send_data_len_post);
    
    
    	int client_fd = socket(AF_INET, SOCK_STREAM, 0);
    
    	printk("client_fd: %d\n\r", client_fd);
    	err = bind(client_fd, (struct sockaddr *)&local_addr,
    		   sizeof(local_addr));
    	printk("bind err: %d\n\r", err);
            // connect
    	err = blocking_connect(client_fd, (struct sockaddr *)res->ai_addr,
    			       sizeof(struct sockaddr_in));
    	printk("connect err: %d\n\r", err);
           //send
        blocking_send(client_fd, send_buf, send_data_len_post, 0);
        k_sleep(1000);
           
    	printk("\n\r HTTP POST Finished. \n\r");
    	close(client_fd);
    
    }

    Thanks so much

    Regards, 

    hng

  • Dear

    I tried to run this http post in a loop

    first, i will check the LTE_M signal "AT+CEREG?"

    It returns:  

    Checking LTE_M signal.............
    
    +CEREG: 0,5,"765D","0110C303",7
    
    OK
    
    Found the LTE_M signal 
    Closing socket
    
      find LTE_M signal........... 
    Checking LTE_M signal.............
    
    +CEREG: 0,5,"765D","0110C303",7
    
    OK
    
    Found the LTE_M signal 
    Closing socket
    
      find LTE_M signal........... 
    Checking LTE_M signal.............
    
    +CEREG: 0,5,"765D","0110C303",7
    
    OK
    
    Found the LTE_M signal 
    Closing socket
    
      find LTE_M signal........... 
    Checking LTE_M signal.............
    
    +CEREG: 0,5,"765D","0110C303",7
    
    OK
    
    Found the LTE_M signal 
    Closing socket
    
      find LTE_M signal........... 
    Checking LTE_M signal.............
    
    +CEREG: 0,5,"765D","0110C303",7
    
    OK
    
    Found the LTE_M signal 
    Closing socket
    
      find LTE_M signal........... 
    Checking LTE_M signal.............
    
    +CEREG: 0,5,"765D","0110C303",7
    
    OK
    
    Found the LTE_M signal 
    Closing socket
    
      find LTE_M signal........... 
    Checking LTE_M signal.............
    
    +CEREG: 0,5,"765D","0110C303",7
    
    OK
    
    Found the LTE_M signal 
    Closing socket
    
      find LTE_M signal........... 
    Checking LTE_M signal.............
    
    Closing socket
    
     Couldnt find LTE_M signal........... 
    Checking LTE_M signal.............
    
    Closing socket
    
     Couldnt find LTE_M signal........... 
    Checking LTE_M signal.............
    

    After 7times, "AT+CEREG?" return NOTHING!

    Why is the problem, the buffer is full? or something wrong in my code? 

    Plz help me. 

    Thanks so much 

    Regards, 

    hng

  • I believe a bug in the BSD library causes this. I get the "no memory" error when call socket send in a loop without any delay in-between. I have reported this internally. A workaround, for now, is to add a delay between each post request. 

Related