[DIY] 智慧家庭氣象觀測站

 多數人都有這樣的困擾,家人白天上班的上班,上課的上課,NAS 24hr 運作,寵物在家獨守空房,沒人可以即時因應氣溫的變化,因此很久以前就有個念頭,希望可以記錄家裡的環境變化甚至可以遠端了解目前狀況。這幾年拜 IOT 科技所賜,這方面的工具也逐漸推出,只是市面產品功能及穩定性參差不齊,也怕買到地雷,還不如依自身的需求自己 DIY 實作看看,功能還可以逐步擴充,重點是價格低廉實惠,庶民百姓都玩得起。


http://digiland.tw/uploads/3_dht11_03.jpg

這樣的需求,首選的工具當然就是用 Arduino 來擔綱囉,以下是演員陣容: 
▼ 主要控制板 Arduino UNO。
http://digiland.tw/uploads/3_dht11_10.jpg

▼ 溫溼度偵測元件 DHT11。
http://digiland.tw/uploads/3_dht11_14.jpg

▼ 因為只要顯示溫溼度等資訊,所以 LCD 選擇便宜的 1602 就足夠使用。
http://digiland.tw/uploads/3_dht11_12.jpg

▼ 這是LCD背面接腳,會選擇 I2C 是因為只要 4 條杜邦線就可以完成連接。
http://digiland.tw/uploads/3_dht11_13.jpg

▼ 如果想要能透過網路連線查詢即時狀態,則需要再加一張 W5100 Ethernet 擴充板。
http://digiland.tw/uploads/3_dht11_11.jpg

▼ 所有演員大集合,準備開工拜拜。
http://digiland.tw/uploads/3_dht11_09.jpg

單機版家庭氣象觀測站
▼ 單機板家庭氣象站接線圖,第一次畫不太熟練,找不到 DHT11 元件 拿 RHT03 充當orz
http://digiland.tw/uploads/3_dht11_15.jpg

▼ 實際接線圖,接得有點混亂,請多包涵jolin
http://digiland.tw/uploads/3_dht11_02.jpg

▼ 單機版程式碼

#include <LiquidCrystal_I2C.h> #include <dht11.h> #include <Wire.h> LiquidCrystal_I2C lcd(0x27, 16, 2); dht11 DHT11; const byte dataPin = 2; void setup() { // put your setup code here, to run once: lcd.init(); lcd.backlight(); } void loop() { // put your main code here, to run repeatedly: int chk = DHT11.read(dataPin); if(chk == 0) { lcd.setCursor(4,0); lcd.print("Temp"); lcd.setCursor(0,1); lcd.print("Humidity"); lcd.setCursor(9,0); lcd.print((float)DHT11.temperature, 2); lcd.print((char) 0xDF); lcd.print("C"); lcd.setCursor(9,1); lcd.print((float)DHT11.humidity, 2); lcd.print("%"); } else { lcd.clear(); lcd.print("Error code:"); lcd.print(chk); } delay(5000); }


▼ 開機後,會將 DHT11 偵測出來的溫溼度數據顯示在 LCD 畫面
http://digiland.tw/uploads/3_dht11_03.jpg

網路版家庭氣象觀測站
單機板似乎只能單機作業眼見為憑,還不如加上網路功能,讓遠端也能透過網路監看家裡的溫溼度狀況,所以以前述的單機板為基礎,我們再加上一片 W5100 Ethernet 擴充板,即可升級為網路版家庭氣象站。

▼ 在 Arduino UNO 上再堆疊一片 W5100,即可擁有網路版的功能。
http://digiland.tw/uploads/3_dht11_05.jpg

▼ 網路版程式碼

