ESP32(MicroPython) 网页控制WS2812

ESP32(MicroPython) 网页控制WS2812

MicroPython提供了网页功能,可以使用HTML和CSS的大部分功能,并通过请求来调整变量的值和显示的文字,但不能添加图片(在线版FontAwsome可以正常使用)。本程序实现了通过网页调节RGB渐变流水灯程序的参数,视频只展示了网页的内容。

要注意的一点是多线程功能启动后可能无法连接WiFi,需要把连接WiFi的代码放到进程内容前面。网页居中排列各内容,未使用框架形式以实现每一行灵活分配按钮数量。按钮颜色分别设置,使用渐变形式。实际运行时发现部分参数的设定会干扰其它参数,原因尚不明确。

代码如下

#导入Pin模块
from machine import Pin
import time
import network
import socket
from neopixel import NeoPixel
import _thread

led1=Pin(15,Pin.OUT)
# 五向导航按键,COM引脚接3.3V
key1 = Pin(21, Pin.IN, Pin.PULL_DOWN)
key2 = Pin(19, Pin.IN, Pin.PULL_DOWN)
key3 = Pin(18, Pin.IN, Pin.PULL_DOWN)
key4 = Pin(5, Pin.IN, Pin.PULL_DOWN)
key5 = Pin(17, Pin.IN, Pin.PULL_DOWN)
key6 = Pin(16, Pin.IN, Pin.PULL_DOWN)
 
pin=13
rgb_num=24
rgb_led=NeoPixel(Pin(pin,Pin.OUT),rgb_num)

#连接的WIFI账号和密码
ssid = " "
password = " "

key_en=1
#按键扫描函数
def key_scan():
    global key_en
    if key_en==1 and (key1.value()==1 or key2.value()==1 or key3.value()==1 or key4.value()==1 or
                      key5.value()==1 or key6.value()==1  ):
        time.sleep_ms(10)
        key_en=0
        if key1.value()==1:
            return 1
        elif key2.value()==1:
            return 2
        elif key3.value()==1:
            return 3
        elif key4.value()==1:
            return 4
        elif key5.value()==1:
            return 5
        elif key6.value()==1:
            return 6
    elif (key1.value()==0 and key2.value()==0 and key3.value()==0 and key4.value()==0 and
          key5.value()==0 and key6.value()==0  ) :
        key_en=1
    return 0

brightness=6
delay=40
mode=1
def key_get(): #获取键值并改变变量的值
    global brightness
    global delay
    global mode
    key=key_scan()
    if key==1 and brightness<30 :
        brightness+=3
    elif key==2 and brightness>3 :
        brightness-=3
    elif key==3 and delay<90 :
        delay+=10
    elif key==4 and delay>10 :
        delay-=10
    elif key==5 and mode<2 :
        mode+=1
    elif key==6 and mode>0 :
        mode-=1     

#WIFI连接
def wifi_connect():
    wlan=network.WLAN(network.STA_IF)  #STA模式
    wlan.active(True)  #激活
    
    if not wlan.isconnected():
        print("conneting to network...")
        wlan.connect(ssid,password)  #输入 WIFI 账号密码
        
        while not wlan.isconnected():
            led1.value(1)
            time.sleep_ms(300)
            led1.value(0)
            time.sleep_ms(300)
        led1.value(0)
        return False
    else:
        led1.value(0)
        print("network information:", wlan.ifconfig())
        return True

