Friday, October 31, 2014

Bluemix UI Updates: No Tricks and All Treats!

Bluemix UI Updates: Bluemix Pumpkin

Trick or treat! Halloween is here in the U.S., and we have a fresh round of Bluemix announcements and Bluemix UI updates. Keep reading for details on all of the new "treats" this month:
  • New IBM and Twitter partnership
  • Addition of Ustream Service
  • Introduction of IBM DataWorks and IBM dashDB Services
  • New Object Storage Service
  • General Availability of Internet of Things Foundation
  • Updated Buildpacks for Java and Node.js
  • Enablement of High Availability for Data Cache and Session Cache Services
  • Enhanced UI functionality, such as:
    • Improved Catalog Filtering
    • Enhanced Pricing Overview
    • Removal of "Add-on" Terminology
    • Miscellaneous Usability improvements and Defects
Bluemix UI Updates: Catalog Filtering

Saturday, October 11, 2014

Node.js on Bluemix: Easier Local Development with cfenv

Updated: Nov. 22, 2014 to reflect deprecation of env.log.

I'm a fan of the cfenv package for Node.js written by Patrick Mueller. It parses environment info in a Bluemix (or Cloud Foundry) application, and provides functions to make it easy to retrieve all of the service data you need from VCAP_SERVICES. It also gives access to other important attributes for port, host name/ip address, URL of the application, etc. On top of that, it detects whether your app is running locally or in the cloud. And, when running locally, it provides handy defaults.

I've written a simple wrapper (a local module called cfenv-wrapper) to make local development of Bluemix/Cloud Foundry apps a little easier. My code parses a local copy of your app's environment data, and extracts the data for VCAP_SERVICES and VCAP_APPLICATION. Then, it passes that information to cfenv's initialization function, getAppEnv. After this initialization, you can use the same cfenv interface just as if you were running in the cloud.

The local environment data can be in JSON or properties files meeting the following requirements:
  • env.json -- JSON file with VCAP_SERVICES info as retrieved by cf env. If env.json is present, the code will also load env_custom.json, which should hold JSON representing your app's user-defined environment variables.
  • env.log file (deprecated) -- If there's no env.json file, the wrapper will load a local copy of your app's env.log file. This is provided for backwards compatibility with earlier versions of cfenv-wrapper. After CF 182, CF stopped providing an env.log file for security reasons.
NOTE: Many cloud services can be accessed with no changes when running locally. In Bluemix, a partial list of these includes Cloudant, Pitney Bowes, and Twilio. In those cases, you can use env.json and env.log with the exact data provided by CF. However, there are services that don't yet allow connections from outside of Bluemix. For those services, you would need to modify your local file so that it uses info specific to installations of those services in your local environment.

New Functions

My wrapper also adds two new functions, not in the original cfenv interface:

  • getEnvVars -- returns JSON data structure containing all environment variables. When running locally, it returns the data from the env_custom.json or env.log files. When running in the cloud, it will return the standard process.env runtime variable.
  • getEnvVar(name) -- returns value of the environment variable with the given name. When running locally, it will try to pull the value from the env_custom.json or env.log files. If it can't find it, it falls back to the standard process.env runtime variable.
These serve to give you a consistent interface for environment variable resolution in both environments.

Code on Github

The code for cfenv-wrapper (and a working sample) is available on GitHub:
The repository's README file contains full details on how to get the code and run either locally or in Bluemix.

Run Live Code

