在 PC 端使用Bluemix的 Internet of Things Foundation 双向传达接收远程装置信息

本文介绍如何利用IBM Bluemix平台和Internet of Things Foundation服务实现远程控制树莓派上的LED灯,并通过按钮触发短信通知。文章详细记录了从设置树莓派到部署应用程序的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在 PC 端使用Bluemix的 [Internet of Things Foundation] 双向传达接收远程装置信息

作者:陳兆麟 Email:chenlin2@ms9.hinet.net

IBM Bluemix针对物联网开发提供了完整开发工具与生态体系资源,例如IBM Internet of Things Foundation开发模块提供完整工具组合,提供功能强大的可扩充服务,可以轻松存取装置以及装置内的数据。更能够协助企业快速打造应用程序、可视化仪表板、行动应用,并且能将物联网数据藉由后端的应用程序进行分析协助开发者快速连结应用程序。IBM Internet of Things Foundation包含以下四大特点:连接:轻松注册并可安全的连接到应用程序;收集:收集来自各种对象的数据,并透过时间序列视图加以管理;组合:以可视化的方式将来自物联网的事件组合成为逻辑流程;管理:具有高度扩充性的服务,能管理连接与订阅。以下范例是在树莓派执行 client 端程序 (for python),同时在 bluemix 端 (server) 执行 web 控制程序;user可使用Win 7 Chrome 浏览器将 LED 亮灭指令经由 [IBM Internet of Things Foundation] 打开或关闭面包板 LED灯;亦可由面包板 button 的控制,经由 [IBM Internet of Things Foundation] 接收此一控制, 启动 server 端 [twilio] 服务将简讯传至指定手机;因为底层的 I/O动作皆已交由 [IBM Internet of Things Foundation] 服务处理, user 不必对硬件直接工作,只须少许程序代码与 [IBM Internet of Things Foundation]沟通,即可完成上述指定动作。

開發環境: win7, raspberry pi 2,bluemix,Internet of Things Foundation

1.使用win32diskmanager將下載的2016-03-18-raspbian-jessie.img 燒入樹莓派SD卡後開機.

