File tree Expand file tree Collapse file tree 1 file changed +80
-0
lines changed Expand file tree Collapse file tree 1 file changed +80
-0
lines changed Original file line number Diff line number Diff line change
1
+ import network
2
+ import socket
3
+ import time
4
+
5
+ from machine import Pin
6
+
7
+ led = Pin (15 , Pin .OUT )
8
+
9
+ ssid = 'YOUR NETWORK NAME'
10
+ password = 'YOUR NETWORK PASSWORD'
11
+
12
+ wlan = network .WLAN (network .STA_IF )
13
+ wlan .active (True )
14
+ wlan .connect (ssid , password )
15
+
16
+ html = """<!DOCTYPE html>
17
+ <html>
18
+ <head> <title>Pico W</title> </head>
19
+ <body> <h1>Pico W</h1>
20
+ <p>%s</p>
21
+ </body>
22
+ </html>
23
+ """
24
+
25
+ max_wait = 10
26
+ while max_wait > 0 :
27
+ if wlan .status () < 0 or wlan .status () >= 3 :
28
+ break
29
+ max_wait -= 1
30
+ print ('waiting for connection...' )
31
+ time .sleep (1 )
32
+
33
+ if wlan .status () != 3 :
34
+ raise RuntimeError ('network connection failed' )
35
+ else :
36
+ print ('connected' )
37
+ status = wlan .ifconfig ()
38
+ print ( 'ip = ' + status [0 ] )
39
+
40
+ addr = socket .getaddrinfo ('0.0.0.0' , 80 )[0 ][- 1 ]
41
+
42
+ s = socket .socket ()
43
+ s .bind (addr )
44
+ s .listen (1 )
45
+
46
+ print ('listening on' , addr )
47
+
48
+ # Listen for connections
49
+ while True :
50
+ try :
51
+ cl , addr = s .accept ()
52
+ print ('client connected from' , addr )
53
+ request = cl .recv (1024 )
54
+ print (request )
55
+
56
+ request = str (request )
57
+ led_on = request .find ('/light/on' )
58
+ led_off = request .find ('/light/off' )
59
+ print ( 'led on = ' + str (led_on ))
60
+ print ( 'led off = ' + str (led_off ))
61
+
62
+ if led_on == 6 :
63
+ print ("led on" )
64
+ led .value (1 )
65
+ stateis = "LED is ON"
66
+
67
+ if led_off == 6 :
68
+ print ("led off" )
69
+ led .value (0 )
70
+ stateis = "LED is OFF"
71
+
72
+ response = html % stateis
73
+
74
+ cl .send ('HTTP/1.0 200 OK\r \n Content-type: text/html\r \n \r \n ' )
75
+ cl .send (response )
76
+ cl .close ()
77
+
78
+ except OSError as e :
79
+ cl .close ()
80
+ print ('connection closed' )
You can’t perform that action at this time.
0 commit comments