Handle errors, and turn off after delay
var http = require('http');
var gpio = require('pi-gpio');
var currentState = false;

var actuator = function(outputValue) {
  console.log('Switching relay: '+outputValue);
  currentState = outputValue;
  gpio.open(11, "output", function(err) {
    gpio.write(11, outputValue, function(err) {
      gpio.close(11);
    });
  });
  if (outputValue) {
    // Turn off after 5sec
    setTimeout(actuator, proces.env.DELAY || 5000, false);
  }
};

var doPollingReq = function() {
  console.log('Starting request...');
  var pollingReqOpts = {
    host: process.env.SERVER_HOSTNAME || 'localhost',
    port: process.env.SERVER_PORT || 3000,
    path: '/alarm',
    method: 'GET'
  };
  var pollingReq = http.request(pollingReqOpts, function(res) {
    console.log('Request being created...');
    res.on('data', function(data) {
      console.log('Got data: '+data);
      var objData = JSON.parse(data);
      if (objData && objData.value !== undefined) {
        actuator(objData.value);
      }
    });
    res.on('close', doPollingReq);
    res.on('end', doPollingReq);
    res.on('error', doPollingReq);
  });
  pollingReq.end();
};

setImmediate(doPollingReq);
var http = require('http');
var gpio = require('pi-gpio');
var currentState = false;

var actuator = function(outputValue) {
  console.log('Switching relay: '+outputValue);
  currentState = outputValue;
  gpio.open(11, "output", function(err) {
    gpio.write(11, outputValue, function(err) {
      gpio.close(11);
    });
  });
};

var doPollingReq = function() {
  console.log('Starting request...');
  var pollingReqOpts = {
    host: process.env.SERVER_HOSTNAME || 'localhost',
    port: process.env.SERVER_PORT || 3000,
    path: '/alarm',
    method: 'GET'
  };
  var pollingReq = http.request(pollingReqOpts, function(res) {
    console.log('Request being created...');
    res.on('data', function(data) {
      console.log('Got data: '+data);
      var objData = JSON.parse(data);
      if (objData && objData.value !== undefined) {
        actuator(objData.value);
      }
    });
    res.on('close', doPollingReq);
    res.on('end', doPollingReq);
  });
  pollingReq.end();
};

setImmediate(doPollingReq);