-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_hilos.py
610 lines (440 loc) · 13.3 KB
/
main_hilos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.textinput import TextInput
from kivy.properties import ObjectProperty
from kivy.clock import Clock
from kivy.config import Config
from threading import Thread
Config.set('input','mouse','mouse')
import time
import threading
import socket
import sys
import os
from os import remove
import os.path as path
from decimal import Decimal
#NOTIFICACIONES
from plyer import notification
#VIBRADOR
from plyer import vibrator
#importamos libreria de JSON
import json
#libreria de tiempo para los delay
from time import sleep
global ban
ban=False
#configuracion de pantalla
Config.set('graphics', 'width', 320)
Config.set('graphics', 'height', 500)
#Poner toogle button para conectar y deconector
#http://pythonmobile.blogspot.com/2014/06/35-togglebutton.html
#Abrimos un archivo txt y la ip que se ingrese se va guardar en el arhivo
IP = open ('ip.txt','w+')
IP.write("192.168.1.1")
ESP_IP= IP.read()
print(ESP_IP)
IP.close()
PUERTO = open ('puerto.txt','w+')
PUERTO.write("8266")
PUERTO.close()
PUERTO = open ('puerto.txt','r')
line= PUERTO.read()
ESP_PORT=line[:-1]
ESP_PORT=int(ESP_PORT)
print(ESP_PORT)
PUERTO.close()
# AF_INET= Direcciones de internet para ipv4
# SOCK_STREAM= tipo de socket para Protocolo TCP
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("prueba esro se repite 12312")
class MySocket:
#AQUI LLEGAN TODOS LOS DATOS Y SE GUARDAN EN UN ARCHIVO TXT
#DETECCION DE CORRIENTE ALTA Y NOTIFICACION
def get_data(self):
try:
print ("ENTRO AL TRY")
data=s.recv(1024)
with open('data.txt', "w") as f:
f.write(data)
sleep(0.1)
with open('data.txt', "r") as f:
strin=str(data)
global DATA
DATA=f.read()
Datas=data.decode()
dato_rec=True
except Exception:
dato_rec=False
if dato_rec== True:
with open('alerta.txt', "w") as f:
f.write(Datas)
sleep(0.1)
with open('alerta.txt', "r") as f:
strin=str(Datas)
I_ALERTA=f.read()
#verificamos que este la clave i_alerta
if "i_alerta" in json_arreglo:
json_arreglo = json.loads(I_ALERTA)
corrientem=eval(json_arreglo.get("i_alerta"))
#NOTIFICACIONES
#Generamos la notificacion con vibracion
title = b"Alerta".decode('utf8')
message = f" Alerta de corriente"
ticker = "La corriente tiene un valor de:" + corrientem + "superando el valor maximo"
app_name = "conexion"
app_icon = "plyer-icon.png"
toast = True
notification.notify(title=title,
message=message,
app_name=app_name,
app_icon=app_icon,
timeout=10,
ticker=ticker,
toast=toast
)
#ponemos a vibrar el telefono
vibrator.vibrate(10)
else:
print("NO HAY ALERTA")
class Box01(BoxLayout):
def __init__(self):
super(Box01,self).__init__()
self.add_widget(BoxNegro())
self.sock = MySocket()
global evento
evento = threading.Event()
threading.Thread(target=self.get_data).start()
def get_data(self):
while True:
self.text = self.sock.get_data()
class BoxNegro(BoxLayout):
# id de los suiches s1,s2...
s1= ObjectProperty()
s2= ObjectProperty()
s3= ObjectProperty()
s4= ObjectProperty()
#s.send(msg)
#CHECKBOX
#def checkbox_clicked(self, instance, value):
#if value is True:
#value_check_ip= True
#print("Checked")
#else:
#print("Unchecked")
#value_check_ip= False
#return value_check_ip
#validar ip... si ingresa una ip mala
#OSError: [Errno 113] No existe ninguna ruta hasta el `host'
#valñidar una exepcion
# BOTON DESCONECTAR
def desconectar(self):
self.img_conect.source= 'noconectado.png'
self.img_update.source= 'noupdate.png'
#enviamos el dato c para que el servidor tambien cierre
dato = str.encode("D")
print("Enviando D")
#s.send(dato)
#Agregamos una execpcion
try:
s.send(dato)
except BrokenPipeError:
print("Conexion Rota")
#cambiamos el titulo de texto
self.msg.text= 'Desconectado'
#Agregamos una execpcion
try:
s.shutdown(socket.SHUT_RDWR)
except OSError:
print("El otro extremo de la conexión no está conectado")
s.close()
# BOTON ACTUALIZAR
def actualizar(self):
#estado = evento.wait() # aqui el wait y nal final de la funcion activamos con set
self.root.evento.set()
dato = str.encode("B")
print("Enviando B")
s.send(dato)
#cambiamos la imagen de actualizar
self.img_update.source= 'update1.png'
data= s.recv(1024)
print(data)
datas=data.decode()
if data != b'A':
with open('update.txt', "w") as f:
f.write(datas)
#for ch in data:
#f.write(datas)
sleep(0.5)
with open('update.txt', "r") as f:
strin=str(datas)
dataa=f.read()
print(dataa)
print(type(dataa))
JSON = json.loads(dataa)
print(JSON)
print(type(JSON))
if "c1" in JSON:
print("La corriente 1 es:", JSON["c1"])
#convertimo esto en decimal para usar la libreria
corriente1=eval(JSON.get("c1"))
co_decimal=Decimal(corriente1)
if co_decimal.compare(Decimal(0.1)) <= Decimal(0.1):
#NO hay corriente, por lo tanto no hay voltaje
#para solventar esto, hayq eu colocar un sensor de voltaje
corriente1=0
else:
print ("Si Hay corriente")
#print(corriente1)
potencia1= str(float(corriente1)*120)
#setiamos la potencia
self.p1.text=potencia1
print(type(corriente1))
#setiamos la corriente
self.i1.text=str(corriente1)
#setiamos la corriente IRMS
corrienteRMS=str(float(corriente1)*.707) #corriente*sqrt(2)
self.i1rms.text=corrienteRMS
elif "i_alerta" in JSON:
print("Corriente ALERTA:", JSON["i_alerta"])
corrientem=str(eval(JSON.get("i_alerta")))
################################################################################################ NOTIFICACIONES
#Generamos la notificacion con vibracion
title = b"Alerta".decode('utf8')
message = f" Alerta de corriente"
ticker = "La corriente tiene un valor de:" + corrientem + "superando el valor maximo"
app_name = "conexion"
app_icon = "plyer-icon.png"
toast = True
notification.notify(title=title,
message=message,
app_name=app_name,
app_icon=app_icon,
timeout=10,
ticker=ticker,
toast=toast
)
#ponemos a vibrar el telefono
vibrator.vibrate(10)
else:
print ("No se encuentra")
#evento.set()
return corriente1
#cliente = s.makefile("rwb", buffering=0)
#cliente.write(b"Hello World!")
#cliente.flush()
#print(cliente)
#print(server)
#line = server.readline()
#print (repr(line))
#line = server.readline()
#print (repr(line))
#print(data)
#datas= data.decode()
#print(datas)
#sleep(0.9)
#with open("update.tx", 'w') as datas:
#datas.write(datas)
# BOTON CONECTAR
def conectar(self):
self.root.stop.set()
#Enviamos una letra A, para que no se confunda con otro dato
#Enviamos un A como ping
dato = str.encode("A")
print("Enviando data")
#Agregamos una execpcion
try:
s.send(dato)
except BrokenPipeError:
print("Conexion Rota")
# variable que recibimos del servidor (nodemcu)
except OSError:
print("Archivo incorrupto en el Socket")
# variable que recibimos del servidor (nodemcu)
data = ""
#recibimos la data con un while
while True:
data= s.recv(1024)
print("Recibiendo data")
print(data)
data=data.decode("utf-8")
print("Decodificando data")
print(data)
#cambiamos el titulo de texto
self.msg.text= 'Conetando'
if data=="A" :
print ("Conetado")
print(data)
#salto de linea
print("")
#cambiamos el titulo de texto
self.msg.text= 'Conetado'
#cambiamos la imagen a enchufe conectado
self.img_conect.source= 'conectado.png'
#llega un JSON con los estados de los suiches guardados en la EEPROM
elif data != 'A':
#datas=data.decode()
print("entrooo al if")
print(data)
suiches = open ('suiches.txt','w+')
suiches.write(data)
suiches.close()
# si no queremos guardar en txt ver https://stackoverflow.com/questions/26247353/issues-with-tcp-on-arduino-reading-strings-from-a-python-tcp-socket-server
with open('suiches.txt', "r") as suiches:
dataa=suiches.read()
json_suiches = json.loads(dataa)
print("El Suiche 1 es:", json_suiches["l1"])
print(type(json_suiches["l1"]))
suiche1=json_suiches["l1"]
if suiche1=="false":
self.s1.active= True
else:
self.s1.active= False
#El shiel rele trabaja con los estados invertidos, invertimos el valor
#print("El Suiche 1 negado es:", json_suiches["l1"])
#suiche1= not json_suiches["l1"]
#print(type(suiche1))
#print(suiche1)
#Suichamos los estados guardados en la EEPROM del nodemcu
#if suiche1 == False:
#self.s1.active= False
#else:
#self.s1.active= False
#self.s4.disabled= True
#salimos del ciclo while con break
break
print("salio del while")
#if data=="AA" :
evento.set()
# RELE 1
def server1(self, instance, value) :
#Llamamo a la funcion conectar para que valiede el estado del
#servidor
#BoxNegro.conectar(self)
#if bandera== True:
if value is True:
#creamos un diccionario en python
#l = load c=current
#Negamos el valor porque el shield rele trabaja al reves
mensaje = {"l1": not value}
#convertimos el diccionario en json
msg = json.dumps(mensaje)
#convertiendo JSON a bytes
msg =str.encode(msg)
print(msg)
s.send(msg)
#print("encendido")
#dato = str.encode("A")
#s.send(dato)
#s.close()
elif value is False:
#print("NO encendido")
#dato = str.encode("B")
#s.send(dato)
#probamos enviando JSON
#creamos un diccionario en python
estado= value
mensaje = {"l1": not estado}
#convertimos el diccionario en json
msg = json.dumps(mensaje)
#convertiendo JSON a bytes
msg =str.encode(msg)
print(msg)
s.send(msg)
#s.close()
# PESTAÑA CONFIG
def guardar_current(self,i_maxima):
#Aqui se enviaran los valores de corrientes al servidor
#creamos el diccionario
msg = {"lm1": 2}
#le asignamos el valor ingresado por la input al diccionario
msg["lm1"]=float(i_maxima)
#convertimos el diccionario en json
msg = json.dumps(msg)
#convertiendo JSON a bytes
msg =str.encode(msg)
print(msg)
#enviamos el JSON
s.send(msg)
#PESTAÑA LOGIN
def guardar_login(self, input_puerto, input_ip):
#Si existe algun dato guardado anteriormente lo eliminamos
if path.exists("puerto.txt"):
remove("puerto.txt")
elif path.exists("ip.txt"):
remove("ip.txt")
elif path.exists("suiches.txt"):
remove("suiches.txt")
elif path.exists("update.txt"):
remove("update.txt")
if self.check_ip.active:
# Esta es la ip ESTATICA o ip defaul
with open('ip.txt', "w") as IP:
IP.write("192.168.11.9")
PUERTO= open('puerto.txt', 'w+')
PUERTO.write(input_puerto)
PUERTO.close()
with open('ip.txt', "r") as IP:
ESP_IP=IP.read()
ESP_PORT = int(input_puerto)
print(ESP_IP)
print(ESP_PORT)
#s.connect((ESP_IP , ESP_PORT))
#elimnamos mensaje
self.msg_error.text= 'Espere un momento'
else:
with open('ip.txt', "w") as IP:
IP.write(input_ip)
with open('ip.txt', "w") as PUERTO:
PUERTO.write(input_puerto)
ESP_IP = str(input_ip)
ESP_PORT = int(input_puerto)
print(ESP_IP)
print(ESP_PORT)
#s.connect((ESP_IP , ESP_PORT))
#elimnamos mensaje
self.msg_error.text= 'Espere un momento'
#return ESP_IP
# Agregamos una exepcion en el caso de que la ip o el puerto este mal
try:
#Connect se usa para conexion remota
s.connect((ESP_IP , ESP_PORT))
self.msg_error.text= 'Conexion Exitosa'
except OSError:
#pass
print("IP incorrecta")
self.msg_error.text= 'Error: IP Incorrecta'
s.close() #cerramos socket
#finally:
#s.close()
class BoxRed(BoxLayout):
None
class BoxGreen(BoxLayout):
None
class BoxBlue(BoxLayout):
None
class MainApp(App):
#Titulo de la ventana
title = "Conexion al servidor"
#Eliminar el menu de opciones de kivy en el telefono
def open_settings(*args):
pass
print("prueba esro se repite 222222")
def build(self):
#Hacemos una instancia de la clase para enviar la IP ingresada
#port_and_ip=BoxNegro()
#port_and_ip.guardar_login.ESP_IP=
print("prueba esro se repite 888888")
try:
#Connect se usa para conexion remota
s.connect((ESP_IP , ESP_PORT))
except OSError:
print("IP incorrecta")
return Box01()
if __name__ == "__main__":
MainApp().run()