玩儿转物联网IoT - 在Beagle Bone Black上运行node.js 程序

物联网(IoT)技术方兴未艾,智能手环,智能血压计,智能眼镜甚至智能鞋垫都开始进入我们的生活,各种智能设备层出不穷,世界已经到了一个“人有多大胆,地有多大产”的时代,不玩儿点物联网都不好意思和人打招呼了。

之前玩儿过开源硬件Arduino,这次再找个功能强劲的Beagle Bone Black(BBB)。 大家可能知道名气更大的树莓派,BBB也是类似的单片计算机开发板,就在名片大小的一块板子上集成了计算机的大多数常见功能,在上面可以运行Linix 操作系统,通过HDMI连上显示器,插上USB鼠标键盘就是一台完整计算机了。此外还可以配备一些外设Cape,比如触摸屏、音频扩展、串口扩展板等等。

玩儿转物联网IoT - 在Beagle Bone Black上运行node.js 程序

如果你感兴趣,我大淘宝应该都可以买得到,我这边没有视频线,只能通过SSH连接到上面了。开箱后通过数据线连接到电脑上,如果板子没问题的话,靠近网卡口的小LED灯会闪烁。从右到左一次编号为USR0~3. USR 0 是心跳频率的闪烁,USR 2闪烁表示CPU活动。其他两个 USR1 当访问SD卡时闪烁, 我没有安装SD卡,所以也不会亮。USR 3当访问 eMMC时闪烁。

玩儿转物联网IoT - 在Beagle Bone Black上运行node.js 程序

设置 Beagle Bone Black

首先在本机上安装相关驱动, 下面是驱动列表,我用的是Macbook Pro,安装下表中下载安装Max OS X对应的那两个就行了,安装很快,需要重启一下。

Operating System USB Drivers Comments
Windows (64-bit) 64-bit installer

If in doubt, try the 64-bit installer first.

  • Note #1: Windows Driver Certification warning may pop up two or three times. Click “Ignore”, “Install” or “Run”
  • Note #2: To check if you're running 32 or 64-bit Windows see this: https://support.microsoft.com/kb/827218.
  • Note #3: On systems without the latest service release, you may get an error (0xc000007b). In that case, please install the following and retry: https://www.microsoft.com/en-us/download/confirmation.aspx?id=13523.
  • Note #4: You may need to reboot Windows.
  • Note #5: These drivers have been tested to work up to Windows 10
Windows (32-bit) 32-bit installer
Mac OS X Network
Serial
Install both sets of drivers.
Linux mkudevrule.sh Driver installation isn't required, but you might find a few udev rules helpful.

驱动安装完毕并重启机器后,就可以通过浏览器链接到BBB了。 BBB已经内置了Linux操作系统并且安装了Node.JS,还提供了BoneScript库,所以我们可以直接用Javascript来操纵硬件。JavaScript还能干这事?是的,我们可以打开Chrome浏览器(IE不行的),打开地址 http://192.168.7.2, 就会进入到运行在BBB上的网站,其中介绍了BoneScript,也有简单的例子直接可以运行,立刻就可以体验通过JavaScript来实现前面提到的LED灯的开关等实例。

玩儿转物联网IoT - 在Beagle Bone Black上运行node.js 程序

为Beagle Bone Black启动WIFI

下面我想在BBB上运行一个我自己的node.js程序,一般都需要下载一些npm包,所以需要先让BBB连接互联网。BBB上已经带了网卡接口,不过插网线还是有些麻烦,用WIFI应该会更方便些。要放BBB访问WIFI需要一个小硬件,我手头刚好有一个,就是前些年炒的很火的360随身WIFI,上图中BBB上插的那个白色的就是。哈哈,想不到这东西当钥匙链当摆设很多年,今天排上了用场。

BBB的系统默认没没有启用WIFI功能,需要SSH登录上去更改一下。首先在Mac 的终端窗口中通过SSH登录到BBB。 BBB的IP地址的192.168.7.2, 其中默认安装了Debian Linux系统,用户名和密码是 debian/temppwd

Restored session: Wed Mar 9 15:13:18 CST 2016

daniels-MacBook-Pro:webglbim danieldu$ ssh debian@192.168.7.2

The authenticity of host '192.168.7.2 (192.168.7.2)' can't be established.

ECDSA key fingerprint is SHA256:426KRGxj0OmPAef1WAl/7nVaM26q6HXd1OM6mXJA9mw.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '192.168.7.2' (ECDSA) to the list of known hosts.

Debian GNU/Linux 7

