| 
 
| 本帖最后由 地下铁 于 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[i] = (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[i] = (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()[0])
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”为彩虹灯效背景光。
 
 三、展品展示
 下面用视频演示一下最终的作品效果。
 
 | 
 | 
|  | 
|
|  | 
|  |  |