1

我在外部 Web 服务器上有一个 PHP 脚本。如果我www.example.com/test/myphp.php在浏览器中查看,服务器运行脚本就好了。现在我希望服务器在我的 Arduino 这么说时运行脚本,而不是在我在浏览器中查看它时运行。我希望 Arduino 远程触发脚本运行。

我是服务器 / php / web 的完全新手,所以任何提示都将不胜感激,尤其是使用 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);
  }
}
4

1 回答 1

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();
}
于 2014-06-14T02:33:31.927 回答