2014年5月24日 星期六

Raspberry Pi 樹莓派 架設ShairPort使用AirPlay播放音樂

由於筆者的Pi上有外接音效卡接喇叭,拿Pi來當AirPlay的裝置似乎不錯用
以下說明怎麼安裝ShairPort讓Pi變成AirPlay裝置

1. 安裝ShairPort相依套件

pi@ShiunPiSnd ~ $ sudo apt-get install libssl-dev libavahi-client-dev libasound2-dev libao-dev avahi-utils
註: 由於筆者用的abrasive/shairport沒有使用到perl,所以不用特地去安裝perl相關套件

2. 建置並安裝ShairPort

# 下載ShairPort原始碼 pi@ShiunPiSnd ~ $ git clone https://github.com/abrasive/shairport pi@ShiunPiSnd ~ $ cd shairport # 檢查設定相依套件是否都安裝正確 pi@ShiunPiSnd ~/shairport $ ./configure Configuring Shairport OpenSSL found libao found PulseAudio or its dev package not found ALSA found Avahi client found getopt.h found CFLAGS: -I/usr/include/alsa -D_REENTRANT LDFLAGS: -lm -lpthread -lssl -lcrypto -lao -lasound -lavahi-common -lavahi-client Configure successful. You may now build with 'make' #開始建置 pi@ShiunPiSnd ~/shairport $ make #安裝到/usr/local/bin pi@ShiunPiSnd ~/shairport $ sudo make install

3. 測試ShairPort

將Pi模擬成AirPlay裝置,並設定名稱為AirPi pi@ShiunPiSnd ~/shairport $ shairport -a "AirPi"

i. Mac使用ShairPort
  壓著鍵盤上的option鍵對喇叭圖示點一下,選擇音源輸出為AirPi
  隨便播放音樂試試看~


ii. iPhone場合
  從畫面下方向上滑叫出控制中心,點選AirPlay後選擇AirPi


4. 設定為開機啟動

# 複製設定檔到相對應的位置 pi@ShiunPiSnd ~/shairport $ sudo cp -r scripts/debian/* /etc/
# 設定使用者為pi pi@ShiunPiSnd ~/shairport $ sudo vi /etc/default/shairport # User and group under whihc shairport should be run # user should have permission to output sound # Check the audio output documentation for details. #USER=shairport USER=pi #GROUP=nogroup
# 修正使用sudo service shairport發生錯誤問題 pi@ShiunPiSnd ~/shairport $ sudo vi /etc/init.d/shairport # User and group under whihc shairport should be run PATH=/sbin:/usr/sbin:/bin:/usr/bin DESC="Shairport Airtunes emulator" NAME=shairport #DAEMON=/usr/bin/shairport DAEMON=/usr/local/bin/shairport # 預設shairport安裝路徑應在/usr/local/bin下
# 設定shairport為開機啟動 pi@ShiunPiSnd ~/shairport $ sudo update-rc.d shairport defaults
重新啟動Pi以後就可以抓到AirPlay裝置啦~
注意:由daemon啟動的ShairPort預設裝置名稱為Pi的host name

參考資料

1. GitHub: abrasive/shairport
2. 使用Shairport 讓你的Raspberry Pi 變成AirPlay 音樂播放器

2014年5月22日 星期四

Raspberry Pi 樹莓派 JDownloader 免空下載器

這年頭難免會從免空上抓東西,而網路上的免空百百種e.g. Dropbox, Google Drive, mega, ... ,又樹莓派這東西省電不站空間掛網整天都無所謂,所以筆者就裝免空下載器啦~

至於為什麼選擇JD(JDownloader)呢? 筆者的原因是由於JD支援的空間多,而且遇到某些常用免空更新無法下載時,JD團隊會快速釋出更新修正問題

由於JD需要用到圖形介面,建議參考筆者 Raspberry Pi 樹莓派 開啟HDMI熱插拔與桌面環境,並透過VNC server進行主螢幕共享 的螢幕共享開啟遠端桌面後都用VNC去控制即可

廢話不多說,以下說明怎麼在樹莓派上裝JD
注意! 裝JD超級費時,建議睡前下安裝指令,等早上起來在把剩下的設定完成~


1. 從JD官方網站下載安裝程式



