from time import sleep_ms, ticks_ms, sleep, time
from machine import I2C, Pin
from i2c_lcd import I2cLcd
import dht
from hx711 import HX711
AddressOfLcd = 0x27
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=400000) # connect scl to GPIO 22, sda to GPIO 21
lcd = I2cLcd(i2c, AddressOfLcd, 2, 16)
#DHT22 Sensor
cb_dht = dht.DHT22(Pin(33))
time_dht = 0
doC = 0
doam = 0
motor_quat = Pin(23, Pin.IN)
#Loadcell Sensor
hx = HX711(dout = 15, pd_sck = 4)
hx.set_scale(206580) #208460
hx.tare()
weight_gram = 0
time_weight = 0
motor_can = Pin(18, Pin.IN)
def get_dht():
global time_dht
global doC, doam
if time() - time_dht >= 2:
try:
cb_dht.measure()
doC = cb_dht.temperature()
doam = cb_dht.humidity()
except OSError as e:
print('Failed to read sensor.')
time_dht = time()
def get_weight():
global time_weight
global weight_gram
if time() - time_weight >= 1:
weight_kg = hx.get_units(10)
weight_gram = 0
for i in range(9):
weight_gram += weight_kg*1000
if (weight_gram/10) < 0:
weight_gram = 0
# blynk.virtual_write(3, round(weight_gram/10))
time_weight = time()
def testLcd(num):
lcd.move_to(3,0)
lcd.putstr('Micropython')
lcd.move_to(0,1)
lcd.putstr("hello " + str(num))
while True:
for i in range(10):
testLcd(i)
sleep_ms(200)
get_dht()
print("Nhiet do:",doC)
weight_gram()
print("Can nang:", weight_gram)