Skip to content

Commit b82f87f

Browse files
Update main.py
1 parent baf2775 commit b82f87f

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

fs/main.py

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from time import sleep
1+
import time
22
import network
3-
from config import wifi_config, usmartpot_config
3+
import utelegram
4+
from config import wifi_config,usmartpot_config,utelegram_config
45
import sensors
56
import actuators
67
import utilitys
@@ -39,27 +40,30 @@
3940
bot = None
4041

4142
# Espera activa hasta 20 segundos para conectar
42-
for _ in range(80):
43+
for _ in range(160):
4344
if sta_if.isconnected():
4445
print('WiFi Conectado')
4546
lcd_display.show_online_status()
4647
try:
4748
utilitys.sync_time()
48-
bot = usmartpot.Ubot(usmartpot_config['email'], usmartpot_config['password'])
49+
bot = usmartpot.Ubot(usmartpot_config['email'],usmartpot_config['password'],usmartpot_config['crop'])
4950
should_send_msg = True
5051
print('Bot inicializado correctamente')
5152
except Exception as e:
5253
print(f"Error al inicializar el bot: {e}")
5354
break
5455
print('Conectando a WiFi...')
5556
lcd_display.show_wifi_spinner()
56-
sleep(0.25)
57+
time.sleep(0.25)
5758
else:
5859
print('No conectado - abortando')
5960

61+
6062
# Mostrar en Display LCD
6163
lcd_display.show_welcome_message()
62-
sleep(5)
64+
time.sleep(5)
65+
66+
token_timestamp = time.time()
6367

6468
# Enviar datos de los sensores cada 60 segundos
6569
while True:
@@ -80,8 +84,34 @@
8084

8185
# Enviar mensaje si el bot está conectado y la variable `should_send_msg` es True
8286
if should_send_msg:
83-
# utilitys.send_msg(bot,utelegram_config['chat'], temp, humidity_air, light, ph, tds, humidity_soil)
84-
utilitys.send_record(bot, usmartpot_config['crop'], temp, humidity_air, light, ph, tds, humidity_soil)
85-
sleep(60)
87+
if time.time() - token_timestamp > 3600:
88+
print("Renovando token…")
89+
if bot.refresh_token():
90+
token_timestamp = time.time()
91+
92+
def handle_uv_light(cmd):
93+
print("→ Activando UV Light")
94+
uv_light.turn_on()
95+
96+
def handle_water_pump(cmd):
97+
print("→ Activando Water Pump")
98+
water_pump.turn_on()
99+
100+
def handle_uv_light_off(cmd):
101+
print("→ Desactivando UV Light")
102+
uv_light.turn_off()
103+
104+
def handle_water_pump_off(cmd):
105+
print("→ Desactivando Water Pump")
106+
water_pump.turn_off()
107+
108+
109+
bot.register("ACTIVATE_UV_LIGHT", handle_uv_light)
110+
bot.register("ACTIVATE_WATER_PUMP", handle_water_pump)
111+
bot.register("DEACTIVATE_UV_LIGHT", handle_uv_light_off)
112+
bot.register("DEACTIVATE_WATER_PUMP", handle_water_pump_off)
113+
114+
bot.create_record(temp, humidity_air, light, ph, tds, humidity_soil)
115+
bot.listen(30)
86116
else:
87-
sleep(60) # Esperar 60 segundos si el bot no fue iniciado
117+
time.sleep(30) # Esperar 60 segundos si el bot no fue iniciado

0 commit comments

Comments
 (0)