參考上圖官方網站的說明 // 用wget從官網抓安裝用script pi@ShiunPiSnd ~ $ wget http://212.117.163.148/jd.sh 接著由於Pi本身的記憶體只有512MB能玩,為了讓系統能順暢運作筆者設定JD最多只能使用256MB的記憶體 // 修改jd.sh內容 pi@ShiunPiSnd ~ $ vi jd.sh
# 將文件中所有的java -Xmx512m取代為java -Xmx256m,例如以下內容 java -Xmx512m -jar jdupdate.jar # 改為 java -Xmx256m -jar jdupdate.jar #筆者使用的版本20140521下載的jd.sh共有7處要修改 

2. 開始安裝JD

由於安裝需要圖形介面,因此請由桌面上的終端機執行以下指令
// 為jd.sh加入執行權限 pi@ShiunPiSnd ~ $ chmod +x jd.sh // 開始安裝! 此動作開始後需等待超過1小時! pi@ShiunPiSnd ~ $ ./jd.sh

3. 設定語系


4. 其他雜項設定

取消使用Firefox整合

JD提示的注意事項,按下cancel即可

外掛方面留下JDLightTray和JDWebinterface就好

5. JDWebinterface設定

由於JD在圖形化介面上執行頗慢的......,建議使用開啟此外掛,之後就可以透過PC上的瀏覽器去控制JD
設定方式如下圖,主要是設定登入的port和帳號密碼

6. 修改預設下載位置

建議預設下載位置改為外接硬碟,避免SD卡一下子就被塞爆XD,至於SD卡被塞爆會發生什麼事呢? 很恐怖,不要問!

7. 重新啟動JD確定設定生效

請勿直接按右上的X關閉,而是要透過"檔案 -> 離開"才能關閉
啟動方式請開啟終端機並輸入./jd.sh在等個1分鐘左右即可

8. 網頁介面

接著筆者使用筆電上的瀏覽器開啟JD網頁介面

再來輸入第5步設定的帳號密碼

最後就可以控制下載介面啦~

最後這下載介面要如何使用就請觀眾們自行摸索了~


參考資料
1. JDownloader官方網站
2. 安裝 jdownloader 在 Raspberry Pi wheezy 系統上

2014年5月21日 星期三

使用CodeIgniter架設RESTful Web Service

完成Apache + PHP的架設後,筆者使用CodeIgniter架設RESTful web service

環境說明

1. 網站在根目錄位置 ~/Sites  (可參考Mac OS X 啟動內建Apache + PHP (Apache篇) )
2. 網站的URL http://127.0.0.1/~shiun
3. Web service的網頁位置 ~/Sites/ws
4. Web service的URL http://127.0.0.1/~shiun/ws

1. 安裝CodeIgniter

官網下載,目前的版本是2.1.4
解壓縮並放到網站根目錄,接著把資料夾名字改成網站名稱(筆者以ws作為網站目錄)

用瀏覽器開啟http://127.0.0.1/~user/ws 試試看CodeIgniter能不能正常執行


2. 設定CodeIgniter Rest Server

從GitHub上下載codeigniter-restserver並放到家目錄中,解壓縮後以下檔案放到對應的資料夾中
//切換到解壓縮後的codeigniter-restservice下 Shiun@ShiunMac ~ cd codeigniter-restserver-master //複製Format.php到網站資料夾下 Shiun@ShiunMac ~/codeigniter-restserver-master cp application/libraries/Format.php ~/Sites/ws/application/libraries/ // 同上,複製REST_Controller.php和reset.php到對應的目錄下 Shiun@ShiunMac ~/codeigniter-restserver-master cp application/libraries/REST_Controller.php ~/Sites/ws/application/libraries/ // rest.php Shiun@ShiunMac ~/codeigniter-restserver-master cp application/config/rest.php ~/Sites/ws/application/config/
將web service預設的輸出格式改成JSON
// 移動到網站資料夾 Shiun@ShiunMac ~/codeigniter-restserver-master cd ~/Sites/ws // 修改rest.php Shiun@ShiunMac ~/Sites/ws vi application/config/rest.php

3. 撰寫Web Service程式碼

使用codeigniter-restserver撰寫程式碼只需要在function名稱的後面加上對應的_get, _post, _put, _delete即可
例如實作index_get, index_post的function後即可對http://127.0.0.1/~shiun/ws/index.php/service/index 進行GET和POST的request

廢話不多說,以下是GET, POST, PUT, DELETE的範例

i. GET

