ESP32 / Webserver lampje 2.1.py
Webserver lampje 2.1.py
Raw
import socket

def web_page():
    return """<html>
    <head> 
        <title>ESP32 Led webserver</title> 
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="preconnect" href="https://fonts.googleapis.com">
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Takri&display=swap" rel="stylesheet">
        <style>html{
        			font-family: Noto Sans Takri; 
        			display:inline-block; 
                    margin: 0px auto; 
                    text-align: center;}
                 h1{
                 	color: #0F3376; 
             	    padding: 1.5vh;
                    font-size: 3.5rem;}
                  p{
                  	font-size: 1.5rem;
               	 	font-family: Arial;}
            .button{
               		display: inline-block; 
               		background-color: MediumSeaGreen; 
               		border: none; 
               		border-radius: 40px; 
               		color: white; 
               		padding: 16px 40px; 
               		text-decoration: none; 
               		font-size: 30px; 
               		margin: 2px; 
               		cursor: pointer;}
           .button2{
           			background-color: Tomato;}
        </style>
    </head>
    <body style="background-color:LightGray;">
        <h1> De enige echte ESP32 web server</h1> 
        <p>
        <a href="/?led=aan"><button class="button">AAN</button>
        </a>
        </p>
        <p>
        <a href="/?led=uit"><button class="button button2">UIT</button> 
        </a>
        </p>
    </body>
</html>"""
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("", 8080))
server_socket.listen(5)

while True:
    connection, address = server_socket.accept()
    print("Got a connection from %s" % str(address))
    request = str(connection.recv(1024))
    print("connect = %s" % request)
    
    if "/?led=aan" in request:
        print("LED AAN")
        led.value(1)
    if "/?led=uit" in request:
        print("LED UIT")
        led.value(0)
    
    
    response = web_page()
    connection.send("HTTP/1.1 200 OK\n")
    connection.send("Content-Type: text/html\n")
    connection.send("Connection: close\n\n")
    connection.sendall(response)
    connection.close()