Forum: Mikrocontroller und Digitale Elektronik Html request mit lwIP


von Florian D. (fdsurfer)


Lesenswert?

Hallo,

ich verwende von Atmel den UC3 auf dem EVK1100. Außerdem die Libs von 
der ASF mit dem FreeRTOS 7 und dem lwIP stack 1.4.0.

Der WebServer läuft. Das heißt, ich bekomme bereits von meinem Router 
eine IP Adresse über den DHCP zugewisen und kann über diese IP auch den 
Web Server aufrufen.
Jetzt möchte ich das aber umgekehrt machen. Soll heißen, ich möchte eine 
Webseite im Internet aufrufen an die ich über post Daten übertrage.
Bisher habe ich das nur auf einen Lokalen Host mit get hin bekommen mit 
diesem Code:

Probleme sind also:
1. wie bekomme ich aus der festen IP eine Adresse wie www.ziel.de hin?

2. Wie baue ich den POST zugiff auf?
1
/*
2
 * html_request.c
3
 *
4
 * Created: 04.04.2015 20:35:00
5
 *  Author: Florian
6
 */ 
7
8
/* Standard includes. */
9
#include <stdio.h>
10
#include <string.h>
11
#include <stdarg.h>
12
13
#include "conf_eth.h"
14
15
/* Scheduler includes. */
16
#include "FreeRTOS.h"
17
#include "task.h"
18
19
20
21
22
#include "html_request.h"
23
#include "EthernetCom.h"
24
25
struct tcp_pcb *testpcb;
26
err_t error;
27
28
void tcp_setup(void)
29
{
30
  uint32_t data = 0xdeadbeef;
31
32
  ON_1_SIG_LED;
33
  /* create an ip */
34
  struct ip_addr ip;
35
  IP4_ADDR(&ip, 192,168,0,13);    //IP of my PHP server
36
37
  /* create the control block */
38
  testpcb = tcp_new();    //testpcb is a global struct tcp_pcb
39
  // as defined by lwIP
40
41
42
  /* dummy data to pass to callbacks*/
43
  tcp_arg(testpcb, &data);
44
  /* register callbacks with the pcb */
45
  
46
  tcp_bind(testpcb, &ip, 80);
47
  
48
  tcp_err(testpcb, tcpErrorHandler);
49
  tcp_recv(testpcb, tcpRecvCallback);
50
  tcp_sent(testpcb, tcpSendCallback);
51
52
  
53
  /* now connect */
54
  tcp_connect(testpcb, &ip, 80, connectCallback);
55
//ON_3_SIG_LED;
56
57
}
58
59
60
/* connection established callback, err is unused and only return 0 */
61
err_t connectCallback(void *arg, struct tcp_pcb *tpcb, err_t err)
62
{
63
  ON_2_SIG_LED;
64
  tcp_send_packet();
65
  return 0;
66
}
67
68
uint32_t tcp_send_packet(void)
69
{
70
  //char *string = "HEAD /process.php?data1=12&data2=5 HTTP/1.0\r\nHost: mywebsite.com\r\n\r\n ";
71
  char *string = "GET /process.php?data1=12&data2=5 HTTP/1.0\r\nHost: mywebsite.com\r\n\r\n ";
72
//  uint32_t len = strlen(string);
73
74
  /* push to buffer */
75
  error = tcp_write(testpcb, string, strlen(string), TCP_WRITE_FLAG_COPY);
76
77
  if (error) {
78
    return 1;
79
  }
80
81
  /* now send */
82
  error = tcp_output(testpcb);
83
  if (error) {
84
    return 1;
85
  }
86
  tcp_close(testpcb);
87
  ON_3_SIG_LED;
88
  return 0;
89
}
90
91
tcp_err_fn tcpErrorHandler(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
92
{
93
  //tcp_close(testpcb);
94
  OFF_1_SIG_LED;
95
  ON_2_SIG_LED;
96
  OFF_3_SIG_LED;
97
  
98
}
99
100
101
err_t tcpRecvCallback(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
102
{
103
  if (p == NULL) {
104
    //tcp_close(testpcb);
105
    return ERR_ABRT;
106
    //} else {
107
  }
108
109
  return 0;
110
}
111
112
113
err_t tcpSendCallback(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
114
{
115
  if (p == NULL) {
116
  ON_1_SIG_LED;
117
    //tcp_close_con();
118
  tcp_close(testpcb);
119
    return ERR_ABRT;
120
    } else {
121
  }
122
123
  return 0;
124
}
125
126
portTASK_FUNCTION( vhtml_Request, pvParameters )
127
{
128
  
129
  for (;;)
130
  {
131
    OFF_1_SIG_LED;
132
    OFF_2_SIG_LED;
133
    OFF_3_SIG_LED;
134
    
135
  tcp_setup();
136
137
    vTaskDelay(10000);
138
  }
139
}

Kann mir jemand vielleicht ein Beispiel für einen POST zugriff geben?

Danke,
Florian

von Rainer U. (r-u)


Lesenswert?

1) mit einer DNS-Abfrage. Entweder, Du suchst vorher manuell die 
IP-Adresse (z.B. mit nslookup www.ziel.de) oder falls dynamisch, musst 
Du Dein Programm erweitern, damit es DNS-Abfragen machen kann.

2) wie es ein PC-Browser auch macht, siehe hier:

http://de.wikipedia.org/wiki/Hypertext_Transfer_Protocol#HTTP_POST

Im konkreten Fall würdest Du also "tcp_send_packet" anpassen, also einen 
anderen String senden, der mit POST beginnt und wie im Link aufgebaut 
ist.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.