Hello,
I have another problem... i work on nrf9160 dk
I try to send http post when i pressed button 2 but this doesn't work..
Below you could see my code who block on printk "http example" and getadrrinfo don't start..
/**************************************************************************************************** *name: *to do: Send send_buf[] to http post *return: ****************************************************************************************************/ void app_http_start(void) { 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; printk("HTTP example\n\r"); 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[] = //"GET / HTTP/1.1\r\nHost: www.google.com:80\r\nConnection: close\r\n\r\n"; "POST / HTTP/1.0\r\nHost: lup.fr\r\nContent-Type: application/json\r\nContent-Length: 41\r\n\r\n\{\"payload\": \"trame NBIOT de nrf9160 v01\"\}\x1A"; int send_data_len = strlen(send_buf); 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); err = blocking_connect(client_fd, (struct sockaddr *)res->ai_addr, sizeof(struct sockaddr_in)); printk("connect err: %d\n\r", err); int num_bytes = send(client_fd, send_buf, send_data_len, 0); printk("send err: %d\n\r", num_bytes); int tot_num_bytes = 0; do { /* TODO: make a proper timeout * * Current solution will just hang * until remote side closes connection */ num_bytes = blocking_recv(client_fd, recv_buf, RECV_BUF_SIZE, 0); tot_num_bytes += num_bytes; if (num_bytes <= 0) { break; } printk("%s", recv_buf); } while (num_bytes > 0); printk("\n\rFinished. Closing socket\n\r"); freeaddrinfo(res); err = close(client_fd); } /**************************************************************************************************** *name: *todo: Fonction interrupt *return: ****************************************************************************************************/ void button_pressed(struct device *gpiob, struct gpio_callback *cb, u32_t pins) { printk("Send data\n"); struct device *dev; dev = device_get_binding(LED_PORT); gpio_pin_configure(dev, LED, GPIO_DIR_OUT); u32_t val = 0U; gpio_pin_read(gpiob, PIN, &val); if(val == 0) { app_http_start(); gpio_pin_write(dev, LED, 1); } else if (val == 1) { gpio_pin_write(dev, LED, 0); } } static struct gpio_callback gpio_cb; void main(void) { struct device *gpiob; printk("Press the user defined button on the board\n"); gpiob = device_get_binding(PORT); if (!gpiob) { printk("error\n"); return; } gpio_pin_configure(gpiob, PIN, GPIO_DIR_IN | GPIO_INT | PULL_UP | EDGE | GPIO_INT_DOUBLE_EDGE | GPIO_INT_ACTIVE_LOW | GPIO_INT_ACTIVE_HIGH | GPIO_INT_DEBOUNCE); gpio_init_callback(&gpio_cb, button_pressed, BIT(PIN)); gpio_add_callback(gpiob, &gpio_cb); gpio_pin_enable_callback(gpiob, PIN); app_socket_start_ON(); Wait_connection(); while (1) { u32_t val = 0U; gpio_pin_read(gpiob, PIN, &val); k_sleep(SLEEP_TIME); } }
Thank's for your help