本專題為兩塊ESP8266-ESP01 ,分別為 Arduino+ESP-01+YL-69 土壤濕度檢測器與LED燈 ,以及 ESP-01 + DHT11 + LED 傳輸給伺服器端,並於伺服器上 WebSite 透過ESP-01上傳儲 存於 MYSQL 資料呈現曲線圖給前端使用者。此外,前端使用者也可控制兩塊ESP01將LED 燈點亮與關閉。
Ardunio Uno + ESP8266 ESP-01連線測試
#include <SoftwareSerial.h>
#define RX 3 //ESP8266 的 TX接3
#define TX 2 //ESP8266 RX接2
String ssid="D-Link_DIR-612"; //無線基地台識別
String pwd="123456789"; //無線基地台密碼
String IP = "192.168.20.3";
String cmd;
SoftwareSerial ESP8266(RX,TX);
void setup()
{
Serial.begin(9600);
ESP8266.begin(9600);
cmd = "AT+RST";
sendToWifi(cmd);//使用AT+RST重新啟動ESP8266
delay(1000); //延遲1秒
//
cmd="AT+CWJAP=\"";
cmd+=ssid;
cmd+="\",\"";
cmd+=pwd;
cmd+="\"";
cmd+="\r\n";
Serial.println("*** SoftSerial connection to ESP8266 ***");
sendToWifi("AT+CWMODE=3\r\n"); // 設定為 STA 模式
sendToWifi(cmd); // 取得連線(視AP連線效能作調整,預設7000毫秒)
//AT+CWJAP="D-Link_DIR-612","123456789"
sendToWifi("AT+CIFSR"); // 獲得 IP
sendToWifi("AT+CIPMUX=1\r\n"); // 開啟多除連接模式
sendToWifi("AT+CIPSERVER=1,80\r\n"); // 設定連接埠為 8080
Serial.println("Ready");
//
}
void loop(){
//手打指令用 Start
if (ESP8266.available()){
Serial.write(ESP8266.read());
}
if (Serial.available()){
ESP8266.write(Serial.read());
}
//手打指令用 END
cmd="AT+CIPSTART=\"TCP\",\""+IP+"\",80";
sendToWifi(cmd);//連線192.168.43.129網頁伺服器
if (ESP8266.find("Error")) {
Serial.println("無法連線網頁伺服器");
return;//跳出,後面程式不再執行
}
String GET="GET /test.php?name=John&hour=13\r\n\r\n";
cmd="AT+CIPSEND=" + String(GET.length());//傳送的字串長度
sendToWifi(cmd);
if (ESP8266.find(">")) { //若ESP8266準備接收資料
sendToWifi(GET); //傳送GET指令、網址與資料
} else { //沒有收到ESP8266準備接收資料的回應
cmd = "AT+CIPCLOSE";//結束連線
sendToWifi(cmd);
}
delay(1000); //延遲1秒
//
}
void sendToWifi(String command){
ESP8266.println(command);
Serial.println(command);
}
Arduino YL69土壤濕度
int SoilPin = A0;
int Standard = 800;
void setup() {
Serial.begin(9600);
}
void loop() {
int result = analogRead(SoilPin);
Serial.print("Soil: ");
Serial.println(result);
if (result > Standard)
{
Serial.println("Alert");
}
delay(1000);
}
Ardunio Uno + ESP8266 ES-01 + YL69土壤濕度
#include <SoftwareSerial.h>
#define RX 3 //ESP8266 的 TX接3
#define TX 2 //ESP8266 RX接2
String ssid="D-Link_DIR-612"; //無線基地台識別
String pwd="123456789"; //無線基地台密碼
String IP = "192.168.20.3";
String cmd;
SoftwareSerial ESP8266(RX,TX);
//土壤濕度用變數
int SoilPin = A0;
int Standard = 800;
void setup()
{
pinMode(13,OUTPUT);
//digitalWrite(13,LOW);
Serial.begin(9600);
ESP8266.begin(9600);
cmd = "AT+RST";
sendToWifi(cmd);//使用AT+RST重新啟動ESP8266
delay(1000); //延遲1秒
//
cmd="AT+CWJAP=\"";
cmd+=ssid;
cmd+="\",\"";
cmd+=pwd;
cmd+="\"";
cmd+="\r\n";
Serial.println("*** SoftSerial connection to ESP8266 ***");
sendToWifi("AT+CWMODE=3\r\n"); // 設定為 STA 模式
sendToWifi(cmd); // 取得連線(視AP連線效能作調整,預設7000毫秒)//AT+CWJAP="D-Link_DIR-612","123456789"
sendToWifi("AT+CIFSR"); // 獲得 IP
sendToWifi("AT+CIPMUX=1\r\n"); // 開啟多除連接模式
sendToWifi("AT+CIPSERVER=1,80\r\n"); // 設定連接埠為 8080
Serial.println("Ready");
//
}
void loop(){
//手打指令用 Start
if (ESP8266.available()){
Serial.write(ESP8266.read());
}
if (Serial.available()){
ESP8266.write(Serial.read());
}
//手打指令用 END
//土壤濕度
int result = analogRead(SoilPin);
Serial.print("Soil: ");
Serial.println(result);
if (result > Standard)
{
Serial.println("Alert");
}
delay(1000);
//連線
cmd="AT+CIPSTART=\"TCP\",\""+IP+"\",80";
sendToWifi(cmd);//連線192.168.43.129網頁伺服器
if (ESP8266.find("Error")) {
Serial.println("無法連線網頁伺服器");
return;//跳出,後面程式不再執行
}
String GET="GET /test.php?soil=";
GET+=result;
GET+="\r\n\r\n";
cmd="AT+CIPSEND=" + String(GET.length());//傳送的字串長度
sendToWifi(cmd);
if (ESP8266.find(">")) { //若ESP8266準備接收資料
sendToWifi(GET); //傳送GET指令、網址與資料
} else { //沒有收到ESP8266準備接收資料的回應
cmd = "AT+CIPCLOSE";//結束連線
sendToWifi(cmd);
}
delay(1000); //延遲1秒
//---LED燈控制
cmd="AT+CIPSTART=\"TCP\",\""+IP+"\",80";
sendToWifi(cmd);//連線192.168.43.129網頁伺服器
if (ESP8266.find("Error")) {
Serial.println("無法連線網頁伺服器");
return;//跳出,後面程式不再執行
}
GET="GET /iot/turn.txt";
GET+="\r\n\r\n";
cmd="AT+CIPSEND=" + String(GET.length());//傳送的字串長度
sendToWifi(cmd);
if (ESP8266.find(">")) { //若ESP8266準備接收資料
ESP8266.print(GET);
delay(1000);
Serial.println("GData >"+GET);
if(ESP8266.find("On"))
{
digitalWrite(13,HIGH);
Serial.print("Turn On!!!");
}
else
{
digitalWrite(13,LOW);
Serial.print("Turn Off!!!");
}
} else { //沒有收到ESP8266準備接收資料的回應
cmd = "AT+CIPCLOSE";//結束連線
sendToWifi(cmd);
}
//
delay(1000);
}
void sendToWifi(String command){
ESP8266.println(command);
Serial.println(command);
}
伺服器接收端(test.php)
<?php
//資料庫 設定
$db_host = "localhost"; //路徑
$db_username = "root"; //帳號
$db_password = "1234"; //密碼
$db_name = "iot20211008";
//pdo -> mysql連線
try{
$db_link = new PDO("mysql:host={$db_host};dbname={$db_name};charset=utf8",$db_username,$db_password);
}catch(PDOException $e){
print "資料庫連結失敗...Msg:{$e->getMessage()}<br/>";
die();
}
echo $_GET["soil"];
$data = [$_GET["soil"]
];
$query_insert = "INSERT INTO sensor (soil, datex) VALUES (?, NOW())";
$stmt = $db_link->prepare($query_insert);
$stmt->execute($data);
//$date = date('Y-m-d H:i:s');
/*
foreach ($_REQUEST as $key => $value)
{
if ($key == "name") {
$name = $value;
}
if ($key == "hour") {
$hour = $value;
}
}
if ($hour < 5 || $hour > 19){
echo "$name,Good Night<br>";
}else if ( $hour < 12 ){
echo "$name,Good Morning<br>";
}else {
echo "$name,Good Afternoon<br>";
}
*/
?>
伺服器顯示頁 (index.php)
<!DOCTYPE html>
<?
include "head.php";
$file_path = "turn.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str = fread($fp,filesize($file_path));//指定讀取大小,這裡把整個檔案內容讀取出來
}
$file_path2 = "turn2.txt";
if(file_exists($file_path2)){
$fp2 = fopen($file_path2,"r");
$str2 = fread($fp2,filesize($file_path2));//指定讀取大小,這裡把整個檔案內容讀取出來
}
?>
<html style="height: 100%">
<head>
<meta charset="utf-8">
<title>IoT專題</title>
<!--<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>-->
<!--<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.3/js/bootstrap.min.js"></script>-->
<!--<link rel="stylesheet" href="bootstrap.css">-->
</head>
<body style="height: 100%; margin: 0">
<center></br>
LED01:  <a class="btn btn-outline-secondary" href="turn.php?a=1on" role="button">On</a>  
<a class="btn btn-outline-secondary" href="turn.php?a=1off" role="button">Off</a>
State:<?echo $str = str_replace("\r\n","<br />",$str);?></br>
</br>
LED02:  <a class="btn btn-outline-secondary" href="turn.php?a=2on" role="button">On</a>  
<a class="btn btn-outline-secondary" href="turn.php?a=2off" role="button">Off</a>
State:<?echo $str2 = str_replace("\r\n","<br />",$str2);?>
</center></br>
<!--<header class="container">
<div class="footer col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-12">
曲線圖練習
</div>
</header>-->
<div class="container">
<div class="row">
<div id="container" class="col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-12" style="height: 500px"></div>
<script type="text/javascript" src="2echarts.min.js"></script>
<script type="text/javascript" src="echarts-gl.min.js"></script>
<!--<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/echarts@4/dist/echarts.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/echarts-gl@1/dist/echarts-gl.min.js"></script>-->
<!--<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/echarts-stat@1/dist/ecStat.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/echarts@4/dist/extension/dataTool.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/echarts@4/map/js/china.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/echarts@4/map/js/world.js"></script>
<script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=xfhhaTThl11qYVrqLZii6w8qE5ggnhrY&__ec_v__=20190126"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/echarts@4/dist/extension/bmap.min.js"></script>-->
<!--<script src="js.js"></script>-->
<?
include "js.php";
?>
</div></div>
<!--
<footer class="container">
<div class="footer col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-12">
<p>曲線圖練習結束 </p>
</div>
</footer>
-->
<?
include "footer.php";
?>
</body>
</html>
伺服器顯示頁曲線圖 (js.php)
<?php
//資料庫 設定
$db_host = "localhost"; //路徑
$db_username = "root"; //帳號
$db_password = "1234"; //密碼
$db_name = "iot20211008";
//pdo -> mysql連線
try{
$db_link = new PDO("mysql:host={$db_host};dbname={$db_name};charset=utf8",$db_username,$db_password);
}catch(PDOException $e){
print "資料庫連結失敗...Msg:{$e->getMessage()}<br/>";
die();
}
$d="";
$da="";
$stmt = $db_link->prepare("SELECT * FROM sensor order by datex DESC ");
$stmt->execute();
$no=$stmt->rowCount();
while($row=$stmt->fetch()){
$d.="'".$row["datex"]."'".",";
$da.="'".$row["soil"]."'".",";
}
$h="";
$ha="";
$ta="";
$stmt = $db_link->prepare("SELECT * FROM sensor2 order by datex DESC");
$stmt->execute();
while($row=$stmt->fetch()){
$h.="'".$row["datex"]."'".",";
$ha.="'".$row["humidity"]."'".",";
$ta.="'".$row["temperature"]."'".",";
}
?>
<script>
var dom = document.getElementById("container");
var myChart = echarts.init(dom);
var app = {};
option = null;
option = {
title: {
text: ''
},
tooltip: {
trigger: 'axis'
},
legend: {
data: ['土壤', '溫度', '濕度']
},
/*grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},*/
toolbox: {
feature: {
saveAsImage: {}
}
},
xAxis: {
type: 'category',
boundaryGap: false,
data: [<?=$d;?>]
},
yAxis: {
type: 'value'
},
series: [
{
name: '土壤',
type: 'line',
stack: '',
data: [<?=$da;?>]
},
{
name: '溫度',
type: 'line',
stack: '',
data: [<?=$ta;?>]
},
{
name: '濕度',
type: 'line',
stack: '',
data: [<?=$ha;?>]
}
]
};
;
if (option && typeof option === "object") {
myChart.setOption(option, true);
//根據視窗的大小變動圖表 --- 重點
window.onresize = function(){
myChart.resize();
}
}
</script>
L E D 控 制 ( t u r n . p h p )
<?
$a=$_GET["a"];
switch($a)
{
case "1on":
$str = "On";
$file = fopen("turn.txt","w+"); //開啟檔案
fwrite($file,$str);
fclose($file);
break;
case "1off":
$str = "Off";
$file = fopen("turn.txt","w+"); //開啟檔案
fwrite($file,$str);
fclose($file);
break;
case "2on":
$str = "On";
$file = fopen("turn2.txt","w+"); //開啟檔案
fwrite($file,$str);
fclose($file);
break;
case "2off":
$str = "Off";
$file = fopen("turn2.txt","w+"); //開啟檔案
fwrite($file,$str);
fclose($file);
break;
}
header( "location:index.php"); //回 index.php
?>
Arduino 原碼 為 123.ino 壓縮檔內
WevSite 原碼 為 iot.rar
MySql 資料庫 為 iot20211008.sql
技術文件 : 專題.pdf