working with LCD

This commit is contained in:
Karl Hudgell 2020-09-14 08:17:22 +01:00
parent 428ae10d18
commit 282a0cf835
6 changed files with 111 additions and 1 deletions

5
app.js

@ -2,12 +2,15 @@ const { Gpio } = require('onoff');
const poll = require('./poll')
const common = require('./libs/common')
const globalVars = require('./libs/globalVars')
const lcd = require('./libs/lcd')
lcd.heading()
// lcd.time()
poll.setInterval
const switchIn = new Gpio('17', 'in', 'both');
switchIn.watch((err, value) => {
switchIn.watch( async (err, value) => {
if (err) {
console.log('Error', err);
}

11
lcd.js Normal file

@ -0,0 +1,11 @@
const LCD = require('raspberrypi-liquid-crystal');
const lcd = new LCD(1, 0x27, 20, 4);
lcd.beginSync();
lcd.clearSync();
lcd.printLineSync(0, 'Welcome to KWorld');
lcd.printLineSync(1, ' then');
lcd.printLineSync(2, 'This is line 3');
lcd.printLineSync(3, 'This is line 4');

22
libs/lcd.js Normal file

@ -0,0 +1,22 @@
const LCD = require('raspberrypi-liquid-crystal');
const lcd = new LCD(1, 0x27, 20, 4);
lcd.beginSync();
module.exports = {
heading: async () => {
lcd.clearSync();
lcd.printLineSync(0, 'MiGenie Hot Water')
lcd.printLineSync(1, ' By Karl')
},
time: async () => {
lcd.setCursorSync(10,2);
lcd.printSync('time')
},
updateScreen: async (input) => {
lcd.setCursorSync(3, 0);
lcd.printLineSync(3, input)
},
}

@ -6,6 +6,8 @@ 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 = {
@ -29,11 +31,13 @@ setInterval(function () {
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)
@ -44,6 +48,7 @@ setInterval(function () {
// Report errors
request.on('error', function (error) {
lcd.updateScreen('Error')
redLED.writeSync(0)
blueLED.writeSync(0)
greenLED.writeSync(1)

24
public/index.html Normal file

@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>
<body>
<h1>Control LED light</h1>
<p><input type="checkbox" id="light"></p>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js"></script> <!-- include socket.io client side script -->
<script>
var socket = io(); //load socket.io-client and connect to the host that serves the page
window.addEventListener("load", function(){ //when page loads
var lightbox = document.getElementById("light");
lightbox.addEventListener("change", function() { //add event listener for when checkbox changes
socket.emit("light", Number(this.checked)); //send button status to server (as 1 or 0)
});
});
socket.on('light', function (data) { //get button status from client
document.getElementById("light").checked = data; //change checkbox according to push button on Raspberry Pi
socket.emit("light", data); //send push button status to back to server
});
</script>
</body>
</html>

45
webserver.js Normal file

@ -0,0 +1,45 @@
var http = require('http').createServer(handler); //require http server, and create server with function handler()
var fs = require('fs'); //require filesystem module
var io = require('socket.io')(http) //require socket.io module and pass the http object (server)
var Gpio = require('onoff').Gpio; //include onoff to interact with the GPIO
var LED = new Gpio(4, 'out'); //use GPIO pin 4 as output
var pushButton = new Gpio(17, 'in', 'both'); //use GPIO pin 17 as input, and 'both' button presses, and releases should be handled
http.listen(8080); //listen to port 8080
function handler (req, res) { //create server
fs.readFile(__dirname + '/public/index.html', function(err, data) { //read file index.html in public folder
if (err) {
res.writeHead(404, {'Content-Type': 'text/html'}); //display 404 on error
return res.end("404 Not Found");
}
res.writeHead(200, {'Content-Type': 'text/html'}); //write HTML
res.write(data); //write data from index.html
return res.end();
});
}
io.sockets.on('connection', function (socket) {// WebSocket Connection
var lightvalue = 0; //static variable for current status
pushButton.watch(function (err, value) { //Watch for hardware interrupts on pushButton
if (err) { //if an error
console.error('There was an error', err); //output error message to console
return;
}
lightvalue = value;
socket.emit('light', lightvalue); //send button status to client
});
socket.on('light', function(data) { //get light switch status from client
lightvalue = data;
if (lightvalue != LED.readSync()) { //only change LED if status has changed
LED.writeSync(lightvalue); //turn LED on or off
}
});
});
process.on('SIGINT', function () { //on ctrl+c
LED.writeSync(0); // Turn LED off
LED.unexport(); // Unexport LED GPIO to free resources
pushButton.unexport(); // Unexport Button GPIO to free resources
process.exit(); //exit completely
});