Web Server
Web Server
# ************************
#
import machine
import time
led = machine.Pin(2,machine.Pin.OUT)
led.off()
# ************************
# Configure the ESP32 wifi
# as Access Point mode.
import network
ssid = 'ESP32-AP-WebServer'
password = '123456789'
ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid=ssid, password=password)
while not ap.active():
pass
print('network config:', ap.ifconfig())
# ************************
# Configure the socket connection
# over TCP/IP
import socket
# ************************
# Function for creating the
# web page to be displayed
def web_page():
if led.value()==1:
led_state = 'ON'
print('led is ON')
elif led.value()==0:
led_state = 'OFF'
print('led is OFF')
while True:
# Socket accept()
conn, addr = s.accept()
print("Got connection from %s" % str(addr))
# Socket receive()
request=conn.recv(1024)
print("")
print("")
print("Content %s" % str(request))
# Socket send()
request = str(request)
led_on = request.find('/?LED=1')
led_off = request.find('/?LED=0')
if led_on == 6:
print('LED ON')
print(str(led_on))
led.value(1)
elif led_off == 6:
print('LED OFF')
print(str(led_off))
led.value(0)
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)
# Socket close()
conn.close()
# ************************#
import machine
import time
led = machine.Pin(2,machine.Pin.OUT)
led.off()
# ************************
# Configure the ESP32 wifi
# as Access Point mode.
import network
ssid = 'ESP32-AP-WebServer'
password = '123456789'
ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid=ssid, password=password)
while not ap.active():
pass
print('network config:', ap.ifconfig())
# ************************
# Configure the socket connection
# over TCP/IP
import socket
# ************************
# Function for creating the
# web page to be displayed
def web_page():
# Get the DHT readings
d.measure()
t = d.temperature()
h = d.humidity()
while True:
# Socket accept()
conn, addr = s.accept()
print("Got connection from %s" % str(addr))
# Socket receive()
request=conn.recv(1024)
print("")
print("Content %s" % str(request))
# Socket send()
request = str(request)
# Socket close()
conn.close()