You can run a working version of the code deployed to Bluemix by using the link below:When you run it, you should see something like the following screenshot split into three sections for Base Info, Services, and Environment Variables. The Services section shows the names of any services the app is bound to. The Environment Variables section shows the standard environment variables common to Cloud Foundry apps plus any custom environment variables. NOTE: The code intentionally prunes VCAP_SERVICES from the Environment Variable output because it typically contains sensitive service credentials. Screenshot of Running cfenv-wrapperBelow, you can see the details for my app in the Bluemix UI. Notice the list of services matches the cfenv-wrapper output:
Bluemix UI: App Details for cfenv-wrapperAlso, for this deployment of the app, I've set three custom environment variables (e.g., CUSTOM_ENV_VAR1, CUSTOM_ENV_VAR2, and CUSTOM_ENV_VAR3). Bluemix UI: Environment Variables for cfenv-wrapperYou can set custom environment variables either in the UI (above) or via the command line:
cf set-env cfenv-wrapper CUSTOM_ENV_VAR1 "Value 1"

Getting Environment Info For Local System

JSON Format
To see a JSON representation of your app's environment data, you can run the following cf command:
cf env APP_NAME
Then, copy the JSON (which starts right after the System-Provided header and ends just before the User-Provided header) into a file named env.json. This file should be in the same place on your local file system that you put the code. That is, as a peer to the server.js file.

If you have user-defined environment variables, put them into a file name env_custom.json. For example, if cf env shows the following user-provided environment variables:
Then, you would want your env_custom.json file to look like this:
    "CUSTOM_ENV_VAR1": "Value 1",
    "CUSTOM_ENV_VAR2": "Value 2",
    "CUSTOM_ENV_VAR3": "Value 3"
env.log Format
NOTE: This section is only applicable if running a version of CF earlier than 182.

To see the contents of env.log, you can run the following cf command:
cf files APP_NAME logs/env.log
Then, copy the output into a file named env.log to the same place on your local file system that you put the code. That is, as a peer to the server.js file.

The Code

The server.js file is shown below. It initializes the cfenv-wrapper and then uses it to retrieve and display base info, names of services bound to the app, and environment variables.

The cfenv-wrapper.js file is shown below. If running locally, it will try to parse env.json in the working directory. If found and successfully parsed, it will then initialize cfenv. If, not it will try to load and parse env.log. If there's no success using env.json nor env.log, then the wrapper will just use cfenv with no special initialization. In addition to this initialization, the code provides functions that "wrap" the functions of cfenv.

Wednesday, October 8, 2014

Bluemix UI Updates: Say Hello to Watson!

twoLogos Bluemix UI Updates: Say Hello to Watson!

We’re pleased to announce another round of Bluemix UI updates. This refresh is especially exciting as we welcome IBM® Watson™ services to the Bluemix Catalog. With this addition, you can now rapidly prototype and build cognitive apps in the cloud!

Along with Watson, we’ve made a number of other major improvements since our August release. These include:

  • Upgrade of three services from beta to general availability: Business Rules, Delivery Pipeline, and MQ Light
  • Update of the Cloud Integration add-on, including the ability to create private services
  • Tighter integration of organizations and spaces into the Dashboard
  • Addition of blog and Twitter feeds to the Bluemix landing page
  • Single sign-on with Bluemix Documentation to provide more personalized sample code snippets
  • Numerous usability improvements and bug fixes

To find out the complete details, READ MORE in my complete post on the Bluemix Developers Blog.

Also, be sure to check out What will you build with Watson? by Nathan Vega.
Bluemix UI Updates: Watson Comes to Bluemix

Sunday, October 5, 2014

IBM Bluemix and IoT: Real-Time Heart Rate and Geolocation

MIO Alpha I Strapless Continuous Heart Rate MonitorSome of my peers in IBM's Emerging Technology group have been creating some really cool Internet of Things (IoT) demos using IBM Bluemix. Two of them, HR Tracker and Race Tracker, are geared toward fitness and activity tracking. They are both capable of displaying the heart rate and geolocation of exercisers as the values change in real-time. In this post, I'll describe both sample apps and give an overview of the technology used to build them.

HR Tracker

