目前使用把nodemcu接感測器(目前接土壤溼度感測器和DHT11),然後把值用AP和php程式的方式傳到資料庫(Sensors),接著一直更新一筆資料,可是都只有土壤溼度感測器可以改成功,顯示的部分home.php也都有從資料庫抓到資料,網頁也都顯示得出來,可是從nodemcu傳過來的DHT11的值都不會更新到資料庫但是序列阜是看的到有值的,我看了好久找不到請問我有那裡打錯嗎?
5/17 目前找出AddData.php的UPDATE部分只要註解到其他兩行子保留一行,例如只留
$sql="UPDATE nowSensors SET Temp='".$Temp."';";這段那Temp的值就一定能更新,可是如果把Humidity的註解解除後就變成偶爾可以Temp和Humidity可以更新,可是幾乎都是只有Humidity有更新,如果是Soilhumidity加其中一個(Temp或Humidity)就一定只有Soilhumidity會更新,請問有甚麼寫法可以讓下面三行值更新時部會被覆寫到嗎?
已解決改成下列寫法就正常了
//AddData.php的10,11,12行改成
$sql="UPDATE nowSensors SET Temp='".$temp."', Humidity='".$humidity."',Soilhumidity='".$soilhumidity."';";
nodemcu程式碼
#include <ESP8266WiFi.h>
#include <DHT.h>
#define DHTPIN D3 //DHT11 D3
#define soilPin A0 //土壤溼度感測器A0
//#define HOST "192.168.2.121"
//const char* ssid = "ASUSLAB";
//const char* password = "ASUSASUS";
#define HOST "192.168.0.14"
const char* ssid = "myssid";
const char* password = "mypassword";
int soilValue;//存儲中間值
int soilMoisture;//定義土壤濕度
DHT dht(DHTPIN, DHT11);
void setup() {
Serial.begin(115200);
delay(10);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Print the IP address
Serial.print("Your NodeMCU IP is : ");
Serial.print(WiFi.localIP());
}
void loop() {
float t;
float h;
t = dht.readTemperature();
h = dht.readHumidity();
while (isnan(h)){
yield();
h = dht.readHumidity();
//return;
}
while (isnan(t)){
yield();
t = dht.readTemperature();
//return;
}
Serial.println("-------------------");
Serial.print("溫度:");
Serial.println(t);
Serial.print("濕度:");
Serial.println(h);
soilValue = analogRead(soilPin); //讀取土壤感測器上的電壓值
soilMoisture = map(soilValue,0,1023,100,0);//把電壓值按照[0,1023]映射到[100,0]
Serial.print("soilMositure: ");
Serial.print(soilMoisture);
Serial.println("%");
WiFiClient client;
if (!client.connect(HOST,80)) {
Serial.println("connection failed");
Serial.println("wait 5 sec...");
delay(5000);
}
String dbserver = HOST;
String getStr = "GET /AddData.php?";
getStr +="Temp=";
getStr += String(t);
getStr +="&Humidity=";
getStr += String(h);
getStr +="&Soilhumidity=";
getStr += String(soilMoisture);
getStr += " HTTP/1.1\r\n";
getStr += "Host: " + String(dbserver) \
+ "\r\n"+ "Connection: close\r\n\r\n";
client.print(getStr);
delay(10);
client.stop();
delay(5000);
}
home.php
<?php
session_start(); //很重要,可以用的變數存在session裡
$username=$_SESSION["username"];
echo "<h1>智能植栽系統 使用者: ".$username."</h1>";
echo "<a href='record.php'>澆水記錄 </a>";
echo "<a href='logout.php'>登出</a>",'<br>';
header('refresh: 5;url="home.php"'); //每5秒刷新home.php頁面
date_default_timezone_set("asia/taipei");
$dsn='mysql:host=127.0.0.1;dbname=Sensors';
try {
$db=new PDO($dsn,'pi','raspberry');
$db->exec("set names utf8");
$sql="select * from nowSensors;";
foreach ($db->query($sql) as $row) {
//echo $row["ID"];
echo "<h1>溫室1</h1>";
echo "溫室狀態:",'<br>';
echo "溫度:",$row["Temp"],"°C",'<br>';
echo "環境濕度:",$row["Humidity"],"%";
echo " 土壤濕度:",$row["Soilhumidity"],"%"," ph值:6.6ph 光照度:500lux<br>";
echo "Co2濃度:1000ppm";
}
} catch (PDOException $e) {
printf("DATABASER_ERRMESG: %s",$e->getMessage());
}
?>
<meta charset="utf-8">
<html>
<head>
<title>智能植栽系統</title>
</head>
<body>
<div id="listBtn" onclick="listBtn()">手動控制</div>
<div id="textlistn" style="display:none;">
植栽1 澆水(mL):
<input type="text" name="Water" id="t1" size="3">
<input type="submit" name="b1" id="b1" value="確定">
施肥(mL):
<input type="text" name="Fertilizer" id="t1" size="3">
<input type="submit" name="b1" id="b1" value="確定">
日照燈(min):
<input type="text" name="LightMin" id="t1" size="3">
<input type="submit" name="b1" id="b1" value="確定">
風扇(min):
<input type="text" name="FanMin" id="t1" size="3">
<input type="submit" name="b1" id="b1" value="確定">
</div>
<script>
AddData.php
<?php
date_default_timezone_set("asia/taipei");
$dns="mysql:host=127.0.0.1;dbname=Sensors";
$Temp=$_GET['Temp'];
$Humidity=$_GET['Humidity'];
$Soilhumidity=$_GET['Soilhumidity'];
//$Motor=$_GET['Motor'];
try{
$db=new PDO($dns,'pi','raspberry');
$db->exec("set names utf8");
$sql="UPDATE nowSensors SET Temp='".$Temp."';";
$sql="UPDATE nowSensors SET Humidity='".$Humidity."';";
$sql="UPDATE nowSensors SET Soilhumidity='".$Soilhumidity."';";
$result=$db->exec($sql);
}catch(PDOException $e){
printf("DATABASE_ERRMESG:%s",$e->getMessage());
}
?>
這段程式碼只有第三行有執行,第一跟第二行在執行之前就被其他的字串複寫了
$sql="UPDATE nowSensors SET Temp='".$Temp."';";
$sql="UPDATE nowSensors SET Humidity='".$Humidity."';";
$sql="UPDATE nowSensors SET Soilhumidity='".$Soilhumidity."';";
$result=$db->exec($sql);
} catch(PDOException $e) {
printf("DATABASE_ERRMESG:%s",$e->getMessage());
//多下面這行,把錯誤訊息放到 log.txt,看看會有什麼錯誤
file_put_contents("log.txt", $e->getMessage());
}
我又測試把濕度"Humidity"和"Soilhumidity"註解掉後Temp又可以顯示了
//$Humidity=$_GET['Humidity'];
//$Soilhumidity=$_GET['Soilhumidity'];
//$sql="UPDATE nowSensors SET Humidity='".$Humidity."';";
//$sql="UPDATE nowSensors SET Soilhumidity='".$Soilhumidity."';";
目前是要嘛只顯示土壤溼度感測器"Soilhumidity"的,要嘛只顯示DHT11的"Temp"和"Humidity"可以一起顯示可是必須把"Soilhumidity"的部分註解
$Soilhumidity=$_GET['Soilhumidity'];
$sql="UPDATE nowSensors SET Soilhumidity='".$Soilhumidity."';";