示例#1
0
// Read the input and print both the raw value and a rough lux value,
function readLightSensorValue() {
    console.log(light.name() + " raw value is " + light.raw_value() +
            ", which is roughly " + light.value() + " lux");
    sendObservation("lights", light.value(), new Date().getTime());
    l = parseFloat(light.value());
    setLcdMessage(u,t);
}
  //define function for reading sensor
  function read_sensor(cb){

  	read_lux = light.value();
  	read_temp = temp.value();

  	cb();
  };
示例#3
0
function lightSensor(req, res) {
    var groveSensor = require('jsupm_grove');
    var aioPin = req.query.aioPin //query for pin number 
    var value = req.query.value; // query for value (either raw or lux)
	var light = new groveSensor.GroveLight(parseInt(aioPin)); // initialising sensor on specified pin 
    var rawVal = light.raw_value().toString();
    var luxVal = light.value().toString();
    
    if (value == 'raw'){
		res.send(rawVal)
	}
	else if (value == 'lux'){
		res.send(luxVal);
	}
	else {
		res.send ("Sorry your input was not valid")
	}
}
    var waiting = setInterval(function() {
    
    var celsius = temperature.value();
        temperatureVal = celsius;
        lightVal = light.value();
    //console.log("light", light.value());
    lcd.cursor(1, 0).print(celsius + "C");
    lcd.cursor(0, 13).print(light.value().toString().slice(0,3));   
    
    try {
        io.emit('temperature', { for: 'everyone', temperature: celsius });    
        io.emit('light', { for: 'everyone', light: light.value() });
    } catch(ex) {
        console.log(ex);
    }
    
    /////////////////
    /*var buffer = new upmMicrophone.uint16Array(128);
    var len = myMic.getSampledWindow(2, 128, buffer);
        console.log("len", len);
    if (len)
    {
        var thresh = myMic.findThreshold(threshContext, 30, buffer, len);
        
        if (thresh) {
            try {
                io.emit('thresh', { for: 'everyone', thresh: thresh });
                console.log(tresh);
            } catch(ex) {};
            //myMic.printGraph(threshContext);
        }
    }*/
    //////////////
    io.emit('canon', { for: 'everyone', 'canonX': servoX.position, 'canonY': servoY.position });  
    switch (GunMode) {              
          case 0:            
              break;
    }
    
}, 500);
//Continously collect and publish data from sensors
function sensorPub() {
	//Publish sensor data
	temperatureData = temperature.getTemperature();
	socket.send(['Grove_temperatureandhumiditysensor_temperature_1_0-1', JSON.stringify({"name": "Grove_temperatureandhumiditysensor_temperature_1_0-1", "datapoints":[[timeMS.getTime(), temperatureData, quality]]})]);
	console.log(temperatureData + " degrees Celsius");
	humidityData = humidity.getHumidity();
	socket.send(['Grove_temperatureandhumiditysensor_humidity_1_0-1', JSON.stringify({"name": "Grove_temperatureandhumiditysensor_humidity_1_0-1", "datapoints":[[timeMS.getTime(), humidityData, quality]]})]);
	console.log(humidityData + " RH");
	lightData = light.raw_value();
	socket.send(['Grove_light_1_1-1', JSON.stringify({"name": "Grove_light_1_1-1", "datapoints":[[timeMS.getTime(), lightData, quality]]})]);
	console.log(lightData + " raw light value");
	moistureData = moisture.value();
	socket.send(['Grove_moisturesensor_1_4-1', JSON.stringify({"name": "Grove_moisturesensor_1_4-1", "datapoints":[[timeMS.getTime(), moistureData, quality]]})]);
	console.log(moistureData + " moisture value");
	console.log("-------------------------");

}
示例#6
0
setInterval(function(){
  console.log(a1.name() + " raw value is " + a1.raw_value() +
            ", which is roughly " + a1.value() + " lux");
	
  smartliving.send(a1.value(), "a1");
},5000);
示例#7
0
文件: main.js 项目: bijilap/Ad-Wise
// Read the input and print both the raw value and a rough lux value,
// waiting one second between readings
function readLightSensorValue() {
    console.log(light.name() + " raw value is " + light.raw_value() +
            ", which is roughly " + light.value() + " lux");
}
示例#8
0
文件: main.js 项目: bijilap/Ad-Wise
//Type Node.js Here :)


var mraa = require('mraa'); //require mraa
console.log(' Hello! This Gideon Starting up... My MRAA Version: ' + mraa.getVersion()); //write the mraa version to the console

