import machine
import dht
from machine import I2C, Pin
import ssd1306
import utime
import socket
from machine import Pin, SoftI2C
# Khởi tạo đối tượng SoftI2C
i2c = SoftI2C(scl=Pin(22), sda=Pin(21))
# Thiết lập chân GPIO cho LED, cảm biến DHT, và đèn LED thông báo độ ẩm
led_pin = Pin(2, Pin.OUT) # Chọn chân GPIO để kết nối LED
dht_sensor = dht.DHT22(Pin(4)) # Chọn chân GPIO để kết nối cảm biến DHT22
humidity_led_pin = Pin(5, Pin.OUT) # Chọn chân GPIO để kết nối đèn LED thông báo độ ẩm
# Thiết lập I2C cho màn hình OLED
i2c = I2C(scl=Pin(22), sda=Pin(21))
i2c = SoftI2C(scl=Pin(22), sda=Pin(21)) # Chọn chân GPIO cho I2C (SCL và SDA)
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c) # Địa chỉ I2C của màn hình OLED và kích thước (128x64 pixels)
# Hàm đọc nhiệt độ và độ ẩm từ cảm biến DHT22
def read_temperature_and_humidity():
dht_sensor.measure()
temperature = dht_sensor.temperature()
humidity = dht_sensor.humidity()
return temperature, humidity
# Đặt ngưỡng nhiệt độ và độ ẩm để cảnh báo
threshold_temperature = 20
threshold_humidity = 70
# Khởi tạo biến đếm cho việc nhấp nháy LED cảnh báo nhiệt độ cao
led_counter = 0
# Hàm đọc cảm biến và trả về dữ liệu dạng chuỗi
def read_sensor():
global temp, temp_percentage, hum
temp = temp_percentage = hum = 0
try:
sensor.measure()
temp, hum = sensor.temperature(), sensor.humidity()
if (isinstance(temp, float) and isinstance(hum, float)) or (isinstance(temp, int) and isinstance(hum, int)):
msg = "{:.1f},{:.1f}".format(temp, hum)
temp_percentage = (temp + 6) / (40 + 6) * 100
hum = round(hum, 2)
return msg
else:
return 'Invalid sensor readings.'
except OSError as e:
return 'Failed to read sensor.'
# Tạo trang web hiển thị dữ liệu cảm biến
def web_page():
html = """<html><head><meta name="viewport" content="width=device-width, initial-scale=1">
<style>body{padding: 20px; margin: auto; width: 50%; text-align: center;}
.progress{background-color: #F5F5F5;} .progress.vertical{position: relative;
width: 25%; height: 60%; display: inline-block; margin: 20px;}
.progress.vertical > .progress-bar{width: 100% !important;position: absolute;bottom: 0;}
.progress-bar{background: linear-gradient(to top, #f5af19 0%, #f12711 100%);}
.progress-bar-hum{background: linear-gradient(to top, #9CECFB 0%, #65C7F7 50%, #0052D4 100%);}
p{position: absolute; font-size: 1.5rem; top: 50%; left: 50%; transform: translate(-50%, -50%); z-index: 5;}</style></head>
<body><h1>DHT Sensor</h1><div class="progress vertical">
<p>"""+str(temp)+"""*<p>
<div role="progressbar" style="height: """+str(temp_percentage)+"""%;" class="progress-bar"></div></div><div class="progress vertical">
<p>"""+str(hum)+"""%</p>
<div role="progressbar" style="height: """+str(hum)+"""%;" class="progress-bar progress-bar-hum"></div></div></body></html>"""
return html
sensor = dht.DHT22(Pin(4))
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)
while True:
try:
conn, addr = s.accept()
print('Connected to %s' % str(addr))
# Xử lý kết nối ở đây
except Exception as e:
print('Error accepting connection:', e)
print('Got a connection from %s' % str(addr))
request = conn.recv(1024)
print('Content = %s' % str(request))
sensor_readings = read_sensor()
print(sensor_readings)
response = web_page()
conn.send('HTTP/1.1 200 OK\n')
conn.send('Content-Type: text/html\n')
conn.send('Connection: close\n\n')
conn.sendall(response)
conn.close()