Step 2: Set up the hardware
Step 3: Build and Run the sample
var azure = require('azure-storage');
var five = require('johnny-five');
var accountName = ''; // Enter your Azure storage account name
var accountKey = ''; // Enter your Azure storage account key
var tableName = 'MyLightSensorData'; // Name of your table to store the light sensor data
var arduinoPort = 'COM3';// 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: 10000 // 10 seconds
});
lightSensor.on('change', function () {
InsertValue(this.value);
});
});
}
function InsertValue(value) {
console.log('Value to insert: ' + value);
// Create entity to store in the table with the value
// of the light sensor and the date.
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()),
};
// Insert the entity in the Azure storage table
tableService.insertEntity(tableName, entity, function (error, result, response) {
if (error) {
console.log(error);
}
});
}
npm install azure
npm install johnny-five
node app.js
Source: