|
1 | | -from time import sleep |
| 1 | +import time |
2 | 2 | import network |
3 | | -from config import wifi_config, usmartpot_config |
| 3 | +import utelegram |
| 4 | +from config import wifi_config,usmartpot_config,utelegram_config |
4 | 5 | import sensors |
5 | 6 | import actuators |
6 | 7 | import utilitys |
|
39 | 40 | bot = None |
40 | 41 |
|
41 | 42 | # Espera activa hasta 20 segundos para conectar |
42 | | - for _ in range(80): |
| 43 | + for _ in range(160): |
43 | 44 | if sta_if.isconnected(): |
44 | 45 | print('WiFi Conectado') |
45 | 46 | lcd_display.show_online_status() |
46 | 47 | try: |
47 | 48 | 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']) |
49 | 50 | should_send_msg = True |
50 | 51 | print('Bot inicializado correctamente') |
51 | 52 | except Exception as e: |
52 | 53 | print(f"Error al inicializar el bot: {e}") |
53 | 54 | break |
54 | 55 | print('Conectando a WiFi...') |
55 | 56 | lcd_display.show_wifi_spinner() |
56 | | - sleep(0.25) |
| 57 | + time.sleep(0.25) |
57 | 58 | else: |
58 | 59 | print('No conectado - abortando') |
59 | 60 |
|
| 61 | + |
60 | 62 | # Mostrar en Display LCD |
61 | 63 | lcd_display.show_welcome_message() |
62 | | - sleep(5) |
| 64 | + time.sleep(5) |
| 65 | + |
| 66 | + token_timestamp = time.time() |
63 | 67 |
|
64 | 68 | # Enviar datos de los sensores cada 60 segundos |
65 | 69 | while True: |
|
80 | 84 |
|
81 | 85 | # Enviar mensaje si el bot está conectado y la variable `should_send_msg` es True |
82 | 86 | 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) |
86 | 116 | 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