Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. import wifimgr
  2. from time import sleep
  3. from machine import Pin
  4.  
  5. try:
  6. import usocket as socket
  7. except:
  8. import socket
  9.  
  10. import esp
  11. esp.osdebug(None)
  12. import gc
  13. gc.collect()
  14.  
  15. led = Pin(2, Pin.OUT)
  16.  
  17.  
  18. wlan = wifimgr.get_connection()
  19. if wlan is None:
  20. print("Could not initialize the network connection.")
  21. while True:
  22. pass # you shall not pass :D
  23.  
  24.  
  25. # Main Code goes here, wlan is a working network.WLAN(STA_IF) instance.
  26. print("ESP OK")
  27.  
  28. def web_page():
  29. if led.value() == 1:
  30. gpio_state="ON"
  31. else:
  32. gpio_state="OFF"
  33.  
  34. html = """<html><head> <title>ESP Web Server</title> <meta name="viewport" content="width=device-width, initial-scale=1">
  35. <link rel="icon" href="data:,"> <style>html{font-family: Helvetica; display:inline-block; margin: 0px auto; text-align: center;}
  36. h1{color: #0F3376; padding: 2vh;}p{font-size: 1.5rem;}.button{display: inline-block; background-color: #e7bd3b; border: none;
  37. border-radius: 4px; color: white; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}
  38. .button2{background-color: #4286f4;}</style></head><body> <h1>ESP Web Server</h1>
  39. <p>GPIO state: <strong>""" + gpio_state + """</strong></p><p><a href="/?led=on"><button class="button">ON</button></a></p>
  40. <p><a href="/?led=off"><button class="button button2">OFF</button></a></p></body></html>"""
  41. return html
  42.  
  43. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  44. s.bind(('', 80))
  45. s.listen(5)
  46.  
  47. while True:
  48. conn, addr = s.accept()
  49. print('Got a connection from %s' % str(addr))
  50. request = conn.recv(1024)
  51. request = str(request)
  52. print('Content = %s' % request)
  53. led_on = request.find('/?led=on')
  54. led_off = request.find('/?led=off')
  55. if led_on == 6:
  56. print('LED ON')
  57. led.value(1)
  58. if led_off == 6:
  59. print('LED OFF')
  60. led.value(0)
  61. response = web_page()
  62. conn.send('HTTP/1.1 200 OK\n')
  63. conn.send('Content-Type: text/html\n')
  64. conn.send('Connection: close\n\n')
  65. conn.sendall(response)
  66. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement