From 172c338d18155b138c7883be6d088ccf0502ec7e Mon Sep 17 00:00:00 2001 From: Dharmindra Kaila Date: Wed, 4 Dec 2019 15:34:16 +0000 Subject: [PATCH] 26 c for temp sensor --- index.js | 92 +++++++++++++++++++++++++++------ modules/lights.js | 112 ++++++++++++++++++++-------------------- views/lights.handlebars | 9 ++-- views/temp.handlebars | 2 +- 4 files changed, 139 insertions(+), 76 deletions(-) diff --git a/index.js b/index.js index 03d83ba..16ce947 100644 --- a/index.js +++ b/index.js @@ -24,6 +24,8 @@ const Lights = require('./modules/lights') const app = new Koa() const router = new Router() const data2 = {} +const data3= {} +let tempfloat = 0 /* CONFIGURING THE MIDDLEWARE */ app.keys = ['darkSecret'] @@ -58,7 +60,29 @@ const wss = new WebSocket.Server({ threshold: 1024 // Size (in bytes) below which messages // should not be compressed. } - }); + }); + const wss2 = new WebSocket.Server({ + port: 8980, + perMessageDeflate: { + zlibDeflateOptions: { + // See zlib defaults. + chunkSize: 1024, + memLevel: 7, + level: 3 + }, + zlibInflateOptions: { + chunkSize: 10 * 1024 + }, + // Other options settable: + clientNoContextTakeover: true, // Defaults to negotiated value. + serverNoContextTakeover: true, // Defaults to negotiated value. + serverMaxWindowBits: 10, // Defaults to negotiated value. + // Below options specified as default values. + concurrencyLimit: 10, // Limits zlib concurrency for perf. + threshold: 1024 // Size (in bytes) below which messages + // should not be compressed. + } + }); /** * The secure home page. * @@ -84,7 +108,36 @@ router.get('/', async ctx => { * @route {GET} /register */ router.get('/register', async ctx => await ctx.render('register')) -router.get('/lights', async ctx => await ctx.render('lights')) +router.get('/lights', async ctx => { + try { + //const temp = await new Temp(dbName) + //data2.two = await temp.reading() + const connection = new WebSocket('ws://localhost:8980') + wss2.on('connection', function connection(ws) { + const client = mqtt.connect('mqtt.coventry.ac.uk', { + host: 'mqtt.coventry.ac.uk', + protocol: 'mqtts', + username: '302CEM', + password: 'n3fXXFZrjw', + port: 8883, + }) + client.on('connect', function () { + client.subscribe('302CEM/Team2/Lights01/', function (err) { + }) + }) + client.on('message', function (topic, message) { + data2.three = message.toString() + //console.log(message.toString()) + console.log(data2.three) + + }) + }); + await ctx.render('lights', {data2: data2.three}) + } + catch(err) { + await ctx.render('error', {message: err.message}) + } +}) router.get('/FrontLightON', koaBody, async ctx => { @@ -110,12 +163,13 @@ router.get('/FrontLightOFF', koaBody, async ctx => { await ctx.render('error', {message: err.message}) } }) - -router.get('/InsideLightOFF', koaBody, async ctx => { +router.post('/MontionSensor', async ctx => { try { + const body = ctx.request.body + console.log(body.Sensor) const lights = await new Lights(dbName) - await lights.insideOff() - alert('inside light is off') + await lights.sensorDelay(body.Sensor) + alert('Sensor Delay set') await ctx.render('lights') } catch(err) { @@ -123,11 +177,11 @@ router.get('/InsideLightOFF', koaBody, async ctx => { } }) -router.get('/InsideLightON', koaBody, async ctx => { +router.get('/TurnSensorOn', async ctx => { try { const lights = await new Lights(dbName) - await lights.insideOn() - alert('inside light is on') + await lights.FrontSensorOn() + alert('Front Sensor turned on') await ctx.render('lights') } catch(err) { @@ -135,13 +189,11 @@ router.get('/InsideLightON', koaBody, async ctx => { } }) -router.post('/MontionSensor', async ctx => { +router.get('/TurnSensorOff', async ctx => { try { - const body = ctx.request.body - console.log(body.Sensor) const lights = await new Lights(dbName) - await lights.sensor(body.Sensor) - alert('Sensor set') + await lights.FrontSensorOff() + alert('Front Sensor turned off') await ctx.render('lights') } catch(err) { @@ -151,6 +203,7 @@ router.post('/MontionSensor', async ctx => { router.get('/temp', async ctx => { try { + //const temp = await new Temp(dbName) //data2.two = await temp.reading() alert('temp refreshed') @@ -169,12 +222,21 @@ router.get('/temp', async ctx => { }) client.on('message', function (topic, message) { data2.two = message.toString() + tempfloat = parseFloat(data2.two) //console.log(message.toString()) console.log(data2.two) }) }); - await ctx.render('temp', {data2: data2.two}) + if (tempfloat < 26) + { + data3.heating = 'heating is on' + } + else + { + data3.heating = 'heating is off' + } + await ctx.render('temp', {data2: data2.two, data3: data3.heating}) } catch(err) { await ctx.render('error', {message: err.message}) diff --git a/modules/lights.js b/modules/lights.js index f459857..90bdfc5 100644 --- a/modules/lights.js +++ b/modules/lights.js @@ -48,13 +48,13 @@ module.exports = class Lights { port: 8883, }) client.on('connect', function () { - client.subscribe('302CEM/Team2/kailad', function (err) { + client.subscribe('302CEM/Team2/FrontLight/LED/', function (err) { if (!err){ var options={ retain:false, clear: true, qos:1}; - client.publish('302CEM/Team2/kailad', 'Exterior light on', options) + client.publish('302CEM/Team2/FrontLight/LED/', 'ON', options) client.end() } }) @@ -72,13 +72,9 @@ module.exports = class Lights { port: 8883, }) client.on('connect', function () { - client.subscribe('302CEM/Team2/kailad', function (err) { + client.subscribe('302CEM/Team2/FrontLight/LED/', function (err) { if (!err){ - var options={ - retain:false, - clear: true, - qos:1}; - client.publish('302CEM/Team2/kailad', 'Exterior light off', options) + client.publish('302CEM/Team2/FrontLight/LED/', 'OFF') client.end() } }) @@ -86,56 +82,57 @@ module.exports = class Lights { }); } - async insideOff() { - const connection = new WebSocket('ws://localhost:3000') - wss.on('connection', function connection(ws) { - const client = mqtt.connect('mqtt.coventry.ac.uk', { - host: 'mqtt.coventry.ac.uk', - protocol: 'mqtts', - username: '302CEM', - password: 'n3fXXFZrjw', - port: 8883, - }) - client.on('connect', function () { - client.subscribe('302CEM/Team2/kailad', function (err) { - if (!err){ - var options={ - retain:false, - clear: true, - qos:1}; - client.publish('302CEM/Team2/kailad', 'inside light off', options) - client.end() - } - }) +async FrontSensorOn() { + const connection = new WebSocket('ws://localhost:3000') + wss.on('connection', function connection(ws) { + const client = mqtt.connect('mqtt.coventry.ac.uk', { + host: 'mqtt.coventry.ac.uk', + protocol: 'mqtts', + username: '302CEM', + password: 'n3fXXFZrjw', + port: 8883, + }) + client.on('connect', function () { + client.subscribe('302CEM/Team2/FrontLight/PIR/', function (err) { + if (!err){ + var options={ + retain:false, + clear: true, + qos:1}; + client.publish('302CEM/Team2/FrontLight/PIR/', 'ENABLE', options) + client.end() + } }) - }); + }) +}); } -async insideOn() { - const connection = new WebSocket('ws://localhost:3000') - wss.on('connection', function connection(ws) { - const client = mqtt.connect('mqtt.coventry.ac.uk', { - host: 'mqtt.coventry.ac.uk', - protocol: 'mqtts', - username: '302CEM', - password: 'n3fXXFZrjw', - port: 8883, - }) - client.on('connect', function () { - client.subscribe('302CEM/Team2/kailad', function (err) { - if (!err){ - var options={ - retain:false, - clear: true, - qos:1}; - client.publish('302CEM/Team2/kailad', 'inside light on', options) - client.end() - } - }) +async FrontSensorOff() { + const connection = new WebSocket('ws://localhost:3000') + wss.on('connection', function connection(ws) { + const client = mqtt.connect('mqtt.coventry.ac.uk', { + host: 'mqtt.coventry.ac.uk', + protocol: 'mqtts', + username: '302CEM', + password: 'n3fXXFZrjw', + port: 8883, + }) + client.on('connect', function () { + client.subscribe('302CEM/Team2/FrontLight/PIR/', function (err) { + if (!err){ + var options={ + retain:false, + clear: true, + qos:1}; + client.publish('302CEM/Team2/FrontLight/PIR/', 'DISABLE', options) + client.end() + } }) - }); + }) +}); } -async sensor(sensor) { + +async sensorDelay(sensor) { const connection = new WebSocket('ws://localhost:3000') wss.on('connection', function connection(ws) { const client = mqtt.connect('mqtt.coventry.ac.uk', { @@ -146,17 +143,20 @@ async sensor(sensor) { port: 8883, }) client.on('connect', function () { - client.subscribe('302CEM/Team2/kailad', function (err) { + client.subscribe('302CEM/Team2/FrontLight/PIR/DELAY/', function (err) { if (!err){ var options={ retain:false, clear: true, - qos:1}; - client.publish('302CEM/Team2/kailad', `"${sensor}";`) + qos:1, + dup: false + }; + client.publish('302CEM/Team2/FrontLight/PIR/DELAY/', `${sensor}`) client.end() } }) }) }); + } } \ No newline at end of file diff --git a/views/lights.handlebars b/views/lights.handlebars index 5dc1698..280fd7f 100644 --- a/views/lights.handlebars +++ b/views/lights.handlebars @@ -11,17 +11,18 @@

Lights Page

here you will be able to turn on or off lights.

+

indoor light is: {{data2}}

-
-

+ +

-
-

+ +

Set Sensor:

diff --git a/views/temp.handlebars b/views/temp.handlebars index c7728a3..5b3c599 100644 --- a/views/temp.handlebars +++ b/views/temp.handlebars @@ -9,7 +9,7 @@

Temp

Display tempature

-

{{data2}}

+

{{data2}} {{data3}}