我有以下Arduino代码。它连接到服务器并将传感器的读数发送给它。来自服务器的回复始终是:
HTTP / 1.1 400错误请求
Arduino代码如下。
#include <Ethernet.h> //Library for Ethernet functions
#include <Client.h> //Library for client functions
#include <OneWire.h> //Library for the onewire bus
#include <SPI.h>
// Ethernet settings
uint8_t hwaddr[6] = {0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xBE}; // MAC address of Arduino
uint8_t ipaddr[4] = {192, 168, 0, 55}; // IP address of Arduino
uint8_t gwaddr[4] = {192, 168, 0, 1}; // IP address of gateway ( for later DNS implementation)
uint8_t subnet[4] = {255, 255, 255, 0}; // Subnet mask ( for later DNS implementation)
uint8_t serverip[4] = {192, 168, 0, 54}; // IP address of server arduino sends data to
uint8_t serverport = 80; // The port the Arduino talks to
EthernetClient client; // Make a new instance from type "Client" named "client", giving it
int numSensors; // a variable to store the number of sensors.
bool connected = false; // Yes-no variable (boolean) to store if the arduino is connected to the server.
int i = 0; // Variable to count the sendings to the server.
void setup() {
Serial.begin(9600); // Start the serial port
Serial.println("Initializing Ethernet.");
Ethernet. begin(hwaddr, ipaddr); // Start up Ethernet
}
void loop() {
if(!connected) { // If "not" connected print: not connected ;)
Serial.println("Not connected");
Serial.println("Connecting to server...");
if (client.connect(serverip, serverport)) { // If connected, set variable connected to "true" and
connected = true;
Serial.println("connected!!");
String data;
data+="";
data+="t0=";
data+=analogRead(A0);
// data+="&&";
// data+="t1=";
// data+=analogRead(A1);
// data+="&&";
// data+="t2=";
// data+=analogRead(A2);
Serial.println("Sending to Server: ");
Serial.println();
client.print("GET /formSubmit.php?" + data);
Serial.print("GET /formSubmit.php?" + data);
client.println(" HTTP/1.1");
Serial.println(" HTTP/1.1");
client.println("Host: http://localhost/PhpProject1");
Serial.println("Host: http://localhost/PhpProject1");
client.println("User-Agent: Arduino");
Serial.println("User-Agent: Arduino");
client.print("Content-Length: ");
Serial.print("Content-Length: ");
client.println(data.length());
Serial.println(data.length());
client.println("Accept: text/html");
Serial.println("Accept: text/html");
client.println("Connection: close");
Serial.println("Connection: close");
client.println();
Serial.println();
if (client.available()) {
char c = client.read();
Serial.print(c);
}
delay(10000);
}
else {
Serial.println("Cannot connect to Server"); // else block if the server connection fails (debugging)
}
}
else {
delay(500); //
while (client.connected() && client.available()) { // When connected and availabe:
char c = client.read(); // read the answer of the server and
Serial.print(c); // print it to serial port
}
//
Serial.println(); //
client.stop(); // Stop the connection and set
connected = false; // "connected" to false
}
}
上传此代码后,串行监视器上的输出为:
Initializing Ethernet.
Not connected
Connecting to server...
connected!!
Sending to Server:
GET /formSubmit.php?t0=433 HTTP/1.0
Host: http://localhost/PhpProject1
User-Agent: Arduino
Content-Length: 6
Accept: text/html
Connection: close
HTTP/1.1 400 Bad Request
Date: Sat, 03 Nov 2012 17:06:00 GMT
Server: Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7
Content-Length: 343
Connection: close
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7 Server at http://localhost/PhpProject1 Port 80</address>
</body></html>
可能是什么问题?
答案 0 :(得分:4)
您请求 错误。
Host
字段不得包含/
,请尝试:Host: localhost
Content-Length
在GET
方法\r\n
而不是\n
终止每一行(取决于服务器)