Connect Salesforce to Intel Edison In 60 Lines Of JavaScript – cylonjs.com – Blinking LED



Connect Salesforce to Intel Edison In 60 Lines Of JavaScript – cylonjs.com – Blinking LED

0 0


cylon-workshop-2014


On Github hybridgroup / cylon-workshop-2014

Connect Salesforce to Intel Edison In 60 Lines Of JavaScript

cylonjs.com

Good Afternoon

This is @Dreamforce

I am @deadprogram

Ringleader

@hybrid_group

The other guy is @adzankich

Serious Programming Guy

@hybrid_group

hybridgroup.com

kidsruby.com

JavaScript Robotics

A JavaScript Framework for Robotics & Physical Computing

Multiple Hardware Devices

Different Hardware Devices

At the Same Time!

Cylon.js makes programming devices as easy as web development

bit.ly/cylon-dreamforce

Getting Started

Start Intel XDK

Signup For Intel XDK

Create a Blank Project

Update package.json

Add manual connection to Edison

Blinking LED

var cylon = require('cylon');

cylon.robot({
  connection: { name: 'edison', adaptor: 'intel-iot' },
  device: { name: 'led', driver: 'led', pin: 4, connection: 'edison' }
})
  .on('ready', function(my) {
    setInterval(function() {
      my.led.toggle();
    }, 1000);
  })
  .start();

LED + Touch Sensor

var cylon = require('cylon');

cylon.robot({
  connection: { name: 'edison', adaptor: 'intel-iot' },
  device: [
    { name: 'led', driver: 'led', pin: 4, connection: 'edison' },
    { name: 'touch', driver: 'button', pin: 3, connection: 'edison' },
  ]
})
  .on('ready', function(my) {
    my.touch.on('press', function() {
      my.led.turnOn();
    });

    my.touch.on('release', function() {
      my.led.turnOff();
    });
  })
  .start();

Salesforce + LED + Touch Sensor

bit.ly/cylon-df14pass

var cylon = require('cylon');
var robotName = 'cylon01';
var username = 'user';
var password = 'password';
// Paste Cylon.js robot code below this line

cylon.robot({
  name: robotName,
  connections: [
    { name: 'edison', adaptor: 'intel-iot' },
    { name: 'sfcon', adaptor: 'force', sfuser: username, sfpass: password}
  ],
  device: [
    { name: 'salesforce', driver: 'force', connection: 'sfcon' },
    { name: 'led', driver: 'led', pin: 4, connection: 'edison' },
    { name: 'touch', driver: 'button', pin: 3, connection: 'edison' },
  ]
})
  .on('ready', function(my) {
    my.salesforce.subscribe('BoardMsgOutbound', function(err, data) {
      if (err != null) {
        console.log(err);
      } else if (data.sobject.board_id__c === my.name) {
        if (data.sobject.touch_sensor__c === true) {
          my.led.turnOn();
        } else if (data.sobject.touch_sensor__c === false) {
          my.led.turnOff();
        }
      }
    });

    my.touch.on('press', function() {
      var toSend = {
        boardId: my.name,
        touchSensor: true
      };
      my.salesforce.post('/Boards/', toSend, function(err, data) {
        if (err != null) {
          console.log("Error sending touch sensor information: " + err);
        } else {
          console.log('Board Msg has been sent to Salesforce.');
        }
      });
    });

    my.touch.on('release', function() {
      var toSend = {
        boardId: my.name,
        touchSensor: false
      };
      my.salesforce.post('/Boards/', toSend, function(err, data) {
        if (err != null) {
          console.log("Error sending touch sensor information: " + err);
        } else {
          console.log('Board Msg has been sent to Salesforce.');
        }
      });
    });
  })
  .start();

Salesforce + LED + Touch Sensor + Analog Sensor

var cylon = require('cylon');
var robotName = 'cylon01';
var username = 'user';
var password = 'password';
// Paste Cylon.js robot code below this line

