import network
import urequests
# set your WiFi Country
rp2.country('CN')
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
# set power mode to get WiFi power-saving off (if needed)
wlan.config(pm = 0xa11140)
wlan.connect('YourHomeSSID', 'YourSSIDPassword')
# Make GET request
r = urequests.get("http://mixleg.com")
print(r.content)
r.close()
远程网页控制
#webled.py
import netman
import socket
from machine import Pin
led = Pin("LED", Pin.OUT)
country = 'CN'
ssid = '2312'
password = 'only'
wifi_connection = netman.connectWiFi(ssid,password,country)
html = """<!DOCTYPE html>
<html>
<head> <title>Pico W</title> </head>
<body> <h1>Pico W</h1>
<p>Current status: %s</p>
<p><a href="http://"""+wifi_connection[0]+"""/light/on">Turn ON</a></p>
<p><a href="http://"""+wifi_connection[0]+"""/light/off">Turn OFF</a></p>
<p>by <a href="http://mixdiy.com">mixdiy.com</a></p>
</body>
</html>
"""
# Open socket
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
print('listening on', addr)
# Initialize LED status
led.value(0)
stateis = "LED is OFF"
# Listen for connections
while True:
try:
cl, addr = s.accept()
print('client connected from', addr)
request = cl.recv(1024)
print(request)
request = str(request)[0:50] # The [0:50] avoids getting the url directory from referer
led_status = request.find('GET / HTTP')
led_on = request.find('/light/on')
led_off = request.find('/light/off')
print( 'led on = ' + str(led_on))
print( 'led off = ' + str(led_off))
if led_status >0:
print("LED status request") # No LED action
if led_on >0:
print("led on")
led.value(1)
stateis = "LED is ON"
if led_off >0:
print("led off")
led.value(0)
stateis = "LED is OFF"
response = html % stateis
cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
cl.send(response)
cl.close()
except OSError as e:
cl.close()
print('connection closed')
#netman.py
import network, rp2
import time
def connectWiFi(ssid,password,country):
rp2.country(country)
wlan = network.WLAN(network.STA_IF)
wlan.config(pm = 0xa11140)
wlan.active(True)
wlan.connect(ssid, password)
# Wait for connect or fail
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('waiting for connection...')
time.sleep(1)
# Handle connection error
if wlan.status() != 3:
raise RuntimeError('network connection failed')
else:
print('connected')
status = wlan.ifconfig()
print( 'ip = ' + status[0] )
return status
AP
import socket
import network
import machine
ssid = 'MicroPython-AP'
password = '123456789'
led = machine.Pin("LED",machine.Pin.OUT)
ap = network.WLAN(network.AP_IF)
ap.config(essid=ssid, password=password)
ap.active(True)
while ap.active() == False:
pass
print('Connection successful')
print(ap.ifconfig())
html = """<!DOCTYPE html>
<html>
<head> <title>Pico W</title> </head>
<body> <h1>Pico W</h1>
<p>Hello from Pico W.</p>
</body>
</html>
"""
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
print('listening on', addr)
led.off()
# Listen for connections
while True:
try:
cl, addr = s.accept()
print('client connected from', addr)
request = cl.recv(1024)
led.on()
print(request)
cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
cl.send(html)
cl.close()
led.off()
except OSError as e:
cl.close()
print('connection closed')