Now controls the fan

This commit is contained in:
Martin Donnelly 2016-06-24 14:44:21 +01:00
parent 14b32f2819
commit da39b810b6

View File

@ -1,12 +1,38 @@
var mqtt = require('mqtt');
var request = require('request');
var mqttClient = function() {
var orgId = '';
var userName = '';
var appKey = 'bob';
this.livingRoom = {
temp: 0,
last: 0
};
this.fanTimer = function() {
'use strict';
var now = new Date;
var mod = 300000 - (now.getTime() % 300000);
var mode = (this.livingRoom.temp > 22.0) ? 'FanOn' : 'FanOff';
var url = ['https://maker.ifttt.com/trigger/',mode,'/with/key/cWvECkeiyAPwmiOPBkXL2D'].join('');
console.log('LR temp:', this.livingRoom.temp);
request(url, function(error, response, body) {
if (!error && response.statusCode === 200) {
console.log(body);
}
});
setTimeout(this.fanTimer.bind(this),mod + 10);
};
this.lighting = 'lights';
this.connected = false;
@ -24,16 +50,25 @@ var mqttClient = function() {
this.client.on('connect', function() {
connected = true;
console.log('Connected to SIlvr Broker');
});
this.fanTimer();
}.bind(this));
this.client.subscribe('lightsOut');
this.client.subscribe('livingroomTemp');
this.client.on('message', function(topic, message) {
console.log(message);
});
var json = JSON.parse(message.toString());
console.log(json);
console.log(json.temp);
this.livingRoom.temp = json.temp;
}.bind(this));
this.isConnected = function() {
return this.connected;
};
};
/*