Last month, I got to be part of an HR Tracker demo at That Conference 2014. For my part, it was a pretty simple process. First, I installed the demo app on my iPhone. Then, I strapped on a brand new, wrist-worn MIO Alpha I Strapless Continuous Heart Rate Monitor, turned it on, and paired it with the phone via Bluetooth. Finally, I hit the Start button in the app. And, before I knew it, a new icon popped up in colleague Mark VanderWiele's browser showing my heart rate and position. Pretty cool! HR Tracker ScreenshotAnd, even cooler, as my heart rate changed on the MIO Alpha, I could see it update almost immediately in the web app. HR Tracker Web App Showing Real-time Heart RateAt this point, I went out for a short walk. While I was gone, Mark could see exactly where I was and how "hard" I was working (via heart rate) in the parking lot and side streets around the Kalahari Resort.

Race Tracker

Race Tracker is similar to HR Tracker, but is geared more toward monitoring data for lots of participants in an event. This morning Bryan Boyd took part in the Medtronic Twin Cities Marathon up here in Minnesota. Of course, running a marathon is way on the other side of the intensity spectrum from my little walk. :)

Via the Race Tracker app (written mostly by Bryan), I was able to "watch" his progress during the race. The animated GIF below (sped up to reduce size) shows Bryan's data updating at he crossed the 20-mile point (on the St. Paul side of the Mississippi River): Race Tracker w/ Bryan Boyd in MarathonI then switched to the satellite view and caught him still going strong at Mile 21: Race Tracker w/ Bryan Boyd in Marathon (Satellite)Race Tracker can also send notifications at pre-determined intervals. For example, the tweet below came through the feed as Bryan reached 24 miles:
So, did Bryan finish? I wish I knew! Around Mile 25, data updates stopped being shown in the web UI. I'm assuming his heart kept beating. So, did the battery on his phone or heart rate monitor run out? Once I find out, I'll try to post an update. :)

Update: I heard from Bryan, and his heart is indeed still beating. He blamed the glitch on "developer error." But, as a fellow developer, I know those things happen with prototypes. :) Oh, and by the way, Bryan also finished the race in a very respectable 3:57.


On the day I tried HR Tracker, Mark conducted a session called From Concept to Operation in 30 minutes Using Services and a PaaS. During his talk, he went through the steps needed to build a Bluemix-based backend for these apps. It was a great demonstration of how quickly the "next big idea" can be prototyped and delivered to run on Bluemix.

Architecturally, MQTT ties all of the components together. Every piece either publishes or subscribes to an MQTT Broker (in this case powered by IBM MessageSight). So, for example, when the iPhone app detects a change in heart rate or GPS position, it publishes a message with that information to the MQTT Broker. The MQTT Broker in turn sends the messages to all subscribers.

As you might have guessed, the Bluemix-based backend is a subscriber. Mark started by creating a new app in the Bluemix UI from the Internet of Things Starter boilerplate. The resulting app runs a Node-RED server and is automatically bound to a new instance of the Cloudant NoSQL DB service. With NODE-Red, you can graphically create a flow that subscribes to an MQTT Broker and takes appropriate action. In this case, it acts by writing the information to the Cloudant DB and by sending a tweet if the runner has crossed a mile marker. So, in just a few minutes you can have a Bluemix app processing IoT data from a large number of devices and putting that data right into a database for later retrieval and analysis.

And, in case you're wondering, the map UI also subscribes to the messages and updates the display as new information arrives.

What's Next?

Since data is stored as it comes in, all sorts of additional features are possible. The Emerging Tech team already has code in HR Tracker to allow people to "replay" past activities or to compete against themselves (or others) on previous routes.

They've also begun exploring interesting things that could be done by applying analytics to all of this data. For example, did someone cheat? Does data indicate someone was grabbing the side of a car for half of their so-called bike ride?

With that, I'll close with a question to anyone who has made it this far in the post. And, I'd love to see some responses in the comments section. :)
If you had access to all of the data generated by this IoT scenario, what would you be interested to find out?

Related Resources