From da39b810b6fcd6fffa2ec1bb4970a17f396fc1a1 Mon Sep 17 00:00:00 2001 From: Martin Donnelly Date: Fri, 24 Jun 2016 14:44:21 +0100 Subject: [PATCH] Now controls the fan --- lib/mqtt/mqttClient.js | 43 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/lib/mqtt/mqttClient.js b/lib/mqtt/mqttClient.js index 4bfac3f..0e2b0d8 100644 --- a/lib/mqtt/mqttClient.js +++ b/lib/mqtt/mqttClient.js @@ -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; }; + + }; /*