首先,对不起我的英语不好,我是法语。 :)使用Arduino以太网盾对webserveur的POST请求
好的,所以我的目标是定期向Web服务器发送温度数据。我在Raspberry Pi计算机上使用LAMP服务器,温度由连接到以太网屏蔽层的Arduino板进行测量。为此,我在Arduino端设置了一个POST请求,将变量“temp”的值发送给服务器。
这部分似乎正常工作,因为client.read()
函数的结果很好,并且与我在Raspberry Pi上托管的页面test.php的结果相匹配。
在这里你可以看到我的Arduino脚本:
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xF6, 0xFF };
byte ip[] = { 192, 168, 0, 9};
byte gateway[] = { x,x,x,x };
EthernetClient client;
String temp= "data=5";
void setup()
{
Ethernet.begin(mac, ip, gateway);
Serial.begin(9600);
Serial.println(Ethernet.localIP());
delay(1000);
delay(1000);
Serial.println("connecting...");
if (client.connect("192.168.0.55",80))
{
Serial.println("Sending to Server: ");
client.println("POST /test.php HTTP/1.1");
Serial.print("POST /test.php HTTP/1.1");
client.println("Host: 192.168.0.55");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Connection: close");
client.println("User-Agent: Arduino/1.0");
client.print("Content-Length: ");
client.println(temp.length());
client.println();
client.print(temp);
client.println();
}
else
{
Serial.println("Cannot connect to Server");
}
}
void loop()
{
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected())
{
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;)
;
}
}
这是我的文件test.php的:
<?php
echo 'Temperature = ' . htmlspecialchars($_POST["data"]) . '!';
?>
client.read(的结果)Arduino的串行终端上为5 ,这证明POST请求和PHP部分正在工作。 但是,如果我通过url:192.168.0.55/test.php访问浏览器,则只会显示“Temperature =”。值(5)缺失。 所以如果有人知道如何直接在我的浏览器上显示值,它可以帮助我很多。
问候 纪尧姆
如果重新感兴趣,可能是更容易使用的数据库,你可能想看看谷歌App Engine的数据存储。 Google App Engine现在也支持PHP。 [谷歌应用程序引擎(https://developers.google.com/appengine/) [PHP入门(https://developers.google.com/appengine/docs/php/gettingstarted/introduction) – 2013-06-23 14:48:07