]>
vault307.fbx.one Git - webserver.git/blob - web_server.py
1 import network
, socket
, time
, machine
3 cat
=machine
.Pin(15,machine
.Pin
.OUT
)
4 red
=machine
.Pin(18,machine
.Pin
.OUT
)
5 green
=machine
.Pin(19,machine
.Pin
.OUT
)
11 wlan
=network
.WLAN(network
.STA_IF
)
13 wlan
.connect(ssid
,password
)
14 while wlan
.isconnected() == False:
15 print('Waiting for connection...')
18 print(f
'Connected on {ip}')
23 connection
=socket
.socket()
24 connection
.bind(address
)
29 def webpage(Gstate
,Rstate
,state
):
34 <meta charset="UTF-8" />
35 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
38 <form action="./lighton">
39 <input type="submit" value="Cat on" />
41 <form action="./lightoff" />
42 <input type="submit" value="Cat off" />
44 <form action="./redon">
45 <input type="submit" value="Red on"/>
47 <form action="./redoff">
48 <input type="submit" value="Red off"/>
50 <form action="./greenon">
51 <input type="submit" value="Green on"/>
53 <form action="./greenoff">
54 <input type="submit" value="Green off"/>
57 <p>Red is {Rstate}</p>
58 <p>Green is {Gstate}</p>
64 def serve(connection
):
70 client
=connection
.accept()[0]
71 request
=client
.recv(1024)
74 request
=request
.split()[1]
77 if request
=='/lighton?':
80 elif request
=='/lightoff?':
83 elif request
=='/redon?':
86 elif request
=='/redoff?':
89 elif request
=='/greenon?':
92 elif request
=='/greenoff?':
95 html
=webpage(Gstate
,Rstate
,state
)
101 connection
=open_socket(ip
)
103 except KeyboardInterrupt: