Forum: Mikrocontroller und Digitale Elektronik Arduino Ethernet Problem


von Achim A. (Firma: augilabs) (womisa)


Lesenswert?

Hallo

ich habe von Reichel eine NETIO-230B Ethernet Steckdose. Dies 
funktioiert mit dem Firefox problemlos.

Beispiel:> 
http://192.168.1.212/tgi/control.tgi?login=p:admin:admin&port=0110

setzt die Steckdosen je nach Einstellung der Portangabe.

Ich wollte den GLEICHEN String mit Hilfe der Arduino Client Library 
absetzen. Aber das gelingt mir nicht. Laut Wireshark steht da TCP 
Protokoll und ist in mehrere Pakete aufgeteilt.
Sollte aber HTTP Protokoll sein und 1 Paket.

Das Standardbeispiel mit der Google suche geht problemlos....

Weiß jemand einen Rat und kann mir weiterhelfen?

Gruß
Achim


1
/*
2
  Web client
3
 
4
 This sketch connects to a website (http://www.google.com)
5
 using an Arduino Wiznet Ethernet shield. 
6
 
7
 Circuit:
8
 * Ethernet shield attached to pins 10, 11, 12, 13
9
 
10
 created 18 Dec 2009
11
 by David A. Mellis
12
 modified 9 Apr 2012
13
 by Tom Igoe, based on work by Adrian McEwen
14
 
15
 */
16
17
#include <SPI.h>
18
#include <Ethernet.h>
19
20
// Enter a MAC address for your controller below.
21
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
22
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
23
byte gateway[] = { 192,168,1,254};
24
// the subnet:
25
byte subnet[] = { 255, 255, 255, 0 };
26
String str="";
27
28
// if you don't want to use DNS (and reduce your sketch size)
29
// use the numeric IP instead of the name for the server:
30
IPAddress server(192,168,1,212);
31
32
// numeric IP for Google (no DNS)
33
//char server[] = "www.google.com";    // name address for Google (using DNS)
34
//char server[] = "www.augilabs.de";    // name address for Google (using DNS)
35
36
37
// Set the static IP address to use if the DHCP fails to assign
38
IPAddress ip(192,168,1,177);
39
40
// Initialize the Ethernet client library
41
// with the IP address and port of the server 
42
// that you want to connect to (port 80 is default for HTTP):
43
EthernetClient client;
44
45
void setup() {
46
 // Open serial communications and wait for port to open:
47
  Serial.begin(9600);
48
 //  while (!Serial) {
49
 //   ; // wait for serial port to connect. Needed for Leonardo only
50
 // }
51
52
  // start the Ethernet connection:
53
 /*
54
  if (Ethernet.begin(mac) == 0) {
55
    Serial.println("Failed to configure Ethernet using DHCP");
56
    // no point in carrying on, so do nothing forevermore:
57
    // try to congifure using IP address instead of DHCP:
58
    Ethernet.begin(mac, ip);
59
  }
60
 */
61
 // initialize the ethernet device
62
  Ethernet.begin(mac, ip, gateway, subnet);
63
//     Ethernet.begin(mac, ip);
64
65
  // give the Ethernet shield a second to initialize:
66
  delay(1000);
67
  Serial.println("connecting...");
68
69
  // if you get a connection, report back via serial:
70
71
  if (client.connect(server, 80)) {
72
     Serial.print("connected to");
73
     Serial.println(server);
74
  
75
    // Make a HTTP request:
76
/*
77
   client.print("GET /tgi/control.tgi?login");
78
   client.print("=");
79
   client.print("p:admin:admin");
80
   client.print("&port=list");
81
   client.println(" HTTP/1.1");
82
    client.println("User-Agent: Arduino");
83
    client.println("Accept: text/html,application/xhtml+xml,application/xml;q=0.9;q=0.8");
84
    client.println();
85
    client.stop();
86
*/
87
   client.println("GET /tgi/control.tgi?login=p:admin:admin&port=0110 HTTP/1.1 ");
88
89
   } 
90
  else {
91
    // kf you didn't get a connection to the server:
92
    Serial.println("connection failed");
93
  }
94
95
}
96
97
void loop()
98
{
99
//      Serial.println("Ich bin im LOOP");
100
101
  // if there are incoming bytes available 
102
  // from the server, read them and print them:
103
  if (client.available()) {
104
    char c = client.read();
105
    Serial.print(c);
106
  }
107
108
  // if the server's disconnected, stop the client:
109
  if (!client.connected()) {
110
    Serial.println();
111
    Serial.println("disconnecting.");
112
    client.stop();
113
114
    // do nothing forevermore:
115
    while(true);
116
  }
117
}

von Sebastian W. (sebastian_w29)


Lesenswert?

HTTP basiert auf TCP.

Die Arduino Ethernet-Library setzt jeden einzelnen print(...) als 
einzelnes TCP-Paket ab, um nicht puffern zu müssen.

In deinem Code fehlt die obligatorische Leerzeile hinter dem HTTP-Header 
(siehe http://tools.ietf.org/html/rfc2616).

Versuch mal client.print("GET 
/tgi/control.tgi?login=p:admin:admin&port=0110 HTTP/1.1\r\n\r\n");

LG, Sebastian

von Achim A. (Firma: augilabs) (womisa)


Lesenswert?

Hi Sebastian,

vielen Dank für den Hinweis

>>>
Die Arduino Ethernet-Library setzt jeden einzelnen print(...) als
einzelnes TCP-Paket ab, um nicht puffern zu müssen.
>>>

..ich habe mehre Varianten probiert. Mit Deinem Hinweis kann ich jetzt 
die Steckdosen schalten.

Vielen Dank
Achim

von womisa (Gast)


Lesenswert?

Hallo Forengemeinde,

ich weiß  dass dieses hier kein Arduino Forum ist, aber mir wurde hier 
schon oft geholfen.
Ich habe mit meinem Arduino Ethernet-Client zur Steuerung meiner 
Netzwerksteckdos ein für mich unüberwindbares Problem.....

Ich schicke unter anderem zur Statusabfrage folgendes Kommando an die 
Steckdose:
1
    client.print("GET /cgi/control.cgi?login=p:admin:admin&port=list HTTP/1.1\r\nHost: 192.168.1.212 Connection: keep-alive\r\n\r\n");

das funktioniert auch soweit ganz gut.

Ziel ist es aber auch einzelne Steckdosen Ein/Aus zu schalten. Dazu 
möchte ich den zu sendenden String dynamisch zusammenbauen.

Erster nicht erfolgreicher Test hierzu ist folgendes:
1
 
2
....
3
String strRequest="GET /cgi/control.cgi?login=p:admin:admin&port=list 
4
HTTP/1.1\r\nHost: 192.168.1.212 Connection: keep-alive\r\n\r\n";
5
.
6
.
7
.
8
       client.print(strRequest);
9
10
....

Ich habe also nichts anderes gemacht, wie das "Kommando" in einen String 
gespeichert und dann abgesetzt.

Leider macht dieses "client.print(strRequest)" mit einer Stringvariablen 
laut Wireshark wieder mehrere TCP Pakete und nicht einen einzigen HTTP 
Request wie bei der funktionierenden Variante. Und das versteht die 
Steckdos nicht.

Hat mir Jemand einen Lösungsvorschlag? Bin für alle Tipps, die mich der 
Lösung näher bringen dankbar.

Gruß
Achim

von womisa (Gast)


Lesenswert?

Hi

ok. Ich habe ne Lösung gefunden. Anstatt ein String habe ich ein char 
Array genommen.....

Warum auch immer, dann gehts.

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.