Added code for Lab 9
This commit is contained in:
parent
4c5757b3cc
commit
5f40f4c866
24 changed files with 796 additions and 0 deletions
106
Lab09/MicroPython/Code/wifi03/WebServer03.py
Normal file
106
Lab09/MicroPython/Code/wifi03/WebServer03.py
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
# 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']
|
||||
|
||||
indexFilename = "index03.html"
|
||||
|
||||
# HTML template for the webpage
|
||||
def webpage(random_value, state):
|
||||
try:
|
||||
with open(indexFilename, 'r') as f:
|
||||
html = f.read()
|
||||
except FileNotFoundError:
|
||||
print("The file was not found.")
|
||||
except IOError:
|
||||
print("An error was encountered reading the file.")
|
||||
|
||||
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')
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue