Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try:
- import usocket as socket
- except:
- import socket
- import time
- import network
- import machine
- import esp
- esp.osdebug(None)
- import gc
- gc.collect()
- ssid = 'Bilu-AP'
- password = '123456789'
- ap = network.WLAN(network.AP_IF)
- ap.active(True)
- ap.config(essid=ssid, password=password)
- while ap.active() == False:
- pass
- print('Connection successful')
- print(ap.ifconfig())
- def web_page():
- if led.value() == 1:
- gpio_state="OFF"
- else:
- gpio_state="ON"
- html = """<html><head> <title>ESP Web Server</title> <meta name="viewport" content="width=device-width, initial-scale=1">
- <link rel="icon" href="data:,"> <style>html{font-family: Helvetica; display:inline-block; margin: 0px auto; text-align: center;}
- h1{color: #0F3376; padding: 2vh;}p{font-size: 1.5rem;}.button{display: inline-block; background-color: #e7bd3b; border: none;
- border-radius: 4px; color: white; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}
- .button2{background-color: #4286f4;}</style></head><body> <h1>Led </h1>
- <p>GPIO state: <strong>""" + gpio_state + """</strong></p><p><a href="/?led=on"><button class="button">ON</button></a></p>
- <p><a href="/?led=off"><button class="button button2">OFF</button></a></p></body></html>"""
- return html
- def sendudp(host, port,ilosc):
- address = ("192.168.4.2", 6969)
- data = 'Ilosc migniec: ' + str(ilosc)
- sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- print("data is type {}".format(type(data)))
- sock.sendto(data, address)
- led = machine.Pin(2, machine.Pin.OUT)
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- s.bind(('', 80))
- s.listen(5)
- a=0
- b=1
- while True:
- conn, addr = s.accept()
- print('Got a connection from %s' % str(addr))
- request = conn.recv(1024)
- request = str(request)
- print('Content = %s' % request)
- led_on = request.find('/?led=on')
- led_off = request.find('/?led=off')
- if led_on == 6:
- print('LED ON')
- while a<b:
- led.value(0)
- time.sleep(1)
- led.value(1)
- time.sleep(1)
- a+=1
- b+=1
- a=0
- if led_off == 6:
- print('LED OFF')
- led.value(1)
- sendudp("192.168.4.3",6969,b)
- response = web_page()
- conn.send('HTTP/1.1 200 OK\n')
- conn.send('Content-Type: text/html\n')
- conn.send('Connection: close\n\n')
- conn.sendall(response)
- conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement