import machine
from machine import Pin
import network
import time
import sys
import json
import random
from machine import Pin, SoftI2C
from machine import Pin, ADC
from i2c_lcd import I2cLcd
import _thread
from _thread import start_new_thread
import dht
# Define pins and variables for sensors and relays
AddressOfLcd = 0x27
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=400000)
lcd = I2cLcd(i2c, AddressOfLcd, 4, 20)
# Khai báo led
led = Pin(2, Pin.OUT)
# Khai báo relays
relay1 = Pin(5, Pin.OUT)
relay2 = Pin(18, Pin.OUT)
relay3 = Pin(19, Pin.OUT)
relay4 = Pin(4, Pin.OUT)
# khai báo đọc cảm biến ánh sáng
ldr1 = ADC(Pin(32, Pin.IN))
#ldr1.atten(ADC.ATTN_11DB) # tín hiệu đầu vào được suy giảm đi khoảng 11dB trước khi ADC
# khai báo đọc cảm biến nhiệt độ và độ ẩm
dht = dht.DHT22(Pin(15)) #chính xác
# khai báo đọc cảm biến chuyển động
pir1 = machine.Pin(27, Pin.IN)
#-------------
sensor_data = {'temperature': 22, 'humidity': 50, 'light': 1000, 'motion': 0}
# đọc ánh sáng xem https://docs.wokwi.com/parts/wokwi-photoresistor-sensor
GAMMA = 0.7
RL10 = 50
def read_ldr():
analog_value = ldr1.read()
voltage = analog_value * 5 / 4095.0
resistance = 2000 * voltage / (1 - voltage / 5)
lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA))
return lux
def read_dht():
dht.measure() # Trigger a measurement from the sensor
return dht.temperature(), dht.humidity() # Return the temperature and humidity values
def read_pir(): # đọc cảm biến chuyển động
return pir1.value()
def control_relay(relay, state):
relay.value(state)
def display_data(sensor_data):
lcd.clear()
lcd.move_to(0,0) #hàng 0 cột 0
lcd.putstr("Temp: " + str(sensor_data['temperature']) + " C")
lcd.move_to(0,1) # hàng 1 cột 0
lcd.putstr("Hum: " + str(sensor_data['humidity']) + " %")
lcd.move_to(0,2)
lcd.putstr("Light: " + str(round(sensor_data['light'], 0)))
lcd.move_to(0,3)
lcd.putstr("Motion: " + str(sensor_data['motion']))
time.sleep(0.6)
# Điều khiển rơlay
relay1_state = False
relay2_state = False
relay3_state = False
relay4_state = False
def control_relay():
global relay1_state, relay2_state, relay3_state, relay4_state # khai báo biến toàn cục
relay1_state = not relay1_state
relay2_state = not relay2_state
relay3_state = not relay3_state
relay4_state = not relay4_state
relay1.value(relay1_state)
relay2.value(relay2_state)
relay3.value(relay3_state)
relay4.value(relay4_state)
time.sleep(0.6)
def blink_led():
while True:
led.value(1) # Turn on LED
time.sleep(1)
led.value(0) # Turn off LED
time.sleep(3)
# Start the LED blinking thread
start_new_thread(blink_led, ())
while True:
# Đọc giá trị các cảm biến
temperature, humidity = read_dht()
light = read_ldr()
motion = read_pir()
# Lưu giá trị các cảm biến vào dictionary
sensor_data = {'temperature': temperature, 'humidity': humidity, 'light': light, 'motion': motion}
# Hiển thị dữ liệu lên LCD
display_data(sensor_data)
print({'temperature': temperature, 'humidity': humidity, 'light': light, 'motion': motion})
control_relay()
time.sleep(3)
# Kiểm tra giá trị cảm biến và điều khi