# 搭建最小系統刷NodeMCU固件及DHT11溫度讀取并上傳服務器的ESP8266實驗
## 一、實驗背景與目標
ESP8266作為一款高性價比的Wi-Fi芯片,因其強大的無線功能和極低的功耗,在物聯網領域廣受歡迎。本實驗將實現以下目標:
1. 搭建ESP8266最小硬件系統
2. 刷寫NodeMCU固件
3. 連接DHT11溫濕度傳感器
4. 實現數據上傳至Web服務器
5. 構建完整的物聯網數據采集鏈路
## 二、硬件準備
### 2.1 核心組件清單
| 組件 | 數量 | 備注 |
|------|------|------|
| ESP8266模塊(ESP-12F) | 1 | 推薦NodeMCU開發板 |
| DHT11傳感器 | 1 | 溫濕度檢測 |
| 10KΩ電阻 | 2 | 上拉電阻 |
| 面包板 | 1 | 原型搭建 |
| 杜邦線 | 若干 | 連接線路 |
### 2.2 最小系統電路搭建
[VCC 3.3V] —-+ | R1 (10K) | +—- GPIO2 | +—- DATA | [GND] ———+
> **注意**:ESP8266工作電壓為3.3V,切勿接5V電源!
## 三、軟件環境準備
### 3.1 必要工具
1. **固件刷寫工具**:
- [esptool](https://github.com/espressif/esptool)
- [NodeMCU Flasher](https://github.com/nodemcu/nodemcu-flasher)
2. **開發環境**:
- Arduino IDE(需安裝ESP8266開發板支持)
- 或Lua編輯器(如ESPlorer)
3. **NodeMCU固件**:
- 自定義固件生成:https://nodemcu-build.com/
### 3.2 驅動安裝(Windows)
1. 下載CH340G驅動
2. 設備管理器識別串口
3. 確認端口號(如COM3)
## 四、刷寫NodeMCU固件
### 4.1 固件下載
推薦使用云構建服務獲取定制固件:
- 勾選必要的模塊:file, gpio, net, node, pwm, uart, wifi, dht
### 4.2 使用esptool刷寫
```bash
esptool.py --port COM3 erase_flash
esptool.py --port COM3 write_flash 0x00000 nodemcu-master-xxx.bin
使用串口工具發送:
print(node.info())
應返回固件版本信息。
| DHT11引腳 | ESP8266連接 |
|---|---|
| VCC | 3.3V |
| DATA | GPIO2 |
| GND | GND |
注意:DATA引腳需接10K上拉電阻
pin = 4 -- GPIO2
dht11 = require("dht11")
dht11.read(pin)
print("Temperature: "..dht11.getTemperature().." C")
print("Humidity: "..dht11.getHumidity().." %")
function sendToServer(temp, humi)
conn = net.createConnection(net.TCP, 0)
conn:on("receive", function(sck, c) print(c) end)
conn:connect(80,"api.thingspeak.com")
conn:send("GET /update?api_key=YOUR_KEY&field1="..temp.."&field2="..humi.." HTTP/1.1\r\n")
conn:send("Host: api.thingspeak.com\r\n")
conn:send("Connection: close\r\n")
conn:send("Accept: */*\r\n\r\n")
end
m = mqtt.Client("ESP-"..node.chipid(), 120)
m:connect("broker.hivemq.com", 1883, 0,
function(client)
m:publish("sensor/temperature", temp, 0, 0)
m:publish("sensor/humidity", humi, 0, 0)
end)
#include <ESP8266WiFi.h>
#include <DHT.h>
#define DHTPIN 2 // GPIO2
#define DHTTYPE DHT11
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
const char* server = "api.thingspeak.com";
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
dht.begin();
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("DHT11 read failed!");
return;
}
WiFiClient client;
if (client.connect(server, 80)) {
String url = "/update?api_key=YOUR_KEY&field1=";
url += String(t);
url += "&field2=";
url += String(h);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + server + "\r\n" +
"Connection: close\r\n\r\n");
delay(10);
}
client.stop();
delay(60000); // 1分鐘間隔
}
-- 配置WiFi
wifi.setmode(wifi.STATION)
wifi.sta.config("SSID","PASSWORD")
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip() == nil then
print("Connecting...")
else
tmr.stop(1)
print("IP: "..wifi.sta.getip())
-- 啟動傳感器讀取
tmr.alarm(0, 60000, 1, function()
readSensor()
end)
end
end)
function readSensor()
dht11.read(4)
local temp = dht11.getTemperature()
local humi = dht11.getHumidity()
sendToServer(temp, humi)
end
wifi.sta.eventMonReg(wifi.STA_WRONGPWD, function()
print("Wrong password!")
end)
低功耗優化:
數據可視化:
安全加固:
通過本實驗,我們完成了從硬件搭建到云端數據上傳的完整物聯網鏈路。ESP8266配合NodeMCU固件展現了強大的物聯網開發能力,DHT11提供了可靠的環境數據采集。這種方案可擴展應用于智能家居、農業監測等多種場景。
完整代碼及資料獲?。?a >https://github.com/example/esp8266-dht11-demo “`
該文檔共計約2400字,包含: - 10個主要章節 - 5個代碼片段 - 3張表格示意圖 - 完整的硬件連接說明 - 兩種編程語言實現方案 - 常見問題解決方案 - 進階發展方向建議
可根據實際實驗情況調整參數和連接方式。建議使用Arduino版本獲得更好的穩定性,Lua版本適合快速原型驗證。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。