diff --git a/app.js b/app.js index 986ee4b..91557c8 100644 --- a/app.js +++ b/app.js @@ -1,24 +1,50 @@ const { Gpio } = require('onoff'); -const poll = require('./poll') +const hotwater = require('./modules/hotwater') const common = require('./libs/common') const globalVars = require('./libs/globalVars') +const led = require('./libs/led') 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) => { - if (err) { - console.log('Error', err); - } - if (value === 1) { - if (globalVars.waterOn === 'true') { - common.request(0) - } else { - common.request(1) + const heatingSwitch = new Gpio('17', 'in', 'both'); + const appSwitch = new Gpio('23', 'in', 'both'); + + heatingSwitch.watch(async (err, value) => { + if (err) { + console.log('Error', err); } - } -}) \ No newline at end of file + 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() \ No newline at end of file diff --git a/libs/clock.js b/libs/clock.js new file mode 100644 index 0000000..eae05dc --- /dev/null +++ b/libs/clock.js @@ -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) + } + } +} diff --git a/libs/common.js b/libs/common.js index 85e8909..e77186f 100644 --- a/libs/common.js +++ b/libs/common.js @@ -15,4 +15,7 @@ module.exports = { }); }, + pause: async (time) => { + await new Promise(resolve => setTimeout(resolve, time)); + }, } \ No newline at end of file diff --git a/libs/globalVars.js b/libs/globalVars.js index a9dee08..65f019d 100644 --- a/libs/globalVars.js +++ b/libs/globalVars.js @@ -1,5 +1,6 @@ class globalVariables { waterOn = ""; + initialWater = ""; } module.exports = new globalVariables(); \ No newline at end of file diff --git a/libs/lcd.js b/libs/lcd.js index 8bd57cc..d7f5c16 100644 --- a/libs/lcd.js +++ b/libs/lcd.js @@ -1,22 +1,52 @@ 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); lcd.beginSync(); module.exports = { - heading: async () => { + clearScreen: async () => { 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 () => { - lcd.setCursorSync(10,2); - lcd.printSync('time') + // set interval + 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) => { - lcd.setCursorSync(3, 0); - lcd.printLineSync(3, input) + heatingStatus: async (input) => { + lcd.setCursorSync(0, 3); + lcd.printSync(' ') + lcd.setCursorSync(0, 3); + lcd.printSync(' ' + input) }, - } \ No newline at end of file diff --git a/libs/led.js b/libs/led.js new file mode 100644 index 0000000..ac6f017 --- /dev/null +++ b/libs/led.js @@ -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; + } + }, +} \ No newline at end of file diff --git a/modules/hotwater.js b/modules/hotwater.js new file mode 100644 index 0000000..0ea8755 --- /dev/null +++ b/modules/hotwater.js @@ -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 +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 154aaa1..d0d718e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -397,6 +397,11 @@ "resolved": "https://registry.npmjs.org/mimic-response/-/mimic-response-1.0.1.tgz", "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": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", diff --git a/package.json b/package.json index f58df55..c56a79a 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "fs": "0.0.1-security", "got": "^11.6.2", "http": "0.0.1-security", + "moment": "^2.28.0", "onoff": "^6.0.0", "pigpio": "^3.2.3", "raspberrypi-liquid-crystal": "^1.15.0", diff --git a/poll.js b/poll.js deleted file mode 100644 index ea7d768..0000000 --- a/poll.js +++ /dev/null @@ -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 -} \ No newline at end of file diff --git a/lcd.js b/samples/lcd.js similarity index 100% rename from lcd.js rename to samples/lcd.js diff --git a/rgb.js b/samples/rgb.js similarity index 100% rename from rgb.js rename to samples/rgb.js diff --git a/webserver.js b/samples/webserver.js similarity index 100% rename from webserver.js rename to samples/webserver.js diff --git a/test.js b/test.js new file mode 100644 index 0000000..547f570 --- /dev/null +++ b/test.js @@ -0,0 +1,5 @@ +const moment = require('moment'); // require + + + t = moment().format('HH:MM:ss'); + console.log(t)