—————————————————————————–
WEB SERVER TO CONTROL 2 LEDs
This is a simple web server program.
2 LEDs are connected to IO21 and IO22 of the MakePython ESP32 development board.
The project controls these LEDs (turns ON or OFF) from a web server application.
For example, the LEDs can be controlled from any device that is on the web, for example a PC, tablet, mobile phone etc.
When activated,a form will appear on the device with buttons and clicking these button will control the LEDs.
The LEDs are named as LED0 and LED1, connected to ports IO21 and IO22 respectively.
Author: Dogan Ibrahim
Date : January, 2022
File : Android
——————————————————————————
# webserver.py
import network
import socket
from machine import Pin
#Configure the pins as outputs
LED0 = Pin(21, Pin.OUT)
LED1 = Pin(22, Pin.OUT)
#Turn OFF the LEDs to start with
LED0.value(0)
LED1.value(0)
# The HTML code
html = “””<!DOCTYPE html>
<html>
<body>
<center><h1>MakePython ESP32 LED ON/OFF</h1></center>
<center><h2>MicroPython Web Server Example with 2 LEDs</h2></center>
<form>
<button name=”LED0″ button style=”color:green” value=”ON” type=”submit”>LED0 ON</button>
<button name=”LED0″ button style=”color=red” value=”OFF” type=”submit”>LED0 OFF</button><br><br>
<button name=”LED1″ button style=”color:green” value=”ON” type=”submit”>LED1 ON</button>
<button name=”LED1″ button style=”color:red” value=”OFF” type=”submit”>LED1 OFF</button>
</form>
</body>
</html>
“””
# This function connects to the Wi-Fi
def Connect_WiFi():
net = network.WLAN(network.STA_IF)
if not net.isconnected():
net.active(True)
net.connect(“BTHomeSpot-XNH”, “49345xyz”)
# Connect to local Wi-Fi
Connect_WiFi()
addr=socket.getaddrinfo(“0.0.0.0”,80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
while True:
conn, address = s.accept()
request = conn.recv(1024)
request = str(request)
LEDON0 = request.find(“/?LED0=ON”)
LEDOFF0 = request.find(“/?LED0=OFF”)
LEDON1 = request.find(“/?LED1=ON”)
LEDOFF1 = request.find(“/?LED1=OFF”)
if LEDON0 != -1:
LED0.value(1)
if LEDOFF0 != -1:
LED0.value(0)
if LEDON1 != -1:
LED1.value(1)
if LEDOFF1 != -1:
LED1.value(0)
resp = html
conn.send(resp)
conn.close()