Skip to content
Permalink
Browse files
26 c for temp sensor
  • Loading branch information
kailad committed Dec 4, 2019
1 parent 24008cb commit 172c338d18155b138c7883be6d088ccf0502ec7e
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 76 deletions.
@@ -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,38 +163,37 @@ 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) {
await ctx.render('error', {message: err.message})
}
})

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) {
await ctx.render('error', {message: err.message})
}
})

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})
@@ -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,70 +72,67 @@ 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()
}
})
})
});
}

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()
}
})
})
});

}
}
@@ -11,17 +11,18 @@
<body>
<h1>Lights Page</h1>
<p>here you will be able to turn on or off lights.</p>
<h2>indoor light is: {{data2}}</h2>
<form action="/FrontLightON" method="get">
<p><input type="submit" value="Turn front Light on"></p>
</form>
<form action="/FrontLightOFF" method="get">
<p><input type="submit" value="Turn front Light off"></p>
</form>
<form action="/InsideLightON" method="get">
<p><input type="submit" value="Turn Inside Light on"></p>
<form action="/TurnSensorOn" method="get">
<p><input type="submit" value="Turn Inside sensor on"></p>
</form>
<form action="/InsideLightOFF" method="get">
<p><input type="submit" value="Turn Inside Light off"></p>
<form action="/TurnSensorOff" method="get">
<p><input type="submit" value="Turn Inisde sensor off"></p>
</form>
<form action="/MontionSensor" method="post">
<p>Set Sensor:<br /><input type="text" name="Sensor" placeholder="value" autofocus></p>
@@ -9,7 +9,7 @@
<body>
<h1>Temp</h1>
<p>Display tempature</p>
<h2>{{data2}}</h2>
<h2>{{data2}} {{data3}}</h2>
<form action="/temp" method="get">
<p><input type="submit" value="refresh temp reading"></p>
</form>

0 comments on commit 172c338

Please sign in to comment.