prjbrook

WiFiAPLEDS1.py turns leds on/off via Pico AP

Aug 11th, 2024
96
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.34 KB | None | 0 0
  1. #Worked ok Tue Aug  6 11:04:01 NZST 2024. Can't get other mode to work  Will play with this one.
  2. import network
  3. import time
  4. import socket
  5. from machine import Pin
  6.  
  7. file = open("simpleled.html")
  8. html = file.read()
  9. file.close()
  10.  
  11. # led = Pin(25, Pin.OUT)
  12. # led.off()
  13. # led.on()
  14.  
  15. led = machine.Pin("LED", machine.Pin.OUT)
  16. #led.off()
  17. led.on()
  18.  
  19. def web_page():
  20.   html = """<html><head><meta name="viewport" content="width=device-width, initial-scale=1"></head>
  21.            <body><h1>Hello 33w21World</h1></body></html>
  22.         """
  23.   return html
  24. #todo make this HTML into separate file
  25.  
  26. # if you do not see the network you may have to power cycle
  27. # unplug your pico w for 10 seconds and plug it in again
  28. def ap_mode(ssid, password):
  29.     """
  30.        Description: This is a function to activate AP mode
  31.  
  32.        Parameters:
  33.  
  34.        ssid[str]: The name of your internet connection
  35.        password[str]: Password for your internet connection
  36.  
  37.        Returns: Nada
  38.    """
  39.     # Just making our internet connection
  40.     ap = network.WLAN(network.AP_IF)
  41.     ap.config(essid=ssid, password=password)
  42.     ap.active(True)
  43.  
  44.     while ap.active() == False:
  45.         pass
  46.     print('AP Mode Is Active, You can Now Connect')
  47.     print('IP Address To Connect to:: ' + ap.ifconfig()[0])
  48.  
  49.     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)   #creating socket object
  50.     s.bind(('', 80))
  51.     s.listen(5)
  52.  
  53.     while True:
  54.       conn, addr = s.accept()
  55.       print('Got a connection from %s' % str(addr))
  56.       request = conn.recv(1024)
  57.       print('Content received by Pico = %s' % str(request))
  58.       #response = web_page()
  59.       #new---
  60.       request = str(request)
  61.       #led_on = request.find('/light/on')
  62.       #led_off = request.find('/light/off')
  63.       led_on = request.find('/ledon')
  64.       led_off = request.find('/ledoff')
  65.       print( 'led on = ' + str(led_on))
  66.       print( 'led off = ' + str(led_off))
  67.       #---finish new---got -1 for off and 308 for on--
  68.       #new new--
  69. #todo sort out indenting
  70.       if led_on > 0:
  71.             print("led on")
  72.             led.value(1)
  73.             stateis = "LED is ON"
  74.       if led_off > 0:
  75.         print("led off")
  76.         led.value(0)
  77.         stateis = "LED is OFF"
  78.  
  79.       response=html #new
  80.       conn.send(response)
  81.       conn.close()
  82.  
  83. ap_mode('NAME',
  84.         'PASSWORD')
  85.  
  86.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment