将树莓派所撷取 sensor 数据存入 Bluemix Cloudant NoSQL database
作者:陳兆麟
Email:chenlin2@ms9.hinet.net
为了让树莓派的物联网系统开发者方便使用 IBM bluemix 所提供的 [internet of Things platform]服务, IBM提供了ibmiotf 模块,让开发者不必花时间为树莓派与其它装置的 I/O 交谈撰写程序,进而加速物联网系统应用的开发;网络上有许多 [bluemix + Arduino] 的范例,大部分都可以 Rpi 取代 Arduino,终究 Rpi 上面能跑的软件超多,user只要保留原先 bluemix 的架构,就可以扩大原先系统的应用规模,提升产值;以下范例就是以原 [bluemix + Arduino] 架构将 GPIO 的sensor 感测值储存于 [Cloudant NoSQL database],更换以 [bluemix + Rpi] 处理相同事件。
開發環境: win7, raspberry pi 2,bluemix,DHT22, HC-SR04
1.使用win32diskmanager將下載的2016-03-18-raspbian-jessie.img 燒入樹莓派SD卡後開機.
2.在window PC 使用 putty 登錄樹梅派 (user:pi ; password:raspberry)
3.Rpi 2接上 2 感測器 DHT22, HC-SR04 電路接線請參考
https://drive.google.com/file/d/0B7_CUSsY2cbnd3QtRVdZMktlem8/view?usp=sharing
https://drive.google.com/file/d/0B7_CUSsY2cbnaFJuWVA4Vmg1cE0/view?usp=sharing
4. Rpi 系統更新
$sudo apt-get update
$sudo apt-get upgrade
5. 安裝 Rpi連接 bluemix [Internet of Things Foundation]的服務library
$cd ~
$curl -LO https://github.com/ibm-messaging/iot-raspberrypi/releases/download/1.0.2/iot_1.0-1_armhf.deb
$sudo dpkg -i iot_1.0-1_armhf.deb
$sudo apt-get install python-dev (安裝 python)
$ git clone https://github.com/adafruit/Adafruit_Python_DHT.git
$ cd Adafruit_Python_DHT
$ sudo python setup.py install (安裝 Adafruit DHT library)
6.進入IBM Bluemix: https://console.ng.bluemix.net/ 點選 Sign Up; 註冊後登錄 (30天試用期免費) 將 username 及 password 記錄
7.建立 Organization 名稱
8.建立 Space 名稱
確認 Organization, Space名稱
9.可選擇不同 Region (United Kingdom,Sydeny,US South)
10.瀏覽器輸入: https://console.ng.bluemix.net/,點選型錄 =>選擇 [ Internet of Things starter]
Name:WeatherMonitoringSystem
產生 1 App , 2 service
11.點選WeatherMonitoringSystem1-iotf-service
點選 [Launch dashboard]
12.點選 [DEVICES]
點選 [Add Device]
13.點選 [ Create device type ]
點選 [ Create device type ]
14.填選 Name = WeatherDT , [next] ->[create]
15.選擇 [device type] WeatherDT ->Next DeviceID 填入WeatherID
16.點選右下角 Next 至結束 Add 將取得以下類似數據 (請記錄)
17. 建立Access key (點選 ACCESS)
點選 [API Keys]
記錄 API key 及 Authentication Token
18. 取得 Rpi deviceID & 安裝 ibmiotf 模組
$service iot getdeviceid (取得 deviceID 類似:b827eba5b236)
$cd ~
$git clone https://github.com/IBM-Bluemix/python-iot-raspberry-pi.git
$cd python-iot-raspberry-pi
$sudo pip install -r requirements.txt (安裝 ibmiotf 模組)
19.定義連結 [internet of thing foundation] 所需參數
$ vi ~/device.cfg (由步驟 16,17數據,編輯以下標籤)
[application]
org=1eaxxx
type= WeatherDT
id=b827ebxxxxx
auth-method=apikey
auth-key=a-1eajjz-pjmxxx
auth-token=guQu-su39Nf5xxxx
domain=internetofthings.ibmcloud.com
20. 下載tem+hum+dis+db.py (附錄6 ),經winscp (附錄3) copy至 /home/pi/python-iot-raspberry-pi,樹莓派將已擷取之溫度,濕度,距離感測值傳至IBM bluemist IoT service。
$ python tem+hum+dis+db.py &
21. [WeatherMonitoringSystem1-iotf-service] 服務顯示已擷取之感測值 (溫度,濕度,距離)
22.點選WeatherMonitoringSystem1.mybluemix.net=>[Go to your Node-RED flow editor]
23. 將原有預設 flow 刪除
24. input 調色盤選擇 [ibmiot],storage調色盤選擇[cloudant]拖曳至[workspace]
25.點選 [cloudant] node,填入以下資料
26.點選 [ibmiot] node,填入以下資料
27.點選[WeatherMonitoringSystem1-cloudant ] ->[Cloudant NoSQL DB] ->Launch
28.建立 weather-db 資料庫
29.點選 [Deploy] 將 IoT 服務所讀取之感測值存入資料庫
30.點選[Database]->[weather-db]->[Pen]
31.顯示已存於database之感測數據
32.整合以下網址範例可將感測數據顯示於 android 手機:
https://drive.google.com/file/d/0B8ToAuLFOH1fWXhlWnJyS1BLVDA/view?usp=sharing
附錄
1.win32diskmanager 安裝及使用:
http://chakra-zh.blogspot.tw/2012/04/windows-win32diskimager-live-usb.html
2.putty安裝及使用:
http://www.ascc.sinica.edu.tw/putty
3.WinSCP 簡易教學(Mini User Guide)
http://www.ascc.sinica.edu.tw/winscp
4.Rpi時間校正:
sudoraspi−config−>InternationalisationOptions−>changeTimeZone−>Asia−>Taipei擴大檔案系統:
sudo raspi-config -> Expand Filesystem -> 按Enter -> OK ->finish-> Yes (重開機)
5.Rpi raspbian image 下載:
http://ftp.jaist.ac.jp/pub/raspberrypi/raspbian/images/
6.tem+hum+dis+db.py (下載網址)
https://drive.google.com/file/d/0B16KfawnoTbmNk0tNHVvSktHX00/view?usp=sharing
7.bluemix 全球系統維護現況
https://developer.ibm.com/bluemix/support/#status
參考文獻:
- https://developer.ibm.com/bluemix/2015/04/02/tutorial-using-a-raspberry-pi-python-iot-twilio-bluemix/
- http://www.ithome.com.tw/pr/103228
- http://www.ithome.com.tw/pr/95725
- https://media.readthedocs.org/pdf/iotf/latest/iotf.pdf
- https://www.twilio.com/
- https://console.ng.bluemix.net/docs/services/IoT/index.html
- https://drive.google.com/file/d/0B8ToAuLFOH1fMlBNMjl1V2tkMjA/view?usp=sharing
- https://drive.google.com/file/d/0B7_CUSsY2cbnTW9PZVBHTnZLOEE/view?usp=sharing
- https://www.youtube.com/watch?v=24JAF9Eu1mk