Firmware development

The following code has been borrowed from Chapter 2, Creating Your First IoT Solution, since it has the same hardware characteristics and the same objectives.

The only modification is in the published JSON: we have to remove the soilMoisture property and add the box ID when transporting from the factory to the warehouse, and add the pallet ID when transporting from the warehouse to the store. 

It retrieves the temperature of the Grove sensor and publishes it to the Watson IoT platform:

var iotf = require("ibmiotf");
var mraa = require('mraa');
var config = require("./device.json");
var deviceClient = new iotf.IotfDevice(config);
var temperatureSensor = new mraa.Aio(3);

var RESISTOR = 100000;
var THERMISTOR = 4250;

var getTemperature = function() {
var sensorReading = temperatureSensor.read();
var R = 1023 / sensorReading - 1;
R = RESISTOR * R;
var temperature = 1 / (Math.log(R/RESISTOR)/THERMISTOR+1/298.15)-273.15;
return temperature;
};

deviceClient.connect();
deviceClient.on('connect', function(){
console.log("connected");
setInterval(function function_name () {
// When transporting from Factory to Warehouse
deviceClient.publish('status','json','{ "foodBoxId":"1", "temperature":+ getTemperature()}', 2);

// When transporting from Warehouse to Store
// deviceClient.publish('status','json','{ "palletId":"1", "temperature":+
// getTemperature()}', 2);

},300000);
});
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.237.29