11from time import localtime
2+ import network
3+ import ntptime
4+ import time
25
36# Códigos de color ANSI para impresión en consola
47RED = '\033 [91m'
912CYAN = '\033 [96m'
1013RESET = '\033 [0m'
1114
15+ def sync_time ():
16+ print ("Sincronizando tiempo NTP..." )
17+ try :
18+ ntptime .settime ()
19+ time .sleep (1 )
20+ print ("Tiempo sincronizado:" , localtime ())
21+ except Exception as e :
22+ print ("No se pudo sincronizar tiempo:" , e )
23+
1224
1325# Función para formatear la fecha y hora actual
1426def get_current_time ():
15- current_time = localtime () # Obtiene la hora local
27+ current_time = localtime ()
1628 return f"{ current_time [0 ]} -{ current_time [1 ]:02} -{ current_time [2 ]:02} { current_time [3 ]:02} :{ current_time [4 ]:02} :{ current_time [5 ]:02} "
1729
18-
1930def print_table (temp , humidity_air , light , ph , tds , humidity_soil ):
2031 # Encabezado de la tabla
2132 print (f"\n +--------------+------------------------+" )
2233 print (f"| " + get_current_time () + " |" )
2334 print (f"+--------------+------------------------+" )
2435 print (f"| Sensor | Valor |" )
2536 print (f"+--------------+------------------------+" )
26-
37+
2738 # Fila para temperatura
2839 if temp is not None :
2940 temp_display = f"{ RED } { temp :7.2f} °C{ RESET :<17} "
3041 else :
3142 temp_display = f"{ RED } N/A{ RESET :<17} "
3243 print (f"| Temp | { temp_display } |" )
33-
44+
3445 # Fila para humedad del aire
3546 if humidity_air is not None :
3647 humidity_air_display = f"{ BLUE } { humidity_air :7.2f} %{ RESET :<18} "
3748 else :
3849 humidity_air_display = f"{ BLUE } N/A{ RESET :<18} "
3950 print (f"| Humedad Aire | { humidity_air_display } |" )
40-
51+
4152 # Fila para luz
4253 if light is not None :
4354 light_display = f"{ YELLOW } { light :7.2f} lux{ RESET :<15} "
4455 else :
4556 light_display = f"{ YELLOW } N/A{ RESET :<15} "
4657 print (f"| Luz | { light_display } |" )
47-
58+
4859 # Fila para pH
4960 if ph is not None :
5061 ph_display = f"{ GREEN } { ph :7.2f} { RESET :<19} "
5162 else :
5263 ph_display = f"{ GREEN } N/A{ RESET :<19} "
5364 print (f"| pH | { ph_display } |" )
54-
65+
5566 # Fila para TDS
5667 if tds is not None :
5768 tds_display = f"{ CYAN } { tds :7.2f} ppm{ RESET :<15} "
5869 else :
5970 tds_display = f"{ CYAN } N/A{ RESET :<15} "
6071 print (f"| TDS | { tds_display } |" )
61-
72+
6273 # Fila para humedad del suelo
6374 if humidity_soil is not None :
6475 humidity_soil_display = f"{ MAGENTA } { humidity_soil :7.2f} %{ RESET :<18} "
6576 else :
6677 humidity_soil_display = f"{ MAGENTA } N/A{ RESET :<18} "
6778 print (f"| Humedad Suelo| { humidity_soil_display } |" )
68-
79+
6980 # Línea de cierre
7081 print (f"+--------------+------------------------+\n " )
7182
72-
7383def send_msg (bot , chat_id , temp , humidity_air , light , ph , tds , humidity_soil ):
7484 # Crear mensaje con los datos de los sensores
7585 sensor_data = (
@@ -91,10 +101,10 @@ def send_msg(bot, chat_id, temp, humidity_air, light, ph, tds, humidity_soil):
91101 except Exception as e :
92102 print (f"Error al enviar el mensaje: { e } " )
93103
94-
95104def send_record (ubot , crop_id , temp , humidity_air , light , ph , tds , humidity_soil ):
96105 """Envía un registro con los datos de sensores a la API SmartPot"""
97106
107+
98108 # Crear las medidas
99109 measures = {
100110 "atmosphere" : str (temp ),
@@ -106,13 +116,6 @@ def send_record(ubot, crop_id, temp, humidity_air, light, ph, tds, humidity_soil
106116 }
107117
108118 try :
109- response_data = ubot .create_record (crop_id , measures )
110-
111- if response_data :
112- print ("Registro enviado exitosamente:" )
113- print (response_data )
114- else :
115- print ("No se recibió respuesta del servidor." )
116-
119+ ubot .create_record (crop_id , measures )
117120 except Exception as e :
118121 print (f"Error al enviar el registro: { e } " )
0 commit comments