问题
I need to send data from Arduino through the WiFi shield to my localhost server and update my database and I'm stuck not knowing where or how has it gone wrong. My Arduino can connect to both the internet and to my localhost server via port 80. My server is set up using xampp and my .php file is located in the htdocs directory.
I'm using HTTP POST on the server side, the code snippet for receiving the HTTP POST are as follow:
$test_str = "";
if(isset($_POST["test_str"])){
echo "OK";
$test_str = $_POST['test_str'];
}
else echo "POST not recieved ";
$lid = $test_str;
echo $lid;
$lid is just a variable I'm going to be using for later. My Arduino code:
String test_str = "";
int testing = 1000;
const char* host = "192.168.0.169";
const int httpPort = 80;
ESP8266Client client;
test_str = "test_str=";
test_str += testing;
void loop(){
if(client.connect(host, httpPort)){
Serial.print("Connected to server ");
client.println("POST /connect.php HTTP/1.1");
client.println("Host: 192.168.0.169");
client.println("User-Agent: Arduino/1.0");
client.println("Connection: close");
client.println("Accept: */*");
client.println("Content-Type: Application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(test_str.length());
client.println("");
client.println(test_str);
Serial.println(" POST Done");
}
}
The server page 192.168.0.169/connect.php should print OK and the received value but instead just showing POST not received.
来源:https://stackoverflow.com/questions/55688276/arduino-wifi-shield-failed-to-communicate-with-localhost-server