Uploading the code

Since we are deploying the application to a Cloud Foundry environment, it's important to know that Cloud Foundry containers expect to have an HTTP port to be published by the container, so even though we are not using the container to expose HTTP resources, we're going to start an Express JS server.

The following code handles the events received from devices and publishes actions if any conditions are met.

  1. Again, the entry point of the code is to load module dependencies and gather the required configuration from the Cloud Foundry environment variables:
var express = require("express");
var cfenv = require("cfenv");
var Client = require("ibmiotf");
var minimumMoisture = parseInt(process.env.MINIMUM_MOISTURE);
var maximumTemperature = parseInt(process.env.MAXIMUM_TEMPERATURE);
  1. Then, load configuration data from the Cloud Foundry environment and generate connection configuration data:
var app = express();
var appEnv = cfenv.getAppEnv();
var iotConfig = appEnv.getService("Internet of Things Platform-mf");
var appClientConfig = {
"org": iotConfig.credentials.org,
"id": "hands-on-iot-app",
"auth-key": iotConfig.credentials.apiKey,
"auth-token": iotConfig.credentials.apiToken
}
  1. The next step is to connect to the IBM Watson IoT Platform and subscribe to target device events:
var appClient = new Client.IotfApplication(appClientConfig);
appClient.connect();
appClient.on("connect", function () {
appClient.subscribeToDeviceEvents();
});
appClient.on("deviceEvent", function (deviceType, deviceId, eventType, format, payload) {
var deviceData = JSON.parse(payload);
  1. Whenever an event is received from the subscription, the application checks whether the temperature reported by the device is higher or the soil moisture is below the thresholds defined. If so, a water event with a specified duration in seconds is published to the device so the watering valve is activated:
  if(deviceData.temperature > maximumTemperature ||
deviceData.soilMoisture < minimumMoisture ) {
console.log("Device, please water the plant for 60 seconds");
var actionData= { duration : 60 };
actionData = JSON.stringify(actionData);
appClient.publishDeviceCommand(deviceType, deviceId, "water", "json", actionData);
}
});
  1. And finally, start the Express server so the IBM Cloud SDK for Node.js container is started and monitored by the Cloud Foundry environment:
var port = process.env.PORT;
app.listen(port, function() {
console.log("App listening!");
});

To deploy the application, open the manifest.yml file and change the name attribute of the application. Then, open a command-line terminal, switch to the application base directory (the place where manifest.yml stands), and deploy the application using the bluemix CLI:

bluemix login

bluemix target -o <your_cloud_foundry_organization_name> -s <space_where_your_app_will_be_deployed>

bluemix cf push

After getting the successful deployment message, check the application logs using the bluemix CLI:

bluemix cf logs <your_application_name> 

The command will retrieve and display the log files from the Cloud Foundry application, as shown next. To ensure you can retrieve these logs, ensure that all application traces are being sent to stdout and stderr:

Log files from the Cloud Foundry application

Looking at the device logs, you can see that whenever any of the conditions were met, they got an action request to water the plants:

At this point, you already have an IoT application and device connected and working properly in the IBM Cloud environment.

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

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