Wednesday, 26 April 2023

CoreElectronics 2: Create a Simple HTTP Server

Part 1 # Hardware Test # Blink and LED (GP15) slowly/quickly while a button (GP16) is not-pressed/pressed from machine import Pin from time import sleep_ms led = Pin(15, Pin.OUT) button = Pin(16, Pin.IN, Pin.PULL_UP) while True: if button.value() == 0: # button pressed delay = 100 # short delay else: delay = 1000 # long delay led.toggle() sleep_ms(delay) #https://core-electronics.com.au/guides/raspberry-pi-pico-w-create-a-simple-http-server/ #https://www.youtube.com/watch?v=AK8UYh7pMGM&t=515s Part 2 # Simple HTTP Server Example # Control an LED and read a Button using a web browser import time import network import socket from machine import Pin led = Pin(15, Pin.OUT) ledState = 'LED State Unknown' button = Pin(16, Pin.IN, Pin.PULL_UP) ssid = 'xxxxx' password = 'xxxxx' wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(ssid, password) html = """ Pico W

Pico W HTTP Server

Hello, World!

%s

""" # 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] ) # 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) # Listen for connections, serve client while True: try: cl, addr = s.accept() print('client connected from', addr) request = cl.recv(1024) print("request:") print(request) request = str(request) led_on = request.find('led=on') led_off = request.find('led=off') print( 'led on = ' + str(led_on)) print( 'led off = ' + str(led_off)) if led_on == 8: print("led on") led.value(1) if led_off == 8: print("led off") led.value(0) ledState = "LED is OFF" if led.value() == 0 else "LED is ON" # a compact if-else statement if button.value() == 1: # button not pressed print("button NOT pressed") buttonState = "Button is NOT pressed" else: print("button pressed") buttonState = "Button is pressed" # Create and send response stateis = ledState + " and " + buttonState 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') #https://core-electronics.com.au/guides/raspberry-pi-pico-w-create-a-simple-http-server/ #https://www.youtube.com/watch?v=AK8UYh7pMGM&t=515s Part 3 # Good graphical interface on HTTP surver # Control an LED and read a Button using a web browser import time import network import socket from machine import Pin led = Pin(15, Pin.OUT) ledState = 'LED State Unknown' button = Pin(16, Pin.IN, Pin.PULL_UP) ssid = 'xxxxx' password = 'xxxxx' wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(ssid, password) # replace the "html" variable with the following to create a more user-friendly control panel html = """

Control Panel









%s

""" # 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] ) # 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) # Listen for connections, serve client while True: try: cl, addr = s.accept() print('client connected from', addr) request = cl.recv(1024) print("request:") print(request) request = str(request) led_on = request.find('led=on') led_off = request.find('led=off') print( 'led on = ' + str(led_on)) print( 'led off = ' + str(led_off)) if led_on == 8: print("led on") led.value(1) if led_off == 8: print("led off") led.value(0) ledState = "LED is OFF" if led.value() == 0 else "LED is ON" # a compact if-else statement if button.value() == 1: # button not pressed print("button NOT pressed") buttonState = "Button is NOT pressed" else: print("button pressed") buttonState = "Button is pressed" # Create and send response stateis = ledState + " and " + buttonState 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') #https://core-electronics.com.au/guides/raspberry-pi-pico-w-create-a-simple-http-server/ #https://www.youtube.com/watch?v=AK8UYh7pMGM&t=515s

No comments: