Page 1 of 1
HTTP Client for Chibios with LWiP
Posted: Sun Apr 13, 2014 8:22 pm
by pawelc06
Hi all,
I was trying to run HTTP client using Chibios and LWIP demo example for stm32 F4 Discovery.
I'm using sockets from LWip.
I wrote client thread which connects (lwip_connect), then sends HTTP request to server (lwip_write), then reads the response (lwip_read).
After running it:
a) connects, but hangs up on blocking lwip_read
b) doesn't connect and returns err_code -10
Has somebody succeeded with connection using Chibios and LWiP?
Could you provide a snippet of code+settings of lwipopts.h?
One remark: I need to download about 100 kB png image from the website.
Best regards
Pawel
Re: HTTP Client for Chibios with LWiP
Posted: Mon Apr 14, 2014 5:19 pm
by pawelc06
Here is the code I use:
Code: Select all
#include "ch.h"
#include "lwip/opt.h"
#include "lwip/arch.h"
#include "lwip/api.h"
#include "lwip/sockets.h"
#define PORT 80
#include "client.h"
//#include <string.h>
static char *http_request =
"GET / HTTP/1.1\r\nHost: 192.168.2.100\r\nConnection: keep-alive\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\nUser-Agent: HTTPClient\r\n\r\nAccept-Encoding: gzip,deflate,sdch\r\nAccept-Language: vi,en-US;q=0.8,en;q=0.6\r\n\r\n";
/**
* Stack area for the http thread.
*/
WORKING_AREA(wa_http_client, WEBC_THREAD_STACK_SIZE);
/**
* HTTP server thread.
*/
msg_t http_client(void *p) {
err_t err;
(void)p;
int sock;
char recv_buf[1500];
int res;
struct sockaddr_in serv_addr;
uint32_t bytes;
lwip_socket_init();
if ((sock = lwip_socket(AF_INET, SOCK_STREAM, 0)) < 0) {
LWIP_DEBUGF(SOCKETS_DEBUG,("Error creating socket\n\r"));
return -1;
}
serv_addr.sin_len = sizeof(serv_addr);
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = PP_HTONS(PORT);
//serv_addr.sin_addr.s_addr = inet_addr("140.211.11.131");
serv_addr.sin_addr.s_addr = inet_addr("192.168.2.100");
int opt = 3000;
res = lwip_setsockopt(sock , SOL_SOCKET, SO_RCVTIMEO, &opt, sizeof(int));
res = lwip_connect(sock, (struct sockaddr *)&serv_addr, sizeof (serv_addr));
res = get_err(sock);
if (res < 0) {
LWIP_DEBUGF(SOCKETS_DEBUG,("Could not connect!!!\n\r"));
lwip_close(sock);
} else {
LWIP_DEBUGF(SOCKETS_DEBUG,("Connected!!!\n\r"));
bytes = lwip_write(sock, http_request, sizeof(http_request));
res = get_err(sock);
LWIP_DEBUGF(SOCKETS_DEBUG,("Bytes wrote: %d\n\r",bytes));
res = lwip_read(sock, recv_buf, 150);
res = get_err(sock);
LWIP_DEBUGF(SOCKETS_DEBUG,("Read return code: %d\n\r",res));
LWIP_DEBUGF(SOCKETS_DEBUG,("Buf%s\n\r",recv_buf));
lwip_close(sock);
}
/* Goes to the final priority after initialization.*/
chThdSetPriority(WEB_THREAD_PRIORITY);
while(1) {
;
}
return RDY_OK;
}
/** @} */
Re: HTTP Client for Chibios with LWiP
Posted: Mon Apr 14, 2014 6:26 pm
by Giovanni
Sorry if I can't answer this but my experience with lwIP does not go beyond the small HTTP demo provided in ChibiOS. I know there are users much more expert with it, hopefully somebody will answer this.
Giovanni
Re: HTTP Client for Chibios with LWiP
Posted: Tue Apr 15, 2014 8:30 pm
by pawelc06
Hi there,
I found the problem!
Whis was in line:
res = lwip_connect(sock, (struct sockaddr *)&serv_addr, sizeof (serv_addr));
Declaration of serv_addr was:
static char *http_request =
"GET / HTTP/1.0\r\nHost: 92.43.118.240\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\nUser-Agent: HTTPClient\r\n\r\nAccept-Encoding: gzip,deflate,sdch\r\nAccept-Language: vi,en-US;q=0.8,en;q=0.6\r\n\r\n";
so sizeof(serv_addr) was returning not the request length, but the length of data pointer = 4.
I changed it to:
bytes = lwip_write(sock, http_request, strlen(http_request));
Now it is working, but only when I connect /write/read/ close for the first time.
Next time I'm getting error -10 during lwip_connect attempt.
I'm just wondering, if closing of socket is correct, or should I maybe use shutdown ?
Re: HTTP Client for Chibios with LWiP
Posted: Tue Apr 15, 2014 8:38 pm
by pawelc06
Additional information: after some time - several minutes, connection is possible again.
So it looks like web server is resseting this "inproper" state after few minutes.
But my question : how to close the socket correctly remains valid.
Anybody?