Created
August 21, 2013 09:19
-
-
Save kartben/6292216 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mqtt = require('mqtt'); | |
var client = mqtt.createClient(1883, "m2m.eclipse.org"); | |
var b = require('bonescript'); | |
var i2c = require('i2c'); | |
var luminosity_pin = 'P9_40'; | |
var led_pin = 'P8_13'; | |
var servo_pin = 'P9_14'; | |
var readSensors = function() { | |
b.analogRead(luminosity_pin, lumCallback); | |
}; | |
setInterval(readSensors, 200); | |
// fake humidity | |
setInterval(function() { | |
var hum = Math.floor((Math.random() * 5) + 95); | |
console.log("Hum: " + hum); | |
client.publish('/benjamin-test/9dd4264bf5dd491da73791dce4275b3b/demohouse.gf.corridor.temperature', hum.toString()); | |
}, 500); | |
function lumCallback(x) { | |
var reading = x.value; | |
// Calculating the voltage in the input of the ADC | |
var voltage = 1.8 * reading; | |
// Calculating the resistance of the photoresistor | |
// in the voltage divider | |
var resistance = (10.0 * 1.8) / voltage - 10.0; | |
// Calculating the intensity of light in lux | |
var illuminance = (255.84 * Math.pow(resistance, -10 / 9)).toFixed(0); | |
console.log("Lum: " + illuminance); | |
client.publish('/benjamin-test/9dd4264bf5dd491da73791dce4275b3b/demohouse.gf.toilet.temperature', illuminance.toString()); | |
} | |
var address = 0x48; | |
var wire = new i2c(address, { | |
device: '/dev/i2c-1', | |
debug: true | |
}); // point to your i2c address, debug provides REPL interface | |
function readI2Ctemperature() { | |
wire.readBytes(0x00, 2, function(err, res) { | |
var temp = (res.readInt16BE(0) / 256).toFixed(1); | |
console.log("Temp: " + temp); | |
client.publish('/benjamin-test/9dd4264bf5dd491da73791dce4275b3b/demohouse.ff.bathroom.temperature', temp.toString()); | |
}); | |
} | |
setInterval(readI2Ctemperature, 100); | |
var state = 0; | |
b.pinMode(led_pin, 'out'); | |
var toggleLED = function() { | |
state = state ? 0 : 1; | |
b.digitalWrite(led_pin, state); | |
}; | |
var stateServo = 0; | |
b.pinMode(servo_pin, 'out'); | |
var toggleServo = function() { | |
var duty_cycle; | |
if (stateServo) { | |
duty_cycle = 0.04; | |
} | |
else { | |
duty_cycle = 0.09; | |
} | |
b.analogWrite(servo_pin, duty_cycle, 60); | |
stateServo = stateServo ? 0 : 1; | |
}; | |
client.subscribe('/benjamin-test/9dd4264bf5dd491da73791dce4275b3b/switch'); | |
client.on('message', function(topic, message) { | |
toggleLED(); | |
toggleServo(); | |
console.log(message); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment