在本教程中,我們將探索有關(guān)這個(gè)有趣的MCU的更多信息,并通過(guò)將NodeMCU與互聯(lián)網(wǎng)連接來(lái)慢慢地潛入物聯(lián)網(wǎng)世界。在這里,我們將使用此模塊在Web瀏覽器上獲取室溫,即我們將制作一個(gè)Web服務(wù)器以LM35作為溫度傳感器顯示溫度。
所需組件:
節(jié)點(diǎn)單片機(jī) - ESP12
LM35 溫度傳感器
面包板
公母連接器
LM35 溫度傳感器:
LM35是一款模擬線性溫度傳感器。其輸出與溫度成正比(以攝氏度為單位)。工作溫度范圍為 -55°C 至 150°C。 輸出電壓變化 10mV 以響應(yīng)oC 溫度升高或下降。它可以采用 5V 和 3.3 V 電源供電,待機(jī)電流小于 60uA。
請(qǐng)注意,LM35 有 3 個(gè)系列變體,即 LM35A、LM35C 和 LM35D 系列。主要區(qū)別在于它們的溫度測(cè)量范圍。LM35D 系列設(shè)計(jì)用于測(cè)量 0 至 100 攝氏度,而 LM35A 系列設(shè)計(jì)用于測(cè)量 -55 至 155 攝氏度的更寬范圍。LM35C 系列設(shè)計(jì)用于測(cè)量 -40 至 110 攝氏度。
將 LM35 與 NodeMCU 連接:
下面給出了將LM35與NodeMCU連接的電路圖:
LM35 是一個(gè)模擬傳感器,因此我們必須將此模擬輸出轉(zhuǎn)換為數(shù)字輸出。為此,我們使用定義為A0的NodeMCU的ADC引腳。我們將 LM35 的輸出連接到 A0。
我們?cè)贜odeMCU的引腳上有3.3 V作為輸出電壓。因此,我們將使用 3.3V 作為 LM35 的 Vcc。
代碼說(shuō)明:
本文末尾提供了帶有演示視頻的完整代碼。在這里,我們將解釋代碼的幾個(gè)部分。
首先,我們必須包含 ESP8266wifi 庫(kù)才能訪問(wèn) Wi-Fi 功能。
#include
然后在 ssid 和密碼字段中輸入您的 Wi-Fi 名稱和密碼。還初始化了變量并在波特率為 115200 的端口 80 上啟動(dòng)服務(wù)器。
const char* ssid = "*********"; // Your ssid
const char* password = "***********"; // Your Password
float temp_celsius = 0;
float temp_fahrenheit = 0;
WiFiServer server(80);
void setup() {
Serial.begin(115200);
通過(guò)調(diào)用這些函數(shù)建立 Wi-Fi 連接。
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
連接可能需要幾秒鐘才能建立,因此請(qǐng)繼續(xù)顯示“...”直到連接不會(huì)建立。然后系統(tǒng)將繼續(xù)等待并檢查客戶端連接...
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi is connected");
server.begin();
Serial.println("Server started");
Serial.println(WiFi.localIP());
}
在循環(huán)部分中,讀取傳感器值并將其轉(zhuǎn)換為攝氏度和華氏度,并在串行監(jiān)視器上顯示這些值。
void loop() {
temp_celsius = (analogRead(A0) * 330.0) / 1023.0; // To convert analog values to Celsius We have 3.3 V on our board and we know that output voltage of LM35 varies by 10 mV to every degree Celsius rise/fall. So , (A0*3300/10)/1023 = celsius
temp_fahrenheit = celsius * 1.8 + 32.0;
Serial.print(" Temperature = ");
Serial.print(temp_celsius);
Serial.print(" Celsius, ");
在網(wǎng)頁(yè)上顯示溫度的 HTML 代碼:
我們?cè)诰W(wǎng)頁(yè)上顯示溫度,以便可以通過(guò)互聯(lián)網(wǎng)從世界任何地方訪問(wèn)它。HTML代碼非常簡(jiǎn)單;我們只需要使用 client.println 函數(shù)來(lái)回顯 HTML 代碼的每一行,以便瀏覽器可以執(zhí)行它。
這部分顯示用于創(chuàng)建顯示溫度值的網(wǎng)頁(yè)的HTML代碼。
WiFiClient client = server.available();
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 10"); // update the page after 10 sec
client.println();
client.println("");
client.println("
");
client.print("
Digital Thermometer
");
client.print("
Temperature (*C)= ");
client.println(temp_celsius);
client.print("
Temperature (F) = ");
client.println(temp_fahrenheit);
client.print("
");
client.println("");
delay(5000);
}
加工:
使用 Arduino IDE 上傳代碼后,打開(kāi)串行監(jiān)視器并按 NodeMCU 上的重置按鈕。
現(xiàn)在,您可以看到該板已連接到您在代碼中定義的Wi-Fi網(wǎng)絡(luò),并且還獲得了IP。復(fù)制此 IP 并將其粘貼到任何 Web 瀏覽器中。確保運(yùn)行 Web 瀏覽器的系統(tǒng)應(yīng)連接到同一網(wǎng)絡(luò)。
您的數(shù)字溫度計(jì)已準(zhǔn)備就緒,溫度將在每 10 秒后在網(wǎng)絡(luò)瀏覽器中自動(dòng)刷新。
#include
const char* ssid = "*********"; // Your ssid
const char* password = "***********"; // Your Password
float temp_celsius = 0;
float temp_fahrenheit = 0;
WiFiServer server(80);
void setup() {
Serial.begin(115200);
pinMode(A0, INPUT);
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 is connected");
server.begin();
Serial.println("Server started");
Serial.println(WiFi.localIP());
}
void loop() {
temp_celsius = (analogRead(A0) * 330.0) / 1023.0; // To convert analog values to Celsius We have 3.3 V on our board and we know that output voltage of LM35 varies by 10 mV to every degree Celsius rise/fall. So , (A0*3300/10)/1023 = celsius
temp_fahrenheit = celsius * 1.8 + 32.0;
Serial.print(" Temperature = ");
Serial.print(temp_celsius);
Serial.print(" Celsius, ");
Serial.print(temp_fahrenheit);
Serial.println(" Fahrenheit");
WiFiClient client = server.available();
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 10"); // update the page after 10 sec
client.println();
client.println("");
client.println("");
client.print("
Digital Thermometer
");
client.print("
Temperature (*C)= ");
client.println(temp_celsius);
client.print("
Temperature (F) = ");
client.println(temp_fahrenheit);
client.print("
");
client.println("");
delay(5000);
}
-
溫度傳感器
+關(guān)注
關(guān)注
48文章
2979瀏覽量
156463 -
LM35
+關(guān)注
關(guān)注
2文章
83瀏覽量
18183 -
Web服務(wù)器
+關(guān)注
關(guān)注
0文章
138瀏覽量
24492
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
LM35溫度傳感器應(yīng)用及特性
如何使用PIC微控制器和LM35溫度傳感器制作數(shù)字溫度計(jì)
![如何使用PIC微控制<b class='flag-5'>器</b>和<b class='flag-5'>LM35</b>溫度傳感<b class='flag-5'>器</b><b class='flag-5'>制作</b>數(shù)字溫度計(jì)](https://file.elecfans.com//web2/M00/7B/00/poYBAGN0lg-AbXHjAAAb9v3BhKE223.jpg)
使用LM35的冷卻風(fēng)扇控制器
基于LM35溫度傳感器的溫控系統(tǒng)設(shè)計(jì)
LM35,datasheet,pdf(Precision C
lm35怎么用(lm35工作原理及內(nèi)部結(jié)構(gòu)_應(yīng)用電路圖)
![<b class='flag-5'>lm35</b>怎么用(<b class='flag-5'>lm35</b>工作原理及內(nèi)部結(jié)構(gòu)_應(yīng)用電路圖)](https://file.elecfans.com/web1/M00/45/95/pIYBAFpxMAqAD5_7AAAl0xXS-WI545.jpg)
lm35測(cè)溫電路圖大全(二款lm35測(cè)溫電路設(shè)計(jì))
![<b class='flag-5'>lm35</b>測(cè)溫電路圖大全(二款<b class='flag-5'>lm35</b>測(cè)溫電路設(shè)計(jì))](https://file.elecfans.com/web1/M00/45/96/pIYBAFpxNlmASQNXAAA_-Pap4Bc791.jpg)
基于LM35溫度傳感器的溫控系統(tǒng)設(shè)計(jì)
![基于<b class='flag-5'>LM35</b>溫度傳感<b class='flag-5'>器</b>的溫控系統(tǒng)設(shè)計(jì)](https://file.elecfans.com/web1/M00/45/93/o4YBAFpxOpaAa-LrAAA_7dzPJqc173.jpg)
基于LM35溫度傳感器的高精度恒溫控制系統(tǒng)
LM35溫度測(cè)量電路,LM35 temperature measurement circuit
LM35與ICL7107構(gòu)成的溫度計(jì),LM35 thermometer
使用LM35的冷卻風(fēng)扇控制器
![使用<b class='flag-5'>LM35</b>的冷卻風(fēng)扇控制<b class='flag-5'>器</b>](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
如何使用Arduino和LM35傳感器制作溫度計(jì)
![如何使用Arduino和<b class='flag-5'>LM35</b>傳感<b class='flag-5'>器</b><b class='flag-5'>制作</b>溫度計(jì)](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
評(píng)論