cylon.robot({
  name: robotName,
  connections: [
    { name: 'edison', adaptor: 'intel-iot' },
    { name: 'sfcon', adaptor: 'force', sfuser: username, sfpass: password}
  ],
  device: [
    { name: 'salesforce', driver: 'force', connection: 'sfcon' },
    { name: 'led', driver: 'led', pin: 4, connection: 'edison' },
    { name: 'touch', driver: 'button', pin: 3, connection: 'edison' },
    { name: 'sensor', driver: 'analogSensor', pin: 0, connection: 'edison' }
  ]
})
  .on('ready', function(my) {
    var ready = false;
    var sensorVal = 0;
    my.salesforce.subscribe('BoardMsgOutbound', function(err, data) {
      if (err != null) {
        console.log(err);
      } else if (data.sobject.board_id__c === my.name) {
        if (data.sobject.touch_sensor__c === true) {
          my.led.turnOn();
        } else if (data.sobject.touch_sensor__c === false) {
          my.led.turnOff();
        }
        var sensor = data.sobject.analog_sensor__c;
        if (sensor != null) {
          console.log("Reading: " + sensor);
        }
      }
    });

    my.touch.on('press', function() {
      ready = true;
      var toSend = {
        boardId: my.name,
        touchSensor: true
      };
      my.salesforce.post('/Boards/', toSend, function(err, data) {
        if (err != null) {
          console.log("Error sending touch sensor information: " + err);
        } else {
          console.log('Board Msg has been sent to Salesforce.');
        }
      });
    });

    my.touch.on('release', function() {
      ready = false;
      var toSend = {
        boardId: my.name,
        touchSensor: false 
      };
      my.salesforce.post('/Boards/', toSend, function(err, data) {
        if (err != null) {
          console.log("Error sending touch sensor information: " + err);
        } else {
          console.log('Board Msg has been sent to Salesforce.');
        }
      });
    });

    my.sensor.on('analogRead', function(data) {
      sensorVal = data;
    });

    setInterval(function() {
      if (ready) {
        var toSend = {
          boardId: my.name,
          touchSensor: true,
          analogSensor: sensorVal
        };
        my.salesforce.post('/Boards/', toSend, function(err, data) {
        if (err != null) {
          console.log("Error sending analog sensor information: " + err);
        }
        });
      }
    }, 2000);
  })
  .start();

Salesforce + LED + Button + Analog Sensor + LCD Display

var cylon = require('cylon');
var robotName = 'cylon01';
var username = 'user';
var password = 'password';
// Paste Cylon.js robot code below this line

function writeToScreen(screen, message) {
  screen.setCursor(0,0);
  screen.write(message);
}

cylon.robot({
  name: robotName,
  connections: [
    { name: 'edison', adaptor: 'intel-iot' },
    { name: 'sfcon', adaptor: 'force', sfuser: username, sfpass: password}
  ],
  device: [
    { name: 'salesforce', driver: 'force', connection: 'sfcon' },
    { name: 'led', driver: 'led', pin: 4, connection: 'edison' },
    { name: 'touch', driver: 'button', pin: 3, connection: 'edison' },
    { name: 'sensor', driver: 'analogSensor', pin: 0, connection: 'edison' },
    { name: 'screen', driver: 'upm-jhd1313m1', connection: 'edison' }
  ]
})
  .on('ready', function() {
    var ready = false;
    var sensorVal = 0;

    writeToScreen(my.screen, "Ready!");

    my.salesforce.subscribe('BoardMsgOutbound', function(err, data) {
      if (err != null) {
        console.log(err);
      } else if (data.sobject.board_id__c === my.name) {
        if (data.sobject.touch_sensor__c === true) {
          my.led.turnOn();
        } else if (data.sobject.touch_sensor__c === false) {
          my.led.turnOff();
        }
        var sensor = data.sobject.analog_sensor__c;
        if (sensor != null) {
          var pad = "0000";
          var message = "Reading: " + (pad+sensor.toString()).slice(-pad.length);
          writeToScreen(my.screen, message);
          console.log(message);
        }
      }
    });

    my.touch.on('press', function() {
      ready = true;
      var toSend = {
        boardId: my.name,
        touchSensor: true
      };
      my.salesforce.post('/Boards/', toSend, function(err, data) {
        if (err != null) {
          console.log("Error sending touch sensor information: " + err);
        } else {
          console.log('Board Msg has been sent to Salesforce.');
        }
      });
    });

    my.touch.on('release', function() {
      ready = false;
      var toSend = {
        boardId: my.name,
        touchSensor: false 
      };
      my.salesforce.post('/Boards/', toSend, function(err, data) {
        if (err != null) {
          console.log("Error sending touch sensor information: " + err);
        } else {
          console.log('Board Msg has been sent to Salesforce.');
        }
      });
    });

    my.sensor.on('analogRead', function(data) {
      sensorVal = data;
    });

    setInterval(function() {
      if (ready) {
        var toSend = {
          boardId: my.name,
          touchSensor: true,
          analogSensor: sensorVal
        };
        my.salesforce.post('/Boards/', toSend, function(err, data) {
        if (err != null) {
          console.log("Error sending analog sensor information: " + err);
        }
        });
      }
    }, 2000);
  })
  .start();

Was that fun?

Join the Robot Evolution!

cylonjs.com

@cylonjs

hybridgroup.com

@hybrid_group

Thank you!

@deadprogram

@adzankich