import machine
import network
from machine import RTC, Pin
import usocket as socket
import ustruct as struct
import select
from time import gmtime, sleep
from machine import Pin , SoftI2C
from time import sleep
from esp32_i2c_lcd import I2cLcd
lcdI2C = SoftI2C (scl = Pin(22), sda = Pin(21) , freq=100000)
lcd = I2cLcd(lcdI2C, 39 , 2 , 16)
SSID = 'Wokwi-GUEST'
PASSWORD = ''
GMT = -6 #Set timezone
NTP_DELTA = 3155673600 if gmtime(0)[0] == 2000 else 2208988800
NTP_HOST = "pool.ntp.org"
def get_ntp_time():
NTP_QUERY = bytearray(48)
NTP_QUERY[0] = 0x1B
try:
addr = socket.getaddrinfo(NTP_HOST, 123)[0][-1]
except OSError:
return 0
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
poller = select.poll()
poller.register(s, select.POLLIN)
try:
s.sendto(NTP_QUERY, addr)
if poller.poll(1000): # tiempo en milisegundos
msg = s.recv(48)
val = struct.unpack("!I", msg[40:44])[0]
return max(val - NTP_DELTA + GMT * 3600, 0)
except OSError:
pass
finally:
s.close()
return 0
def set_time_rtc():
t = get_ntp_time()
tm = gmtime(t)
rtc.datetime((tm[0] , (tm[1]) , (tm[2]) , (tm[6] +1), (tm[3]), tm[4], tm[5], 0))
#Tuple (year, month, day, weekday, hour, minute, second, tzinfo)
lcd.clear()
lcd.putstr("Conectando wifi")
#print("Connecting to WiFi", end="")
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
while not wlan.isconnected():
#print(".", end="")
lcd.putstr(".")
sleep(0.5)
print(" Connected!")
lcd.clear()
lcd.putstr("Wifi conectado")
sleep(0.5)
print(wlan.ifconfig())
rtc = RTC()
set_time_rtc()
Epochtime = rtc.datetime()
if((Epochtime[0])== 2000 or (Epochtime[0])== 1970):
while True:
lcd.clear()
lcd.putstr("Error de sincronizacion")
print("Error de sincronizacion")
sleep(1)
lcd.clear()
lcd.putstr("Reiniciando sistema")
print("Reiniciando sistema")
sleep(1)
machine.reset()
lcd.clear()
lcd.putstr("Reloj sincronizado")
sleep(0.750)
led = Pin(2, Pin.OUT)
led2 = Pin(12, Pin.OUT)
desired_hour = 22
desired_minute = 02
desired_second = 00
finish_hour = 22
finish_minute = 03
finish_second = 00
while True:
current_time = rtc.datetime()
lcd.clear()
if (current_time[4]) <= 9:
lcd.putstr("0")
lcd.putstr(str(current_time[4]))
lcd.putstr(":")
if (current_time[5]) <= 9:
lcd.putstr("0")
lcd.putstr(str(current_time[5]))
lcd.putstr(":")
if (current_time[6]) <= 9:
lcd.putstr("0")
lcd.putstr(str(current_time[6]))
lcd.move_to(0, 1)
if (current_time[2]) <= 9:
lcd.putstr("0")
lcd.putstr(str(current_time[2]))
lcd.putstr("/")
if (current_time[1]) <= 9:
lcd.putstr("0")
lcd.putstr(str(current_time[1]))
lcd.putstr("/")
if (current_time[0]) <= 9:
lcd.putstr("0")
lcd.putstr(str(current_time[0]))
current_hour = current_time[4]
current_minute = current_time[5]
current_second = current_time[6]
if (current_hour == desired_hour and
current_minute == desired_minute and
current_second == desired_second):
# Turn on the LED
led.on()
led2.on()
print("LED turned on at", desired_hour, ":", desired_minute, ":", desired_second)
lcd.clear()
lcd.putstr("Riego encendido")
sleep(0.5)
if(current_hour == finish_hour and
current_minute == finish_minute and
current_second == finish_second):
led.off()
led2.off()
print("LED turned off at", desired_hour, ":", desired_minute, ":", desired_second)
lcd.clear()
lcd.putstr("Riego apagado")
sleep(0.5)
sleep(1)
print(rtc.datetime())
print("\n")