Shiun@ShiunMac ~/Sites/ws vi application/controller/Service.php
<?php
require APPPATH.'/libraries/REST_Controller.php';

class Service extends REST_Controller {
    public function index_get() {
        $data = array(
            "key1" => "value1",
            "key2" => "value2",
            "Method" => "GET",
        );
        $this->response($data);
    }
}
?>

ii. POST

    public function index_post() {
        $value1 = $this->post("key1");
        $value2 = $this->post("key2");
        $res = array(
            "key1" => $value1,
            "key2" => $value2,
            "Method" => "POST",
        );
        $this->response($res, 200);
    }

iii. PUT

    public function index_put() {
        $value1 = $this->put("key1");
        $value2 = $this->put("key2");
        $res = array(
            "key1" => $value1,
            "key2" => $value2,
            "Method" => "PUT",
        );
        $this->response($res, 200);
    }

iv. DELETE

    public function index_delete($id) {
        $res = array(
            "id" => $id,
            "Method" => "DELETE",
        );
        $this->response($res, 200);
    }

4. 測試RESTful web service是否架設成功

筆者使用Firefox的外掛RESTClient測試以上的程式是否正確
啟動RESTClient後請先加入Content-Type: application/json這個header


加入成功後會顯示以下畫面

接著首先來測試GET
  選擇Method: GET並填上URL: http://127.0.0.1/~shiun/ws/index.php/service/index 再來按下Send即可在Response欄位中看到結果

再來測試POST
  選擇Method: POST,URL維持不變但body中填入JSON格式的key1和key2參數,接著按下SEND就可以看到結果~

剩下的PUT和DELETE也可以透過一樣的方式測試因此筆者不再贅述

參考資料
1. CodeIgniter繁體中文官方網站

2014年5月19日 星期一

Mac OS X 啟動內建Apache + PHP (PHP篇)

前一篇的Mac OS X 啟動內建Apache + PHP (Apache篇)中已經說明如何在Mac上開啟內建的Apache作為網站server

接下來說明怎麼將PHP模組載入

1. 開啟Apache設定模組
Shiun@ShiunMac ~ sudo vi /etc/apache2/httpd.conf

找到PHP模組並取消註解 (砍掉前面的'#'就對了XD)
LoadModule php5_module libexec/apache2/libphp5.so

2. 重啟Apache
Shiun@ShiunMac ~ sudo apachectl restart

3. 寫個測試用的php檔吧~
Shiun@ShiunMac ~ vi ~/Sites/test.php
<?php
phpinfo();
?>

接下來開啟網頁http://127.0.0.1/~user/test.php,經典的phpinfo網頁出現~~~

4. 最後建立php的設定檔php.ini吧
檢查/etc下有沒有php.ini這個檔案,確定沒有就把/etc/php.ini.default複製到/etc下吧
Shiun@ShiunMac ~ sudo cp /etc/php.ini.default /etc/php.ini

打完收工~

參考資料

2014年5月18日 星期日

Mac OS X 啟動內建Apache + PHP (Apache篇)

筆者最近弄的東西需要PHP支援,因此需要在手邊的電腦上架Apache + PHP來用(謎之音: 直接在server上直接改程式就好啦 (被毆) )

由於Mac OS X本身就有UN*X的特性,因此系統內建Apache也不奇怪。以下說明如何開啟內建的Apache

1. 開啟內建的Apache

Shiun@ShiunMac ~ sudo apachectl start 直接指令後就可以開瀏覽器看http://localhost or http://127.0.0.1 就能看到這經典畫面: It works!

2. 建立使用者網頁資料夾

預設的網頁資料夾在/Library/WebServer/Document
不過筆者的目的是架測試環境而已,因此筆者決定在自己家目錄下建網頁資料夾
Shiun@ShiunMac ~ mkdir ~/Sites
接著建立測試網頁
Shiun@ShiunMac ~ vi ~/Sites/test.html
<html>
<head></head>
<body>
Test work!
</body>
</html>

測試打開Chrome一看403 Forbidden.........這哪招Orz


3. 開啟使用者網頁資料夾權限

403是因為沒有指定改資料夾的路徑與權限,因此要進行Apache的設定
檢查是否有使用者設定檔存在,若沒有則新增一份
Shiun@ShiunMac ~ cd / etc/apache2/users Shiun@ShiunMac ~ ls -al
ls -al的結果是空的代表還沒有設定過
新增設定檔內容
Shiun@ShiunMac ~ vi /etc/apache2/users
<Directory "/Users/你的使用者名稱/Sites/"> Options Indexes MultiViews AllowOverride All Order allow,deny Allow from all </Directory>

