作者 | Stack Thomas
译者 | 弯月
出品 | CSDN(ID:CSDNnews)

注意:该项目没有考虑家庭网络安全问题。你应当使用密码,并确保网络无法被外界访问。
在这里插入图片描述

我没有任何IT方面的经验,但在研究了一段时间后,成功地完成了该项目,这都要归功于本文中提到的几个开源项目。
该项目需要的硬件:

  • 树莓派,这是一个小型计算机(我使用的是Zero V2,但任何支持WiFi的树莓派均可)
  • WS2812全彩LED灯带(我用的是5条50灯珠的灯带串联)
  • 5伏电源适配器(确保电源适配器能为LED灯带提供足够的功率,我用了多个USB充电器)
  • 绿色电工胶带(用来隐藏灯带上的红绿白色电线)
  • 一个箱子,用于放置这些设备

第一步:设置树莓派

安装树莓派系统并安装Node-RED和必要的软件包。
安装树莓派系统,如果你使用无头设置,确保启用了SSH或VNC。
你可以用任何方法进入树莓派(对于无头设置,SSH是最简单的方法)
确保树莓派系统为最新版本。
使用下述命令安装Node-RED:

bash <(curl -sLhttps://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)

设置Node-RED在计算机启动时自动运行:

sudo systemctl enable nodered.service

如果你想使用LED HAT来控制灯光,则需要装一个树莓派专用的PiCube 4x4x4 LEDcube HAT,这是一个能控制64个灯珠的单色(红蓝绿)的LED设备。
将PiCube安装到树莓派的40针GPIO排针上。
打开Git Bash控制台,使用下述命令下载代码库:

git clone https://github.com/sbcshop/PiCube.git

代码将被下载到home/pi 目录下。使用ls命令查看目录列表,然后使用cd命令进入目录:


cd PiCube

demo.py是PiCube的示例代码。运行下述命令测试并尝试PiCube。

python3 demo.py

这段代码会在PiCube上显示不同的灯光效果。你也可以修改代码来创建自己的效果。

第二步:设置Node-RED

在这里插入图片描述

我们可以通过Node-RED所在的树莓派(http://localhost:1880)或本地网络中的任意计算机来访问。如果从其他计算机访问,应该使用树莓派的主机名或IP地址,即http://hostname>:1880。在树莓派上执行hostname -i可以看到IP地址。
我们需要添加另外两个模块,以便控制LED。我们需要添加:

  • node-red-dashboard
  • node-red-node-pi-neopixel
    如果你想使用命令行来添加,只需要通过链接找到命令,确保将其放在正确的目录下,以便Node-RED能发现。或者,你也可以通过UI来添加:
  • 点击右上角的Menu按钮
  • 在下拉菜单中选择Managepalette
  • 选择Install选项卡
  • 在搜索框中输入node-red-dashboard
  • 点击install
  • 完成后,再输入node-red-node-pi-neopixel
  • 点击install
  • 最后点击Close
    现在Node-RED应该有了所有点亮灯带所需的模块。接下来:
  • 点击右上角的Menu
  • 在下拉菜单中选择Import
    输入下述代码:
[{"id":"85fce9c6579baaa0","type":"tab","label":"ChristmasLightsSimple","disabled":false,"info":"","env":[]},{"id":"cc28a93a30fc342a","type":"ui_colour_picker","z":"85fce9c6579baaa0","name":"ColorPicker","label":"COLORPICKER","group":"d2ace9cc.9f3998","format":"rgb","outformat":"string","showSwatch":true,"showPicker":true,"showValue":true,"showHue":false,"showAlpha":false,"showLightness":true,"square":"false","order":1,"width":0,"height":0,"passthru":true,"topic":"","x":370,"y":360,"wires":[["024561a721d6f79c"]]},{"id":"024561a721d6f79c","type":"function","z":"85fce9c6579baaa0","name":"SetColor","func":"var count = global.get('count')||0;\nmsg.payload= msg.payload.replace(/[rgb()\\s]/g,\"\");\nif(count===0){\n    msg.payload = msg.payload;\n}\nelse{\n    msg.payload = (count-1) + \",\" +msg.payload;\n}\n\nreturnmsg;","outputs":1,"noerr":0,"x":540,"y":360,"wires":[["c0297b03d780b3ae"]]},{"id":"693df877b38a2cf1","type":"ui_button","z":"85fce9c6579baaa0","name":"Clear","group":"d2ace9cc.9f3998","order":6,"width":0,"height":0,"passthru":false,"label":"CLEAR","tooltip":"","color":"","bgcolor":"","icon":"","payload":"0,0,0","payloadType":"str","topic":"","x":190,"y":460,"wires":[["a96f4433de81777e","663a12cf6cede0ef"]]},{"id":"206d8115b7db9943","type":"ui_dropdown","z":"85fce9c6579baaa0","name":"SelectStrip Color","label":"SELECT STRIPCOLOR","tooltip":"","place":"SELECTSTRIP COLOR","group":"d2ace9cc.9f3998","order":4,"width":0,"height":0,"passthru":true,"multiple":false,"options":[{"label":"RED","value":"255,0,0","type":"str"},{"label":"GREEN","value":"0,255,0","type":"str"},{"label":"BLUE","value":"0,0,255","type":"str"},{"label":"WHITE","value":"255,255,255","type":"str"},{"label":"PURPLE","value":"255,0,255","type":"str"},{"label":"CYAN","value":"0,255,255","type":"str"},{"label":"YELLOW","value":"255,255,0","type":"str"}],"payload":"","topic":"","topicType":"str","className":"","x":370,"y":420,"wires":[["c0297b03d780b3ae"]]},{"id":"9d8f5d23eeba3096","type":"delay","z":"85fce9c6579baaa0","name":"","pauseType":"rate","timeout":"1","timeoutUnits":"seconds","rate":"4","nbRateUnits":"0.0101","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"outputs":1,"x":710,"y":560,"wires":[["c0297b03d780b3ae"]]},{"id":"c0297b03d780b3ae","type":"rpi-neopixels","z":"85fce9c6579baaa0","name":"WS2812BLEDSTRIP","pixels":"250","bgnd":"","fgnd":"","wipe":"0","mode":"pixels","rgb":"grb","brightness":"40","gamma":true,"x":960,"y":400,"wires":[]},{"id":"40080ab07f9975a1","type":"ui_button","z":"85fce9c6579baaa0","name":"Christmas","group":"d2ace9cc.9f3998","order":5,"width":0,"height":0,"passthru":true,"label":"Christmas","tooltip":"","color":"","bgcolor":"","icon":"","payload":"true","payloadType":"bool","topic":"","x":200,"y":620,"wires":[["5db1f6bc1f363b40"]]},{"id":"34273e36c70ad99f","type":"debug","z":"85fce9c6579baaa0","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"reset","targetType":"msg","x":750,"y":700,"wires":[]},{"id":"5db1f6bc1f363b40","type":"function","z":"85fce9c6579baaa0","name":"Christmas","func":"varnumberOfLEDs = 250;\n\nvar j = 0\n\nwhile (j<numberOfLEDs+1){\n        var setColor = j+','+ 255 +','+ 0 +','+0;\n       node.send({payload:setColor});\n       j++;\n        var setColor =j+','+ 0 +','+ 255 +','+ 0;\n       node.send({payload:setColor});\n       j++;\n        var setColor =j+','+ 0 +','+ 0 +','+ 255;\n       node.send({payload:setColor});\n       j++;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":620,"wires":[["c0297b03d780b3ae"]]},{"id":"a96f4433de81777e","type":"change","z":"85fce9c6579baaa0","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"payload","tot":"msg"},{"t":"delete","p":"payload","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":520,"wires":[["9d8f5d23eeba3096","34273e36c70ad99f"]]},{"id":"663a12cf6cede0ef","type":"delay","z":"85fce9c6579baaa0","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"outputs":1,"x":530,"y":480,"wires":[["c0297b03d780b3ae"]]},{"id":"d2ace9cc.9f3998","type":"ui_group","name":"Christmastree","tab":"14b7bf29.77ead1","order":1,"disp":true,"width":"6","collapse":false},{"id":"14b7bf29.77ead1","type":"ui_tab","name":"Lights","icon":"dashboard","order":1,"disabled":false,"hidden":false}]
  • 单击Import
  • 现在应该能看到图中的流程了
  • 单击右上角的Deploy
    打开新的浏览器窗口,访问http://hostname>:1880/ui(将hostname>改成树莓派的IP地址)。现在应该能看到用户界面了。
    注意:这段代码中灯光强度设置为40%,这样能限制电源的功率,亮度也不错。如果想改变这个值,双击流程末尾的rpi-neopixels节点,在输入框中修改。

第三步:连接

在连接任何电线之前,确保关闭树莓派!LED的连线很简单,只需要连接好LED的地线和信号线即可。但是要确保连线正确。我的连接方式如下:

  • 电源——红色
  • 树莓派的6脚——地线——白色
  • 树莓派的12脚(GPIO 18)——信号线——绿色(这就是为什么只能通过HDMI输出音频,因为音频也使用了同一个引脚)

现在可以给LED灯带连上电源了。根据灯带长度,你可能需要在灯带的多个点连接多个电源(藏在圣诞树后面就好)。
任何裸露的电线都要用电工胶布进行绝缘,任何设备都要确保小孩无法接触。
设置好之后,启动树莓派和灯带。访问http://hostname?:1880/ui(将hostname>改成你的树莓派的IP地址)。点击色环的任意位置,即可改变灯光颜色。
参考链接:

  • https://shopmakergenix.blogspot.com/2021/12/neopixel-christmas-tree-lights.html
Logo

20年前,《新程序员》创刊时,我们的心愿是全面关注程序员成长,中国将拥有新一代世界级的程序员。20年后的今天,我们有了新的使命:助力中国IT技术人成长,成就一亿技术人!

更多推荐