地下铁 发表于 2024-4-19 16:27:24

FireBeetle 2 ESP32 C6 文创小夜灯

本帖最后由 地下铁 于 2024-4-19 16:27 编辑

一、项目目标:
   结合siot2,使用c6和ws2812灯带制作一个地方特点的文创小夜灯。
二、制作过程:
1、搭建电路
我用了一条10灯的灯带,把信号线接在C6的16号引脚上。

2、编写程序
import machine,time
import neopixel
import network
import siot

# 初始化NeoPixel灯带
NUM_PIXELS = 10
PIN = 16
pixels = neopixel.NeoPixel(machine.Pin(PIN), NUM_PIXELS)

# 初始化wifi
ssid = 'your wifi name'
password = 'your wifi password'
wlan = network.WLAN(network.STA_IF)
wlan.active(True)

# 初始化siot
SERVER="192.168.2.21"# 运行siot服务的ip地址
CLIENT_ID=""
IOT_UserName='siot'
IOT_PassWord='dfrobot'
siot.init(CLIENT_ID,SERVER,user=IOT_UserName,password=IOT_PassWord)
      
def light(r,g,b):
    for i in range(NUM_PIXELS):
      pixels = (r, g, b)# 设置颜色
      pixels.write()# 更新灯带状态
      time.sleep_ms(50)# 等待一段时间   
#设置彩虹效果
def rainbow_effect():
    for i in range(NUM_PIXELS):
      hue = int(i * (255 / NUM_PIXELS))
      pixels = (hue, 255 - hue, 0)# 设置颜色
      pixels.write()# 更新灯带状态
      time.sleep_ms(50)# 等待一段时间
#设置回调函数
def sub_cb(topic, msg):
    if (msg.decode() == "rb"):
      for _ in range(10):
            light(0,0,0)
            time.sleep_ms(50)
            rainbow_effect()
            time.sleep_ms(50)
            light(0,0,0)
    if (msg.decode() == "y"):
      light(0,0,0)
      time.sleep_ms(50)
      light(255,140,0)
      #time.sleep_ms(50)
      #light(0,0,0)
    if (msg.decode() == "w"):
      light(0,0,0)
      time.sleep_ms(50)
      light(248,248,255)

#主程序开始
#检查wifi,siot是否已经连接
if not wlan.isconnected():
    print('connect wifi...')
    wlan.connect(ssid,password)
    while not wlan.isconnected():
      pass
print('connect success')
print('ip address',wlan.ifconfig())
siot.connect()
#将订阅主题和回调函数进行绑定
siot.set_callback(sub_cb)
siot.getsubscribe(topic="siot/rb")
siot.loop()
while 1:
    siot.check_msg()#重复检查传入信息,进行调度分拣
time.sleep(2)

3、设计结构

4、组装作品


5、调试作品

用pc或手机访问siot服务器的服务页面,使用siot和dfrobot登录。
目前设计了三种效果:
发送”w”为冷色调背景光,发送”y”为暖色调背景光发送”rb”为彩虹灯效背景光。
         
三、展品展示
下面用视频演示一下最终的作品效果。
https://www.bilibili.com/video/BV15J4m1W7PU/?share_source=copy_web&vd_source=9b753ed5f6c2a6c50047481fa71b49a5

hnyzcj 发表于 2024-4-20 08:51:57

不错不错
页: [1]
查看完整版本: FireBeetle 2 ESP32 C6 文创小夜灯