Hello from Pico W.
31 | 32 | 33 | """ 34 | 35 | addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1] 36 | s = socket.socket() 37 | s.bind(addr) 38 | s.listen(1) 39 | 40 | print('listening on', addr) 41 | led.off() 42 | 43 | # Listen for connections 44 | while True: 45 | try: 46 | cl, addr = s.accept() 47 | print('client connected from', addr) 48 | request = cl.recv(1024) 49 | led.on() 50 | print(request) 51 | 52 | cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n') 53 | cl.send(html) 54 | cl.close() 55 | led.off() 56 | 57 | except OSError as e: 58 | cl.close() 59 | print('connection closed') 60 | -------------------------------------------------------------------------------- /ap_connect.py: -------------------------------------------------------------------------------- 1 | # Access point interface 2 | 3 | import network 4 | import time 5 | 6 | ssid = 'PiPicoAp' 7 | password = 'password' 8 | 9 | ap = network.WLAN(network.AP_IF) 10 | ap.config(essid=ssid, password=password) 11 | ap.active(True) 12 | 13 | # wait for wifi to go active 14 | wait_counter = 0 15 | while ap.active() == False: 16 | print("waiting " + str(wait_counter)) 17 | time.sleep(0.5) 18 | pass 19 | 20 | print('WiFi active') 21 | status = ap.ifconfig() 22 | print('IP address = ' + status[0]) 23 | print('subnet mask = ' + status[1]) 24 | print('gateway = ' + status[2]) 25 | print('DNS server = ' + status[3]) 26 | -------------------------------------------------------------------------------- /connect.py: -------------------------------------------------------------------------------- 1 | # Pi Pico connection to home Wi-Fi network 2 | 3 | import utime 4 | import network 5 | from NetworkCredentials import NetworkCredentials 6 | 7 | ssid = NetworkCredentials.ssid 8 | password = NetworkCredentials.password 9 | 10 | # set Wi-Fi to station interface 11 | wlan = network.WLAN(network.STA_IF) 12 | # activate the network interface 13 | wlan.active(True) 14 | # connect to Wi-Fi network 15 | wlan.connect(ssid, password) 16 | 17 | # wait for connection 18 | max_wait = 10 19 | while max_wait > 0: 20 | """ 21 | 0 STAT_IDLE -- no connection and no activity, 22 | 1 STAT_CONNECTING -- connecting in progress, 23 | -3 STAT_WRONG_PASSWORD -- failed due to incorrect password, 24 | -2 STAT_NO_AP_FOUND -- failed because no access point replied, 25 | -1 STAT_CONNECT_FAIL -- failed due to other problems, 26 | 3 STAT_GOT_IP -- connection successful. 27 | """ 28 | if wlan.status() < 0 or wlan.status() >= 3: 29 | # connection successful 30 | break 31 | max_wait -= 1 32 | print('waiting for connection... ' + str(max_wait)) 33 | utime.sleep(1) 34 | 35 | # check connection 36 | if wlan.status() != 3: 37 | # No connection 38 | raise RuntimeError('network connection failed') 39 | else: 40 | # connection successful 41 | print('wlan connected') 42 | status = wlan.ifconfig() 43 | print('IP address = ' + status[0]) 44 | print('subnet mask = ' + status[1]) 45 | print('gateway = ' + status[2]) 46 | print('DNS server = ' + status[3]) 47 | -------------------------------------------------------------------------------- /gettime.py: -------------------------------------------------------------------------------- 1 | # use web service to get date and time information 2 | 3 | import utime 4 | import network 5 | import urequests 6 | from NetworkCredentials import NetworkCredentials 7 | 8 | ssid = NetworkCredentials.ssid 9 | password = NetworkCredentials.password 10 | 11 | # set WiFi to station interface 12 | wlan = network.WLAN(network.STA_IF) 13 | # activate the network interface 14 | wlan.active(True) 15 | # connect to wifi network 16 | wlan.connect(ssid, password) 17 | 18 | max_wait = 10 19 | # wait for connection 20 | while max_wait > 0: 21 | """ 22 | 0 STAT_IDLE -- no connection and no activity, 23 | 1 STAT_CONNECTING -- connecting in progress, 24 | -3 STAT_WRONG_PASSWORD -- failed due to incorrect password, 25 | -2 STAT_NO_AP_FOUND -- failed because no access point replied, 26 | -1 STAT_CONNECT_FAIL -- failed due to other problems, 27 | 3 STAT_GOT_IP -- connection successful. 28 | """ 29 | if wlan.status() < 0 or wlan.status() >= 3: 30 | # connection successful 31 | break 32 | max_wait -= 1 33 | print('waiting for connection... ' + str(max_wait)) 34 | utime.sleep(1) 35 | 36 | # check connection 37 | if wlan.status() != 3: 38 | # No connection 39 | raise RuntimeError('network connection failed') 40 | else: 41 | # connection successful 42 | print('wlan connected') 43 | status = wlan.ifconfig() 44 | print('ip = ' + status[0]) 45 | 46 | # main loop 47 | while True: 48 | start = utime.ticks_ms() 49 | response = urequests.get('https://timeapi.io/api/Time/current/zone?timeZone=Europe/London') 50 | print('request took ' + str(utime.ticks_ms() - start) + "ms") 51 | print(response.content) 52 | print(response.json()['dateTime']) 53 | utime.sleep(5) 54 | -------------------------------------------------------------------------------- /simpleled.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |Click the buttons to control the LED
8 | 9 | 10 | 11 | 12 |The LED is **ledState**
13 | 14 | 15 | -------------------------------------------------------------------------------- /simpleled.py: -------------------------------------------------------------------------------- 1 | # use web interface to control an LED 2 | 3 | import utime 4 | import network 5 | import socket 6 | import urequests 7 | from machine import Pin 8 | from NetworkCredentials import NetworkCredentials 9 | 10 | ssid = NetworkCredentials.ssid 11 | password = NetworkCredentials.password 12 | 13 | # set WiFi to station interface 14 | wlan = network.WLAN(network.STA_IF) 15 | # activate the network interface 16 | wlan.active(True) 17 | # connect to wifi network 18 | wlan.connect(ssid, password) 19 | 20 | max_wait = 10 21 | # wait for connection 22 | while max_wait > 0: 23 | """ 24 | 0 STAT_IDLE -- no connection and no activity, 25 | 1 STAT_CONNECTING -- connecting in progress, 26 | -3 STAT_WRONG_PASSWORD -- failed due to incorrect password, 27 | -2 STAT_NO_AP_FOUND -- failed because no access point replied, 28 | -1 STAT_CONNECT_FAIL -- failed due to other problems, 29 | 3 STAT_GOT_IP -- connection successful. 30 | """ 31 | if wlan.status() < 0 or wlan.status() >= 3: 32 | # connection successful 33 | break 34 | max_wait -= 1 35 | print('waiting for connection... ' + str(max_wait)) 36 | utime.sleep(1) 37 | 38 | # check connection 39 | if wlan.status() != 3: 40 | # No connection 41 | raise RuntimeError('network connection failed') 42 | else: 43 | # connection successful 44 | print('wlan connected') 45 | status = wlan.ifconfig() 46 | pico_ip = status[0] 47 | print('ip = ' + status[0]) 48 | 49 | # Open socket 50 | # addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1] 51 | addr = (pico_ip, 80) 52 | s = socket.socket() 53 | s.bind(addr) 54 | s.listen(1) 55 | print('listening on', addr) 56 | 57 | led = Pin(28, Pin.OUT) 58 | led.off() 59 | led_state = False 60 | 61 | # main loop 62 | while True: 63 | client, client_addr = s.accept() 64 | raw_request = client.recv(1024) 65 | # translate byte string to normal string variable 66 | raw_request = raw_request.decode("utf-8") 67 | print(raw_request) 68 | 69 | # break request into words (split at spaces) 70 | request_parts = raw_request.split() 71 | http_method = request_parts[0] 72 | request_url = request_parts[1] 73 | 74 | if request_url.find("/ledon") != -1: 75 | # turn LED on 76 | led_state = True 77 | led.on() 78 | elif request_url.find("/ledoff") != -1: 79 | # turn LED off 80 | led_state = False 81 | led.off() 82 | else: 83 | # do nothing 84 | pass 85 | 86 | led_state_text = "OFF" 87 | if led_state: 88 | led_state_text = "ON" 89 | 90 | file = open("simpleled.html") 91 | html = file.read() 92 | file.close() 93 | 94 | html = html.replace('**ledState**', led_state_text) 95 | client.send(html) 96 | client.close() 97 | -------------------------------------------------------------------------------- /simpleled_ap.py: -------------------------------------------------------------------------------- 1 | # use web interface to control an LED 2 | 3 | import utime 4 | import network 5 | import socket 6 | import urequests 7 | from machine import Pin 8 | from NetworkCredentials import NetworkCredentials 9 | 10 | ssid = 'PiPicoAp' 11 | password = 'password' 12 | 13 | ap = network.WLAN(network.AP_IF) 14 | ap.config(essid=ssid, password=password) 15 | ap.active(True) 16 | 17 | # wait for wifi to go active 18 | wait_counter = 0 19 | while ap.active() == False: 20 | print("waiting " + str(wait_counter)) 21 | time.sleep(0.5) 22 | pass 23 | 24 | print('WiFi active') 25 | status = ap.ifconfig() 26 | pico_ip = status[0] 27 | print('ip = ' + status[0]) 28 | 29 | # Open socket 30 | # addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1] 31 | addr = (pico_ip, 80) 32 | s = socket.socket() 33 | s.bind(addr) 34 | s.listen(1) 35 | print('listening on', addr) 36 | 37 | led = Pin(28, Pin.OUT) 38 | led.off() 39 | led_state = False 40 | 41 | # main loop 42 | while True: 43 | client, client_addr = s.accept() 44 | raw_request = client.recv(1024) 45 | # translate byte string to normal string variable 46 | raw_request = raw_request.decode("utf-8") 47 | print(raw_request) 48 | 49 | # break request into words (split at spaces) 50 | request_parts = raw_request.split() 51 | http_method = request_parts[0] 52 | request_url = request_parts[1] 53 | 54 | if request_url.find("/ledon") != -1: 55 | # turn LED on 56 | led_state = True 57 | led.on() 58 | elif request_url.find("/ledoff") != -1: 59 | # turn LED off 60 | led_state = False 61 | led.off() 62 | else: 63 | # do nothing 64 | pass 65 | 66 | led_state_text = "OFF" 67 | if led_state: 68 | led_state_text = "ON" 69 | 70 | file = open("simpleled.html") 71 | html = file.read() 72 | file.close() 73 | 74 | html = html.replace('**ledState**', led_state_text) 75 | client.send(html) 76 | client.close() 77 | --------------------------------------------------------------------------------