我是 arduino 和 PHP 的新手,我正在尝试将带有以太网屏蔽的 arduino uno 的温度和湿度读数发送到 PHP 网页,然后从 PHP 网页控制风扇、一些阀门等。我的第一个问题是我无法在 PHP 网页上实际显示温度读数。请参阅随附的 arduino 代码和 PHP 代码。我真的很感激这方面的任何帮助或建议。
谢谢
Arduino代码:
#include <Ethernet.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(192,168,1,110); // IP-adress of server arduino sends data to
EthernetClient client;
bool connected = false;
int sensorInPin = 0;
float temperature = 0;
void setup(void)
{
Serial.begin(9600);
Serial.println("Initializing Ethernet.");
delay(1000);
Ethernet.begin(mac);
Serial.println("LM35 Sensor ");
analogReference(INTERNAL);
}
void printTenths(int value)
{
// prints a value of 123 as 12.3
Serial.print(value / 100);
Serial.print(".");
Serial.println(value % 10);
}
void loop(void)
{
int span = 20;
int aRead = 0;
for (int i = 0; i < span; i++)
{
aRead = aRead+analogRead(sensorInPin);
}
aRead = aRead / 20;
temperature =((100*1.1*aRead)/99)*10;
if(!connected)
{
Serial.println("Not connected");
if (client.connect(server,80))
{
connected = true;
//int temp = analogRead(A1);
Serial.print("Temp is ");
Serial.println(temperature);
Serial.println();
Serial.println("Sending to Server: ");
client.print("GET /index.php?t0=");
Serial.print("GET /index.php?t0=");
client.print(temperature);
Serial.print(temperature);
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 代码
<?php
echo date("d.m.Y-H:i:s") . " Temperature= " . $_GET['t0'];
?>