Problem Statement:
I have attached 4 LEDs on gpios as (2, 13, 14, 15) of ESP-32. I am connecting it with Wi-Fi Network using usocket lib for HTTP requests. I want to add a feature where we get a command from HTTP Requests as LED pin number and onTime and offTime (in sec/ msec). I want to ON that particular LED for onTime period and OFF that LED for offTime period and this should happen periodically like eg., Periodic: ON LED For T1 --– OFF LED for T2 –-- ON for T1 --- OFF LED for T2 –-- ON for T1....
I have used Timers for this purpose.
My Implementation:
Code: Select all
import machine
import socket
import ure
import time
import network
from machine import Timer
# Connect to Wifi
w = network.WLAN()
w.active(True)
w.connect("ssid", "password")
while not w.isconnected():
time.sleep(1)
print(w.ifconfig())
LEDS = [machine.Pin(i, machine.Pin.OUT) for i in (2, 13, 14, 15)]
def LED_off(pin, value):
LEDS[int(pin)].value(1)
return "LED %s set to: %s" % (pin, "OFF")
def LED_on(pin, value):
LEDS[int(pin)].value(1)
return "LED %s set to: %s" % (pin, "ON")
def led_var_toggle(pin, T1=1000, T2=1000):
tim = Timer(0)
tim1 = Timer(1)
tim.init(period=T1, mode=Timer.PERIODIC, callback= lambda t: LEDS[pin].value(not LEDS[pin].value()))
tim1.init(period=T2+T2 mode=Timer.PERIODIC, callback= lambda t: LEDS[pin].value(not LEDS[pin].value()))
return "Executed"
def buildResponse(response):
# BUILD HTTP RESPONSE HEADERS
return '''HTTP/1.0 200 OK\r\nContent-type: text/html\r\nContent-length: %d\r\n\r\n%s''' % (len(response), response)
s = socket.socket()
s.bind(("", 80))
s.listen(1)
while True:
cl, addr = s.accept() # creating client and accepting all requests
print('client connected from', addr)
request = str(cl.recv(1024))
print("\n\nREQUEST: ", request)
obj = ure.search("GET (.*?) HTTP\/1\.1", request) # parsing url to split the command
print("\n\nobj: ", obj)
print("obj.group(0): ",obj.group(0))
print("obj.group(1): ",obj.group(1))
if not obj:
cl.send(buildResponse("INVALID REQUEST"))
else:
path = obj.group(1)
# =============== LED0X=2 =================
if pathpath.startswith("/LED00=2"):
cl.send(buildResponse("LED 1 ON for T1 sec and OFF for T2 sec: %s" % led_var_toggle(0)))
elif pathpath.startswith("/LED01=2"):
cl.send(buildResponse("LED 2 ON for T1 sec and OFF for T2 sec: %s" % led_var_toggle(1)))
elif pathpath.startswith("/LED02=2"):
cl.send(buildResponse("LED 3 ON for T1 sec and OFF for T2 sec: %s" % led_var_toggle(2)))
elif pathpath.startswith("/LED03=2"):
cl.send(buildResponse("LED 4 ON for T1 sec and OFF for T2 sec: %s" % led_var_toggle(3)))
# =============== LED0X=0 =================
elif path.startswith("/LED00=0"):
pin = parameters.get("pin", 0)
value= parameters.get("value", 0)
cl.send(buildResponse("SET LED:\n%s" % LED_off(pin, value)))
elif path.startswith("/LED01=0"):
pin = parameters.get("pin", 1)
value= parameters.get("value", 0)
cl.send(buildResponse("SET LED:\n%s" % LED_off(pin, value)))
elif path.startswith("/LED02=0"):
pin = parameters.get("pin", 2)
value= parameters.get("value", 0)
cl.send(buildResponse("SET LED:\n%s" % LED_off(pin, value)))
elif path.startswith("/LED03=0"):
pin = parameters.get("pin", 3)
value= parameters.get("value", 0)
cl.send(buildResponse("SET RELAY:\n%s" % LED_off(pin, value)))
# =============== LED0X=1 =================
elif path.startswith("/LED00=1"):
pin = parameters.get("pin", 0)
value= parameters.get("value", 1)
cl.send(buildResponse("SET LED:\n%s" % LED_on(pin, value)))
elif path.startswith("/LED01=1"):
pin = parameters.get("pin", 1)
value= parameters.get("value", 1)
cl.send(buildResponse("SET :\n%s" % LED_on(pin, value)))
elif path.startswith("/LED02=1"):
pin = parameters.get("pin", 2)
value= parameters.get("value", 1)
cl.send(buildResponse("SET :\n%s" % LED_on(pin, value)))
elif path.startswith("/LED03=1"):
pin = parameters.get("pin", 3)
value= parameters.get("value", 1)
cl.send(buildResponse("SET :\n%s" % LED_on(pin, value)))
cl.close()
ip_address/LED0X=0 # Will turn off the LED0X
ip_address/LED0X=1 # Will turn on the LED0X
ip_address/LED0X=2 # Will turn toggle the LED0X
Here X is the number of LED, Eg., for turning LED 1 ON => ip_address/LED00=1
Thanks in advance.