0% found this document useful (0 votes)
24 views

Web Server

This document describes how to create a web server on an ESP32 microcontroller using MicroPython. It configures the ESP32 as an access point and sets up a socket connection to listen for requests on port 80. It then defines a function to generate an HTML page that displays the state of an LED or readings from a DHT sensor. When a request is received, it checks for commands to turn the LED on or off and returns the HTML page with the latest sensor data over the socket connection before closing it.

Uploaded by

Giani Buzatu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Web Server

This document describes how to create a web server on an ESP32 microcontroller using MicroPython. It configures the ESP32 as an access point and sets up a socket connection to listen for requests on port 80. It then defines a function to generate an HTML page that displays the state of an LED or readings from a DHT sensor. When a request is received, it checks for commands to turn the LED on or off and returns the HTML page with the latest sensor data over the socket connection before closing it.

Uploaded by

Giani Buzatu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Exemplul 1

# ************************
#
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

# 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 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('',80)) # specifies that the socket is reachable by any address the
machine happens to have
s.listen(5) # max of 5 socket connections

# ************************
# 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')

    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 = 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()

Example # 2, Display DHT sensor readings


through 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

# 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 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('',80)) # specifies that the socket is reachable by any address the
machine happens to have
s.listen(5) # max of 5 socket connections

# DHT sensor initializations


import dht
d = dht.DHT22(machine.Pin(23))
# If you will use DHT11, change it to:
# d = dht.DHT11(machine.Pin(23))

# ************************
# Function for creating the
# web page to be displayed
def web_page():
# Get the DHT readings
d.measure()
t = d.temperature()
h = d.humidity()

    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 = 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)

# Create a socket reply


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()

You might also like