Exemplul 1
# ************************
#
import machine
import time
led = [Link](2,[Link])
[Link]()
# ************************
# Configure the ESP32 wifi
# as Access Point mode.
import network
ssid = 'ESP32-AP-WebServer'
password = '123456789'
ap = [Link](network.AP_IF)
[Link](True)
[Link](essid=ssid, password=password)
while not [Link]():
pass
print('network config:', [Link]())
# ************************
# Configure the socket connection
# over TCP/IP
import socket
# AF_INET - use Internet Protocol v4 addresses
# SOCK_STREAM means that it is a TCP socket.
# SOCK_DGRAM means that it is a UDP socket.
s = [Link](socket.AF_INET, socket.SOCK_STREAM)
[Link](('',80)) # specifies that the socket is reachable by any address the
machine happens to have
[Link](5) # max of 5 socket connections
# ************************
# Function for creating the
# web page to be displayed
def web_page():
if [Link]()==1:
led_state = 'ON'
print('led is ON')
elif [Link]()==0:
led_state = 'OFF'
print('led is OFF')
html_page = """<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<center><h2>ESP32 Web Server in MicroPython </h2></center>
<center>
<form>
<button type='submit' name="LED" value='1'> LED ON </button>
<button type='submit' name="LED" value='0'> LED OFF </button>
</form>
</center>
<center><p>LED is now <strong>""" + led_state +
"""</strong>.</p></center>
</body>
</html>"""
return html_page
while True:
# Socket accept()
conn, addr = [Link]()
print("Got connection from %s" % str(addr))
# Socket receive()
request=[Link](1024)
print("")
print("")
print("Content %s" % str(request))
# Socket send()
request = str(request)
led_on = [Link]('/?LED=1')
led_off = [Link]('/?LED=0')
if led_on == 6:
print('LED ON')
print(str(led_on))
[Link](1)
elif led_off == 6:
print('LED OFF')
print(str(led_off))
[Link](0)
response = web_page()
[Link]('HTTP/1.1 200 OK\n')
[Link]('Content-Type: text/html\n')
[Link]('Connection: close\n\n')
[Link](response)
# Socket close()
[Link]()
Example # 2, Display DHT sensor readings
through web server:
# ************************#
import machine
import time
led = [Link](2,[Link])
[Link]()
# ************************
# Configure the ESP32 wifi
# as Access Point mode.
import network
ssid = 'ESP32-AP-WebServer'
password = '123456789'
ap = [Link](network.AP_IF)
[Link](True)
[Link](essid=ssid, password=password)
while not [Link]():
pass
print('network config:', [Link]())
# ************************
# Configure the socket connection
# over TCP/IP
import socket
# AF_INET - use Internet Protocol v4 addresses
# SOCK_STREAM means that it is a TCP socket.
# SOCK_DGRAM means that it is a UDP socket.
s = [Link](socket.AF_INET, socket.SOCK_STREAM)
[Link](('',80)) # specifies that the socket is reachable by any address the
machine happens to have
[Link](5) # max of 5 socket connections
# DHT sensor initializations
import dht
d = dht.DHT22([Link](23))
# If you will use DHT11, change it to:
# d = dht.DHT11([Link](23))
# ************************
# Function for creating the
# web page to be displayed
def web_page():
# Get the DHT readings
[Link]()
t = [Link]()
h = [Link]()
html_page = """<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="refresh" content="1">
</head>
<body>
<center><h2>ESP32 Web Server in MicroPython </h2></center>
<center><p>Temperature is <strong>""" + str(t) + """
C.</strong>.</p></center>
<center><p>Humidity is <strong>""" + str(h) + """
%.</strong>.</p></center>
</body>
</html>"""
return html_page
while True:
# Socket accept()
conn, addr = [Link]()
print("Got connection from %s" % str(addr))
# Socket receive()
request=[Link](1024)
print("")
print("Content %s" % str(request))
# Socket send()
request = str(request)
# Create a socket reply
response = web_page()
[Link]('HTTP/1.1 200 OK\n')
[Link]('Content-Type: text/html\n')
[Link]('Connection: close\n\n')
[Link](response)
# Socket close()
[Link]()