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
# ################################ Mo ta thiet bi
# # relay dieu khien hoat dong may bom nuoc/may bom dung dich phan bon
# # phần máy bơm hoạt động luân phiên đang bị auto hoạt động và chưa có biện pháp ngắt nguồn
# # servo dieu khien mai che nang
# # LRD la cam bien anh sang (cai mau xanh xanh nam tren breadboard)
# # LRD -> servo
# # cam bien do am dat la chip custom mau xanh luc o duoi
# # nut bam thuc hien dieu khien thu cong
# ################################
# #cam bien nhiet do, do am
# sensor = dht.DHT22(Pin(14))
# h = -1
# time.sleep(10)
# ################################
# #man hinh led
# I2C_ADDR = 0x27
# totalRow = 2
# totalColumns = 16
# 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()
# ################################
#cam bien anh sang
#muc anh sang khong tot: direct sunlight (>30,000 lux)
wifi.connect_ap()
adc = ADC(Pin(27))
# print(adc.read_u16())
# atten_value = ADC.ATTN_11DB
# adc.atten(atten_value)
rl10 = 50000 # LDR resistance at 10lux
gamma = 0.7 # log(R) / log(lux)
#tinh quang tro
def calculate_resistance():
value = adc.read()
voltage_ratio = value / (4095 - value)
return 10000 * voltage_ratio
#R = R_10 * (lux / 10) ^ -γ => lux = 10 * (R / R_10) ^ -1/γ
#tinh lux
def calculate_lux(resistance):
return 10 * math.pow(resistance / (rl10), -1 / gamma)
# ################################
# #servo
# motor = Servo(pin = 23)
# # motor.move(n) # quay n°
# motor.move(0)
# print("Mở màn che")
# ################################
# #relay
# #relay led đỏ bơm nước tưới
# #relay led vàng bơm dung dịch phân bón
# waterRelay = Pin(25, Pin.OUT) # nước
# fertilizerRelay = Pin(26, Pin.OUT) # phân bón
# # relay.off cổng COM kết nối NC
# # relay.on cổng COM kết nối NO
# # relay.on()
# ################################
# #soil_sensor_chip
# # khoảng giá trị đầu ra 350-1024
# # 350 là có nước, 1024 là không có nước
# # cách dùng: soilMois.read()
# soilMois = ADC(Pin(12))
# soilMois.atten(ADC.ATTN_11DB)
# #test button
# button = Pin(4, Pin.OUT)
# ################################
# # Khai báo biến đo lường
# t = -1 #nhiệt độ
# h = -1 #độ ẩm
# lux = -1 #ánh sáng
# soilMoisterValue = -1
countingTime = -1
# #Các mảng này để lưu trữ và tính giá trị trung bình trong một khoảng thời gian
# t_stack = []
# h_stack = []
# lux_stack = []
# # Hàm khởi động máy bơm tưới nước
# def watering(duration):
# waterRelay.on();
# time.sleep(duration)
# waterRelay.off()
# # client_id = "720e81de16ff4f6786bbed0c1da73392.s2.eu.hivemq.cloud"
# # server = "broker.hivemq.com"
# # client = MQTTClient(client_id, server)
# # client.connect()
while True:
# Bộ đếm giây
countingTime += 1
# print(countingTime)
time.sleep(1)
# #Kiểm tra trạng thái tưới nước
# isWatered = False
# #reset bộ đếm sau mỗi 24h
# if countingTime > 24*60*60:
# countingTime = 0
# # Đo sau mỗi 5 giây
if countingTime % 5 == 0:
# # đo từ cảm biến nhiệt độ, độ ẩm
# sensor.measure()
# t = sensor.temperature()
# h = sensor.humidity()
# #Lưu giá trị đo lường vào stack
# t_stack.append(t)
# h_stack.append(h)
# Đo giá trị ánh sáng, đất
lux = calculate_lux(calculate_resistance()) # đo độ sáng
print(lux)
# lux_stack.append(lux) #Lưu vào stack
# soilMoisterValue = soilMois.read() #đo độ ẩm đất
# #Hiển thị lên led
# lcd.clear()
# lcd.move_to(0, 0)
# lcd.putstr("Temperature:" + str(t))
# lcd.move_to(1, 0)
# lcd.putstr("Humidity:" + str(h))
# lcd.move_to(2, 0)
# lcd.putstr("Lux:" + str(lux))
# lcd.move_to(3, 0)
# lcd.putstr("Soil Moister:" + str(soilMoisterValue))
# # print("Send data to broker")
# # client.publish("IoT_MQTT_TemperatureA", str(t))
# # time.sleep(2)
# if countingTime % (10*60) == 0: # Xử lí dữ liệu nhiệt độ, độ ẩm sau mỗi 10p
# tempAverage = sum(t_stack)/len(t_stack)
# humidAverage = sum(h_stack)/len(h_stack)
# # isWatered = False
# # print(tempAverage)
# # print(humidAverage)
# # Gửi dữ liệu nhiệt độ, độ ẩm trung bình trong 10p vừa rồi lên mqtt/thingspeak/ifttt
# #...........
# #Khởi động máy bơm nước để tưới khi nhiệt độ > 40
# if (tempAverage > 40 or humidAverage < 15) and isWatered == False:
# watering(10) #Hàm định nghĩa ở trên
# countingTime += 10
# isWatered = True
# t_stack.clear()
# h_stack.clear()
# if countingTime % (60*60) == 0: #Xử lí dữ liệu ánh sáng sau mỗi 1h
# luxAverage = sum(lux_stack)/len(lux_stack)
# # print(luxAverage)
# # Gửi dữ liệu ánh sáng trung bình trong 1h lên mqtt/thingspeak/ifttt
# #............
# # Đóng màn che nắng khi chỉ số ánh sáng vượt 30.000 lux
# print(luxAverage)
# if (luxAverage > 30000):
# motor.move(180)
# print("Đóng màn che")
# else:
# motor.move(0)
# print("Mở màn che")
# lux_stack.clear()
# if countingTime % (12*60*60) == 0: #Xử lí dữ liệu cảm biến đất sau mỗi 12h
# #Gửi dữ liệu độ ẩm đất lên mqtt/thingspeak/ifttt
# #.............
# #Tưới nước khi độ ẩm đất và không khí quá thấp
# if (soilMoisterValue > 1020 and humidity < 15) and isWatered == False:
# watering(10)
# countingTime += 10
# isWatered = True