首先,對不起我的英語不好,我是法語。 :)使用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