Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Worked ok Tue Aug 6 11:04:01 NZST 2024. Can't get other mode to work Will play with this one.
- import network
- import time
- import socket
- from machine import Pin
- file = open("simpleled.html")
- html = file.read()
- file.close()
- # led = Pin(25, Pin.OUT)
- # led.off()
- # led.on()
- led = machine.Pin("LED", machine.Pin.OUT)
- #led.off()
- led.on()
- def web_page():
- html = """<html><head><meta name="viewport" content="width=device-width, initial-scale=1"></head>
- <body><h1>Hello 33w21World</h1></body></html>
- """
- return html
- #todo make this HTML into separate file
- # if you do not see the network you may have to power cycle
- # unplug your pico w for 10 seconds and plug it in again
- def ap_mode(ssid, password):
- """
- Description: This is a function to activate AP mode
- Parameters:
- ssid[str]: The name of your internet connection
- password[str]: Password for your internet connection
- Returns: Nada
- """
- # Just making our internet connection
- ap = network.WLAN(network.AP_IF)
- ap.config(essid=ssid, password=password)
- ap.active(True)
- while ap.active() == False:
- pass
- print('AP Mode Is Active, You can Now Connect')
- print('IP Address To Connect to:: ' + ap.ifconfig()[0])
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #creating socket object
- s.bind(('', 80))
- s.listen(5)
- while True:
- conn, addr = s.accept()
- print('Got a connection from %s' % str(addr))
- request = conn.recv(1024)
- print('Content received by Pico = %s' % str(request))
- #response = web_page()
- #new---
- request = str(request)
- #led_on = request.find('/light/on')
- #led_off = request.find('/light/off')
- led_on = request.find('/ledon')
- led_off = request.find('/ledoff')
- print( 'led on = ' + str(led_on))
- print( 'led off = ' + str(led_off))
- #---finish new---got -1 for off and 308 for on--
- #new new--
- #todo sort out indenting
- 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 #new
- conn.send(response)
- conn.close()
- ap_mode('NAME',
- 'PASSWORD')
Advertisement
Comments
-
- Still some work on find statements required. Rest is pretty raw too.
Add Comment
Please, Sign In to add comment