首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Ethernet-Shield Arduino运行远程PHP脚本

从Ethernet-Shield Arduino运行远程PHP脚本
EN

Stack Overflow用户
提问于 2013-11-13 03:59:40
回答 1查看 6K关注 0票数 1

我有一个外部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命令下运行吗?

代码语言:javascript
复制
#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);
  }
}
EN

回答 1

Stack Overflow用户

发布于 2014-06-14 10:33:32

不能确定这一点,但是..我认为你应该在循环中包含这段代码。而不是在外面。

代码语言:javascript
复制
 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();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19938744

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档