Book Image

Intel Galileo Blueprints

By : Marco Schwartz
Book Image

Intel Galileo Blueprints

By: Marco Schwartz

Overview of this book

Table of Contents (19 chapters)
Intel Galileo Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
1
Setting Up the Galileo Board and the Development Environment
Index

Writing the code for the project


We will now look at the code for the project. Once again, we will use Node.js as the base of our application, and we will code it inside the Intel XDK software that we already used in previous chapters.

Here is the complete code for this part:

// Includes
var mraa = require("mraa");
var util = require('util');

// Sensors & relay pins
var light_sensor_pin = new mraa.Aio(0);

var motion_sensor_pin = new mraa.Gpio(8);
motion_sensor_pin.dir(mraa.DIR_IN);

var relay_pin = new mraa.Gpio(7);
relay_pin.dir(mraa.DIR_OUT);

// PubNub client
var pubnub = require("pubnub")({
    ssl           : true,  // <- enable TLS Tunneling over TCP
    publish_key   : "your_publish_key",
    subscribe_key : "your_subscribe_key"
});

// Function to publish light level
function send_light_level() {

  // Measure light level
  var a = light_sensor_pin.read();
  var light_level = a/1024*100;
  light_level = light_level.toPrecision(4);

  // Send it to PubNub
  var message = {...