Internet of ThingsOpen HabSmartHome
Bài 4.0: Đọc nhiệt độ trên cảm biến DHT11
1. Giới thiệu cảm biến DHT11
– DHT11 là cảm biến nhiệt độ, độ ẩm rất thông dụng hiện nay vì chi phí rẻ và rất dễ lấy dữ liệu. Cảm biến được tích hợp bộ tiền xử lý tín hiệu giúp dữ liệu nhận về được chính xác mà không cần phải qua bất kỳ tính toán nào.
– Đặc điểm : Điện áp hoạt động : 3V – 5V (DC); Dải nhiệt độ hoạt động : 0°C ~ 50°C, sai số ±2°C ; Khoảng cách truyển tối đa: 20m

2. Nguyên lí hoạt động
Dữ liệu đo được truyền từ DHT11 thông qua ESP8266. Nhờ giao thức MQTT dữ liệu đến và hiển thị trên openHAB

3. Chuẩn bị
Thao tác giống với điều khiển Led nhưng giờ bạn phải sửa file .items và code trong arduino.
Bước 1: Chỉnh sửa file .items theo mẫu:
1 2 3 4 5 6 7 8 9 |
Group Tang1 "Tầng 1" <groundfloor> Group PhongKhach1 "Phòng Khách" <video> (Tang1) Group Toilet1 "Toilet" <bath> (Tang1) Switch Den_PhongKhach "Đèn" <light> (PhongKhach1) {mqtt=">[broker: Galileo/PhongKhach1/Den:command: On:On],>[broker:Galileo/PhongKhach1/Den:command:OFF:OFF]"} Number Temperature1 "Temperature [%.1f C]" <temperature> (PhongKhach1) {mqtt="<[broker:Galileo/PhongKhach1/temperature:state:default]"} |
Bước 2: Chỉnh sửa code trong arduino
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
#include <ESP8266WiFi.h> #include <Wire.h> #include <PubSubClient.h> #include "DHT.h" #define DHTPIN 12 //D6 #define DHTTYPE DHT11 // DHT 11 DHT dht(DHTPIN, DHTTYPE); #define wifi_ssid "lophocvui.com" #define wifi_password "88889999" #define mqtt_server "192.168.1.167" //#define mqtt_user "user" //#define mqtt_password "password" #define temperature_celsius_topic "Galileo/PhongKhach1/temperature" WiFiClient espClient; PubSubClient client(espClient); void setup() { Serial.begin(115200); dht.begin(); setup_wifi(); client.setServer(mqtt_server, 1883); } void setup_wifi() { delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(wifi_ssid); WiFi.begin(wifi_ssid, wifi_password); while (WiFi.status() != WL_CONNECTED){ delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void reconnect(){ while (!client.connected()){ if (client.connect("client_Galileo")){ Serial.println("connected"); }else{ delay(5000); } } } void loop(){ if (!client.connected()){ reconnect(); } client.loop(); delay(2000); float t = dht.readTemperature(); client.publish(temperature_celsius_topic, String(t).c_str(), true); } |
4. Lắp đặt

– Video minh họa:
Nếu có gì thắc mắc, các bạn có thể đặt các câu hỏi trực tiếp Fanpage và Group
Chúc các bạn thành công !
————————
Chi tiết liên hệ:
?Website: lophocvui.com
☎️Hotline: 0983811938