我接受了关于Arduino温度和湿度传感器的任务。我使用MySQL创建了一个数据库,我创建了一个PHP文件来显示和接收来自Arduino IDE的数据。然后我多次检查编码并对其进行了多次修改,但它仍然无法将数据发送给PHP。
这是我添加到MySQL的代码(read_temperature)
<?php
$server = 'localhost';
$username = 'root';
$password = '';
$db_name = 'datawebserver';
$conn = mysqli_connect($server, $username, $password, $db_name);
if(mysqli_connect_errno()) {
echo 'Connection to database failed : '.mysqli_connect_errno();
}
$ahum1 = $_POST['humidity'];
$&temp1 = $_POST['temperature'];
mysqli_query($conn, "INSERT INTO 'templog' ('temperature', 'humidity') VALUES ('".$&temp1."', '".$ahum1."')");
mysqli_close($conn);
header('Location: index.php');
?>
这是来自MySQL的显示数据代码:
<?php
// ======= start connect.php ======= //
$server = 'localhost'; // default : localhost
$username = 'root'; // default : root
$password = '';
$db_name = 'datawebserver';
$conn = mysqli_connect($server, $username, $password, $db_name);
// Check the connection
if(mysqli_connect_errno()) {
echo 'Connection to database failed : '.mysqli_connect_error();
}
// ======= end connect.php ======= //
$result = mysqli_query($conn, "SELECT * FROM templog ORDER BY timeStamp DESC");
?>
<html>
<head>
<title>Sensor Data</title>
</head>
<body>
<h1>Temperature / moisture sensor readings</h1>
<table border="1" cellspacing="1" cellpadding="1">
<tr>
<td> Timestamp </td>
<td> Temperature 1 </td>
<td> Moisture 1 </td>
</tr>
<?php
if($result) {
while($row = mysqli_fetch_array($result)) {
echo '<tr>';
echo '<td> '.$row['timeStamp'].' </td>';
echo '<td> '.$row['temperature'].' </td>';
echo '<td> '.$row['humidity'].' </td>';
echo '</tr>';
}
// free result set
mysqli_free_result($result);
}
// tutup connection mysqli
mysqli_close($conn);
?>
</table>
</body>
</html>
这是Arduino IDE的代码:
#include <SPI.h>
#include <Ethernet.h>
#include <DHT.h>
#include <String.h>
// ---------------------------------------------- Web Server CONFIG
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10,10,1,253);
IPAddress gateway(10,10,1,1);
IPAddress subnet(255,255,255,0);
EthernetClient client;
// ---- Humidity Module CONFIG
#define DHTPIN A0 // What pin we're connected to
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
Serial.println("Serial connection established");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
}
dht.begin();
Serial.println("DHT11 connection established");
}
void loop() {
delay(1000);
// ---- Humidity Module ACTION
Serial.println("Humidity Module ACTION");
// Wait a few seconds between measurements.
int t = 0; // temperature
int h = 0; // humidity
String data = "";
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old'
// (its a very slow sensor)
h = dht.readHumidity();
delay(500);
// Read temperature as Celsius (the default)
t = dht.readTemperature();
delay(500);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// The Serial.print & Serial.println commands are optional
// (I used them for testing purposes)
Serial.print("Temp: ");
Serial.println(t);
Serial.print("Hum: ");
Serial.println(h);
// Preparing the data from the sensors to send via the
// ethernet shield
data = "ahum1=" + String(h) + "&temp1=" + String(t);
Serial.println(data); // For testing purposes
// ---- Ethernet Shield ACTION
if (client.connect("127.0.0.1", 80)) {
// Your domain
Serial.println("Client connected.");
client.println("POST arduino4/read_temperature.php HTTP/1.1");
client.println("Host: 127..0.0.1"); // Your domain
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(data.length());
client.println();
client.println(data);
}
if (client.connected()) {
client.stop();
}
// Now wait approximately one hour
// 1000ms * 60 = 60 000ms = 1 min
// 60 000ms * 60 = 3 600 000ms = 1 h
// The delay() function should know it's a long int,
// that's why there is a 'L' included at the end
delay(3600000L);
}
BTW我对Arduino一无所知,所以如果我提出愚蠢的问题,我会提前道歉。