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

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

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

  • Dear

    I had 2s delay between

    How long should I add ?

    Thanks for fast responds, 

    Regards, 

    hng

  • A 2-second delay should be sufficient. Can you try to increase the stack sizes in your project *.conf file?

    e.g.,

    CONFIG_MAIN_STACK_SIZE= 8192

    CONFIG_HEAP_MEM_POOL_SIZE=8192

  • Dear

    I tried, the same thing happened  

    int nRF_LTE_check(void)
    {
        int err;
        const char *ATREG="AT+CEREG?"; 
    	int at_socket_fd = socket(AF_LTE, 0, NPROTO_AT);
        printk("Checking LTE_M signal.............\n\r");
    
    	if (at_socket_fd < 0) {
    		err = 1; 
    	}
        else {
        int bytes_written = send(at_socket_fd, ATREG, strlen(ATREG), 0);
        if (bytes_written > 0) {
    	int r_bytes = blocking_recv(at_socket_fd, recv_buf,sizeof(recv_buf), MSG_DONTWAIT);
    	if (r_bytes > 0) {
    	printk("%s", recv_buf);
        if (recv_buf== LTEfound) 
           {
          err = 0; 
          printk("Found LTE_M signal \n");
            }
        else {
           printk("Couldnt find the LTE_M signal \n");
           err =1;
            }
     }
      }
              }
    	printk("Closing socket\n\r");
        (void)close(at_socket_fd);
        return err; 
    }

    my loop: 

    #include <zephyr.h>
    #include <net/socket.h>
    #include <misc/printk.h>
    #include <zephyr/types.h>
    #include <math.h>
    #include <uart.h>
    #include <soc.h>
    #include <gpio.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>     /* atof */
    #include <autoconf.h>
    
    void main (void)
    {
      printk("Test is starting \n"); 
    
       
        
        while(1){
    
       
          int lte_err= nRF_LTE_check();
          if(lte_err==0){ printk("  find LTE_M signal........... \n"); }
          else{ printk(" Couldnt find LTE_M signal........... \n");}
          k_sleep(2000);
       
        }
       
    }

    and my prj.conf

    CONFIG_SERIAL=y
    CONFIG_TRUSTED_EXECUTION_NONSECURE=y
    CONFIG_UART_INTERRUPT_DRIVEN=y
    CONFIG_UART_1_NRF_UARTE=y
    CONFIG_UART_2_NRF_UARTE=y
    
    CONFIG_GPIO=y
    
    CONFIG_NETWORKING=y
    CONFIG_NET_BUF_USER_DATA_SIZE=1
    CONFIG_NET_SOCKETS_OFFLOAD=y
    CONFIG_NET_SOCKETS=y
    CONFIG_NET_SOCKETS_POSIX_NAMES=y
    CONFIG_NET_RAW_MODE=y
    CONFIG_BSD_LIBRARY=y
    
    
    CONFIG_STDOUT_CONSOLE=y
    
    CONFIG_TEST_RANDOM_GENERATOR=y
    
    CONFIG_LOG=n
    CONFIG_LOG_DEFAULT_LEVEL=4
    #CONFIG_HEAP_MEM_POOL_SIZE=1024
    CONFIG_MAIN_STACK_SIZE= 8192
    CONFIG_HEAP_MEM_POOL_SIZE=8192
    
    
    # LTE link control
    #CONFIG_LTE_LINK_CONTROL=y
    CONFIG_UART_2_NRF_TX_BUFFER_SIZE=1024

  • Can you upload your entire project directory here so I can try to debug it? Some of the variable definitions are missing from the code snippets above. 

Related