1.23KiB; Python | 2019-09-04 20:14:38+02 | SLOC 46
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
#----- COMMUNICATION -----#
19
20
#Sends warning to central server
21
def send(ip, portn):
22 1
    import socket␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣
23
    sock = socket.socket()
24 1
␣␣␣␣
25
    sock.connect((ip, portn))
26 1
␣␣␣␣
27
    sock.send("1")
28 1
␣␣␣␣
29
    print("succesful")
30
    sock.close()
31
32
33
#----- EXECUTION -----#
34
35 2
import socket
36
37
#Web declaration
38
39
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
40
s.bind(('', 1883))
41
s.listen(2)
42
43
#Pin declaration
44
45
a0 = Pin(26, Pin.OUT)
46
a2 = Pin(34, Pin.IN)
47
48
while True:
49
    if a0.value() == 0:
50
        conn, addr = s.accept()
51
        print('Got a connection from %s' % str(addr))
52
        request = conn.recv(1024)
53
        request = str(request.decode())
54
        print(request)
55
        if request == "on":
56
            a0.value(1)
57
            print("Alarma encendida")
58
        conn.close()
59
    else:
60
        if a2.value():
61
            a0.value(0)
62
            print("Alarma Apagada")