//Libraries needed by the Sensors
var groveSensor = require('jsupm_grove');
var sensorModule = require('jsupm_ttp223');

// Create the temperature sensor object using AIO pin 0
// Create the touch sensor object using GPIO pin 2
// Create the light sensor object using AIO pin 3
var temp = new groveSensor.GroveTemp(0);
var touch = new sensorModule.TTP223(2);
var light = new groveSensor.GroveLight(3);

console.log(temp.name());
console.log(touch.name());
console.log(light.name());

// Read the temperature ten times, printing both the Celsius and
// equivalent Fahrenheit temperature, waiting one second between readings
function readTempSensorValue() {
    var celsius = temp.value();
    var fahrenheit = celsius * 9.0/5.0 + 32.0;
    console.log(celsius + " degrees Celsius, or " +
                Math.round(fahrenheit) + " degrees Fahrenheit");
}

// Read the input and print both the raw value and a rough lux value,
board.on("ready", function() {
        
    var servoX = new five.Servo({
      pin: 5,
      startAt: 90,
      range: [90, 160]
    });
    
    var servoY = new five.Servo({
      pin: 6,
      startAt: 50,
      range: [40, 60]
    });
    var servoXCurrent = 90;
    var servoYCurrent = 20;
    var servoYDefault = 40;
    
    servoX.stop();
    servoY.stop();
    
    servoX.to(90);
    servoY.to(servoYDefault);
    
    
  var lcd = new five.LCD({
    controller: "JHD1313M1"
  });

  lcd.bgColor("#00ff00");
  lcd.cursor(0, 0).print("GUN MODE: 0");
    
var temperature = new groveSensor.GroveTemp(3);
var light = new groveSensor.GroveLight(1);
console.log(temperature.name());
console.log(light.name());
    
/*var upmMicrophone = require("jsupm_mic");
// Attach microphone to analog port A0
var myMic = new upmMicrophone.Microphone(1);

var threshContext = new upmMicrophone.thresholdContext;
threshContext.averageReading = 0;
threshContext.runningAverage = 0;
threshContext.averagedOver = 2;*/
    

    var waiting = setInterval(function() {
    
    var celsius = temperature.value();
        temperatureVal = celsius;
        lightVal = light.value();
    //console.log("light", light.value());
    lcd.cursor(1, 0).print(celsius + "C");
    lcd.cursor(0, 13).print(light.value().toString().slice(0,3));   
    
    try {
        io.emit('temperature', { for: 'everyone', temperature: celsius });    
        io.emit('light', { for: 'everyone', light: light.value() });
    } catch(ex) {
        console.log(ex);
    }
    
    /////////////////
    /*var buffer = new upmMicrophone.uint16Array(128);
    var len = myMic.getSampledWindow(2, 128, buffer);
        console.log("len", len);
    if (len)
    {
        var thresh = myMic.findThreshold(threshContext, 30, buffer, len);
        
        if (thresh) {
            try {
                io.emit('thresh', { for: 'everyone', thresh: thresh });
                console.log(tresh);
            } catch(ex) {};
            //myMic.printGraph(threshContext);
        }
    }*/
    //////////////
    io.emit('canon', { for: 'everyone', 'canonX': servoX.position, 'canonY': servoY.position });  
    switch (GunMode) {              
          case 0:            
              break;
    }
    
}, 500);
    
  

    
  var button = new five.Button(2);

  // The following will turn the Led
  // on and off as the button is
  // pressed and released.
  button.on("press", function() {
    buzzer.periodicBuzzerActivity(1);
      if (GunMode<3) { //3
          GunMode++;
      } else {
          GunMode = 0;
      }
      console.log("GunMode", GunMode);
  });

  button.on("release", function() {
    buzzer.periodicBuzzerActivity(0);
      
      lcd.cursor(0, 10).print(GunMode);
      switch (GunMode) {
          case 0:
              lcd.bgColor("#00ff00");

              servoX.to(90);
              servoY.to(servoYDefault);
              servoX.stop();
              servoY.stop();
              playSong(["client/sounds/gun_mode_0.mp3","client/sounds/gun_is_disabled.mp3"], function() {
              });
              break;
          case 2: 
              lcd.bgColor("#0000ff");
              servoX.stop();
              servoY.stop();
              servoX.sweep();
              //servoX.to(160, 1000);
              console.log("sweep1");
              /*servoY.sweep({
                  //interval: 3000,
                  step: 10
              });*/
              playSong(["client/sounds/gun_mode_2.mp3","client/sounds/Automatic_gun_mod.mp3"], function() {
                  //playSong("client/sounds/Automatic_gun_mod.mp3");
              });
              break;
          case 3: 
              lcd.bgColor("#0088ff");
              servoX.stop();
              servoY.stop();
              servoX.sweep();
              servoY.sweep();
              //servoX.to(160, 1000);
              console.log("sweep1");
              /*servoY.sweep({
                  //interval: 3000,
                  step: 10
              });*/
              playSong(["client/sounds/gun_mode_3.mp3","client/sounds/Automatic_gun_mod.mp3"], function() {
                  //playSong("client/sounds/Automatic_gun_mod.mp3");
              });
              break;
          case 1: 
              lcd.bgColor("#ff0000");
              servoX.stop();
              servoY.stop();
              servoX.to(90);
              servoY.to(servoYDefault);
              servoXCurrent = 90;
              servoYCurrent = servoYDefault;
              playSong( ["client/sounds/gun_mode_1.mp3", "client/sounds/manual_control_for_gun.mp3"], function() {
                  //playSong("client/sounds/manual_control_for_gun.mp3");
              });
              break;
              
          /*case 3: 
              lcd.bgColor("#ff00ff");
              servoX.stop();
              servoY.stop();
              servoX.to(90);
              servoY.to(servoYDefault);
              servoXCurrent = 90;
              servoYCurrent = servoYDefault;
              break;*/
          default:
      }
  });
    
process.on('SIGINT', function()
{
  clearInterval(myInterval);
    servoX.to(90);
    servoY.to(servoYDefault);
  console.log("Exiting...");
  process.exit(0);
});
    
    
    /*var proximity = new five.Proximity({
     //controller: "HCSR04",
     // pin: 7
      //pin: "A2"
      controller: "HCSR04I2CBACKPACK"
    });*/

// When exiting: clear interval and print message

    var led = new five.Led(4);
    led.blink(500);
    
  var joystick = new five.Joystick(["A0", "A1"]);

  // Observe change events from the Joystick!
  joystick.on("change", function() {
      try {
        io.emit('stick', { for: 'everyone', stickX: this.x, stickY: this.y });
      } catch(ex) {
          console.log(ex);
      }
      
      if (GunMode === 1) {
      lcd.cursor(1, 12).print(this.y.toString().slice(0,4));      
      lcd.cursor(1, 7).print(this.x.toString().slice(0,4));
          
      //console.log("  x            : ", this.x);
      //console.log("  y            : ", this.y);
      
      if (Math.abs(this.x)>0.3) {
          servoXCurrent += this.x*4;
          if (servoXCurrent>160) {
              servoXCurrent = 160;
          }
          if (servoXCurrent<90) {
              servoXCurrent = 90;
          }
          servoXCurrent = Math.round(servoXCurrent);
          servoX.to(servoXCurrent);
      }
      
      if (Math.abs(this.y)>0.2) {
          servoYCurrent += this.y*3;
          if (servoYCurrent>60) {
              servoYCurrent = 60;
          }
          if (servoYCurrent<servoYDefault) {
              servoYCurrent = servoYDefault;
          }
          servoYCurrent = Math.round(servoYCurrent);
          servoY.to(servoYCurrent);
      }    
      }
  });
    
  var acceleration = new five.Accelerometer({
    controller: "MMA7660"
  });

  acceleration.on("change", function() {
      
      try {
        io.emit('accel', { for: 'everyone', accelX: this.x, accelY: this.y, accelZ: this.z, pitch: this.pitch, roll: this.roll, acceleration: this.acceleration, inclination: this.inclination, orientation: this.orientation});
      } catch(ex) {
          console.log(ex);
      }
      
    /*if (GunMode === 3) {
    lcd.cursor(1, 12).print(this.y.toString().slice(0,4));
      
    lcd.cursor(1, 7).print(this.x.toString().slice(0,4));   
    
      
      if (Math.abs(this.x - 1)>0.3) {
          servoXCurrent += (this.x - 1);
          if (servoXCurrent>140) {
              servoXCurrent = 140;
          }
          if (servoXCurrent<40) {
              servoXCurrent = 40;
          }
          
          servoX.to(servoXCurrent);
      }
      
      if (Math.abs(this.y)>0.1) {
          servoYCurrent += this.y;
          if (servoYCurrent>140) {
              servoYCurrent = 140;
          }
          if (servoYCurrent<20) {
              servoYCurrent =20;
          }
          servoY.to(servoYCurrent);
      } 
      }*/
    
  });
    


    
});