BeagleBoard.org Debian Image 2015-03-01

Support/FAQ: http://elinux.org/Beagleboard:BeagleBoneBlack_Debian

default username:password is [debian:temppwd]

debian@192.168.7.2's password:

debian@beaglebone:~$

查看网络接口,并没有WIFI:

debian@beaglebone:~$ ip -4 a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN

inet 127.0.0.1/8 scope host lo

3: usb0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

inet 192.168.7.2/30 brd 192.168.7.3 scope global usb0

那需要编辑 /etc/network/interface 文件, 如WIFI Example中写的那样,把前面的注释去掉,填写我家的WIFI SSID和密码,Ctrl+O保持后Ctrl+X退出。

debian@beaglebone:~$ sudo nano /etc/network/interfaces

# WiFi Example

auto wlan0

iface wlan0 inet dhcp

wpa-ssid “242”

wpa-psk “pwdpwd”

然后重启服务后再看一下,WIFI已经启用了

debian@beaglebone:~$ sudo /etc/init.d/networking restart

[ ok ] Restarting networking (via systemctl): networking.service.

debian@beaglebone:~$ ip -4 a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN

inet 127.0.0.1/8 scope host lo

3: usb0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

inet 192.168.7.2/30 brd 192.168.7.3 scope global usb0

6: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000

inet 192.168.0.17/24 brd 192.168.0.255 scope global wlan0

测试一下,看看baidu能不能通,哈哈,果然可以上网了 :)

debian@beaglebone:~$ wget http://www.baidu.com

2016-03-09 07:47:14 http://www.baidu.com/

Resolving www.baidu.com (www.baidu.com)… 14.215.177.38, 14.215.177.37

Connecting to www.baidu.com (www.baidu.com)|14.215.177.38|:80… connected.

HTTP request sent, awaiting response… 200 OK

Length: unspecified [text/html]

Saving to: `index.html'

[ <=> ] 98,181 1.67K/s in 11s

2016-03-09 07:47:26 (8.92 KB/s) - `index.html' saved [98181]

运行node.js 程序

下来可以在创建一个简单的node.js 应用程序,让它运行在BBB上。BBB上默认的node.js版本是0.10

debian@beaglebone:~$ node —version

v0.10.29

debian@beaglebone:~$ npm —version

1.4.21

下面先创建一个最简单的node.js程序试试看:

debian@beaglebone:~$ mkdir helloBBB

debian@beaglebone:~$ cd helloBBB/

debian@beaglebone:~/helloBBB$ npm init

This utility will walk you through creating a package.json file.

It only covers the most common items, and tries to guess sane defaults.

See `npm help json` for definitive documentation on these fields

and exactly what they do.

Use `npm install <pkg> —save` afterwards to install a package and

save it as a dependency in the package.json file.

Press ^C at any time to quit.

name: (helloBBB)

version: (0.0.0)

description: simple node.js app

entry point: (index.js)

test command:

git repository:

keywords:

author: Daniel Du

license: (ISC) MIT

About to write to /home/debian/helloBBB/package.json:

{

“name”: “helloBBB”,

“version”: “0.0.0”,

“description”: “simple node.js app “,

“main”: “index.js”,

“dependencies”: {

“express”: “^4.13.4”

},

“devDependencies”: {},

“scripts”: {

“test”: “echo "Error: no test specified" && exit 1”

},

“author”: “Daniel Du”,

“license”: “MIT”

}

Is this ok? (yes)

debian@beaglebone:~/helloBBB$ ls

node_modules package.json

debian@beaglebone:~/helloBBB$ nano index.js

用nano 编辑器创建index.js, 文件,建立一个最简单的express web应用。为了防止冲突,我把端口改成了8000:

var express = require('express');
var app = express(); app.get('/', function (req, res) {
res.send('Hello World! This node.js application running from Beagle Bone Black!');
}); app.listen(8000, function () {
console.log('Example app listening on port 8000!');
});

然后运行它:

debian@beaglebone:~/helloBBB$ ls

index.js  node_modules  package.son

debian@beaglebone:~/helloBBB$ npm install

debian@beaglebone:~/helloBBB$ node index.js

回到Mac上,打开浏览器,输入 http://192.168.7.2:8000, 哈哈, 成功! 第一个简单的node.js 程序就在BBB上跑起来了。

玩儿转物联网IoT - 在Beagle Bone Black上运行node.js 程序

上一篇:linux 安装java环境(jdk)


下一篇:一种简单的生产环境部署Node.js程序方法