working time, screen, organize a bit

This commit is contained in:
Karl Hudgell 2020-09-14 12:11:37 +01:00
parent 282a0cf835
commit db5597d702
14 changed files with 215 additions and 88 deletions

58
app.js
View File

@ -1,24 +1,50 @@
const { Gpio } = require('onoff'); const { Gpio } = require('onoff');
const poll = require('./poll') const hotwater = require('./modules/hotwater')
const common = require('./libs/common') const common = require('./libs/common')
const globalVars = require('./libs/globalVars') const globalVars = require('./libs/globalVars')
const led = require('./libs/led')
const lcd = require('./libs/lcd') const lcd = require('./libs/lcd')
lcd.heading()
// lcd.time()
poll.setInterval
const switchIn = new Gpio('17', 'in', 'both'); async function main() {
lcd.clearScreen()
await lcd.intro()
lcd.time()
hotwater.setInterval
switchIn.watch( async (err, value) => { const heatingSwitch = new Gpio('17', 'in', 'both');
if (err) { const appSwitch = new Gpio('23', 'in', 'both');
console.log('Error', err);
} heatingSwitch.watch(async (err, value) => {
if (value === 1) { if (err) {
if (globalVars.waterOn === 'true') { console.log('Error', err);
common.request(0)
} else {
common.request(1)
} }
} if (value === 1) {
}) if (globalVars.waterOn === 'true') {
common.request(0)
} else {
common.request(1)
}
}
})
appSwitch.watch(async (err, value) => {
if (err) {
console.log('Error', err);
}
if (value === 1) {
console.log('pressed')
}
})
}
process.stdin.resume();
process.on('SIGINT', function () {
lcd.clearScreen()
led.set('off')
process.exit(1)
});
main()

12
libs/clock.js Normal file
View File

@ -0,0 +1,12 @@
const moment = require('moment'); // require
module.exports = {
time: () => {
try {
t = moment().format('HH:MM');
return t
} catch (error) {
console.log(error)
}
}
}

View File

@ -15,4 +15,7 @@ module.exports = {
}); });
}, },
pause: async (time) => {
await new Promise(resolve => setTimeout(resolve, time));
},
} }

View File

@ -1,5 +1,6 @@
class globalVariables { class globalVariables {
waterOn = ""; waterOn = "";
initialWater = "";
} }
module.exports = new globalVariables(); module.exports = new globalVariables();

View File

@ -1,22 +1,52 @@
const LCD = require('raspberrypi-liquid-crystal'); const LCD = require('raspberrypi-liquid-crystal');
const common = require('./common')
const globalVars = require('./globalVars')
const clock = require('./clock');
const { time } = require('./clock');
const lcd = new LCD(1, 0x27, 20, 4); const lcd = new LCD(1, 0x27, 20, 4);
lcd.beginSync(); lcd.beginSync();
module.exports = { module.exports = {
heading: async () => { clearScreen: async () => {
lcd.clearSync(); lcd.clearSync();
lcd.printLineSync(0, 'MiGenie Hot Water') },
lcd.printLineSync(1, ' By Karl') clearLine: async (line) => {
line = line - 1
lcd.setCursorSync(0, line);
lcd.printSync(' ')
},
intro: async () => {
lcd.clearSync();
lcd.printLineSync(1, 'MiGenie Status')
lcd.printLineSync(2, ' By Karl')
await common.pause(3000)
module.exports.heading('Hot Water')
},
heading: async (title) => {
lcd.clearSync();
lcd.printLineSync(2, title)
}, },
time: async () => { time: async () => {
lcd.setCursorSync(10,2); // set interval
lcd.printSync('time') var tid = setInterval(mycode, 60000);
async function mycode() {
lcd.setCursorSync(15, 0);
let f
f = await clock.time()
lcd.printSync(f)
}
function abortTimer() { // to be called when you want to stop the timer
clearInterval(tid);
}
}, },
updateScreen: async (input) => { heatingStatus: async (input) => {
lcd.setCursorSync(3, 0); lcd.setCursorSync(0, 3);
lcd.printLineSync(3, input) lcd.printSync(' ')
lcd.setCursorSync(0, 3);
lcd.printSync(' ' + input)
}, },
} }

35
libs/led.js Normal file
View File

@ -0,0 +1,35 @@
const { Gpio } = require('onoff');
const redLED = new Gpio('4', 'out');
const blueLED = new Gpio('27', 'out')
const greenLED = new Gpio('22', 'out')
module.exports = {
set: async (colour) => {
switch (colour) {
case 'red':
redLED.writeSync(1)
blueLED.writeSync(0)
greenLED.writeSync(0)
break;
case 'blue':
blueLED.writeSync(1)
redLED.writeSync(0)
greenLED.writeSync(0)
break;
case 'green':
blueLED.writeSync(0)
redLED.writeSync(0)
greenLED.writeSync(1)
break;
case 'off':
blueLED.writeSync(0)
redLED.writeSync(0)
greenLED.writeSync(0)
break;
default:
break;
}
},
}

