Servidor web en micropython y esp32 [thonny-html-display 7 segmentos]

preview_player
Показать описание
Codigo

Рекомендации по теме
Комментарии
Автор

#Guardamos nuestro documento .py en la memoria del esp32 (micropython divice)



wifi_credentials [Lo guardamos con este nombre y ahi va la informacion de la red a la cual nos queremos conectar]

codigo:

ssid = 'Redmi Note 10S'
password = 'clave'



Pagina_web_display(le colocamos un nombre)

codigo:

import machine
led1 = machine.Pin(5, machine.Pin.OUT)
led2 = machine.Pin(18, machine.Pin.OUT)
led3 = machine.Pin(19, machine.Pin.OUT)
led4 = machine.Pin(21, machine.Pin.OUT)
led1.off()
led2.off()
led3.off()
led4.off()

#
# Configure the ESP32 wifi
# as STAtion mode.
import network
import wifi_credentials

sta = network.WLAN(network.STA_IF)
if not sta.isconnected():
print('connecting to network...')
sta.active(True)
#sta.connect('your wifi ssid', 'your wifi password')
sta.connect(wifi_credentials.ssid, wifi_credentials.password)
while not sta.isconnected():
pass
print('network config:', sta.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 led1.value()==1:
led1_state = 'ON'
print('led1 is ON')
elif led1.value()==0:
led1_state = 'OFF'
print('led1 is OFF')

if led2.value()==1:
led2_state = 'ON'
print('led2 is ON')
elif led2.value()==0:
led2_state = 'OFF'
print('led2 is OFF')

if led3.value()==1:
led3_state = 'ON'
print('led3 is ON')
elif led3.value()==0:
led3_state = 'OFF'
print('led3 is OFF')

if led4.value()==1:
led4_state = 'ON'
print('led4 is ON')
elif led4.value()==0:
led4_state = 'OFF'
print('led4 is OFF')

html_page = """
<html>
<head>
<meta content="width=device-width, initial-scale=1" name="viewport"></meta>
</head>
<body>
<center><h2>ESP32 Web Server in MicroPython </h2></center>
<center>
<form>
<button name="LED1" type="submit" value="1"> LED1 ON </button>
<button name="LED1" type="submit" value="0"> LED1 OFF </button>
<button name="LED2" type="submit" value="1"> LED2 ON </button>
<button name="LED2" type="submit" value="0"> LED2 OFF </button>
<button name="LED3" type="submit" value="1"> LED3 ON </button>
<button name="LED3" type="submit" value="0"> LED3 OFF </button>
<button name="LED4" type="submit" value="1"> LED4 ON </button>
<button name="LED4" type="submit" value="0"> LED4 OFF </button>
</form>
</center>
<center><p>LED1 is now <strong>""" + led1_state + """</strong>.</p></center>
<center><p>LED2 is now <strong>""" + led2_state + """</strong>.</p></center>
<center><p>LED3 is now <strong>""" + led3_state + """</strong>.</p></center>
<center><p>LED4 is now <strong>""" + led4_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)

led1_on = request.find('/?LED1=1')
led1_off = request.find('/?LED1=0')

led2_on = request.find('/?LED2=1')
led2_off = request.find('/?LED2=0')

led3_on = request.find('/?LED3=1')
led3_off = request.find('/?LED3=0')

led4_on = request.find('/?LED4=1')
led4_off = request.find('/?LED4=0')


if led1_on == 6:
print('LED1 ON')
print(str(led1_on))
led1.value(1)
elif led1_off == 6:
print('LED1 OFF')
print(str(led1_off))
led1.value(0)

if led2_on == 6:
print('LED2 ON')
print(str(led2_on))
led2.value(1)
elif led2_off == 6:
print('LED2 OFF')
print(str(led2_off))
led2.value(0)

if led3_on == 6:
print('LED3 ON')
print(str(led3_on))
led3.value(1)
elif led3_off == 6:
print('LED3 OFF')
print(str(led3_off))
led3.value(0)

if led4_on == 6:
print('LED4 ON')
print(str(led4_on))
led4.value(1)
elif led4_off == 6:
print('LED4 OFF')
print(str(led4_off))
led4.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()

Alejandrorb
join shbcf.ru