(http://ftp.jaist.ac.jp/pub/raspberrypi/raspbian/images/raspbian-2016-03-18/2016-03-18-raspbian-jessie.zip)

2.在window PC 使用 putty 登錄樹梅派 (user:pi ; password:raspberry)

3. LED ,電阻, push button 在 Rpi 2接線圖

Rpi 2 GPIO 接脚图

4. Rpi 系統更新

$sudo apt-get update
$sudo apt-get upgrade

5. 安裝 Rpi GPIO library

$git clone git://git.drogon.net/wiringPi
$cd wiringPi
$./build

6. 安裝 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

7. 取得 Rpi deviceID

$service iot getdeviceid (取得 deviceID 類似:b827eba5b236)

8. 進入IBM Bluemix: https://console.ng.bluemix.net/ 點選 Sign Up

註冊後登錄 (30天試用期免費) 將 username 及 password 記錄

建立 Organization 名稱

建立 Space 名稱

確認 Organization, Space名稱

9.可選擇不同 Region (United Kingdom,Sydeny,US South)

10.瀏覽器輸入: https://console.ng.bluemix.net/,點選型錄 =>選擇 [ Internet of Things Platform] 服務

11.服務名稱=>iot-python,點選 [create]

12. 點選 iot-python

點選 [Launch dashboard]

13.點選 [device]

點選 [Add Device]

14.點選 [ Create device type ]

點選 [ Create device type ]

15.填選 Name = raspberrypi , [next] ->[create]

16.選擇 [device type] raspberry->Next

將步驟 7 DeviceID 填入

17.點選右下角 Next 至結束 Add 將取得以下類似數據 (請記錄)


###同時顯示對應實體裝置的Device 對應表

18.編輯 /etc/iotsample-raspberrypi/device.cfg , 將步驟17 內容貼入

$sudo vi /etc/iotsample-raspberrypi/device.cfg
org=qs138v
type=raspberrypi
id=b827ebabxxxx
auth-method=token
auth-token=*k2F_-EWS?YlvvWCN*

19.重啟動 Rpi 的IOT 驅動程式將 Rpi 內 CPU Data 傳至前項之 DeviceID

$sudo service iot restart

點選 Device ID

顯示 Rpi 傳來數據

20. 建立Access key (點選 ACCESS)


###點選 [API Keys]

###記錄 API key 及 Authentication Token

21.下載範例程式

$cd ~
$git clone https://github.com/IBM-Bluemix/python-iot-raspberry-pi.git
$cd python-iot-raspberry-pi

22.安裝 requirements.txt 所定義的 package

$sudo pip install -r requirements.txt

23.定義連結 [internet of thing foundation] 所需參數

$ vi ~/device.cfg (編輯以下標籤)
 [application]
 org=1eaxxx
 type=raspberrypi
 id=b827ebxxxxx
 auth-method=apikey
 auth-key=a-1eajjz-pjmxxx
 auth-token=guQu-su39Nf5xxxx
 domain=internetofthings.ibmcloud.com

24.啟動範例 (client 端)

$cd ~/python-iot-raspberry-pi
$python client.py (按下麵包板之 button,顯示Button Pushed,Ctrl+c 結束)

25.至 twilio申請帳號: https://www.twilio.com/try-twilio

26.登錄後需以手機接收認證碼 (一支手機對應一帳號)

27. 點選 [home] [Dashboard] 記錄 ACCOUNT SID , AUTH TOKEN, 點選 [phone Number]

點選 [Manager Numbers]

申請免費電話 (發話端,用來傳簡訊使用)

顯示免費電話號碼

設定權限可發送簡訊至台灣: 例如點選 Taiwan

https://www.twilio.com/console/sms/settings/geo-permissions

28. 至 bluemix CATALOG 點選 twilio 服務

29. service name:iot-twilio 將前項Account SID,Auth Token填入,點選CREATE

30. 至 bluemix CATALOG 點選 Runtimes ->python

31. Name 要與步驟 34 所定義一致, Host 須世界唯一

點選Environment Variables-> User defined -> Add ->填入DEVICE_ID(步驟 7), PHONE_NUMBER_FROM (步驟 27 免費電話號碼) => save

點選 [BIND A SERVICE OR API]

選擇 iot-python 及 iot-twilio bind => ADD

32.根據以下網址安裝 git (for win 7), 要在環境變數 path 設置路徑

https://git-for-windows.github.io/

根據以下網址安裝 cf (for win 7), 要在環境變數 path 設置路徑

https://github.com/cloudfoundry/cli/releases

33.在 Win7 terminal命令列,執行以下指令

$git clone https://github.com/IBM-Bluemix/python-iot-raspberry-pi.git
$ cd python-iot-raspberry-pi

34.更新 manifest.yml 之 name (不可使用範例之名稱,須與步驟 31同名)

35.上傳 server 端程式至 bluemix

不同 Region (United Kingdom,Sydeny,US South),不同選擇

api.ng.bluemix.net
api..au-syd.bluemix.net
api.eu-gb.bluemix.net

$cf login -a https://api.eu-gb.bluemix.net (登錄至 United Kingdom bluemix)
$cf push (上傳 server 程式)

36. 同步驟24.啟動Rpi範例 (client 端)

$cd ~/python-iot-raspberry-pi
$python client.py

37. bluemix 已完成安裝之 application 及 Service,

點選 jobs-iot-python-xxxxx.mybluemix.net

實驗操作步驟

(1) turn on, turn off (控制麵包板 led 燈亮滅)
(2) 將在twilio申請帳號的手機號碼及文字填入(測試用,其他手機必須付費)
(3) 點選 [save my phone number and text message]
(4) 按下麵包板button 則手機會收到 [my text] 簡訊

38. 若跳過 (1) 直接 (2) (3) (4) 次序會造成 server 當機,必須 RESTART APP

39.手機接收簡訊

附錄

  1. win32diskmanager 安裝及使用:
    http://chakra-zh.blogspot.tw/2012/04/windows-win32diskimager-live-usb.html
  2. putty安裝及使用:
    http://www.ascc.sinica.edu.tw/putty
  3. Rpi raspbian image 下載:
    http://ftp.jaist.ac.jp/pub/raspberrypi/raspbian/images/
  4. Rpi 校正時間 :
    $sudo raspi-config
    Internationalisation Options->change Time Zone ->Asia->Taipei
  5. Rpi 擴大檔案系統:
    $ sudo raspi-config -> Expand Filesystem -> 按Enter -> OK ->finish-> Yes (重開機)
  6. bluemix 全球系統維護現況
    https://developer.ibm.com/bluemix/support/#status

參考文獻:

  1. https://developer.ibm.com/bluemix/2015/04/02/tutorial-using-a-raspberry-pi-python-iot-twilio-bluemix/
  2. http://www.ithome.com.tw/pr/103228
  3. http://www.ithome.com.tw/pr/95725
  4. https://media.readthedocs.org/pdf/iotf/latest/iotf.pdf
  5. https://www.twilio.com/
  6. https://console.ng.bluemix.net/docs/services/IoT/index.html
  7. https://drive.google.com/folderview?id=0B7_CUSsY2cbnbm90UTNDWmcwaGs&usp=sharing
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值