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

Post http on interrupt

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

Parents
  • Is HTTP_HOST a valid addr?

    Does this project work: http_post.zip?

    Best regards,

    Simon

  • I try your function and this is the result:

    (15:18:17.972) wait+CEREG: 0,1,"C356","001172CE",9<CR>
    (15:18:18.999) OK<CR>
    (15:18:18.999) HTTP example
    (15:18:18.999) <CR>getaddrinfo err: 0
    (15:18:24.319) <CR>client_fd: 3
    (15:18:24.319) <CR>bind err: 0
    (15:18:24.319) <CR>connect err: 0
    (15:18:24.483) <CR>send err: 134
    (15:18:24.529) <CR>HTTP example
    (15:18:29.495) <CR>getaddrinfo err: 0
    (15:18:29.495) <CR>client_fd: 3
    (15:18:29.495) <CR>bind err: 0
    (15:18:29.542) <CR>connect err: 0
    (15:18:30.002) <CR>send err: 134
    (15:18:30.002) <CR>HTTP example
    (15:18:35.012) <CR>getaddrinfo err: 0
    (15:18:35.059) <CR>client_fd: 3
    (15:18:35.059) <CR>bind err: 0
    (15:18:35.059) <CR>connect err: 0
    (15:18:35.748) <CR>send err: 134

    And on ptsv2.com i see "hel" for payload and not "hello"..

  • It seems like it is working then. Could you test that code with your initial HTTP_HOST (not ptsv2.com)? If that isn't working, the problem is not with the code, but probably the server you are posting to.

    The reason you see "hel" instead of "hello" is probably because "content-length" is set to 3.

    You may consider using snprintf in your code, which allows you to dynamically change the message you are sending.

    Best regards,

    Simon

Reply
  • It seems like it is working then. Could you test that code with your initial HTTP_HOST (not ptsv2.com)? If that isn't working, the problem is not with the code, but probably the server you are posting to.

    The reason you see "hel" instead of "hello" is probably because "content-length" is set to 3.

    You may consider using snprintf in your code, which allows you to dynamically change the message you are sending.

    Best regards,

    Simon

Children
No Data
Related