【LattePanda教學】lattepanda + Azure #2 上傳類比資料到Azure IoT hub

| 作者/攝影 | 曾吉弘 |
| 時間 | 3小時 |
| 難度 |
★★★★★ |
| 材料表 |
|
請參閱 [LattePanda教學] lattepanda + Azure #1 環境建置] 本文將介紹如何上傳類比資料(電位計)到Azure IoT hub,並可在 Visual Studio 與 Power BI 中檢視資料。
環境建置
硬體
在此有兩種做法,使用 Lattepanda 感測器套件 中的 Analog Rotation Potentiometer sensor 接在 Lattepanda 上的 Gravity 4號接腳上,請參考下圖:
或採用一般的電位計,中央腳位接到 Lattepanda 的A0腳位,另外兩隻腳一端接 5V,一端接GND即可,完成如下圖:
Lattepanda Arduino端
請上傳StandardFirmata 到 Lattepanda 的 Arduino 晶片中,別忘了 Board 要選 Arduino Leonardo,COM port 請根據 Windows裝置管理員為準,本程式中為 COM4。
Lattepanda Windows端:
請用 git client 下載 azure iot sdk,請安裝 Windows 版 git 工具之後在 powershell (cmd + r 之後搜尋 powershell )下執行以下指令把檔案下載到 lattepanda。在此我們預設的路徑皆為 C:\Users\Lattepanda:
git clone https://github.com/Azure/azure-iot-sdks.git
完成之後在其中的 /build 資料夾中找到 dev-setup.cmd,點擊兩下即可開始安裝程序。安裝完成之後可在同路徑下看到多了一個 node_modules 資料夾。
安裝Node.js所需套件
請在powershell 中執行以下指令,johnny-five serialport 這兩個是我們在 app.js 中會用到的套件:
npm install johnny-five serialport
建立Azure storage
Azure Storage
Azure Storage 是微軟所提供的雲端儲存方案,提供不同客戶在耐用性、取得性以及規模彈性上的各種需求,大到科學、金融用的數百TB以上資料,小到個人網站,都可在此完成。
請登入Azure portal,New -> Storage -> Storage account。並根據以下參數來建立 Storage account:
| Name:自行取一個名稱,在此為 cavedu
Storage service ecryption: Disabled Subscription: Free Trial (根據您帳號而定) Resource group: Create new 或 USe existing 都可以,在此我建立一個新的: cavedu location: 根據您所在地,在此我選 East Asia |
![]() |
建立完成後會看到以下畫面,請注意右下角的 URL 要記起來,後續與 Power BI 連接時會用到
Settings / Access Keys 下面的 key1 / key2也記起來,請任選一組輸入在後續要執行的程式中。
程式說明
Lattepanda Arduino端
請確認 StandardFirmata 程式已經上傳 Lattepanda 的 Arduino 晶片並執行,如果不確定的話可以再上傳一次或按下板子上的 Reset 鈕讓 Arduino 晶片重新執行程式。Arduino端程式一定要先執行,後續 app.js 去呼叫 Arduino COM port 時才可正常運作,否則就會顯示錯誤訊息。
Lattepanda Windows端
請開啟power shell,切換到您解壓縮 azure-iot-sdk的資料夾下,(我們的路徑是 C:\Users\lattepanda),使用您喜歡的編輯程式建立一個名為 app.js 的檔案並貼入以下內容。您只要修改 accountName、accountKey與tableName這三筆資料即可,如下圖:
app.js
var azure = require('azure-storage');
var five = require('johnny-five');
var accountName = 'XXX'; // Enter your Azure storage account name
var accountKey = 'OOO'; // Enter your Azure storage account key
var tableName = 'MyLightSensorData'; // Name of your table to store the light sensor data
var arduinoPort = 'COM4';// Enter your Arduino Port
var tableService = azure.createTableService(accountName, accountKey);
if (CreateTable()) {
InitializeBoard();
}
// Create a table in Azure storage
function CreateTable() {
tableService.createTableIfNotExists(tableName, function (error, result, response) {
if (error) {
console.log(error);
return false;
}
});
return true;
}
// Initialize the Arduino board with Johnny-Five
function InitializeBoard() {
var board = new five.Board({ port: arduinoPort });
board.on('ready', function () {
lightSensor = new five.Sensor({
pin: "A0",
freq: 30000 // 上傳間隔30秒
});
lightSensor.on('change', function () {
InsertValue(this.value);
});
});
}
function InsertValue(value) {
console.log('Value to insert: ' + value);
//建立一個實體,將光感測器值與日期存在資料表中
var entGen = azure.TableUtilities.entityGenerator;
var entity = {
PartitionKey: entGen.String('Light'),
RowKey: entGen.String(String(Date.now())),
intValue: entGen.Int32(value),
dateValue: entGen.DateTime(new Date().toISOString()),
};
//將實體插入Azure storage table
tableService.insertEntity(tableName, entity, function (error, result, response) {
if (error) {
console.log(error);
}
});
}
執行與檢視資料
請在powershell 下切換到您的您解壓縮 azure-iot-sdk的資料夾下(我們的路徑是 C:\Users\lattepanda),執行以下指令來執行程式。 /XX 資料夾之後執行以下指令:
node app.js
即可看到以下畫面,由console中可看到程式已經陸續上傳多筆資料了。後續我們會在 VisualStudio 中檢視您所上傳的資料。
回到 Azure portal 的 storage acount,在左側選單中找到 Activity log選項,可以看到方才執行的動作。
在 Visual Studio中檢視資料
開啟VisualStudio時,請點選Connect to Azure並用您的Azure帳號登入。
在工具列的 View 中,點選 Server Explorer
登入您的 Azure 帳號之後,即可在 Storage 選項中找到您所建立的 Table 名稱(MyLightSensorData),點選即可看到資料喔!
在PowerBI中檢視資料( 選用)
Power BI 是微軟提供的商務分析工具套件,可用來分析資料及共用深入資訊。透過自行定義或預設的儀表板模組,使用者可以匯入、分析並取得資料中的重要資訊,協助決策。
登入Power BI 之後,您可以在此檢視 Azure storage 中的資料,就是上述我們所執行的程式結果。請點選下圖的 [取得 資料庫],並登入您的 Azure 帳號再輸入您的 server 路徑以及資料庫名稱,完成之後即可看到同一筆資料了
參考資料:
- http://www.lattepanda.com/docs/#Send_light_sensor_data_to_Azure_storage
- https://developer.microsoft.com/en-us/windows/iot/samples/azuredatauploader
- 建議使用powershell,開啟方法:win+R快捷見,然後輸入powershell就可以開啟了
- 建議使用git,下載網址:https://git-scm.com/download/win
- 打開powershell,cd 到你想裝的地方,預設是直接在使用者的家目錄,然後輸入git clone https://github.com/Azure/azure-iot-sdks.git
- 根據教學文它需要把其中node的submodule一併裝進來,所以輸入:git submodule update --init -- node
這樣就搞定了
相關文章:
- LattePanda系列文章
- [LattePanda教學] lattepanda + Azure #1 環境建置
- [LattePanda拿鐵熊貓 ] 簡單使用LettePanda遠端遙控3D列印機不求人
- LattePanda 拿鐵熊貓教學:Arduino與Visual Studio環境設定
- LattePanda 拿鐵熊貓教學#2:讀取按鈕狀態,使用Visual Studio
- LattePanda 拿鐵熊貓教學#1:LED 閃爍,使用Visual Studio
本文發表於lattepanda 拿鐵熊貓、Microsoft Azure,並標記azure、iot、lattepante。





















留言 💬 (0)
還沒有留言,來當第一個。