Advertisement
Guest User

Untitled

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