Skip to content
Permalink
66009468df
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
144 lines (129 sloc) 4.08 KB
#!/usr/bin/env node
const Koa = require('koa');
const app = new Koa()
const Router = require('koa-router')
const WebSocket = require('ws');
const mqtt = require('mqtt');
const json = require('koa-json');
const path = require('path');
const render = require('koa-ejs')
const router = Router();
const port = 9001
var LightOnOff = false;
var LightConnected = false;
render(app, {
root: path.join(__dirname, 'views'),
layout: 'layout',
viewExt: 'html',
cashe: false,
debug: false,
})
const wss = new WebSocket.Server({
port: 3000,
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.
}
});
router.post('/ExteriorLightOnOff', onoff);
router.get('/Lights', async ctx =>{
await ctx.render('lights');
if (LightConnected == false){
LightConnected = true;
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){
client.publish('302CEM/Team2/kailad', 'Subbed')
}
})
})
});
}
else {
}
})
async function onoff(ctx) {
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,
})
if (LightOnOff == false){
client.on('connect', function () {
client.subscribe('302CEM/Team2/kailad', function (err) {
if (!err){
LightOnOff = true;
client.publish('302CEM/Team2/kailad', 'Turn on the Exterior Light')
}
})
})
}
else if (LightOnOff == true){
client.on('connect', function () {
client.subscribe('302CEM/Team2/kailad', function (err) {
if (!err){
LightOnOff = false;
client.publish('302CEM/Team2/kailad', 'Turn off the Exterior Light')
}
})
})
}
});
ctx.redirect('/lights')
}
router.get ('/temperature', async ctx =>{
await ctx.render('temperature');
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/temp01', function (err) {
if (!err){
client.publish('302CEM/Team2/temp01', 'subbed')
}
})
})
client.on('message', function(topic, message, packet){
ws.send(`${message}`)
})
});
})
app.use(json());
app.use(router.routes());
module.exports = app.listen(port, () => console.log(`listening on port ${port}`))
//mosquitto_sub -v -h mqtt.coventry.ac.uk --cafile mqtt.crt -p 8883 -u 302CEM -P n3fXXFZrjw -t 302CEM/Team2/kailad
//mosquitto_pub -h mqtt.coventry.ac.uk -p 8883 --cafile mqtt.crt -u 302CEM -P n3fXXFZrjw -t 302CEM/Team2/kailad -m "why doesnt mqtt work?"