Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. try:
  2. import usocket as socket
  3. except:
  4. import socket
  5.  
  6. import time
  7. import network
  8. import machine
  9. import esp
  10. esp.osdebug(None)
  11.  
  12. import gc
  13. gc.collect()
  14.  
  15. ssid = 'Bilu-AP'
  16. password = '123456789'
  17.  
  18. ap = network.WLAN(network.AP_IF)
  19. ap.active(True)
  20. ap.config(essid=ssid, password=password)
  21.  
  22.  
  23. while ap.active() == False:
  24. pass
  25.  
  26. print('Connection successful')
  27. print(ap.ifconfig())
  28.  
  29. def web_page():
  30. if led.value() == 1:
  31. gpio_state="OFF"
  32. else:
  33. gpio_state="ON"
  34.  
  35. html = """<html><head> <title>ESP Web Server</title> <meta name="viewport" content="width=device-width, initial-scale=1">
  36. <link rel="icon" href="data:,"> <style>html{font-family: Helvetica; display:inline-block; margin: 0px auto; text-align: center;}
  37. h1{color: #0F3376; padding: 2vh;}p{font-size: 1.5rem;}.button{display: inline-block; background-color: #e7bd3b; border: none;
  38. border-radius: 4px; color: white; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}
  39. .button2{background-color: #4286f4;}</style></head><body> <h1>Led </h1>
  40. <p>GPIO state: <strong>""" + gpio_state + """</strong></p><p><a href="/?led=on"><button class="button">ON</button></a></p>
  41. <p><a href="/?led=off"><button class="button button2">OFF</button></a></p></body></html>"""
  42. return html
  43.  
  44. def sendudp(host, port,ilosc):
  45. address = ("192.168.4.2", 6969)
  46. data = 'Ilosc migniec: ' + str(ilosc)
  47. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  48. print("data is type {}".format(type(data)))
  49. sock.sendto(data, address)
  50.  
  51. led = machine.Pin(2, machine.Pin.OUT)
  52. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  53. s.bind(('', 80))
  54. s.listen(5)
  55.  
  56. a=0
  57. b=1
  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. while a<b:
  69. led.value(0)
  70. time.sleep(1)
  71. led.value(1)
  72. time.sleep(1)
  73. a+=1
  74. b+=1
  75. a=0
  76. if led_off == 6:
  77. print('LED OFF')
  78. led.value(1)
  79. sendudp("192.168.4.3",6969,b)
  80. response = web_page()
  81. conn.send('HTTP/1.1 200 OK\n')
  82. conn.send('Content-Type: text/html\n')
  83. conn.send('Connection: close\n\n')
  84. conn.sendall(response)
  85. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement