我有一个外部web服务器上的PHP脚本。如果我在浏览器中查看www.example.com/test/myphp.php,服务器会很好地运行脚本。现在,我希望服务器在Arduino提示时运行该脚本,而不是在我的浏览器中查看该脚本时运行。我想让Arduino远程触发脚本运行。
我完全是一个服务器/ php / web的新手,所以任何提示都将不胜感激,特别是对于GET synatx (如果这是正确的命令?)。Arduino有一个以太网屏,可以很好地连接到服务器。下面的代码正确连接,但与我在浏览器中打开www.example.com/test/myphp.php时不同,当从Arduino访问PHP文件时,它实际上并不运行:(
你有什么建议可以让我修改这个代码,让PHP文件在我的Arduino命令下运行吗?
#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);
}
}发布于 2014-06-14 10:33:32
不能确定这一点,但是..我认为你应该在循环中包含这段代码。而不是在外面。
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();
}https://stackoverflow.com/questions/19938744
复制相似问题