Subscribing to actions

After publishing events for the device, it's time to define actions that need to be handled by the device. In our case, John, the user of the solution, wants to be able to water his plants whenever a defined soil moisture threshold is detected, whenever the temperature reaches a configurable value, or whenever he triggers an event to water the plant.

The water flow is controlled by the solenoid valve, which is opened and closed by the relay module. The following code is an update of the previous one, including the subscription for the water-the-plant action, which will open the valve for a minute.

  1. The starting point is to import all package dependencies, define the RESISTOR and THERMISTOR constants with predefined values, and load the configuration from device.json:
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 moistureSensor = new mraa.Aio(0);
var relayControl = new mraa.Gpio(2);
var RESISTOR = 100000;
var THERMISTOR = 4250;
  1. Then, create helper functions to transform sensor readings into usable values. The following functions are responsible for retrieving the sensor values from the actual device and transforming them into human-understandable values:
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;
};
var getSoilMoisture = function() {
var sensorReading = moistureSensor.read();
return sensorReading;
};
  1. The next step is to create a helper function to activate the solenoid valve, wait for the amount of time requested (the secondsToWater variable's value), and then deactivate the valve so watering will stop:
var waterPlant = function(secondsToWater) {
relayControl.write(1);
setTimeout(function() {
pinD2.write(0);
},secondsToWater * 1000);
  1. Connect to the IBM Watson IoT Platform and create a publishing function that will publish events to the platform every five minutes:
deviceClient.connect();
deviceClient.on('connect', function(){
console.log("connected");
setInterval(function function_name () {
deviceClient.publish('status', 'json', '{ "temperature": ' + getTemperature() +', "soilMoisture": ' + getSoilMoisture() + '}', 2);
},300000);
});
  1. And create a function that subscribes to the water event, triggering the waterPlant function:
deviceClient.on("command", function (commandName,format,payload,topic) {
if(commandName === "water") {
var commandPayload = JSON.parse(payload.toString());
console.log("Watering the plant for " + commandPayload.duration + " seconds.");
waterPlant(commandPayload.duration);
} else {
console.log("Command not supported.. " + commandName);
}
});

This concludes the coding of our device firmware so that it can perform the desired actions of our user, John.

..................Content has been hidden....................

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