Intel EdsionでNode.jsのサーバを動かす
続いて、Node.js + expressでWebサーバを立ち上げてみます
パッケージマネージャの更新
Yoctoではapt-getとかではなく、opkgというパッケージマネージャを使うようです。これを最新にしておきます。 こちらを参考に
# cd /etc/opkg # curl http://nonnoise.github.io/Edison/_sources/Edison/base-feeds.conf # curl http://nonnoise.github.io/Edison/_sources/Edison/intel-iotdk.conf # curl http://nonnoise.github.io/Edison/_sources/Edison/mraa-upm.conf
アップデートしておきます
# opkg update # opkg upgrade
Node.jsの確認
とりあえずコマンド叩いてみましょう
# node > (^C again to quit) >
間違いなくNode.jsが入ってます。バージョンは
# node -v v0.10.28
そして、Nodeのパッケージマネージャであるnpmも入ってます
# npm Usage: npm <command> where <command> is one of: add-user, adduser, apihelp, author, bin, bugs, c, cache, completion, config, ddp, dedupe, deprecate, docs, edit, explore, faq, find, find-dupes, get, help, help-search, home, i, info, init, install, isntall, issues, la, link, list, ll, ln, login, ls, outdated, owner, pack, prefix, prune, publish, r, rb, rebuild, remove, repo, restart, rm, root, run-script, s, se, search, set, show, shrinkwrap, star, stars, start, stop, submodule, t, tag, test, tst, un, uninstall, unlink, unpublish, unstar, up, update, v, version, view, whoami npm <cmd> -h quick help on <cmd> npm -l display full usage info npm faq commonly asked questions npm help <term> search for help on <term> npm help npm involved overview Specify configs in the ini-formatted file: /home/root/.npmrc or on the command line via: npm <command> --key value Config info can be viewed via: npm help config npm@1.4.9 /usr/lib/node_modules/npm
expressのインストール
npmが入っているので、npmでexpressをグローバルインストールします。
# npm install -g express
expressコマンドを使いたいのですが、そのままではパスが通っていません。どこにインストールされたのか調べます
# find / -name express /usr/lib/node_modules/iotkit-agent/node_modules/.bin/express /usr/lib/node_modules/iotkit-agent/node_modules/express /usr/lib/node_modules/iotkit-agent/node_modules/express/bin/express
どうやらグローバルインストールされたコマンドは.bin以下にシンボリックリンクが配置されるようなので、ここにパスを通します。ただしYoctoでは".bashrc"とか".bash_profile"ではなく、".profile"を読み込むようなので、ホームディレクトリに".profile"ファイルを作成して、そこにexportでパスを追加します。vimも無いようで、viエディタを使います。
# vi .profile
.profileにパスを追加します
export PATH=/usr/lib/node_modules/iotkit-agent/node_modules/.bin:$PATH
読み込み直します(rebootしてちゃんと読み込まれることを確認した方がいいでしょう)
# source .profile
パスを見てみます
# echo $PATH /usr/lib/node_modules/iotkit-agent/node_modules/.bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
expressでNode.jsサーバを作成
ここまで出来たら、あとはカンタンです。expressでフレームワークを生成して、Nodeを起動するだけです。テンプレートエンジンはejsを使います。
# express -e server create : server create : server/package.json create : server/app.js create : server/public create : server/public/javascripts create : server/public/images create : server/public/stylesheets create : server/public/stylesheets/style.css create : server/routes create : server/routes/index.js create : server/routes/user.js create : server/views create : server/views/index.ejs install dependencies: $ cd server && npm install run the app: $ node app # cd server && npm install # node app.js
ifconfigで調べたEdisonのIPアドレスにポート3000でアクセスしてみましょう。
というわけで、カンタンにWebサーバを起動させることができます。
Intel Edisonのセットアップ
買おうかどうしようか考えていましたが、なぜか手元にあるので、セットアップを備忘録的にまとめておきます。
Intel Edsionとは
IntelがIoT向けに発表した超小型でLinuxが動作するチップセット。購入して動かすには単体購入だけでは面倒なため、どちらかの拡張ボードと一緒に購入することになります。
本体はArduino Unoと比較しても小さいですね。
今回はBreakout Board Kitを入手したので、そのセットアップを書いておきます。Breakout Boad Kitはこんな感じです
起動
2つのマイクロUSBケーブルを用意します。1本は電源取得用。もう1本はいわゆるデバッグシリアル用です。
デバッグシリアルはFTDIドライバが入っていれば認識します。Macのscreenコマンドでボーレートを115200に設定しておき、電源をつなぐとしばらくすると起動ログが流れます。
そしてログイン画面(デバイス名をすでに変えているのでedison0となっています) LinuxのディストリビューションはYocto Projectです
初期ログインは"root" パスワードはありません。
ファームウェアのアップデート
購入直後(2014/10/28)時点のファームでは、文字入力時に最初の1文字を取りこぼすなど、不安定なのでまずはファームウェアを書き換えて最新にします。
バーションは以下のコマンドで確認できます
# cat /etc/version edison-rel1-maint-weekly_build_16_2014-10-14_14-56-19 <-- 書き換え後なので現時点の最新
Macでの書き換えはこのページに記述がありますが、以下に手順を
Edisonの電源をUSBケーブルでMacから取っていれば、Mac上にEdisonというUSBメモリがあるはずです。まずはその中味を全て削除しておきます。Mac上で
※削除コマンドは気をつけて下さい
$ cd /Volumes/Edison $ rm -rf * $ rm –rf \.*
ファームウェアはIntelの公式ページの最新版「Edison Yocto complete image」をダウンロードしてきます。てっきりイメージファイルかと思ったら、そうではありません。zipファイルを展開して、マウントされているEdisonのUSBメモリに展開されたファイルを全てコピーします。
今度はEdison上から、以下のコマンドでリブートさせつつ書き換えを行います
# reboot ota
自分の場合、2回リブートしたような気がしましたが、ファームウェアの更新が成功すれば、ログイン状態になります。書き換えが成功したかどうかは
# cat /etc/version
で確認しましょう。
セットアップ
最新の状態にしたら、Edisonの初期セットアップを行っておきます。設定項目は以下になります。
- Edisonのデバイス名を設定
- rootログインのパスワード設定
- WiFiの設定
セットアップ用のスクリプトが用意されているので、それを使います
# configure_edison --setup
以下設定画面(CUI)はこんな感じ
Configure Edison: Device Name Give this Edison a unique name. This will be used for the access point SSID and mDNS address. Make it at least five characters long (leave empty to skip): ----- Configure Edison: Device Password Enter a new password (leave empty to abort) This will be used to connect to the access point and login to the device. Password: *********** Please enter the password again: *********** ----- Configure Edison: WiFi Connection Scanning: 1 seconds leftt 0 : Rescan for networks 1 : Manually input a hidden SSID 2 : hogehoge 3 : fugafuga Enter 0 to rescan for networks. Enter 1 to input a hidden network SSID. Enter a number between 2 to 15 to choose one of the listed network SSIDs:
全ての設定が終了すると以下のメッセージが出ますので、まずはifonfigを叩いてIPアドレスが付与されているのを確認しておきましょう。
Initiating connection to honypod. Please wait... Attempting to enable network access, please check 'wpa_cli status' after a minute to confirm. Done. Please connect your laptop or PC to the same network as this device and go to http://192.168.**.** or http://edison0.local in your browser. root@edison0:~#
メッセージにもありますが、どうやら自動的にhttpサーバーが起動するようなのでアクセスしてみます。
設定したデバイス名とIPアドレスが表示されます
※どこかでSSHを有効にするかどうか聞かれたような気がするけど、覚えてない....