Added Pico W lab code
This commit is contained in:
parent
4dbe7119d2
commit
a23bf2ca82
29 changed files with 974 additions and 7 deletions
13
picow-lab-code/Lab10Code/blink.py
Normal file
13
picow-lab-code/Lab10Code/blink.py
Normal file
|
@ -0,0 +1,13 @@
|
|||
import machine
|
||||
|
||||
import time
|
||||
|
||||
led = machine.Pin("LED", machine.Pin.OUT) #configure GPIO-15 Pin as an output pin and create and led object for Pin class
|
||||
|
||||
while True:
|
||||
led.value(True) #turn on the LED
|
||||
time.sleep(1) #wait for one second
|
||||
led.value(False) #turn off the LED
|
||||
time.sleep(1) #wait for one second
|
||||
|
||||
|
12
picow-lab-code/Lab10Code/blink15.py
Normal file
12
picow-lab-code/Lab10Code/blink15.py
Normal file
|
@ -0,0 +1,12 @@
|
|||
import machine
|
||||
|
||||
import time
|
||||
|
||||
led = machine.Pin(15, machine.Pin.OUT) #configure GPIO-15 Pin as an output pin and create and led object for Pin class
|
||||
|
||||
while True:
|
||||
led.value(True) #turn on the LED
|
||||
time.sleep(1) #wait for one second
|
||||
led.value(False) #turn off the LED
|
||||
time.sleep(1) #wait for one second
|
||||
|
26
picow-lab-code/Lab10Code/pwm_fade.py
Normal file
26
picow-lab-code/Lab10Code/pwm_fade.py
Normal file
|
@ -0,0 +1,26 @@
|
|||
# Example using PWM to fade an LED.
|
||||
|
||||
import time
|
||||
import machine
|
||||
from machine import Pin, PWM
|
||||
|
||||
|
||||
# Construct PWM object, with LED on Pin(25).
|
||||
pwm = PWM(Pin(15))
|
||||
|
||||
# Set the PWM frequency.
|
||||
pwm.freq(1000)
|
||||
|
||||
# Fade the LED in and out a few times.
|
||||
duty = 0
|
||||
direction = 1
|
||||
for _ in range(8 * 256):
|
||||
duty += direction
|
||||
if duty > 255:
|
||||
duty = 255
|
||||
direction = -1
|
||||
elif duty < 0:
|
||||
duty = 0
|
||||
direction = 1
|
||||
pwm.duty_u16(duty * duty)
|
||||
time.sleep(0.001)
|
26
picow-lab-code/Lab10Code/pwm_fade2.py
Normal file
26
picow-lab-code/Lab10Code/pwm_fade2.py
Normal file
|
@ -0,0 +1,26 @@
|
|||
# Example using PWM to fade an LED.
|
||||
|
||||
import time
|
||||
import machine
|
||||
from machine import Pin, PWM
|
||||
|
||||
|
||||
# Construct PWM object, with LED on Pin(25).
|
||||
pwm = PWM(Pin(15))
|
||||
|
||||
# Set the PWM frequency.
|
||||
pwm.freq(1000)
|
||||
|
||||
# Fade the LED in and out a few times.
|
||||
duty = 0
|
||||
direction = 1
|
||||
for _ in range(8 * 256):
|
||||
duty += direction
|
||||
if duty > 255:
|
||||
duty = 255
|
||||
direction = -1
|
||||
elif duty < 0:
|
||||
duty = 0
|
||||
direction = 1
|
||||
pwm.duty_u16(duty * duty)
|
||||
time.sleep(0.01)
|
31
picow-lab-code/Lab10Code/pwm_fade3.py
Normal file
31
picow-lab-code/Lab10Code/pwm_fade3.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
# Example using PWM to fade an LED.
|
||||
|
||||
import time
|
||||
import machine
|
||||
from machine import Pin, PWM
|
||||
|
||||
def pwmFade():
|
||||
# Fade the LED in and out a few times.
|
||||
duty = 0
|
||||
direction = 1
|
||||
for _ in range(8 * 256):
|
||||
duty += direction
|
||||
if duty > 255:
|
||||
duty = 255
|
||||
direction = -1
|
||||
elif duty < 0:
|
||||
duty = 0
|
||||
direction = 1
|
||||
pwm.duty_u16(duty * duty)
|
||||
time.sleep(0.001)
|
||||
return 0
|
||||
|
||||
|
||||
# Construct PWM object, with LED on Pin(25).
|
||||
pwm = PWM(Pin(15))
|
||||
|
||||
# Set the PWM frequency.
|
||||
pwm.freq(1000)
|
||||
|
||||
# Fade the LED in and out a few times.
|
||||
pwmFade()
|
9
picow-lab-code/Lab10Code/secrets-stcc.py
Normal file
9
picow-lab-code/Lab10Code/secrets-stcc.py
Normal file
|
@ -0,0 +1,9 @@
|
|||
secrets = {
|
||||
'location': 'STCC',
|
||||
'ssid': 'cset@stcc',
|
||||
'password': 'c1s2e3t4',
|
||||
}
|
||||
|
||||
SSID = "cset@stcc"
|
||||
PASSWORD = "c1s2e3t4"
|
||||
|
8
picow-lab-code/Lab10Code/secrets.py
Normal file
8
picow-lab-code/Lab10Code/secrets.py
Normal file
|
@ -0,0 +1,8 @@
|
|||
secrets = {
|
||||
'location': 'STCC',
|
||||
'ssid': 'cset@stcc',
|
||||
'password': 'c1s2e3t4',
|
||||
}
|
||||
|
||||
SSID = "cset@stcc"
|
||||
PASSWORD = "c1s2e3t4"
|
122
picow-lab-code/Lab10Code/webServer1.py
Normal file
122
picow-lab-code/Lab10Code/webServer1.py
Normal file
|
@ -0,0 +1,122 @@
|
|||
# Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-web-server-micropython/
|
||||
|
||||
# Import necessary modules
|
||||
import network
|
||||
import socket
|
||||
import time
|
||||
import random
|
||||
from machine import Pin
|
||||
from secrets import secrets
|
||||
|
||||
# Create an LED object on pin 'LED'
|
||||
led = Pin('LED', Pin.OUT)
|
||||
|
||||
# Wi-Fi credentials
|
||||
ssid = secrets['ssid']
|
||||
password = secrets['password']
|
||||
|
||||
# HTML template for the webpage
|
||||
def webpage(random_value, state):
|
||||
html = f"""
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Pico Web Server</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
</head>
|
||||
<body>
|
||||
<h1>Raspberry Pi Pico Web Server</h1>
|
||||
<h2>Led Control</h2>
|
||||
<form action="./lighton">
|
||||
<input type="submit" value="Light on" />
|
||||
</form>
|
||||
<br>
|
||||
<form action="./lightoff">
|
||||
<input type="submit" value="Light off" />
|
||||
</form>
|
||||
<p>LED state: {state}</p>
|
||||
<h2>Fetch New Value</h2>
|
||||
<form action="./value">
|
||||
<input type="submit" value="Fetch value" />
|
||||
</form>
|
||||
<p>Fetched value: {random_value}</p>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
return str(html)
|
||||
|
||||
# Connect to WLAN
|
||||
wlan = network.WLAN(network.STA_IF)
|
||||
wlan.active(True)
|
||||
wlan.connect(ssid, password)
|
||||
|
||||
# Wait for Wi-Fi connection
|
||||
connection_timeout = 10
|
||||
while connection_timeout > 0:
|
||||
if wlan.status() >= 3:
|
||||
break
|
||||
connection_timeout -= 1
|
||||
print('Waiting for Wi-Fi connection...')
|
||||
time.sleep(1)
|
||||
|
||||
# Check if connection is successful
|
||||
if wlan.status() != 3:
|
||||
raise RuntimeError('Failed to establish a network connection')
|
||||
else:
|
||||
print('Connection successful!')
|
||||
network_info = wlan.ifconfig()
|
||||
print('IP address:', network_info[0])
|
||||
|
||||
# Set up socket and start listening
|
||||
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
|
||||
s = socket.socket()
|
||||
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
s.bind(addr)
|
||||
s.listen()
|
||||
|
||||
print('Listening on', addr)
|
||||
|
||||
# Initialize variables
|
||||
state = "OFF"
|
||||
random_value = 0
|
||||
|
||||
# Main loop to listen for connections
|
||||
while True:
|
||||
try:
|
||||
conn, addr = s.accept()
|
||||
print('Got a connection from', addr)
|
||||
|
||||
# Receive and parse the request
|
||||
request = conn.recv(1024)
|
||||
request = str(request)
|
||||
print('Request content = %s' % request)
|
||||
|
||||
try:
|
||||
request = request.split()[1]
|
||||
print('Request:', request)
|
||||
except IndexError:
|
||||
pass
|
||||
|
||||
# Process the request and update variables
|
||||
if request == '/lighton?':
|
||||
print("LED on")
|
||||
led.value(1)
|
||||
state = "ON"
|
||||
elif request == '/lightoff?':
|
||||
led.value(0)
|
||||
state = 'OFF'
|
||||
elif request == '/value?':
|
||||
random_value = random.randint(0, 20)
|
||||
|
||||
# Generate HTML response
|
||||
response = webpage(random_value, state)
|
||||
|
||||
# Send the HTTP response and close the connection
|
||||
conn.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
|
||||
conn.send(response)
|
||||
conn.close()
|
||||
|
||||
except OSError as e:
|
||||
conn.close()
|
||||
print('Connection closed')
|
||||
|
23
picow-lab-code/Lab10Code/wifiConnect1.py
Normal file
23
picow-lab-code/Lab10Code/wifiConnect1.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
import network
|
||||
import secrets
|
||||
from utime import sleep, ticks_ms, ticks_diff
|
||||
from secrets import secrets
|
||||
|
||||
print('Connecting to WiFi Network Name:', secrets['ssid'])
|
||||
wlan = network.WLAN(network.STA_IF)
|
||||
wlan.active(True)
|
||||
|
||||
start = ticks_ms() # start a millisecond counter
|
||||
|
||||
if not wlan.isconnected():
|
||||
wlan.connect(secrets['ssid'], secrets['password'])
|
||||
print("Waiting for connection...")
|
||||
counter = 0
|
||||
while not wlan.isconnected():
|
||||
sleep(1)
|
||||
print(counter, '.', sep='', end='', )
|
||||
counter += 1
|
||||
|
||||
delta = ticks_diff(ticks_ms(), start)
|
||||
print("Connect Time:", delta, 'milliseconds')
|
||||
print('IP Address:', wlan.ifconfig()[0])
|
Loading…
Add table
Add a link
Reference in a new issue