cancel
Showing results for 
Search instead for 
Did you mean: 

LWIP send data

zavodny
Associate II
Posted on March 18, 2014 at 20:08

Hi,

I use LWIP and FreeRTOS like WEB server. It's OK. But now, I want send data to otger page for example by GET. Like this: http://www.page.com/p.php?json=1234 Can you help me please? Here is my code:


portCHAR PAGE_BODY[512];

struct
netconn *conn1 = NULL;


struct
ip_addr local_ip; 

struct
ip_addr remote_ip; 

int
rc1, rc2; 


strcat
((
char
*) PAGE_BODY, 
''HTTP/1.0 ''
);

strcat
((
char
*) PAGE_BODY, 
''Method: POST\r\n''
);

strcat
((
char
*) PAGE_BODY, 
''Server: Ethernet termostat\r\n''
);

strcat
((
char
*) PAGE_BODY, 
''Content-Length: 9\r\n''
);

strcat
((
char
*) PAGE_BODY, 
''json=1234\r\n\r\n''
);

strcat
((
char
*) PAGE_BODY, 
''Content-type: application/x-www-form-urlencoded\n\n''
);



/* Create a new TCP connection handle */

conn1 = netconn_new(NETCONN_TCP);


if
(conn1!= NULL)

{

local_ip.addr = NULL;

rc1 = netconn_bind ( conn1, &local_ip, 0 ); 

IP4_ADDR(&remote_ip, 46, 28, 105, 62);

//remote_ip.addr = remote_ip; // static or by netconn_gethostbyname ()

rc2 = netconn_connect ( conn1, &remote_ip, 80 ); 


if
( rc1 != ERR_OK || rc2 != ERR_OK )

{


netconn_write(conn1, PAGE_BODY, 
strlen
(PAGE_BODY), NETCONN_COPY);

netconn_delete ( conn1 );

}

}

else

{

printf
(
''can not create netconn''
);

}

#lmgtfy:-stm32-resources #send-data-frm-stm32-to-webserver
18 REPLIES 18
Posted on March 18, 2014 at 20:42

Likely not the issue here, but you really should ensure the string space is NUL terminated before using strcat(), ideally the first should be strcpy(). And the double LF/CR terminating the request should surely be at the end, followed by ''Content-Length'' of actual data.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
zavodny
Associate II
Posted on March 18, 2014 at 20:55

Thanks, but this code has stopped at line 17. And I don't know why? This row is OK, when I using web server.

zavodny
Associate II
Posted on March 18, 2014 at 21:18

Now, all is OK. But data is not received in page. Where is mistake?

zavodny
Associate II
Posted on March 18, 2014 at 21:34

Now I try this:


struct
netconn *conn1 = NULL;


struct
ip_addr local_ip; 

struct
ip_addr remote_ip; 

int
rc1, rc2; 


/* Create a new TCP connection handle */

conn1 = netconn_new(NETCONN_TCP);


if
(conn1!= NULL)

{

local_ip.addr = NULL;

rc1 = netconn_bind ( conn1, NULL, 0 ); 

IP4_ADDR(&remote_ip, 46, 28, 105, 62);

//remote_ip.addr = remote_ip; // static or by netconn_gethostbyname ()

rc2 = netconn_connect ( conn1, &remote_ip, 80 ); 


if
( rc1 != ERR_OK || rc2 != ERR_OK )

{


netconn_write(conn1, 
''POST /save_data.php HTTP/1.0 Method: POST
Server: Ethernet termostat
Content-Length: 9
Content-type: application/x-www-form-urlencoded

json=1234
''
, 152, NETCONN_COPY);

netconn_delete ( conn1 );

}

}

It's working, but the web page isn't receive POST and nothing do.
Posted on March 18, 2014 at 22:03

static const char out[] = ''POST /save_data.php HTTP/1.0

Server: Ethernet Thermostat

Content-Length: 9

Content-Type: application/x-www-form-urlencoded



json=1234'';
 netconn_write(conn1, out, sizeof(out)-1, NETCONN_COPY);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
zavodny
Associate II
Posted on March 18, 2014 at 22:17

I try it and nothing. And sometimes at line 16 returns error. 

zavodny
Associate II
Posted on March 18, 2014 at 23:09

And what about IP address? I have web page on address termostat.vzap.eu/post.php and if I translate to IP address, then is 46.28.105.62. But if I write this IP address to web browser, then server return error. How I set netconn for this problem?

zavodny
Associate II
Posted on March 19, 2014 at 01:00

Now I analyze packets in wireshark. If I apply this command: netconn_connect ( conn1, &remote_ip, 80 ); Wireshark receive TCP packet. Then receive next, and next same named TCP Retransmission. Where is problem? Why PC don't send ACK? 

Posted on March 19, 2014 at 01:41

I'm not a big LwIP fan, I'm more of a Berkeley Sockets guy.

Do you need to specify a port somewhere? Should you expect a response from the Server? Should you be careful closing the connection?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..