JeanCarl's Adventures

Registration Node-RED application with Cloudant

August 29, 2016 | Node-RED

In my last blog post, I shared my newest Node-RED lab, Custom Nodes in Node-RED. I showed how to create a custom node to sort an array, use a HTML PDF node from the Node-RED community, and build a registration application in a Node-RED application running on IBM Buemix.

Photo

At the end of the lab I leave a challenge to the developer to make the application persistent. Let me explain a bit more.

Node-RED has a global context that lives in memory. When you store data, such as with the JavaScript in the following code, it isn’t written to disk, to a database, or any persistent storage.

global.set('attendeelist', ['Joe', 'Alex', 'Mary']);

When the Node-RED application stops, this data goes bye-bye. A Node-RED application could stop for a number of reasons:

  • Cloud Foundry restarts the application instance
  • you bind a new service and restage the application
  • you intentionally shut down the instance
  • Node-RED crashes

Using the global context in this case isn’t the best idea after all. In this blog post, I’ll show how a couple of changes to the flows can utilize a Cloudant NoSQL database and make the list live on.

If you’re running Node-RED on IBM Bluemix, the good news is you don’t have to create any new services. Behind the scenes, Node-RED uses a Cloudant NoSQL database to store the flows, for a similar reason as I mentioned above. You see, if Node-RED kept the flows in memory, they would disappear on a restart. If Node-RED stored the flows in the filesystem, as a locally installed Node-RED application does, when IBM Bluemix redeploys the application the flows would be lost as the filesystem isn’t retained. So, keeping the flows in Cloudant makes a lot of sense.

Photo

Using the Cloudant nodes that are included in the node-red-node-cf-cloudant npm package, we can add them into the flow and select a database.

For the flow connected to the POST /register node, we’ll add three nodes and modify two existing nodes.

Add a change node to move the name property from the payload to the msg object. This saves the name from being overwritten in the msg.payload object. Set the id property to set the id of the document id we’ll pull from Cloudant.

Photo

Connect a cloudant in node, using a database name such as names.

Photo

Modify the existing node labeled Add Attendee to List. Instead of using the global context, we’ll check to see if the attendee list document exists in Cloudant. If not, we’ll create one. Instead of storing the list back into the global context, we’ll append it the msg.payload.attendeelist property.

// If no document was found, create a new one.
if(!msg.payload) {
    msg.payload = {
        attendeelist: [], 
        _id: 'attendeelist'
    };
}

msg.payload.attendeelist.push(msg.name);

return msg;

Photo

Add a Cloudant out node which will take the msg.payload and store it in a JSON document in the Cloudant database.

Photo

Lastly, modify the Display Confirmation template and change {{payload.name}} to {{name}} to read the msg.name property.

Photo

The modified flow should look like the one shown below.

Photo

If we submit a name or two and look at the document in the Cloudant dashboard (access the dashboard through the service tile in your IBM Bluemix account), we’ll see the document expanding.

Photo

For the flow connected to the GET /roster node, we’ll add a couple of new nodes.

Add a change node to set the id of the document we’ll pull from Cloudant.

Photo

Add a Cloudant out node which gets the document, if it exists, from our database.

Photo

Add a switch node. This will check to see if the document exists and was returned.

Photo

For the case that the document does exist, add a change node to move the attendeelist into the msg.payload property.

Photo

For the case that the document doesn’t exist, add another change node to set the msg.payload to an empty array.

Photo

This will make sure the msg.payload is an array, empty or populated with names. Both this scenarios are then passed to the sort node with the completed flow shown below.

Photo

Go visit the /roster endpoint of your Node-RED application.

Photo

To see what happens if the Bluemix application restarts, open a new window and go to the application overview. Click on the restart icon. When the application comes back online, the list will remain.

Oh, and there’s one more thing that’s left. How do you clear out the list?

I added a /roster/reset endpoint that fetches the attendeelist document in Cloudant, and if it exists, sets it to an empty array and stores in back into Cloudant. The JSON is shown below.

