一、基础硬件介绍
最权威最详细信息科参见官网:
树莓派官网
树莓派提供了一组对外的 IO 接口,称为 GPIO( 通用 IO 接口,General-purpose input/output)。
它的 40 个脚的定义如下图:
通过 GPIO ,树莓派可以与其他电子元件连接。
二、LED 控制脚本
下面,我们使用 Node 脚本控制 LED。
上图中第11针(GPIO17)这个针脚的电流是脚本可以控制的。接下来我们将在树莓派上新建一个实验目录,并安装控制 GPIO 的 Node 模块rpio。
1.安装Node模块
npm是什么?简单说npm就是“把各种代码模块集中管理的工具”,要什么代码就不要四处搜罗了,直接在这里就可以找到了。
- 安装npm
sudo apt-get install npm
结果发现报错,说有依赖项冲突什么的。找到解决方法:
inux的版本依赖问题很令人纠结,不过我们可以通过使用aptitude软件包管理器来解决这个依赖问题,aptitude是可以选择合适的版本与匹配软件安装。
- 安装aptitude工具
sudo apt-get install aptitude
再利用aptitude来安装npm
sudo aptitude install npm
上述命令执行后:
会弹出是否接受:第一次要选择N 不知道这是个啥情况,然后第二次选择Y,然后以后好像就正常了,选择Y接受建议就行了。 好了现在npm可以使用了,可以接着在树莓派上安装node了sudo apt install nodejs
查看版本
node -v
版本号是12.22.5
新建一个实验目录,运行:
mkdir led-demo && cd led-demo
npm init -y
npm install -S rpio
发现执行“npm install -S rpio”的时候出错。
换了几种安装方式还是错的:
sudo npm cache clean -f
sudo npm install npm -g
也是错误,报错信息:
request to https://registry.npmjs.org/npm failed, reason: connect EHOSTUNREACH 104.16.16.35:443
使用“sudo npm install rpio”一样的结果
报错信息:
pi@raspberrypi:~ $ sudo npm install rpio
npm ERR! code EHOSTUNREACH
npm ERR! syscall connect
npm ERR! errno EHOSTUNREACH
npm ERR! request to https://registry.npmjs.org/rpio failed, reason: connect EHOSTUNREACH 104.16.17.35:443
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2022-03-11T09_51_24_796Z-debug.log
使用“npm config list -l”查看信息:
pi@raspberrypi:~ $ npm config list -l
; "default" config from default values
_auth = (protected)
access = null
all = false
allow-same-version = false
also = null
always-auth = false
audit = true
audit-level = null
auth-type = "legacy"
before = null
bin-links = true
browser = null
ca = null
cache = "/home/pi/.npm"
cache-lock-retries = 10
cache-lock-stale = 60000
cache-lock-wait = 10000
cache-max = null
cache-min = 10
cafile = null
call = ""
cert = null
ci-name = null
cidr = null
color = true
commit-hooks = true
depth = null
description = true
dev = false
diff = []
diff-dst-prefix = ""
diff-ignore-all-space = false
diff-name-only = false
diff-no-prefix = false
diff-src-prefix = ""
diff-text = false
diff-unified = null
dry-run = false
editor = "vi"
engine-strict = false
fetch-retries = 2
fetch-retry-factor = 10
fetch-retry-maxtimeout = 60000
fetch-retry-mintimeout = 10000
fetch-timeout = 300000
force = false
foreground-script = false
format-package-lock = true
fund = true
git = "git"
git-tag-version = true
global = false
global-style = false
globalconfig = "/etc/npmrc"
heading = "npm"
https-proxy = null
if-present = false
ignore-prepublish = false
ignore-scripts = false
include = []
include-staged = false
init-author-email = ""
init-author-name = ""
init-author-url = ""
init-license = "ISC"
init-module = "/home/pi/.npm-init.js"
init-version = "1.0.0"
init.author.email = ""
init.author.name = ""
init.author.url = ""
init.license = "ISC"
init.module = "/home/pi/.npm-init.js"
init.version = "1.0.0"
json = false
key = null
legacy-bundling = false
legacy-peer-deps = false
link = false
local-address = undefined
loglevel = "notice"
logs-max = 10
; long = false ; overridden by cli
maxsockets = 50
message = "%s"
metrics-registry = "https://registry.npmjs.org/"
node-options = null
node-version = "v12.22.5"
noproxy = null
npm-version = "7.5.2"
offline = false
; omit = [] ; overridden by cli
only = null
optional = true
otp = null
package = []
package-lock = true
package-lock-only = false
parseable = false
prefer-offline = false
prefer-online = false
; prefix = "/usr" ; overridden by builtin
preid = ""
production = false
progress = true
proxy = null
read-only = false
rebuild-bundle = true
registry = "https://registry.npmjs.org/"
rollback = true
save = true
save-bundle = false
save-dev = false
save-exact = false
save-optional = false
save-prefix = "^"
save-prod = false
scope = ""
script-shell = null
scripts-prepend-node-path = "warn-only"
searchexclude = null
searchlimit = 20
searchopts = ""
searchstaleness = 900
shell = "/bin/bash"
shrinkwrap = true
sign-git-commit = false
sign-git-tag = false
sso-poll-frequency = 500
sso-type = "oauth"
strict-peer-deps = false
; strict-ssl = true ; overridden by user
tag = "latest"
tag-version-prefix = "v"
timing = false
tmp = "/tmp"
umask = 0
unicode = true
update-notifier = true
usage = false
; user-agent = "npm/{npm-version} node/{node-version} {platform} {arch} {ci}" ; overridden by cli
userconfig = "/home/pi/.npmrc"
version = false
versions = false
viewer = "man"
; "builtin" config from /usr/share/nodejs/npm/npmrc
globalignorefile = "/etc/npmignore"
prefix = "/usr/local"
; "user" config from /home/pi/.npmrc
strict-ssl = false
; "cli" config from command line options
long = true
omit = []
user-agent = "npm/7.5.2 node/v12.22.5 linux arm"
参考文献
- 树莓派新手入门教程