#网页数据
def web_page():
    global brightness
    global mode
    global delay
  
    html = """<html><head> <title>ESP32 WS2812 Control</title> <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="icon" href="data:,"> <style>html{font-family: Helvetica; display:inline-block; margin: 0px auto; text-align: center;}
        h1{color: #0F3376; padding: 1vh;}p{font-size: 1rem;}.button{display: inline-block; background-color: #141414; border: none; 
        border-radius: 4px; color: white; padding: 8px 15px; text-decoration: none; font-size: 20px; margin: 2px; cursor: pointer;}
        .button2{background-color: #282828;}.button3{background-color: #3c3c3c;}.button4{background-color: #505050;}
        .button5{background-color: #646464;}.button6{background-color: #787878;}.button7{background-color: #8c8c8c;}
        .button8{background-color: #a0a0a0;}.button9{background-color: #b4b4b4;}.button10{background-color: #c8c8c8;}
        .button11{background-color: #f01010;}.button12{background-color: #10f010;}.button13{background-color: #1010f0;}
        .button14{background-color: #0000ff;}.button15{background-color: #0f0fe1;}.button16{background-color: #1e1ed2;}
        .button17{background-color: #2d2dc3;}.button18{background-color: #3c3cb4;}.button19{background-color: #4b4ba5;}
        .button20{background-color: #5a5a96;}.button21{background-color: #696987;}.button22{background-color: #787878;}
        a:link {text-decoration:none;}a:visited {text-decoration:none;}a:hover {text-decoration:none;}
        a:active {text-decoration:none;}  /* selected link */</style></head>
        <body> <h1>ESP32 WS2812 Control</h1> <p><a href="/?d1">Brightness: <strong>""" + str(brightness) + """</strong></a></p>
        <p><a href="/?b1"><button class="button">03</button></a><a href="/?b2"><button class="button button2">06</button></a>
        <a href="/?b3"><button class="button button3">09</button></a><a href="/?b4"><button class="button button4">12</button></a>
        <a href="/?b5"><button class="button button5">15</button></a><a href="/?b6"><button class="button button6">18</button></a>
        <a href="/?b7"><button class="button button7">21</button></a><a href="/?b8"><button class="button button8">24</button></a>
        <a href="/?b9"><button class="button button9">27</button></a><a href="/?b10"><button class="button button10">30</button></p></a>
        <p><a href="/?d2">Mode: <strong>""" + str(mode) + """</strong></a></p>
        <p><a href="/?b11"><button class="button button11">0</button></a>
        <a href="/?b12"><button class="button button12">1</button></a>
        <a href="/?b13"><button class="button button13">2</button></a></p>
        <p><a href="/?d3">Delay: <strong>""" + str(delay) + """</strong></a></p>
        <p><a href="/?b14"><button class="button button14">10</button></a><a href="/?b15"><button class="button button15">20</button></a>
        <a href="/?b16"><button class="button button16">30</button></a><a href="/?b17"><button class="button button17">40</button></a>
        <a href="/?b18"><button class="button button18">50</button></a><a href="/?b19"><button class="button button19">60</button></a>
        <a href="/?b20"><button class="button button20">70</button></a><a href="/?b21"><button class="button button21">80</button></a>
        <a href="/?b22"><button class="button button22">90</button></a></p></body></html>"""
    return html

wifi_connect()
#SOCK_STREAM表示的是TCP协议,SOCK_DGRAM表示的是UDP协议
my_socket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)  #创建socket连接
# 将socket对象绑定ip地址和端口号
my_socket.bind(('', 80))
# 相当于电话的开机 括号里的参数表示可以同时接收5个请求
my_socket.listen(5)
def server(i1,i2):
    global brightness
    global mode
    global delay
    
    while True:
        # 进入监听状态,等待别人链接过来,有两个返回值,
        #一个是对方的socket对象,一个是对方的ip以及端口
        client, addr = my_socket.accept()
        print('Got a connection from %s' % str(addr))
        # recv表示接收,括号里是最大接收字节
        request = client.recv(1024)
        request = str(request)
        print('Content = %s' % request)
        b1 = request.find('/?b1')
        b2 = request.find('/?b2')
        b3 = request.find('/?b3')
        b4 = request.find('/?b4')
        b5 = request.find('/?b5')
        b6 = request.find('/?b6')
        b7 = request.find('/?b7')
        b8 = request.find('/?b8')
        b9 = request.find('/?b9')
        b10 = request.find('/?b10')
        b11 = request.find('/?b11')
        b12 = request.find('/?b12')
        b13 = request.find('/?b13')
        b14 = request.find('/?b14')
        b15 = request.find('/?b15')
        b16 = request.find('/?b16')
        b17 = request.find('/?b17')
        b18 = request.find('/?b18')
        b19 = request.find('/?b19')
        b20 = request.find('/?b20')
        b21 = request.find('/?b21')
        b22 = request.find('/?b22')
        d1 = request.find('/?d1')
        d2 = request.find('/?d2')
        d3 = request.find('/?d3')
        if b1 == 6:
            brightness=3
        if b2 == 6:
            brightness=6
        if b3 == 6:
            brightness=9
        if b4 == 6:
            brightness=12
        if b5 == 6:
            brightness=15
        if b6 == 6:
            brightness=18
        if b7 == 6:
            brightness=21
        if b8 == 6:
            brightness=24
        if b9 == 6:
            brightness=27
        if b10 == 6:
            brightness=30
        if b11 == 6:
            mode=0
        if b12 == 6:
            mode=1
        if b13 == 6:
            mode=2
        if b14 == 6:
            delay=10
        if b15 == 6:
            delay=20
        if b16 == 6:
            delay=30
        if b17 == 6:
            delay=40
        if b18 == 6:
            delay=50
        if b19 == 6:
            delay=60
        if b20 == 6:
            delay=70
        if b21 == 6:
            delay=80
        if b22 == 6:
            delay=90
        if d1 == 6:
            brightness=6
        if d2 == 6:
            mode=1
        if d3 == 6:
            delay=40    
        response = web_page()
        client.send('HTTP/1.1 200 OK\n')
        client.send('Content-Type: text/html\n')
        client.send('Connection: close\n\n')
        client.sendall(response)
        client.close()