[{"id":"5b4b22dc.61ee4c","type":"http in","z":"91de3816.0cb58","name":"","url":"/roster/reset","method":"get","swaggerDoc":"","x":120,"y":600,"wires":[["51bbe093.193fd"]]},{"id":"ea2d7463.bdc538","type":"template","z":"91de3816.0cb58","name":"List Cleared","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"\n    \n        <p>List is cleared. <a href="/register">Register</a></a></p>\n    \n\n","x":960,"y":605,"wires":[["1a2d5124.ce8247"]]},{"id":"1a2d5124.ce8247","type":"http response","z":"91de3816.0cb58","name":"","x":1141,"y":605,"wires":[]},{"id":"11ff7f50.11b839","type":"cloudant out","z":"91de3816.0cb58","name":"","cloudant":"","database":"names","service":"","payonly":true,"operation":"insert","x":910,"y":540,"wires":[]},{"id":"ae78ad10.af961","type":"cloudant in","z":"91de3816.0cb58","name":"names","cloudant":"","database":"names","service":"","search":"_id_","design":"","index":"","x":470,"y":600,"wires":[["3cde74b8.0ad03c"]]},{"id":"51bbe093.193fd","type":"change","z":"91de3816.0cb58","name":"","rules":[{"t":"set","p":"payload.id","pt":"msg","to":"attendeelist","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":600,"wires":[["ae78ad10.af961"]]},{"id":"3cde74b8.0ad03c","type":"switch","z":"91de3816.0cb58","name":"","property":"payload","propertyType":"msg","rules":[{"t":"nnull"},{"t":"else"}],"checkall":"false","outputs":2,"x":590,"y":600,"wires":[["dae60c18.2c6058"],["ea2d7463.bdc538"]]},{"id":"dae60c18.2c6058","type":"change","z":"91de3816.0cb58","name":"Empty List","rules":[{"t":"set","p":"payload.attendeelist","pt":"msg","to":"[]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":734,"y":571,"wires":[["11ff7f50.11b839","ea2d7463.bdc538"]]}]

Photo

Photo

The completed Node-RED application is shown below. You can find the completed flow in my GitHub repo.

Photo

Custom Nodes in Node-RED

August 26, 2016 | Node-RED

Since I first saw Node-RED at a hackathon last year, I’ve been intrigued with what it could do. From simple nodes that make a webpage, to using Watson services like Language Translation and Text to Speech, to pulling in Weather Company Data, to analyzing a body of text or images with AlchemyAPI, oh, and of course the Internet of Things, Node-RED makes prototyping together an application quickly really easy.

However, I often get asked by developers who come to my workshops about how extensible Node-RED can be. Often times Node-RED is compared to other block programming tools that are used to teach kids how to think logically. In reality, Node-RED is for kids of all ages.

Node-RED either wins the developer over, or developers get lost in the “too basic for a real coder” mindset. Some developers are pretty adamant about writing code, and aren’t satisfied with the simple function node available. True, Node-RED abstracts out the code, but on purpose. Being able to wire something up quickly can save some time for other more important things.

In my latest lab, Custom Nodes in Node-RED, I wrote a tutorial to show how to write your own custom node in Node-RED running on IBM Bluemix. It’s a powerful responsibility, so use the force wisely. Although the example is simple, sorting an array, it shows how simple it is behind the scenes.

Photo

I used the IBM Bluemix DevOps Service editor to create the two files (a JavaScript file and a HTML file) for the node, and then deployed the changes to the Node-RED application in IBM Bluemix.

Photo

Photo

I also show how to install nodes from the community like the HTML PDF node. I came across this node as I was planning this lab, and it didn’t disappoint. I should go back and create my reports in a PDF file for my manager from Node-RED, eh?

Photo

The lab finishes off with a sample registration application. It uses the global context to store names of attendees who check in, and then displays the list in alphabetical order in a PDF file.

Photo

Photo

The completed flows are shown below.

Photo

I left a challenge up to the developer to make the list of attendees persistent using a Cloudant NoSQL database. When the application restarts, the in-memory global context list of attendees is wiped out. Since Node-RED uses Cloudant to store the flows, there are only a couple of changes in the flows that are necessary to make this happen. Stay tuned for my next blog post where I show my solution to this challenge.

Weather Company Data for IBM Bluemix

July 31, 2016 | Node-RED

We’ve been having some beautiful weather in the Bay Area this summer. And what better time than now to play around with the new updates to the Weather Company Data for IBM Bluemix service available in IBM Bluemix. Formerly known as Insights for Weather, the weather service offers even more endpoints and capabilities.

Photo

In the Weather Company for IBM Bluemix Node-RED lab, I show how to use several endpoints: almanac, alerts, forecast, current observations, and time series.

Photo Photo

The complete flow can be imported into Node-RED.

Photo

To make this demo versatile, I’ve also added the option in the Node-RED flows to return both HTML webpages and the JSON returned from the weather service.

Many of these Weather Company Data API endpoints offer options for both GPS coordinates (latitude/longitude coordinates) and postal codes inputs. I have found that postal codes are easier (and more human-readable) than the GPS coordinates.

Photo

During a recent IBM Hack Night around the Weather Company Data APIs, I was inspired with new ideas:

  • finding the best vacation location for weather similar to where you live
  • mapping and correlating diseases with weather conditions
  • getting the weather texted to you before you leave your home in a high-rise building
  • finding the best hiking locations

If you’re interested in the same lab, but in Node.js, you can find a Node.js version in my GitHub repo here.

Analyzing Twitter User’s Personality in Node-RED

July 01, 2016 | Node-RED

Have you heard that Twitter timelines are public and that anyone can data mine what we tweet. From understanding what you might buy, to understanding how a competitor’s product is being talked about, to what’s trending, a Twitter timeline can be full of rich data ready to be analyzed.

Did you know you can also analyze your personality based on the tweets in your timeline? IBM has built a couple of sample applications that analyze a variety of things with the results of the Watson Personality Insights service available in IBM Bluemix. From matching your personality to a celebrity with Your Celebrity Match, to analyzing what type of designer your personality matches with Roztayger, Personality Insights offers scores of the Big 5 Personality traits (Openness, Conscientiousness, Agreeableness, Intro/Extroversion, and Emotional Range) and subcategories that can be used in your own applications to add additional data points about your users.

Photo

Using Node-RED to quickly prototype the application, I created a Node-RED lab that analyzes your personality using the tweets. It uses the Insights for Twitter Bluemix service and the Watson Personality Insights service to retrieve and analyze the tweets.

Photo

You can either use an existing Node-RED application, or deploy a new Node-RED starter boilerplate application in IBM Bluemix. Bind the Insights for Twitter service and Watson Personality Insights service to your application.

Photo

Photo

You can choose to walk through the lab and add the nodes manually, or import the whole flow by selecting Import -> Clipboard from the menu in Node-RED.

Photo

There are several ways to use this application. You can choose to analyze a specific user’s tweets, using the query parameter q=from:dothewww. You can also search for tweets using a search term and analyze the collective personality of matching tweets.

Both options offer two formats of the data returned: JSON output that can be used in third-party applications and a basic HTML webpage that can be extended or included in a webpage.

Photo

Photo

So…where does your personality take you?

Analyzing text messages via the Tone Analyzer service

May 25, 2016 | Conferences

I’m at the Twilio SIGNAL conference with an update to Tone LED Pin demo. If you recall, I analyzed tweets from Twitter around a hashtag or search term and took the most prevalent social tone from IBM Watson’s Tone Analyzer service to graph per minute stats on an NeoPixel LED ring. Yes, it is still a glorious wearable pie chart.

Taking advantage of the super simple to use Twilio service, I connected a phone number that takes in text messages and process those instead of tweets. The changes to my application were also pretty simple, removing one node and adding three nodes. Here’s how I modified my application to use Twilio. If you haven’t deployed the application, you find the instructions on a previous blog post.

First, let’s setup an HTTP endpoint in the Node-RED application instead of the Twitter node. Delete the Twitter node. Add a HTTP endpoint to expose the processing flow to the URL /twiliocallback.

Photo

Add a change node to move some data around. The first rule preserves the text message info in the msg.text property, useful if you want to respond back to the phone number the message comes from. The second rule sets the msg.payload property to the body of the text message, which is used by the Tone Analyzer node.

Photo

Finally, add a HTTP response node, and connect the nodes together as shown below.

Photo

Click on Deploy in the top right corner. That’s it for the application. All that’s left is to get a phone number from Twilio and instruct Twilio where to send the text messages to.

Register for a Twilio account and create a new phone number. You can take a little creative time to find that really awesome phone number using their search tool.

On the settings page for your new Twilio number, set the messaging webhook to point to your Node-RED application’s HTTP endpoint.

Photo

Send a text message to the Twilio phone number and see the results from the IBM Watson Tone Analyzer service included in the graphs and on the connected LED Pin.