本專題為兩塊ESP8266-ESP01 ,分別為 Arduino+ESP-01+YL-69 土壤濕度檢測器與LED燈 ,以及 ESP-01 + DHT11 + LED 傳輸給伺服器端,並於伺服器上 WebSite 透過ESP-01上傳儲 存於 MYSQL 資料呈現曲線圖給前端使用者。此外,前端使用者也可控制兩塊ESP01將LED 燈點亮與關閉。
ESP8266 ES-01獨立使用
STEP 1. 清空 Arduino Uno 內存,將Arduino Uno 做中介燒入。
void setup() {}
void loop() {}
STEP 2. 檔案->偏好設定->額外的開發版管理員網址 填入 http://arduino.esp8266.com/stable/package_esp8266com_index.json
然後到 工具-> 開發板管理員 填入 ESP8266 就會看到esp8266 by ESP8266 Community 接著安裝 就可以在開發板選項中看到 ESP8266 這個系列了
Step 3. 選擇開發板
開發板 : Generic ESP8266 Module
Flash Mode : DIO
Flash Zize : 1M (512K SPIFFS)
序列埠 : Arduino Uno
Arduino Uno & ESP-01 線路
(用Arduino 當燒錄中介 所以TX與RX 是直接對接,IO0(GPIO0) 接Low後上電就會進入燒錄模 式燒錄完記得改為空接)
Step 5. 燒錄測試 (*注意 燒錄後內建的AT command會被覆蓋掉 若有需要則需上官網下載重新燒入) 選擇 檔案 ->範例 -> Basics -> Blink 將腳位改為 2 (即IO2)
void setup() {
pinMode(2, OUTPUT);
}
void loop() {
digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(2, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
然後按"上傳",就開始燒錄Code了 當跑到 100% 後就燒錄完成了。
ESP8266 ES-01獨立使用 透過 WebSite 點亮 LED
#include <ESP8266WiFi.h>
#define _baudrate 9600
const char* ssid = "D-Link_DIR-612";//type your ssid
const char* password = "123456789";//type your password
int ledPin = 2; // GPIO2 of ESP8266
WiFiServer server(80);
const char* host = "192.168.20.3";
const int httpPort = 80;
unsigned long time_interval=60000;
void setup() {
Serial.begin( _baudrate );
pinMode(2, OUTPUT);
Serial.println( "ESP8266 Ready!" );
// Connecting to a WiFi network
Serial.print("Connect to... ");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println( "" );
Serial.println( "WiFi connected" );
Serial.println( "IP address: " );
Serial.println( WiFi.localIP() );
Serial.println( "" );
}
String turnx="";
void loop() {
// 設定 ESP8266 作為 Client 端
WiFiClient client;
if (!client.connect(host, httpPort)) {
Serial.println( "connection failed" );
return;
}
delay(1000);
String url = "/iot/turn2.txt";
url+="\r\n";
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: keep-alive\r\n\r\n");
//"Connection: close\r\n\r\n");
delay(1000);
// 讀取所有從 IoT Server 的回應並輸出到串列埠
while(client.available())
{
String line = client.readStringUntil('\r');
//Serial.print(line);
turnx=line;
}
if(turnx=="On")
{
digitalWrite(2, HIGH);
Serial.print("trun="+turnx+"\r\n");
}
else
{
digitalWrite(2, LOW);
Serial.print("Notrun="+turnx+"\r\n");
}
delay(1000);
}
ESP8266 ES-01獨立使用 DHT11 傳送至WebSite
Step1. 安裝 DHT11 程式庫 工具 > 程式庫 > DHT Sensor library
Step2.將程式燒入( GPIO 0 若在開機時被偵測到 Low, ESP8266 將進入韌體燒寫模式 (透過 UART), 否則 GPIO 0 即為一般的 GPIO 埠.)
#include <ESP8266WiFi.h>
//DHT
#include <DHT.h>
#define DHTTYPE DHT11
#define DHTPIN 0
//
#define _baudrate 9600
const char* ssid = "D-Link_DIR-612";//type your ssid
const char* password = "123456789";//type your password
int ledPin = 2; // GPIO2 of ESP8266
WiFiServer server(80);
const char* host = "192.168.20.3";
const int httpPort = 80;
unsigned long time_interval=60000;
//DHT
DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266
float humidity, temp_f; // 從 DHT-11 讀取的值
unsigned long previousMillis = 0; // will store last temp was read
const long interval = 2000; // interval at which to read sensor
void setup() {
Serial.begin( _baudrate );
pinMode(ledPin, OUTPUT);
Serial.println( "ESP8266 Ready!" );
// Connecting to a WiFi network
Serial.print("Connect to... ");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println( "" );
Serial.println( "WiFi connected" );
Serial.println( "IP address: " );
Serial.println( WiFi.localIP() );
Serial.println( "" );
// Start the server
//server.begin();
}
String turnx="";
void loop() {
// 設定 ESP8266 作為 Client 端
WiFiClient client;
if (!client.connect(host, httpPort)) {
Serial.println( "connection failed" );
return;
}
delay(1000);
String url = "/iot/turn2.txt";
url+="\r\n";
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: keep-alive\r\n\r\n");
//"Connection: close\r\n\r\n");
delay(1000);
// 讀取所有從 IoT Server 的回應並輸出到串列埠
while(client.available())
{
String line = client.readStringUntil('\r');
//Serial.print(line);
turnx=line;
}
if(turnx=="On")
{
digitalWrite(ledPin, HIGH);
Serial.print("trun="+turnx+"\r\n");
}
else
{
digitalWrite(ledPin, LOW);
Serial.print("Notrun="+turnx+"\r\n");
}
delay(1000);
gettemperature();//DHT偵測
}
void gettemperature() {
// 量測間等待至少 2 秒
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
// 將最後讀取感測值的時間紀錄下來
previousMillis = currentMillis;
// 讀取溫度大約 250 微秒!
humidity = dht.readHumidity(); // 讀取濕度(百分比)
temp_f = dht.readTemperature(true); // 讀取溫度(華氏)
// 檢查兩個值是否為空值
if (isnan(humidity) || isnan(temp_f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
else
{
WiFiClient client;
if (!client.connect(host, httpPort)) {
Serial.println( "connection failed" );
return;
}
delay(1000);
//Serial.print(temp_f);
String url = "/iot/insert.php?t=";
url+=temp_f;
url+="&h=";
url+=humidity;
url+="\r\n";
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
Serial.println("Send DHT sensor ok!");
delay(1000);
}
}
}
Step3. WebSite 程式 (insert.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();
}
$data = [$_GET["t"],
$_GET["h"]
];
$query_insert = "INSERT INTO sensor2 (temperature,humidity ,datex) VALUES (?,?, NOW())";
$stmt = $db_link->prepare($query_insert);
$stmt->execute($data);
?>
資料庫結構
本專題資料庫名稱為: iot20211008 資料表分別為 :
sensor 紀錄土壤偵測的數值
sensor2 紀錄DHT11 溫溼度的數值
相關原碼:
https://reurl.cc/OkME6g Arduino 原碼 為 123.ino 壓縮檔內
WevSite 原碼 為 iot.rar
MySql 資料庫 為 iot20211008.sql
技術文件 : 專題.pdf