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

CoreElectronics 1: Connecting to the Internet

# A simple example that: # - Connects to a WiFi Network defined by "ssid" and "password" # - Performs a GET request (loads a webpage) # - Queries the current time from a server import network # handles connecting to WiFi import urequests # handles making and servicing network requests import utime # Connect to network wlan = network.WLAN(network.STA_IF) wlan.active(True) # Fill in your network name (ssid) and password here: ssid = 'xxxxx' password = 'xxxxx' wlan.connect(ssid, password) # Example 1. Make a GET request for google.com and print HTML # Print the html content from google.com print("1. Querying google.com:") r = urequests.get("http://www.google.com") print(r.content) r.close() # Example 2. urequests can also handle basic json support! Let's get the current time from a server print("\n\n2. Querying the current GMT+0 time:") r = urequests.get("http://date.jsontest.com") # Server that returns the current GMT+0 time. #print the tuple print(r.json()) # Get the 'date and time' value from the JSON response date_str = r.json()['date'] time_str = r.json()['time'][:-3] # Remove the last 3 characters (' PM' or ' AM') # Print only the time part print("Today's date GMT+0 is ", date_str.split(' ')) print("Current time GMT+0 is ",time_str.split(' ')) #Adding [0] will remove the [AM/PM] string #https://core-electronics.com.au/guides/raspberry-pi-pico-w-connect-to-the-internet/ #https://www.youtube.com/watch?v=GiT3MzRzG48&t=306s