Node.js & Raspberry Pi & WS2812B RGB LEDs strip All In One
Node.js & Raspberry Pi & WS2812B RGB LEDs strip All In One
Node.js & Raspberry Pi & WS2812B RGB LED 灯带
Node.js
onoff
To interface with the GPIO
on the Raspberry Pi using Node.js, we will use a Module called "onoff
".
# install
$ npm install onoff
https://www.npmjs.com/package/onoff
https://github.com/fivdi/onoff
//include onoff to interact with the GPIO
var Gpio = require('onoff').Gpio;
var LED = new Gpio(18, 'out');
function blinkLED() { //function to start blinking
if (LED.readSync() === 0) { //check the pin state, if the state is 0 (or off)
LED.writeSync(1); //set pin state to 1 (turn LED on)
} else {
LED.writeSync(0); //set pin state to 0 (turn LED off)
}
}
//run the blinkLED function every 500ms
var blinkInterval = setInterval(blinkLED, 500);
function endBlink() { //function to stop blinking
clearInterval(blinkInterval); // Stop blink intervals
LED.writeSync(0); // Turn LED off
LED.unexport(); // Unexport GPIO to free resources
}
setTimeout(endBlink, 3000); //stop blinking after 3 seconds
https://www.npmjs.com/package/rpi-ws281x-native
pigpio
# 依赖项 pigpio C library
$ sudo apt-get update
$ sudo apt-get install pigpio
# version
$ pigpiod -v
https://github.com/joan2937/pigpio
$ npm install -S pigpio
https://www.npmjs.com/package/pigpio
https://github.com/fivdi/pigpio
const Gpio = require('pigpio').Gpio;
const PIN = 17;
const led = new Gpio(PIN, {mode: Gpio.OUTPUT});
// PWM 占空比
let dutyCycle = 0;
setInterval(() => {
led.pwmWrite(dutyCycle);
dutyCycle += 5;
if (dutyCycle > 255) {
dutyCycle = 0;
}
}, 20);
https://www.npmjs.com/package/pigpio#pulse-an-led-with-pwm
GPIO
General Purpose Input Output
Breadboard
面包版
电路连通
图解
demos
GPIO channel 占用 bug ❌
RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
$ ./gpio.py 3
arg1 = 3
⏰ current datetime = 2023-05-13 00:37:25
/home/pi/Desktop/node-led/./gpio.py:28: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
GPIO.setup(PIN, GPIO.OUT)
n = 3
开始闪烁⏳
i = 0
i = 1
i = 2
结束闪烁 👌🏻
https://raspberrypi.stackexchange.com/questions/55143/gpio-warning-channel-already-in-use
GPIO.cleanup()
将您在 RPi.GPIO
中使用的任何 GPIO 设置为 INPUT
模式。
如果该行为是可取的,请在退出脚本
之前调用该方法
GPIO.cleanup()
#!/usr/bin/env python3
# coding: utf8
import RPi.GPIO as GPIO
from datetime import datetime
import time
import sys
arg1 = sys.argv[1]
print("arg1 =", arg1);
# shell 获取时间戳 ✅
# SH_DATE=$(TZ=':Asia/Shanghai' date '+%Y-%m-%d %T');
# datetime = $(date '+%Y-%m-%d %T')
# Python 获取时间戳 ✅
now = datetime.now()
# 转换为指定的格式
currentTime = now.strftime("%Y-%m-%d %H:%M:%S")
print("⏰ current datetime =", currentTime);
# $ pinout 命令查看,或 https://pinout.xyz/
# 指定 BCM 模式下的 GPIO 针脚编号是 12
# 对应的物理针脚编号是 32
PIN = 12
# BCM 模式
GPIO.setmode(GPIO.BCM)
# 指定 GPIO 针脚为一个电流输出针脚
GPIO.setup(PIN, GPIO.OUT)
# 输出低电平
GPIO.output(PIN, GPIO.LOW)
# index
i = 0
# 类型转换,str => int
n = int(arg1)
print("n =", n)
print('开始闪烁⏳')
while (i < n):
print("i =", i)
# 高电平,LED 点亮
GPIO.output(PIN, GPIO.HIGH)
# 休眠 1 秒,防止 LED 长时间点亮烧坏了
time.sleep(1.0)
# 低电平,LED 熄灭
GPIO.output(PIN, GPIO.LOW)
# 休眠 1 秒
time.sleep(1.0)
i = i + 1
# 输出低电平,LED 关闭
# GPIO.output(PIN, GPIO.LOW)
# 清除,释放内存
GPIO.cleanup()
print('结束闪烁 👌🏻')
poweroff
$ sudo reboot
$ sudo shutdown -h now
PCB Design Software
Fritzing
refs
https://www.w3schools.com/nodejs/nodejs_raspberrypi.asp
©xgqfrms 2012-2025
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17396558.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
2022-05-12 iOS App LaunchScreen storyboard not work bug All In One
2022-05-12 SwiftUI In Action All In One
2022-05-12 js download canvas image All In One
2021-05-12 前端可视化布局生成器 All in One
2020-05-12 DDD
2020-05-12 skills share & free videos
2020-05-12 Dark Web & 暗网 & Tor