from machine import Pin, SoftI2C, ADC, PWM
from servo import Servo
import dht
import math
import time
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
import wifi
from umqtt.simple import MQTTClient
import uasyncio as asyncio
################################
#man hinh led
I2C_ADDR = 0x27
totalRow = 4
totalColumns = 20
i2c = SoftI2C(scl = Pin(22), sda = Pin(21), freq = 10000) #initializing I2C method for ESP32
lcd = I2cLcd(i2c, I2C_ADDR, totalRow, totalColumns)
lcd.putstr("Hello")
time.sleep(1)
# lcd.clear()
# wifi.connect_ap()
#MQTT
# client_id = "720e81de16ff4f6786bbed0c1da73392.s2.eu.hivemq.cloud"
# server = "broker.hivemq.com"
# client = MQTTClient(client_id, server)
# client.connect()
#ThinkSpeak
# client_id1 = "CQAfHRwVKCMuDy8FGh8VHDs"
# user_name1 = "CQAfHRwVKCMuDy8FGh8VHDs"
# password1 = "iTtNunL32EtXNrMKsSudXmAB"
# server1 = "mqtt3.thingspeak.com"
# client1 = MQTTClient(client_id = client_id1, server =server1, user=user_name1, password=password1)
# client1.connect()
# publish_topic = b"channels/2384358/publish"
#####################
# Giải thích giá trị nút bấm
# value = 1 là nút chưa được bấm, thiết bị tự động hoạt động bình thường
# value = 0 là nút được bấm, bắt buộc khởi động thiết bị
white = Pin(18, Pin.IN, Pin.PULL_UP) # CHỌN CHẾ ĐỘ ĐIỀU KHIỂN THỦ CÔNG/TỰ ĐỘNG
red = Pin(2, Pin.IN, Pin.PULL_UP) #khởi động máy bơm nước (RELAY CÓ LED ĐỎ)
yellow = Pin(4, Pin.IN, Pin.PULL_UP) #khởi động máy bơm nước (RELAY CÓ LED VÀNG)
green = Pin(5, Pin.IN, Pin.PULL_UP) #khởi động servo mở rèm che
while True:
print("white:", white.value())
print("red:", red.value())
print("yellow:", yellow.value())
print("green:", green.value())
print()
# Nhận dữ liệu từ MQTT và gán vào các biến sau
# Code here.................
# t =
# h =
# lux =
# soilMoisterValue =
#Hiển thị lên led
# lcd.clear()
# lcd.putstr(f"Temp: {t}\n")
# lcd.putstr(f"Humid: {h}\n")
# lcd.putstr(f"Lux: {lux}\n")
# lcd.putstr(f"SoilMois: {soilMoisterValue}\n")
# Gửi giá trị nút bấm lên MQTT theo đúng thiết bị
# Nếu giá trị white = 0 thì chuyển sang chế độ thủ công, mọi điều khiển đều theo các phím thủ công
if white.value() == 0:
white_msg = "white_0"
# Nếu giá trị red = 0 (nút đỏ được bấm) thì bắt buộc bật máy bơm nước (RELAY LED ĐỎ)
# Ngược lại với giá trị 1
if red.value() == 0:
red_msg = "red_0"
# Nếu giá trị yellow = 0 (nút vàng được bấm) thì bắt buộc bật máy bơm nước (RELAY LED VÀNG)
# Ngược lại với giá trị 1
if yellow.value() == 0:
yellow_msg = "yellow_0"
# Nếu giá trị green = 0 (nút xanh được bấm) thì mở rèm che (motor.move(0))
# Ngược lại với giá trị 1 (motor.move(180))
if green.value() == 0:
green_msg = "green_0"
time.sleep(1)