玩兒轉物聯網IoT - 在Beagle Bone Black上運行node.js 程序

物聯網(IoT)技術方興未艾,智能手環,智能血壓計,智能眼鏡甚至智能鞋墊都開始進入咱們的生活,各類智能設備層出不窮,世界已經到了一個「人有多大膽,地有多大產」的時代,不玩兒點物聯網都很差意思和人打招呼了。javascript

以前玩兒過開源硬件Arduino,此次再找個功能強勁的Beagle Bone Black(BBB)。 你們可能知道名氣更大的樹莓派,BBB也是相似的單片計算機開發板,就在名片大小的一塊板子上集成了計算機的大多數常見功能,在上面能夠運行Linix 操做系統,經過HDMI連上顯示器,插上USB鼠標鍵盤就是一臺完整計算機了。此外還能夠配備一些外設Cape,好比觸摸屏、音頻擴展、串口擴展板等等。html


1096742727.jpg

若是你感興趣,我大淘寶應該均可以買獲得,我這邊沒有視頻線,只能經過SSH鏈接到上面了。開箱後經過數據線鏈接到電腦上,若是板子沒問題的話,靠近網卡口的小LED燈會閃爍。從右到左一次編號爲USR0~3. USR 0 是心跳頻率的閃爍,USR 2閃爍表示CPU活動。其餘兩個 USR1 當訪問SD卡時閃爍, 我沒有安裝SD卡,因此也不會亮。USR 3當訪問 eMMC時閃爍。java


526334993.jpg  

設置 Beagle Bone Blacknode

首先在本機上安裝相關驅動, 下面是驅動列表,我用的是Macbook Pro,安裝下表中下載安裝Max OS X對應的那兩個就好了,安裝很快,須要重啓一下。linux



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

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

  • 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燈的開關等實例。web

Screen Shot 2016-03-09 at 5.10.55 PM.png

爲Beagle Bone Black啓動WIFIexpress

下面我想在BBB上運行一個我本身的node.js程序,通常都須要下載一些npm包,因此須要先讓BBB鏈接互聯網。BBB上已經帶了網卡接口,不過插網線仍是有些麻煩,用WIFI應該會更方便些。要放BBB訪問WIFI須要一個小硬件,我手頭恰好有一個,就是前些年炒的很火的360隨身WIFI,上圖中BBB上插的那個白色的就是。哈哈,想不到這東西當鑰匙鏈當擺設不少年,今天排上了用場。npm

BBB的系統默認沒沒有啓用WIFI功能,須要SSH登陸上去更改一下。首先在Mac 的終端窗口中經過SSH登陸到BBB。 BBB的IP地址的192.168.7.2, 其中默認安裝了Debian Linux系統,用戶名和密碼是 debian/temppwdjson

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上跑起來了。

Screen Shot 2016-03-09 at 4.33.14 PM.png

相關文章
相關標籤/搜索