Post 使用Arduino以太网屏蔽在webserveur上发布请求
首先,很抱歉我的英语不好,我是法国人 好的,我的目标是定期向web服务器发送温度数据。我使用Raspberry Pi计算机上的LAMP服务器,温度是从连接到以太网屏蔽的Arduino板测量的。为此,我在Arduino端设置了一个POST请求,将变量“temp”的值发送到服务器 这部分似乎工作正常,因为Post 使用Arduino以太网屏蔽在webserveur上发布请求,post,webserver,request,arduino,ethernet,Post,Webserver,Request,Arduino,Ethernet,首先,很抱歉我的英语不好,我是法国人 好的,我的目标是定期向web服务器发送温度数据。我使用Raspberry Pi计算机上的LAMP服务器,温度是从连接到以太网屏蔽的Arduino板测量的。为此,我在Arduino端设置了一个POST请求,将变量“temp”的值发送到服务器 这部分似乎工作正常,因为client.read()函数的结果很好,并且与我在Raspberry Pi上托管的page test.php的结果相匹配 在这里您可以看到我的Arduino脚本: #include <Eth
client.read()
函数的结果很好,并且与我在Raspberry Pi上托管的page 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(;;)
;
}
}
#包括
#包括
字节mac[]={0x90,0xA2,0xDA,0x0D,0xF6,0xFF};
字节ip[]={192,168,0,9};
字节网关[]={x,x,x};
以太网络客户端;
字符串temp=“data=5”;
无效设置()
{
以太网开始(mac、ip、网关);
Serial.begin(9600);
Serial.println(Ethernet.localIP());
延迟(1000);
延迟(1000);
Serial.println(“连接…”);
if(客户端连接(“192.168.0.55”,80))
{
Serial.println(“发送到服务器:”);
client.println(“POST/test.php HTTP/1.1”);
Serial.print(“POST/test.php HTTP/1.1”);
client.println(“主机:192.168.0.55”);
client.println(“内容类型:application/x-www-form-urlencoded”);
client.println(“连接:关闭”);
client.println(“用户代理:Arduino/1.0”);
client.print(“内容长度:”);
client.println(temp.length());
client.println();
客户端打印(临时);
client.println();
}
其他的
{
Serial.println(“无法连接到服务器”);
}
}
void循环()
{
if(client.available()){
char c=client.read();
连续打印(c);
}
如果(!client.connected())
{
Serial.println();
Serial.println(“断开”);
client.stop();
对于(;;)
;
}
}
这是我的文件test.php:
<?php
echo 'Temperature = ' . htmlspecialchars($_POST["data"]) . '!';
?>
Arduino串行终端上client.read()的结果是5,这证明POST请求和PHP部分工作正常。
但是,如果我在浏览器的url:192.168.0.55/test.php上运行,则只会显示“Temperature=”。缺少值(5)。
因此,如果有人知道如何在我的浏览器上直接显示值,这对我有很大帮助
问候
Guillaume好吧,您做得不对,但您没有正确理解整个http请求概念,因此需要很长时间才能详细解释。简而言之: 您的arduino正在向服务器发送一个post http请求。因为您没有指定页面,所以它的目标是默认页面,很可能是index.php。您应该在那里捕获POST数据并将其存储在某个地方,很可能是一个DB,但也可能是一个文件 当您显示test.php页面时,在该请求期间没有发布数据(这不是由arduino控制器启动的数据),因此没有显示任何内容。您应该做的是查询数据库以显示上一步中存储的数据 编辑: 以下是您应该做的几步:
嗯,你做得不对,但这是整个http请求的概念,你没有得到正确的,所以需要很长的时间来详细解释。简而言之: 您的arduino正在向服务器发送一个post http请求。因为您没有指定页面,所以它的目标是默认页面,很可能是index.php。您应该在那里捕获POST数据并将其存储在某个地方,很可能是一个DB,但也可能是一个文件 当您显示test.php页面时,在该请求期间没有发布数据(这不是由arduino控制器启动的数据),因此没有显示任何内容。您应该做的是查询数据库以显示上一步中存储的数据 编辑: 以下是您应该做的几步:
谢谢你的快速回答,巴图德!因此,如果我在我的“test.php”文件上创建并设置一个数据库(例如MySql或Sqlite),它就会工作,并且我将能够显示数据?嗯。。。你对PhP到底了解多少?有