Friday, June 06, 2014

My first Node-Red node: gcalendar

First thing I found when I was playing with homA was the calendar component that reads an event from a Google calendar - I loved the idea and the implementation. So when I got interested in Node-Red my first thought was: wouldn't it be cool if there was a node like that? I checked the extra nodes repository and couldn't find one so I thought this could be the best opportunity for me to dive deeper into node.js and node-red.

It wasn't all that easy at first since the Node-Red website didn't have any docs at that time about how to write a node - thanks to Nicholas O'Leary a guide is now available, not 100% finished but great enough to help anyone starting on this path. Anyway, looking at existing nodes and experimenting with stuff I was able to do this so here is my first node: gcalendar. While working on this I hit some snags:
  • To get authorized to a Google calendar, OAuth2 must be used. I could have used Alexander's code from the homA calendar component but since I have no experience with OAuth2 I was not confident I can figure it out. Alexander is great and he always helped me when I asked him something but didn't want to take his time with minor things. On the other hand, a while back I heard about Temboo and never tried to use it so this looked like the perfect time to do so. And I'm happy I did: following the steps on the GetNextEvent page
  • I was able in a few minutes to create the app on Google API, get the credentials and get the code to call this Choreo and get the calendar data. There are 2 drawbacks to this approach, none of them a deal breaker for me:
    • there are 3 more properties to set now to configure this node, all related to Temboo - since they don't change in time, this is not a big issue;
    • Temboo only allows 1000 calls per month for free - since I don't plan to call this node more than 10 times a day, this limitation doesn't really affect me; anyway, I like their service a lot and if I'm going to be using them more, I won't mind paying for the next tier service.
  • After the main JavaScript coding was done, I had to figure out the .html file for the node. This was quite challenging without having any docs and it took me a lot of trials to figure out how and what to do; this page was of great help - all of this is now documented on this page which explains everything in great detail.
  • The first version of the node (let's call it v0.1) had all the properties set in the node directly which worked fine but it was a pain to have to re-enter them every time when I created the node during testing; plus, they were saved in the flow JSON which is not a good idea at all. Hardcoding them worked fine for testing but not an option for the final code. So now it was time to figure out the credentials part. First pass was to hardcode them in the settings.js file in Node-Red install dir which worked (this is sort of v1.0 of my node, documented in this file I kept for reference) but not a good idea according to the answers I got to my question. Based on the answers I got from Charalampos and Dave and after a lot more messing with the code, I was able to come up with the current version on my gcalendar node, where credentials are stored in a separate file. There is plenty more I can do to improve this version, but this is the one I use now and I am really happy with it.
In my next post, I will try to show how I use this node in a real flow.

Monday, June 02, 2014

Starting with Node-Red

A couple years ago I started to look into connecting devices to the Internet and played with quite a few services, like Pachube then Cosm now Xively and a few others - this all evolved in what is now named the Internet of Things (IoT) and a lot more services like Xively are now available. These services are usually independent of one another and each has a different way of setting up the communication with the device which makes things a bit difficult for beginners; these devices are sometimes hobby oriented like Arduino or Raspberry Pi but in other case they are products that monitor the home and also take actions (like NinjaBlocks, SmartThings, Insteon, TCP Lighting and so many many more). To make working with these devices easier (detection, customization, adding rules, communication with other services) new frameworks and applications are coming up, like TheThingSystem (TTS - node.js based, v1.8 was just released a couple days ago), OpenHab (Java based), Alexander Rust's homA and others.

A few months back, a new application showed up on my radar: Node-Red; built on node.js, Node-Red is another great product started by IBM researchers. Briefly said, Node-Red provides a browser-based flow editor that makes it easy to wire together flows using a wide variety of nodes presented in the palette; flows can be then deployed to the runtime in the same interface. No point in getting into more details about it, you can read more on the Node-Red website and in various articles like this one.

At that time I was digging a lot into the homA app and its components and I loved it; I was just getting started with the rules engine (which is way cool, by the way); but after discovering Node-Red and after seeing Alexander's comment to this google+ post I realized that I can use Node-Red to simplify the interaction between my devices and the outside world, so I started to research it more. There are great resources around it, the ones I use the most are:
  • Node-Red docs;
  • flows contributed by people working on Node-Red and 3rd parties;
  • Node-Red github repo;
  • other nodes contributed by people working on Node-Red and 3rd parties;
  • the Node-Red google group - Nicholas O'Leary and Dave C-J are awesome, their extremely fast answers helped me a ton.
There are many other interesting resources, like:
I will try to add more resources as I find them. I hope you will like Node-Red as much as I do.