def light(i1,i2):
    global brightness
    global mode
    global delay
    count=0
    count1=0
    count2=23
    while True:
        key_get()
        if count==23 :
            count=0
        if mode==0 : #关灯
            for i in range(rgb_num):
                rgb_led[i]=(0, 0, 0)
                rgb_led.write()
        if mode==1 : #非对称
            temp=0
            i=count
            count+=1
            r=8*brightness #红色渐变到绿色
            g=0
            while temp<8 :
                r-=brightness
                g+=brightness
                if i>23 :
                    i-=24
                rgb_led[i]=(r, g, 0)
                i+=1
                temp+=1
            temp=0
            g=8*brightness #绿色渐变到蓝色
            b=0
            while temp<8 :
                g-=brightness
                b+=brightness
                if i>23 :
                    i-=24
                rgb_led[i]=(0, g, b)
                i+=1
                temp+=1
            temp=0
            b=8*brightness #蓝色渐变到红色
            r=0
            while temp<8 :
                b-=brightness
                r+=brightness
                if i>23 :
                    i-=24
                rgb_led[i]=(r, 0, b)
                i+=1
                temp+=1
            rgb_led.write()    
            time.sleep_ms(delay)
        if count1==12 :
            count1=0
        if count2==11 :
            count2=23     
        if mode==2 : #对称
            temp=0  #前半部分
            i=count1
            count1+=1
            r=8*brightness #红色渐变到绿色
            g=0
            while temp<4 :
                r-=2*brightness
                g+=2*brightness
                if i>11 :
                    i-=12
                rgb_led[i]=(r, g, 0)
                i+=1
                temp+=1
            temp=0
            g=8*brightness #绿色渐变到蓝色
            b=0
            while temp<4 :
                g-=2*brightness
                b+=2*brightness
                if i>11 :
                    i-=12
                rgb_led[i]=(0, g, b)
                i+=1
                temp+=1
            temp=0
            b=8*brightness #蓝色渐变到红色
            r=0
            while temp<4 :
                b-=2*brightness
                r+=2*brightness
                if i>11 :
                    i-=12
                rgb_led[i]=(r, 0, b)
                i+=1
                temp+=1
            temp=0  #后半部分
            i=count2
            count2-=1
            r=8*brightness #红色渐变到绿色
            g=0
            while temp<4 :
                r-=2*brightness
                g+=2*brightness
                if i<12 :
                    i+=12
                rgb_led[i]=(r, g, 0)
                i-=1
                temp+=1
            temp=0
            g=8*brightness #绿色渐变到蓝色
            b=0
            while temp<4 :
                g-=2*brightness
                b+=2*brightness
                if i<12 :
                    i+=12
                rgb_led[i]=(0, g, b)
                i-=1
                temp+=1
            temp=0
            b=8*brightness #蓝色渐变到红色
            r=0
            while temp<4 :
                b-=2*brightness
                r+=2*brightness
                if i<12 :
                    i+=12
                rgb_led[i]=(r, 0, b)
                i-=1
                temp+=1    
            rgb_led.write()    
            time.sleep_ms(delay)

i1=0
i2=0
_thread.start_new_thread(server, (i1,i2))
_thread.start_new_thread(light, (i1, i2))

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MicroPython是一种Python3的实现,可以运行在微控制器上。ESP32是一种基于ESP32芯片的开源硬件平台,它支持WiFi和蓝牙连接,并提供了很多GPIO引脚来连接传感器和执行器。声音传感器是一种检测环境噪声的传感器,可以用来检测声音的强度和频率。WS2812B灯是一种数字RGB LED灯,可以通过单一的数据引脚来控制多个灯的颜色和亮度。 在MicroPython中,可以通过内置的库来控制ESP32的GPIO引脚、WiFi和蓝牙连接。同时,也可以使用第三方库来连接和控制声音传感器和WS2812B灯。例如,可以使用pyaudio库来获取声音传感器的数据,使用neopixel库来控制WS2812B灯的颜色和亮度。 以下是一个示例代码,演示如何使用ESP32连接声音传感器和WS2812B灯: ```python import machine import neopixel import pyaudio # 设置GPIO引脚 LED_PIN = 19 MIC_PIN = 32 # 初始化WS2812B灯 num_leds = 8 np = neopixel.NeoPixel(machine.Pin(LED_PIN), num_leds) # 初始化声音传感器 chunk = 1024 sample_rate = 44100 p = pyaudio.PyAudio() stream = p.open(format=pyaudio.paInt16, channels=1, rate=sample_rate, input=True, input_device_index=MIC_PIN, frames_per_buffer=chunk) # 循环获取声音传感器数据并控制WS2812B灯 while True: data = stream.read(chunk) rms = audioop.rms(data, 2) # 将声音强度映射到灯的亮度上 brightness = int(rms / 1000) # 将RGB颜色设置为亮度的值 color = (brightness, brightness, brightness) # 将颜色应用到所有的灯上 for i in range(num_leds): np[i] = color np.write() ``` 需要注意的是,以上代码仅为示例,实际使用时需要根据具体情况进行修改和优化。同时,也需要考虑到MicroPython在资源限制方面的限制,例如内存和处理器速度等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路易斯720

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值