我正在从事一个项目与Arduino和以太网盾。
我想在循环中执行一个php脚本(驻留在我的服务器上)。
#include <SPI.h>
#include <Ethernet.h>
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,77); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
String HTTP_req; // stores the HTTP request
void setup()
{
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
Serial.begin(9600); // for diagnostics
}
void loop()
{
EthernetClient client = server.available(); // try to get client
if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
HTTP_req += c; // save the HTTP request 1 char at a time
Serial.print("connected");
client.println("GET http://domain.com/arduino/scripts/script_motion_detection_driveway.php HTTP/1.0");
client.println();
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
}当我运行代码并加载页面时,它不是执行脚本,而是打印以下代码:
GET http://domain.com/arduino/scripts/script_motion_detection_driveway.php HTTP/1.0一遍又一遍..。
它之所以在循环中,而不是在setup中,是因为GET请求最终将被放在一个if语句中来测试一个条件。
要执行脚本,我需要更改哪些内容?
发布于 2015-04-28 06:31:51
你应该检查这个例子:http://www.arduino.cc/en/Tutorial/WebClient
您希望在不通知客户端连接到服务器的情况下执行GET请求。
if (client.connect(server, 80)) {然后发出http请求。
// 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();然后,您尝试读取请求
if (client.available()) {
char c = client.read();
Serial.print(c);
}通过将这些部分以比上面的脚本更好的方式组合在一起,您将成功地获得php脚本的输出。
https://stackoverflow.com/questions/29568141
复制相似问题