arduino web客户端,使用php打开led



我需要帮助。现在mo Arduino连接到web服务器。在Web服务器上,我会有一个php页面,可以打开和关闭LED。

不幸的是,我不知道如何编写.php页面,也不知道如何和Arduino接口。我在网上搜索了很多,但没有找到对我有帮助的指南。

我已经做了同样的事情,但使用Arduino网络服务器,它工作得很好。我现在希望Arduino是客户端,另一个服务器向他们发送参数。

有人有什么想法吗?

这是我的草图(成功连接到服务器):

#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress server(xxx,xxx,xxx,xxx);  //
// char server[] = "www.google.com";    // name address for Google (using DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192,168,1,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);
  pinMode(2, OUTPUT);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    // client.println("GET /search?q=arduino HTTP/1.1");
    // client.println("Host: www.google.com");
    // client.println("Connection: close");
    client.println();
    digitalWrite(2, HIGH);
  } 
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}
void loop()
{
  // if there are incoming bytes available 
  // from the server, read them and 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);
  }
}

谢谢!

我正在分享我最后一年的btech项目"使用互联网控制设备"的代码链接。(当然,使用arduino+以太网作为服务器可以很容易地做到这一点,但问题是你需要将路由器端口转发,以便从本地网络外部访问服务器。从安全方面来看,端口转发风险很小。)

我使用了apache服务器(为了测试我安装在笔记本电脑中,后来我使用了托管网站)和Arduino+EthernetShield作为客户端。Arduino在获取XML文件后向服务器发送HTTP请求,解析XML并控制设备。我使用PHP创建UI和更新XML文件。。。。

正如你所问的"我现在希望Arduino是客户端,另一台服务器向他们发送参数"

在服务器中放入一个xml或文本文件。(现在我正在考虑test.xml,如果您想使用文本文件,请在文本文件中放入<1,0,1,0,1>)

<?xml version="1.0" encoding="utf-8"?>
<devices>
<device name="1">
<state>OFF</state>
</device>
<device name="2">
<state>ON</state>
</device>
</devices>

让您的arduino每秒向服务器发送一次xml文件的HTTP请求。

//sending HTTP request for xml file
client.println("GET /test.xml HTTP/1.1");
client.println( "Host: localhost");
client.println(); 

获取xml文件后,您需要解析xml文件,以读取状态标记中的值(ON或OFF)(检查arduino代码中的xmlread()函数来解析xml)

以下链接包含4个文件class.php、xmlupdate.php、test.xml,带有xmldevicecontrol.ino(arduino代码)

class.php用于通过xmlupdate.php创建UI(按钮等)和更新XML文件

xmlupdate.php将更新XML文件(test.XML

withxmldevicecontrol.ino是arduino代码,arduino从服务器获取text.xml文件,解析xml并控制设备。

使用这个arduino可以控制10个设备(使用端口扩展器IC可以控制许多设备)

代码链接:

https://drive.google.com/folderview?id=0BxWdBbr_6RYkSXVwcGxOa3pxTDA&usp=共享

最新更新