#include <LiquidCrystal_I2C.h> #include <dht11.h> #include <Wire.h> #include <SPI.h> #include <Ethernet.h> LiquidCrystal_I2C lcd(0x27, 16, 2); byte mac[] = { 0xF0, 0x7B, 0xCB, 0x4B, 0x7C, 0x9F }; dht11 DHT11; const byte dataPin = 2; EthernetServer server(80); void setup() { // put your setup code here, to run once: lcd.init(); lcd.backlight(); Ethernet.begin(mac); } void loop() { // put your main code here, to run repeatedly: char buffer[5] = ""; int chk = DHT11.read(dataPin); IPAddress ip = Ethernet.localIP(); EthernetClient client = server.available(); if (client) { while (client.connected()) { if (client.available()) { client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(); client.println("<!doctype html>"); client.println("<html>"); client.println("<meta charset="utf-8" />"); client.println("<meta http-equiv='Refresh' CONTENT='30'>"); client.println("<title>智慧家庭氣象觀測站"); client.println("</head>"); client.print("溫度:"); client.print(dtostrf(DHT11.temperature, 5, 2, buffer)); client.print("°C<br>"); client.print("溼度:"); client.print(dtostrf(DHT11.humidity, 5, 2, buffer)); client.print("%"); client.println("</body>"); break; } } delay(1); client.stop(); } lcd.setCursor(0,0); lcd.print(ip); if(chk == 0) { lcd.setCursor(0,1); lcd.print(dtostrf(DHT11.temperature, 5, 2, buffer)); lcd.print((char) 0xDF); lcd.print("C"); lcd.setCursor(8,1); lcd.print(dtostrf(DHT11.humidity, 5, 2, buffer)); lcd.print("%"); } else { lcd.clear(); lcd.print("Error code:"); lcd.print(chk); } delay(5000); }


▼ 開機後,W5100 會啟動 DHCP 功能取得一組可用的 IP,並顯示在 LCD 上,第二列才會顯示目前的溫度及溼度。
http://digiland.tw/uploads/3_dht11_06.jpg

▼ 有了 IP,可以方便透過瀏覽器讀取目前的溫溼度狀態。
http://digiland.tw/uploads/3_dht11_07.jpg

▼ 透過 VPN 連線,甚至可以在辦公室遠端連線回來查看家裡的溫溼度資訊。
http://digiland.tw/uploads/3_dht11_08.jpg

後記
這樣的實作花費大概台幣 500 元上下就能擁有,程式碼也很簡單易學,後續可延伸的方向:1.透過 MRTG 功能可以定時記錄家裡溫溼度的變化。2.改成無線網路,讓偵測點不受限於網路線。3.增加繼電器,可以設定條件自動開啟或關閉家電,例如設定溫度達 25 度自動開啟電風扇,溼度達 80% 開啟除溼機等等,當然也可透過網路遠端控制開關。4.增加其他監控功能,例如瓦斯偵測、PM2.5偵測等等,這樣的 DIY 只要有創意就會有樂趣。

備註:為讓顯示順利,程式碼中的"<"字元皆使用全形符號,使用時請自行替換。

 
引用通告地址: 點擊獲取引用地址
評論: 43 | 引用: 0 | 閱讀: 6811 | 轉發
Jiya Saxena [ 2025-02-06 15:07 | 回覆 | 編輯/刪除 ]
we will give the ideal match to address every one of your issues which are still to be satisfied. Posh Indian Escorts in Jaipur are extremely well known in the business.
kavyamehra [ 2025-02-17 11:32 | 回覆 | 編輯/刪除 ]
We have a large number of Mahipalpur Escorts service to serve you. Likewise, we have different other escort Girls classes whom you can profit. Yet, our most favored class is just Delhi autonomous escort in Mahipalpur.
shwetaroy [ 2025-04-07 20:17 | 回覆 | 編輯/刪除 ]
You are definitely not living up to the motto of life if you don't have enough amusement and tanginess in your life! However, it is not as simple as it may seem to Mahipalpur Escort in the twenty-first century by keeping yourself and those around you happy.
發表評論
暱 稱(*): 密 碼:
網 址: E - mail:
驗證碼(*): 驗證碼圖片 選 項:
頭 像:
內 容(*):