72
modules/hotwater.js Normal file
View File

@ -0,0 +1,72 @@
const http = require('http')
const { Gpio } = require('onoff');
const globalVars = require('../libs/globalVars')
const led = require('../libs/led')
const redLED = new Gpio('4', 'out');
const blueLED = new Gpio('27', 'out')
const greenLED = new Gpio('22', 'out')
const lcd = require('../libs/lcd')
setInterval(function () {
var rest_options = {
host: '192.168.4.5',
port: 2020,
path: '/water/status',
method: 'GET'
};
var request = http.request(rest_options, function (response) {
var content = "";
// Handle data chunks
response.on('data', function (chunk) {
content += chunk;
});
// Once we're done streaming the response, parse it as json.
response.on('end', function () {
var data = JSON.parse(content);
let waterOn = data.waterOn
if (globalVars.waterOn === "" || globalVars.waterOn === "error") {
if (waterOn === 'true') {
lcd.heatingStatus('Heating')
led.set('red')
globalVars.waterOn = waterOn
} else {
lcd.heatingStatus('Not Heating')
led.set('blue')
globalVars.waterOn = waterOn
}
} else {
if (waterOn != globalVars.waterOn) {
if (waterOn === 'true') {
lcd.heatingStatus('Heating')
led.set('red')
globalVars.waterOn = waterOn
} else {
lcd.heatingStatus('Not Heating')
led.set('blue')
globalVars.waterOn = waterOn
}
}
}
});
});
// Report errors
request.on('error', function (error) {
lcd.heatingStatus('Error')
led.set('green')
lcd.heatingStatus('Error No Data')
globalVars.waterOn = 'error'
});
request.end();
}, 5000);
module.exports = {
setInterval
}

5
package-lock.json generated
View File

@ -397,6 +397,11 @@
"resolved": "https://registry.npmjs.org/mimic-response/-/mimic-response-1.0.1.tgz", "resolved": "https://registry.npmjs.org/mimic-response/-/mimic-response-1.0.1.tgz",
"integrity": "sha512-j5EctnkH7amfV/q5Hgmoal1g2QHFJRraOtmx0JpIqkxhBhI/lJSl1nMpQ45hVarwNETOoWEimndZ4QK0RHxuxQ==" "integrity": "sha512-j5EctnkH7amfV/q5Hgmoal1g2QHFJRraOtmx0JpIqkxhBhI/lJSl1nMpQ45hVarwNETOoWEimndZ4QK0RHxuxQ=="
}, },
"moment": {
"version": "2.28.0",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.28.0.tgz",
"integrity": "sha512-Z5KOjYmnHyd/ukynmFd/WwyXHd7L4J9vTI/nn5Ap9AVUgaAE15VvQ9MOGmJJygEUklupqIrFnor/tjTwRU+tQw=="
},
"ms": { "ms": {
"version": "2.1.2", "version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",

View File

@ -12,6 +12,7 @@
"fs": "0.0.1-security", "fs": "0.0.1-security",
"got": "^11.6.2", "got": "^11.6.2",
"http": "0.0.1-security", "http": "0.0.1-security",
"moment": "^2.28.0",
"onoff": "^6.0.0", "onoff": "^6.0.0",
"pigpio": "^3.2.3", "pigpio": "^3.2.3",
"raspberrypi-liquid-crystal": "^1.15.0", "raspberrypi-liquid-crystal": "^1.15.0",

63
poll.js
View File

@ -1,63 +0,0 @@
const http = require('http')
const { Gpio } = require('onoff');
const globalVars = require('./libs/globalVars')
const redLED = new Gpio('4', 'out');
const blueLED = new Gpio('27', 'out')
const greenLED = new Gpio('22', 'out')
const lcd = require('./libs/lcd')
setInterval(function () {
var rest_options = {
host: '192.168.4.5',
port: 2020,
path: '/water/status',
method: 'GET'
};
var request = http.request(rest_options, function (response) {
var content = "";
// Handle data chunks
response.on('data', function (chunk) {
content += chunk;
});
// Once we're done streaming the response, parse it as json.
response.on('end', function () {
var data = JSON.parse(content);
let waterOn = data.waterOn
globalVars.waterOn = waterOn
if (waterOn === 'true') {
lcd.updateScreen('Heating')
redLED.writeSync(1)
blueLED.writeSync(0)
greenLED.writeSync(0)
} else {
lcd.updateScreen('Not Heating')
blueLED.writeSync(1)
redLED.writeSync(0)
greenLED.writeSync(0)
}
});
});
// Report errors
request.on('error', function (error) {
lcd.updateScreen('Error')
redLED.writeSync(0)
blueLED.writeSync(0)
greenLED.writeSync(1)
console.log("Error while calling endpoint.", error);
});
request.end();
}, 5000);
module.exports = {
setInterval
}

5
test.js Normal file
View File

@ -0,0 +1,5 @@
const moment = require('moment'); // require
t = moment().format('HH:MM:ss');
console.log(t)