DFRobot
Published © GPL3+

Send Light Sensor Data to Azure Storage

It shows how to send data from a light sensor to Azure storage.

BeginnerProtip30 minutes503
Send Light Sensor Data to Azure Storage

Things used in this project

Hardware components

LattePanda 2GB/32GB (Unactivated)
LattePanda 2GB/32GB (Unactivated)
×1

Software apps and online services

Azure data uploader

Story

Read more

Code

sample

C/C++
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);
  }
});
}

Credits

DFRobot
66 projects • 153 followers
Empowering Creation for Future Innovators
Contact

Comments

Please log in or sign up to comment.