Remote Control of LED Brightness
Preparation
Hardware:
- FireBeetle-ESP32 × 1
Software:
- uPyCraft IDE
Code Position:
- File → Examples →Communicate → webServer → webpwm.py
- File → Examples →Communicate → webServer → webCtrl.htm
Operation Steps
1. Download and run webCtrl.htm file, the code is as below.
2. Modify SSID (Wi-Fi Name) and password in the webpwm.py, download and run, the code is as below.
#Hardware Platform: FireBeetle-ESP32
from machine import Pin, PWM
import network
import os
import time
import socket
import gc
SSID="XXXXXXXX" #set the wifi ID
PASSWORD="XXXXXXXX" #set the wifi password
wlan=None
s=None
led=None
def connectWifi(ssid, passwd):
global wlan
wlan=network.WLAN(network.STA_IF) #create a wlan object
wlan.active(True) #active the network interface
wlan.disconnect() #disconnect the last wifi
wlan.connect(ssid,passwd) ##connect to WiFi
while(wlan.ifconfig()[0] == '0.0.0.0'):
time.sleep(1)
return True
def ajaxWebserv():
#Control the minimum Ajax in Webserver.
global s,led
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #create stream socket
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #Set the value of the given socket option
s.bind((wlan.ifconfig()[0], 80)) #bind ip and port
s.listen(1) #listen stream socket
while True:
conn, addr = s.accept() #Accept a connection,conn is a new socket object
#print("Got a connection from %s" % str(addr))
request = conn.recv(1024) #Receive 1024 byte of data from the socket
conn.sendall('HTTP/1.1 200 OK\nConnection: close\nServer: FireBeetle\nContent-Type: text/html\n\n')
request = str(request)
ib = request.find('Val=') #find the string 'Val=' from request
if ib > 0 :
ie = request.find(' ', ib) #init address of the index with ib,then find ' '
Val = request[ib+4:ie] #get the string of ib+4 to ie in the request
print("Val =", Val)
led.duty(int(Val) * 100) #set the duty of led
conn.send(Val) #send data
else:
with open('webCtrl.htm', 'r') as html: #open file 'webCtrl.htm' with readonly
conn.sendall(html.read()) #read data from 'webCtrl.htm',and send all of the data
conn.sendall('\r\n')
conn.close() #close file
#print("Connection wth %s closed" % str(addr))
#Capture exceptions, stop running when interrupted by accident.
try:
led=PWM(Pin(2), freq=100) #create led object
led.init()
led.duty(0)
connectWifi(SSID, PASSWORD)
ajaxWebserv()
except:
if (s):
s.close() #close socket
led.deinit() #turn off led
wlan.disconnect() #disconnect wifi
wlan.active(False) #disable wifi
3. Input the ID address that runs in the browser as below.
Result
The onboard LED brightness can be adjusted by moving the block. The terminal will show the corresponding brightness, shown as below.