我在外部Web服务器上有一个PHP脚本。如果我在浏览器中查看www.example.com/test/myphp.php
,服务器就可以正常运行脚本。现在我希望服务器在我的Arduino这样说时运行脚本,而不是在浏览器中查看时。我希望Arduino远程触发脚本运行。
我是服务器/ php /网络的完全新手,所以任何提示都会受到赞赏,特别是使用GET synatx(如果这甚至是正确的命令?)。 Arduino有一个以太网屏蔽,可以很好地连接到服务器。下面的代码正确连接,但不像我在浏览器中打开www.example.com/test/myphp.php
时,从Arduino访问PHP文件时实际上并不运行:(
你有什么建议我可以修改这个以便在我的Arduino告诉它时运行PHP文件吗?
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "www.example.com";
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192,168,0,177);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("SERIAL: Failed to configure Ethernet using DHCP");
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("SERIAL: connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
client.print("GET /test/myphp.php");
client.println(" HTTP/1.1");
client.println("Host: www.example.com");
client.println("User-Agent: Arduino");
client.println("Accept: text/html");
client.println("Connection: close");
client.println();
}
else {
// if you didn't get a connection to the server:
Serial.println("SERIAL: connection failed");
}
}
void loop()
{
// if there are incoming bytes available, print them
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
while(true);
}
}
答案 0 :(得分:0)
不确定这个但是..我认为你应该在循环中包含这段代码。 不在它之外。
if (client.connect(server, 80)) {
client.print("GET /test/myphp.php");
client.println(" HTTP/1.1");
client.println("Host: www.example.com");
client.println("User-Agent: Arduino");
client.println("Accept: text/html");
client.println("Connection: close");
client.println();
}