筆者的設定如下圖所示

接下來重啟Apache再使用瀏覽器就能看到正常畫面了~

今天先到此這裡吧~





參考資料

2014年5月14日 星期三

Raspberry Pi 樹莓派 開啟HDMI熱插拔與桌面環境,並透過VNC server進行主螢幕共享

今天正式開始執行Pi家庭劇院化!

筆者的Pi環境如下
  OS:Raspbian 2014-01-07

由於筆者都已經用Pi接上HDMI輸出桌面,實在沒有理由再去用vnc server開其他虛擬桌面,所以筆者決定先搞個HDMI熱插拔、開啟桌面環境與螢幕共享,以下就來說明怎麼實現吧~

HDMI熱插拔

  啟動HDMI hot plugin功能:開啟SD卡插入電腦的讀卡機中,修改config.txt文件將hdmi_force_hotplug=1註解取消


小抱怨: 由於筆者這裡的HDMI switch有點秀斗,有時候會抓不到Pi的HDMI訊號,又Pi不支援hot plugin(即開機時若沒接上HDMI,HDMI就無法啟動)就只好開啟HDMI hot plugin了Orz

開啟桌面環境

預設的Pi是指令模式的,需要透過指令啟動桌面環境
1. 進入Pi的設定畫面
pi@server ~ $ sudo raspi-config

2. 選擇3. Enable Boot to Desktop/Scratch Choose whether to boot into a desktop environment, Scratch, or the command-line

3. 選擇Desktop Log in as user 'pi' at the graphical desktop

4. 選擇完後會回到主選單,接著選擇Finish並Reboot

接著重開機完畢後就可以看到桌面環境了~

螢幕共享(Screen Sharing) [2]

通常透過vnc server可以開啟多個虛擬桌面,但筆者希望遠端連進來的桌面都是同一個,因此就把主螢幕的桌面共享吧
1. 安裝套件vino
pi@server ~ $ sudo apt-get install vino

2. 開啟vino設定工具
pi@server ~ $ vino-preferences
勾選以下三個選項並輸入遠端登入的密碼
  • Allow other users to view your desktop
  • Allow other users to control your desktop
  • Require the user to enter this password: xxxxxxxx



3. 接著來建立開機啟動的檔案
pi@server ~ $ sudo vi /etc/sudoers.d/vsrv.sh
加入以下內容
#! /bin/bash
/usr/lib/vino/vino-server

開啟執行權限
pi@server ~ $ sudo chmod +x /etc/sudoers.d/vsrv.sh

4. 要求Pi開啟桌面環境(LXDE)時啟動vino
pi@server ~ $ sudo vi /etc/xdg/lxsession/LXDE/autostart


2015/02/25 update: Raspbian 2014-12-24後的版本有更新桌面系統,要在啟動桌面環境時啟動vino需改為修改以下檔案
pi@server ~ $ sudo vi /etc/xdg/lxsession/LXDE-pi/autostart


加入以下內容到最後一行
@/etc/sudoers.d/vsrv.sh

5. 重開機後就完工啦~
pi@server ~ $ sudo reboot



參考資料

2014年5月13日 星期二

Raspberry Pi 樹莓派 螢幕擷取

最近在玩Raspberry Pi這小東西,目標是要把Raspberry Pi弄成AirPlay音樂播放器、影片播放器、NAS、免空下載器。 因此先試著練習怎麼玩螢幕截圖以便做筆記

1. 下載螢幕截圖套件: scrot
pi@server ~ $ sudo apt-get install scrot
2. 使用scrot指令截圖
pi@server ~ $ scrot
接著就能看到這精美的截圖啦~


Q. 如果想透過指令截圖又不想截到下指令的視窗要怎麼處理呢?
A. 使用-cd (Cool Down) 參數就行惹~
-c讓scrot在終端機倒數剩下幾秒
-d代表延遲幾秒後才截圖

詳細指令為
pi@server ~ $ scrot -cd 5 過5秒後scrot就會自動截圖,只要趁這5秒內把下指令的視窗縮小就好了~

除此之外scrot還支援不少參數可以玩,有興趣的朋友們可以去研究一下~