我几天前问了一个相关的问题,现在尝试使用不同的方法实现我的目标,但它也没有用。我无法理解为什么,所以如果你知道我错过了什么,请发表评论:)
在我目前的设置中,Arduino(带有以太网盾2)尝试在服务器上启动php GET脚本并向其发送一个简单的值。 这是我的代码,只是尝试发送值42:
#include <Ethernet2.h> //library for ethernet functions
#include <SPI.h>
#include <Client.h> //library for client functions
// Ethernet settings
byte mac[] = {0x90,0xA2,0xDA,0x0D,0x8B,0xB3}; //Replace with your Ethernet shield MAC
byte ip[] = { 192,168,1,105}; //The Arduino device IP address
byte subnet[] = { 255,255,255,0};
byte gateway[] = { 192,168,0,1};
IPAddress server(134,119,45,10); // IP-adress of server arduino sends data to
EthernetClient client;
bool connected = false;
void setup(void)
{
Serial.begin(9600);
Serial.println("Initializing Ethernet.");
delay(1000);
Ethernet.begin(mac);
Serial.println("Status ");
analogReference(INTERNAL);
}
void loop(void){
if(!connected)
{
Serial.println("Not connected");
if (client.connect(server,80))
{
connected = true;
Serial.print("Status is ");
Serial.println("42");
Serial.println();
Serial.println("Sending to Server: ");
client.print("GET /php_get_write.php?state=");
Serial.print("GET /php_get_write.php?state=");
client.print("42");
Serial.print("42");
Serial.println();
client.println();
client.println("HTTP/1.1\r\n");
Serial.println();
Serial.println("HTTP/1.1\r\n");
client.println("Host: localhost\r\n");
Serial.println("Host: localhost\r\n");
client.println();
client.println("User-Agent: Arduino\r\n");
Serial.println("User-Agent: Arduino\r\n");
client.println("Accept: text/html\r\n");
Serial.println("Accept: text/html\r\n");
client.println();
Serial.println();
delay(1000);
}
else
{
Serial.println("Cannot connect to Server");
}
}
else
{
delay(1000);
while (client.connected() && client.available())
{
char c = client.read();
Serial.print(c);
}
Serial.println();
client.stop();
connected = false;
}
}
在服务器上,我有获取值的脚本并将其写入文件:
<?php
$file = "status.txt";
$status = $_GET["state"];
echo "Getting data from Arduino and writing to file...";
echo $_GET["state"];
file_put_contents ($file,$status);
?>
一个脚本,从文件中读取值并显示它:
<?php
echo ("Status of toilets:");
$data = file_get_contents("status.txt");
echo ($data);
?>
我很确定php代码是正确的,问题在于arduino或服务器的配置。由于我对这些主题的专业知识有限,所以我希望你能够知道出了什么问题,并指导我走上正确的道路! 可能是php.ini配置?或者是arduino代码?
提前感谢您的帮助!
答案 0 :(得分:1)
我没有arduino,因此无法帮助您找到具体的答案。
但有两个提示和一个预感:
http://134.119.45.10/php_get_write.php?state=42
,看看是否有效