SF1 and the Internet of Things

SF1 and the Internet of Things

How to control your office using SF1 + NodeJS + Arduino


Bruno Fagundez

May 02, 2014


  1. 3.

    What is the Internet of things The Internet of things

    refers to uniquely identifiable objects and their virtual representations in an Internet-like structure. Kevin Ashton proposed the term Internet of things in 1999, though people have discussed the concept since at least 1991.
 Today however, the term Internet of things (commonly abbreviated as IoT) denotes advanced connectivity of devices, systems, and services that goes beyond the traditional M2M and covers a variety of protocols, domains and applications. Source: Wikipedia
  2. 4.

    What is NodeJS Node.js is a software platform for scalable

    server-side and networking applications. Node.js applications are written in JavaScript, and can be run within the Node.js runtime on Windows, Mac OS X and Linux with no changes. Source: Wikipedia
  3. 5.

    Arduino is a single-board microcontroller, intended to make the application

    of interactive objects or environments more accessible.
 Introduced in 2005, it was designed to give students an inexpensive and easy way to program interactive objects. It comes with a simple integrated development environment (IDE) that runs on regular personal computers and allows to write programs for Arduino using C or C++. What is an Arduino Source: Wikipedia
  4. 6.

    Salesforce1 is a mobile app development platform for everyone. It

    allows incredible freedom for ISVs, developers, administrators, and every user to innovate. ! This approach to unlocking mobile app development for organizations is built for today’s needs: mobile and social solutions delivered in days or weeks. ! Designed for scale with open APIs for extensibility and integration, and powerful developer tools, there’s no limit to what developers and ISVs can build on the platform. What is Salesforce 1 Source: SF1 Developer guide
  5. 7.

    This project ▪ Control lights remotely using SF1 ▪ Know

    when somebody is ringing the bell at the office
  6. 9.

    Salesforce ▪ Created a custom object called "Light"
 - This

    object has 2 fields "State" and "PIN" PushTopic pushTopic = new PushTopic(); pushTopic.Name = 'LightStateChanges'; pushTopic.Query = 'SELECT Id, Name, State__c, PIN__c FROM Light__c'; pushTopic.ApiVersion = 30.0; pushTopic.NotifyForOperationCreate = true; pushTopic.NotifyForOperationUpdate = true; pushTopic.NotifyForFields = 'Referenced'; insert pushTopic; ▪ Created a Streaming Push Topic to monitor changes on the object:
  7. 10.

    NodeJS ▪ Interaction with the Arduino is made using Johnny

 URL: https://github.com/rwaldron/johnny-five
 What it does: 
 Establishes a connection with the Arduino through USB, can interact with connected components in real time.
 ▪ Interaction with Salesforce is made using NForce
 URL: https://github.com/kevinohara80/nforce
 What it does:
 Connects to Salesforce using credentials and interacts with all APIs available including force.com Streaming API.
  8. 11.

    NodeJS ▪ Connects to our salesforce instance using NForce. ▪

    Subscribes to our force.com Streaming PushTopic LightStateChanges ▪ Establish a connection with the Arduino // When the Arduino board is ready and connected board.on("ready", function(){ // nforce Authentication org.authenticate({ username: '<arduinousername>', password: '<arduinopwd>' }, function(err, oauth) { // subscribe to a pushtopic var str = org.stream({ topic: 'LightStateChanges', oauth: oauth }); str.on('data', function(data) { // interacts with the data received when a record changes .... }); }); });
  9. 14.

    Chatter ▪ Interacting with the Arduino through chatter ▪ Created

    a chatter group called Office gadgets ▪ Created a trigger on FeedItem object and look for the commands 
 !lightsON and !lightsOFF to change our lights state.
  10. 16.

    NodeJS // Instantiate button (pin 8) button = new five.Button(8);

    // when the button is pressed... button.on("down", function() { ! // create a chatter post (FeedItem) var post = nforce.createSObject('FeedItem'); post.set('Body', 'DING-DONG! Somebody is at the front door ringing the bell.'); post.set('ParentId','0F9i0000000LNqZ'); ! org.insert({ sobject: post, oauth: oauth }, function(err, resp){ if(err) console.log(err) }); }); Every time the button is pressed a FeedItem is created using NForce
  11. 18.

    NodeJS // LIGHT SENSOR HANDLING photoresistor = new five.Sensor({ pin:

    "A5", freq: 250, threshold: 100 }); ! // "data" get the current reading from the photoresistor photoresistor.on("change", function() { if(this.value < 300 ){} if(this.value > 600 ){} }); When the voltage readings of the photoresistor hit certain values a chatter message is created to turn lights on or off.
  12. 19.