dqwh1218 2013-11-12 19:59
浏览 47

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

I have a PHP script on an external web server. If I view www.example.com/test/myphp.php in my browser, the server runs the script just fine. Now I want the server to run the script when my Arduino says so, instead of when I view it in my browser. I want the Arduino to remotely trigger the script to run.

I am a complete newbie to servers / php / the web, so any tips would be appreciated, especially with the GET synatx (if that's even the right command?). The Arduino has an ethernet shield and can connect to the server fine. The below code correctly connects, but unlike when I open www.example.com/test/myphp.php in my browser, the PHP file doesn't actually run when it's accessed from the Arduino :(

Do you have any suggestions for how I can modify this to get the PHP file to run when my Arduino tells it to?

#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);
  }
}
  • 写回答

1条回答 默认 最新

  • dqgxazo4483 2014-06-14 02:33
    关注

    Not sure about this but.. i think that you should include this piece of code inside the loop. Not Outside it.

     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();
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分