Last active
August 29, 2015 14:17
-
-
Save lollotek/88e46aee0228a5381cab to your computer and use it in GitHub Desktop.
main.js - intellykitchen
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 mraa = require('mraa'); | |
console.log("\n"); | |
console.log("╦┌┐┌┌┬┐┌─┐┬ ┬ ┬ ┬ ┬┌─┬┌┬┐┌─┐┬ ┬┌─┐┌┐┌"); | |
console.log("║│││ │ ├┤ │ │ └┬┘ ├┴┐│ │ │ ├─┤├┤ │││"); | |
console.log("╩┘└┘ ┴ └─┘┴─┘┴─┘┴ ┴ ┴┴ ┴ └─┘┴ ┴└─┘┘└┘"); | |
console.log("\nWelcome to the Intelly Kitchen tutorial based on Intel Edison and Lelylan \n"); | |
console.log("(This is a proof of concept and could not properly work as expected. Any help appreciated [email protected]) \n"); | |
console.log("To control this light, change the device settings with"); | |
console.log("the device you've defined in Lelylan dashboard.\n"); | |
/* Device Settings */ | |
var device = { id: '<DEVICE_ID>', secret: '<DEVICE_SECRET>' }; | |
/* Lelylan Settings */ | |
// MQTT client ID | |
clientId = device.id; | |
// device topics | |
var in_topic = 'devices/' + device.id + '/get' // receiving messages | |
, out_topic = 'devices/' + device.id + '/set'; // publishing messages | |
/* Smart Pan Settings */ | |
// device type property IDs | |
// (the smart pan type ID is 55055efb5a06678498000001) | |
var type = { | |
'status': { id: '55055eff5a06673360000001' }, | |
'temperature': { id: '5505607f5a06673360000004' }, | |
'air': { id: '550560935a06673360000005' }, | |
'tempLimit': { id: '550560a65a0667cff2000001' }, | |
}; | |
// status property | |
var status; | |
// payload message | |
var message; | |
/* MQTT Connection */ | |
// MQTT connection settings | |
var mqtt = require('mqtt') | |
// client settings | |
var settings = { | |
host: '178.62.108.47', // MQTT server IP | |
port: '8883', // MQTT server port | |
username: device.id, // device.id as client username | |
password: device.secret, // device.secret as client password | |
protocol: 'ssl', | |
rejectUnauthorized : false | |
} | |
/* LED definition */ | |
var boardLight = new mraa.Pwm(3); // LED hooked up to digital pin 3 | |
boardLight.enable(true); // Set the gpio direction to output | |
var ledState = true; // Boolean to hold the state of led | |
boardLight.period_us(255) // Accepts intensity values between 0 and 255 | |
/* Temperature definition */ | |
var analogPin0 = new mraa.Aio(0); | |
/* Gas detection definition */ | |
var analogPin1 = new mraa.Aio(1); | |
// Fixed default temperature limit (100 for the boiling water) | |
var tempLimit = 100; | |
/* Lelylan MQTT client connection */ | |
var client = mqtt.connect(settings); | |
console.log('[Intel] Trying to connect to Lelylan MQTT servers'); | |
client.on('connect', function() { | |
console.log('Client successfully connected'); | |
/* Temperature reading */ | |
function readTemp() { | |
var analogValue = Math.round(analogPin0.read()/22, 2); | |
setTimeout(readTemp, 10000); | |
console.log('Publishing the temperature'); | |
message = {"properties": [{ "id": type.temperature.id, "value": analogValue}]}; | |
client.publish(out_topic, JSON.stringify(message)); | |
/* Light actuator */ | |
// if the temperature limit is surpassed the led is turned on | |
if (analogValue > tempLimit) { | |
console.log("Turn on light (TEMPERATURE) - ", analogValue); | |
boardLight.write(255); | |
message = {"properties": [{ "id": type.status.id, "value": "on"}]}; | |
client.publish(out_topic, JSON.stringify(message)); | |
} else { | |
console.log("Turn off light (TEMPERATURE) - ", analogValue); | |
boardLight.write(0); | |
message = {"properties": [{"id": type.status.id, "value": "off"}]}; | |
client.publish(out_topic, JSON.stringify(message)); | |
} | |
} | |
/* Gas reading */ | |
function readAir() { | |
var analogValue = analogPin1.read(); | |
setTimeout(readAir, 30000); | |
console.log('Publishing the AIR'); | |
message = {"properties": [{ "id": type.air.id, "value": analogValue}]}; | |
client.publish(out_topic, JSON.stringify(message)); | |
/* Light actuator */ | |
// if the gas limit goes over the security value turns the light on | |
if (analogValue > 500) { | |
console.log("Turn on light (GAS) - ", analogValue); | |
boardLight.write(255); | |
message = {"properties": [{ "id": type.status.id, "value": "on"}]}; | |
client.publish(out_topic, JSON.stringify(message)); | |
} else { | |
console.log("Turn off light (GAS) - ", analogValue); | |
boardLight.write(0); | |
message = {"properties": [{"id": type.status.id, "value": "off"}]}; | |
client.publish(out_topic, JSON.stringify(message)); | |
} | |
} | |
readTemp(); | |
readAir(); | |
/* Subscribes for incoming messages */ | |
client.subscribe(in_topic); | |
client.on('message', function(topic, message) { | |
var prop = JSON.parse(message).properties[0]; | |
console.log('Received message', status); | |
/* Set the temperature limit */ | |
if (prop.id == light.tempLimit.id) { | |
tempLimit = prop.value; | |
boardLight.write(255); | |
console.log('The temperature limit was set to', tempLimit); | |
} | |
client.publish(out_topic, JSON.stringify(message)); | |
console.log('Message received (topic)', topic, '(message)', message); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment