Some of the repetitive violations were suppressed.
2.87KiB; Python | 2019-09-04 20:14:38+02 | SLOC 96
1
#----- CLIENT CONNECTION -----#
2
3
from machine import Pin
4
import network
5 2
import time
6
7
ssid = 'AndroidAP'            #SSID of the network
8
password = 'hola1234'   #Password of the network
9
10
station = network.WLAN(network.STA_IF)
11
station.active(True)
12
station.connect(ssid, password)
13
while station.isconnected() == False:
14
    pass
15
print('Connection successful')
16
print(station.ifconfig())
17
18
#----- WEB SERVICE -----#
19
20
def saf():
21
    html ="""
22
    <html>
23
        <head>
24
        <title>SAF</title>
25
        <meta name="viewport" content="width=device-width, initial-scale=1">
26
        <link rel="icon" href="data:,">
27
        <style>
28
            html{font-family: Helvetica; display:inline-block; margin: 0px auto; text-align: center;}
29
            h1{color: #0F3376; padding: 2vh;}p{font-size: 1.5rem;}
30
            .button{display: inline-block; background-color: #31ad00; border: none;
31
            border-radius: 4px; color: white; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}
32
            .button2{background-color: #f30c0c;}
33
        </style>
34
        </head>
35
        <body>
36
        <h1>Sistem Against Fires</h1>
37
        <p><a href="/?fire=yes"><button class="button">ALERT FIREFIGHTERS</button></a></p>
38
        <h1>Sistema de Iluminación</h1>
39
        <p><a href="/?light=on"><button class="button">ON</button></a></p>
40
        <p><a href="/?light=off"><button class="button">OFF</button></a></p>
41
        </body>
42
    </html>
43
    """
44
    return html
45
46
#----- COMMUNICATION -----#
47
48
#Sends warning to central server
49
def send(ip, portn):
50 1
    import socket␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣
51
    sock = socket.socket()
52 1
␣␣␣␣
53
    sock.connect((ip, portn))
54 1
␣␣␣␣
55
    sock.send("on")
56 1
␣␣␣␣
57
    print("succesful")
58
    sock.close()
59
60
def sendon(ip, portn):
61 1
    import socket␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣
62
    sock = socket.socket()
63 1
␣␣␣␣
64
    sock.connect((ip, portn))
65 1
␣␣␣␣
66
    sock.send("on")
67 1
␣␣␣␣
68
    print("succesful")
69
    sock.close()
70
71
def sendoff(ip, portn):
72 1
    import socket␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣
73
    sock = socket.socket()
74 1
␣␣␣␣
75
    sock.connect((ip, portn))
76
␣␣␣␣
77
    sock.send("off")
78
␣␣␣␣
79
    print("succesful")
80
    sock.close()
81
82
83
#----- EXECUTION -----#
84
#Server IP
85
host = "192.168.43.80"
86
port = 8080
87
88
#LIght System
89
hostl = "192.168.43.38"
90
portl = 3000␣
91
92 2
import socket
93
94
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
95
s.bind(('', 80))
96
s.listen(5)
97
98
#Port Declaration
99
a5 = Pin(4, Pin.OUT)
100
101
while True:
102
    conn, addr = s.accept()
103
    print('Got a connection from %s' % str(addr))
104
    request = conn.recv(1024)
105
    request = str(request)
106
    print('Content = %s' % request)
107
108
    fion = request.find('/?fire=yes')
109
    if fion == 6:
110
        send(host, port)
111
        pass
112
113
    lion = request.find('/?light=on')
114
    if lion == 6:
115
        a5.value(1);
116
        sendon(hostl, portl)
117
118
    lioff = request.find('/?light=off')
119
    if lioff == 6:
120
        a5.value(0);
121
        sendoff(hostl, portl)
122
␣␣␣␣␣␣␣␣
123
    response = saf()
124
    conn.send(response)
125
    conn.close()