與 IoT 相關的雲端服務為數眾多,例如:ThingSpeak、WoT.City、AWS、IBM Bluemix、MediaTek MCS、Google Firebase 等,並在透過這些雲端服務可在對資料進行資料視覺化、資料分析與其它的應用,而本文透過 Raspberry Pi 將溫溼度資料傳送到 IBM Bluemix 。
執行步驟
前置準備作業
1.準備 Raspberry Pi 3 Model B 開發板
2.準備 Micro SD (已安裝 Raspbian) - 【 透過 MacOS 安裝 Raspbian 教學 】
3.將 Micro SD 裝入到 Raspberry Pi 中
4.已設定網路
5.安裝 GrovePi+
6.安裝 Grove – Temperature and Humidity Sensor (D4)
7.安裝 GROVE - LCD RGB BACKLIGHT (I2C-2)
8.安裝 IDE ( Sublime Text or Visual Studio Code )
9.Windows 平台,請安裝 Putty 與 FileZilla
http://oranwind.org/-linkit/
10.其它前置作業
請參考 [ Raspberry Pi ] 透過 Python 顯示溫溼度資訊到 Grove LCD
Sensor 、 LCD 、 GrovePi+ 與 Raspberry Pi 連接圖
IBM Bluemix 端 - Publish
Step 1. 到 IBM Bluemix 網站申請帳號
https://console.ng.bluemix.net/
Step 2. 登入 IBM Bluemix 網站
Step 3. 點擊上方 型錄
Step 4. 點擊左邊 物聯網
Step 5. 點擊 Internet of Things Platform
Step 6. 點擊右邊 建立
Step 7. 點擊 啟動儀表板
Step 8. 點擊左邊 裝置
Step 9. 點擊右邊 新增裝置
Step 10. 點擊 建立裝置類型
Step 11. 點擊 建立裝置類型
Step 12. 輸入 名稱
➙ 輸入完後點擊右下方的 下一步
Step 13. 點擊右下方的 下一步
Step 14. 點擊右下方的 下一步
Step 15. 點擊右下方的 建立
Step 16. 點擊右下方的 下一步
Step 17. 輸入 裝置 ID
➙ 輸入完後點擊右下方的 下一步
Step 18. 點擊右下方的 下一步
Step 19. 點擊右下方的 下一步
Step 20. 點擊右下方的 新增
Step 21. 將裝置認證資訊記錄下來
Raspberry Pi 端 - Publish
Step 1. 透過 Python 讀取溫溼度資訊
- 更新程式中 deviceOptions 的
org
、type
、id
、auth-token
欄位
#****************************************************
# Import Package
#****************************************************
import time
import sys
import ibmiotf.application
import ibmiotf.device
import json
sys.path.append('/home/pi/rpi/code/Package')
import grovepi
from grove_rgb_lcd import *
#****************************************************
# Set IBM Bluemix Config, Pin No
#****************************************************
sensor = 4
blue = 0 # The Blue colored sensor.
white = 1 # The White colored sensor.
deviceOptions = {
"org": "組織 ID",
"type": "裝置類型",
"id": "裝置 ID",
"auth-method": "token",
"auth-token": "鑑別記號"
}
#****************************************************
# Set IBM Bluemix Connection
#****************************************************
try:
deviceCli = ibmiotf.device.Client(deviceOptions)
except Exception as e:
print("Caught exception connecting device: %s" % str(e))
sys.exit()
deviceCli.connect()
#****************************************************
# Publish Event To IBM Bluemix
#****************************************************
while True:
[temperature,humidity] = grovepi.dht(sensor,blue)
print("temp = %.02f C humidity =%.02f%%"%(temperature, humidity))
msg = json.JSONEncoder().encode({"d":{"Temperature":temperature, "Humidity":humidity}})
print "send payload: " + msg
deviceCli.publishEvent("TemperatureHumidity", "json", msg)
time.sleep(1)
Step 2. 將 Python Code 傳送到 Raspberry Pi
- 請參考 傳送檔案與登入到開發板
Step 3. 安裝相關套件於 Raspberry Pi
sudo pip install ibmiotf
Step 4. 執行剛傳到 Raspberry Pi 中的 Python Code
python 檔名.py
Step 5. Console 執行畫面
- IBM Bluemix 上所看到的資訊
- 查看資料內容
IBM Bluemix 端 - Subscribe
Step 1. 點擊左邊的 應用程式
Step 2. 點擊右邊的 產生 API 金鑰
Step 3. 產生 API 金鑰
- 記錄
API 金鑰
與鑑別記號
後點擊右下角的Generate
Step 4. 透過 Python 來執行 Subscribe
- 更新程式中 deviceOptions 的
org
、id
、auth-key
、auth-token
欄位
import signal
import time
import sys
import json
import ibmiotf.application
def myEventCallback(myEvent):
print("%-33s%-32s%s: %s" % (myEvent.timestamp.isoformat(), myEvent.device, myEvent.event, json.dumps(myEvent.data)))
def interruptHandler(signal, frame):
client.disconnect()
sys.exit(0)
options = {
"org": "組織 ID",
"id": "裝置 ID",
"auth-method": "apikey",
"auth-key": "API 金鑰",
"auth-token": "鑑別記號"
}
try:
client = ibmiotf.application.Client(options)
client.connect()
except Exception as e:
print(str(e))
sys.exit()
print("(Press Ctrl+C to disconnect)")
client.deviceEventCallback = myEventCallback
client.subscribeToDeviceEvents()
while True:
time.sleep(1)
Step 5. 執行 Subscribe 的 Python Code
python 檔名.py