Changed the fanToggling to only be done by one script, changed server.js to reflect these changes and also needed to do some changes to index.html. These being checking the return text and some linting.

This commit is contained in:
2017-02-19 11:55:48 +01:00
parent 5c4cf195e0
commit 0cb52876a1
13 changed files with 110 additions and 541 deletions

2
.gitignore vendored
View File

@@ -1 +1,3 @@
node_modules/
npm-debug.log
nohup.out

View File

@@ -1,106 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="apple-mobile-web-app-capable" content="yes">
<title>Fan Controller</title>
<link href='http://fonts.googleapis.com/css?family=Ubuntu' rel='stylesheet' type='text/css'>
<!-- Latest compiled and minified CSS -->
<link rel="apple-touch-icon" sizes="180x180" href="images/apple-touch-icon.png">
<link rel="icon" type="image/png" href="images/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="images/favicon-16x16.png" sizes="16x16">
<link rel="manifest" href="images/manifest.json">
<link rel="mask-icon" href="images/safari-pinned-tab.svg" color="#5bbad5">
<link rel="shortcut icon" href="images/favicon.ico">
<meta name="msapplication-config" content="images/browserconfig.xml">
<meta name="theme-color" content="#ffffff">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<style type="text/css">
html * {
font-family: 'Ubuntu', sans-serif !important;
}
.myContainer {
margin-top: 20px;
margin-left: auto;
margin-right: auto;
width: 100%;
}
.myBg {
background-color: #eee;
}
.btn {
height: 60px;
width: 120px;
margin-right: 10px;
margin-left: 10px;
}
.state_display {
height: 150px;
width: 80%;
margin-top: 70px;
margin-left: auto;
margin-right: auto;
padding-top: 45px;
background-color: white;
border-style: solid;
border-width: 0px;
border-radius: 4px;
text-align: center;
font-size: 40px;
}
.state_display h4 {
position: absolute;
margin-top: -70px;
}
.footer {
width: 100%;
margin: 0 auto;
position: fixed;
bottom: 0;
padding-bottom: 15px;
text-align: center;
}
</style>
</head>
<body class="myBg">
<div class="myContainer">
<div style="text-align:center; margin-bottom:20px;">
<h2>Fan Controller</h2>
</div>
<hr>
<div style="text-align:center;">
<button class="btn btn-success" id="on" type="submit" onclick="UserAction('on')">ON</button>
<button class="btn btn-danger" id="off" type="submit" onclick="UserAction('off')">OFF</button>
</div>
<div class="state_display">
<h4>Current fan state:</h4>
<span>ON</span>
</div>
<hr>
</div>
</div>
<div class="footer">
<p class="muted">
<small>Powered by C.H.I.P</small>
</p>
<img src="images/chip.png" style="width: 60px;" />
</body>
<script type="text/javascript">
function UserAction(state) {
var xhttp = new XMLHttpRequest();
xhttp.open("POST", state, false);
xhttp.setRequestHeader("Content-type", "application/text");
xhttp.send();
}
</script>
</html>

355
nohup.out
View File

@@ -1,355 +0,0 @@
Example app listening at http://0.0.0.0:3000
Example app listening at http://0.0.0.0:3000
Example app listening at http://0.0.0.0:3000
Got a POST request for OFF
finished
Got a POST request for ON
finished
Got a POST request for OFF
finished
off
off
Got a POST request for ON
finished
Got a POST request for OFF
finished
off
Got a POST request for ON
finished
Got a POST request for OFF
finished
off
Got a POST request for ON
finished
Got a POST request for OFF
finished
off
off
Example app listening at http://0.0.0.0:3000
off
off
off
off
off
Got a POST request for ON
finished
Got a POST request for OFF
finished
off
Got a POST request for OFF
finished
Got a POST request for ON
finished
Got a POST request for OFF
finished
Got a POST request for ON
finished
Got a POST request for OFF
finished
Got a POST request for ON
finished
on
Got a POST request for OFF
finished
off
Got a POST request for ON
finished
on
on
on
Got a POST request for OFF
finished
Got a POST request for ON
finished
Got a POST request for OFF
finished
Got a POST request for ON
finished
Got a POST request for OFF
finished
Got a POST request for OFF
finished
Got a POST request for ON
finished
Got a POST request for OFF
finished
Got a POST request for OFF
finished
Got a POST request for ON
finished
Got a POST request for OFF
finished
Got a POST request for ON
finished
Got a POST request for OFF
finished
off
off
off
Got a POST request for ON
finished
Got a POST request for OFF
finished
Got a POST request for OFF
Got a POST request for OFF
Got a POST request for OFF
Got a POST request for OFF
Got a POST request for OFF
Got a POST request for OFF
Got a POST request for OFF
finished
finished
finished
finished
finished
/home/chip/fanController/server.js:40
if (err) throw err;
^
Error: RuntimeError: Error setting up channel CSID0, maybe already exported? (gpio_export: could not write '132' to /sys/class/gpio/export (Device or resource busy))
at PythonShell.parseError (/home/chip/fanController/node_modules/python-shell/index.js:183:17)
at terminateIfNeeded (/home/chip/fanController/node_modules/python-shell/index.js:98:28)
at ChildProcess.<anonymous> (/home/chip/fanController/node_modules/python-shell/index.js:88:9)
at ChildProcess.emit (events.js:98:17)
at Process.ChildProcess._handle.onexit (child_process.js:809:12)
----- Python Traceback -----
File "led-csid0-off.py", line 4, in <module>
GPIO.setup("CSID0", GPIO.OUT) #set CSID0 as an output
Example app listening at http://0.0.0.0:3000
off
off
Example app listening at http://0.0.0.0:3000
off
off
Example app listening at http://0.0.0.0:3000
off
off
off
off
Got a POST request for OFF
finished
Example app listening at http://0.0.0.0:3000
off
Got a POST request for OFF
finished
off
off
off
off
off
Got a POST request for OFF
finished
Example app listening at http://0.0.0.0:3000
off
Got a POST request for OFF
Got a POST request for OFF
finished
finished
off
off
off
off
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:904:11)
at Server._listen2 (net.js:1042:14)
at listen (net.js:1064:10)
at Server.listen (net.js:1138:5)
at EventEmitter.listen (/home/chip/fanController/node_modules/express/lib/application.js:617:24)
at Object.<anonymous> (/home/chip/fanController/server.js:46:18)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
Example app listening at http://0.0.0.0:3000
off
Got a POST request for OFF
finished
off
off
off
Got a POST request for OFF
finished
Got a POST request for OFF
finished
off
off
off
off
off
Example app listening at http://0.0.0.0:3000
off
Got a POST request for OFF
/home/chip/fanController/server.js:40
if (err) throw err;
^
Error: python: can't open file 'home/chip/fanController/led-csid0-off.py': [Errno 2] No such file or directory
at PythonShell.parseError (/home/chip/fanController/node_modules/python-shell/index.js:190:17)
at terminateIfNeeded (/home/chip/fanController/node_modules/python-shell/index.js:98:28)
at ChildProcess.<anonymous> (/home/chip/fanController/node_modules/python-shell/index.js:88:9)
at ChildProcess.emit (events.js:98:17)
at Process.ChildProcess._handle.onexit (child_process.js:809:12)
Example app listening at http://0.0.0.0:3000
off
off
Got a POST request for OFF
/home/chip/fanController/server.js:40
if (err) throw err;
^
Error: python: can't open file 'home/chip/fanController/led-csid0-off.py': [Errno 2] No such file or directory
at PythonShell.parseError (/home/chip/fanController/node_modules/python-shell/index.js:190:17)
at terminateIfNeeded (/home/chip/fanController/node_modules/python-shell/index.js:98:28)
at ChildProcess.<anonymous> (/home/chip/fanController/node_modules/python-shell/index.js:88:9)
at ChildProcess.emit (events.js:98:17)
at Process.ChildProcess._handle.onexit (child_process.js:809:12)
Example app listening at http://0.0.0.0:3000
off
Got a POST request for OFF
finished
Got a POST request for OFF
finished
Got a POST request for OFF
finished
Got a POST request for OFF
finished
Got a POST request for OFF
finished
off
Got a POST request for OFF
finished
off
off
Got a POST request for OFF
finished
off
off
Got a POST request for ON
finished
on
on
on
Got a POST request for OFF
finished
Got a POST request for ON
finished
Got a POST request for OFF
finished
off
Got a POST request for ON
finished
on
Got a POST request for ON
finished
on
Got a POST request for OFF
finished
Got a POST request for ON
finished
Got a POST request for ON
finished
on
Got a POST request for OFF
finished
Got a POST request for ON
finished
on
Got a POST request for OFF
finished
Got a POST request for ON
finished
on
Got a POST request for OFF
finished
Got a POST request for ON
finished
Got a POST request for OFF
finished
Got a POST request for OFF
finished
Got a POST request for ON
finished
on
Got a POST request for OFF
finished
off
Got a POST request for OFF
finished
off
Got a POST request for ON
finished
on
Got a POST request for OFF
finished
Got a POST request for ON
finished
Got a POST request for OFF
finished
off
Got a POST request for ON
finished
on
Got a POST request for OFF
finished
Got a POST request for ON
finished
Got a POST request for OFF
finished
off
Got a POST request for OFF
finished
off
Got a POST request for ON
finished
Got a POST request for OFF
finished
off
off
Got a POST request for ON
finished
on
Got a POST request for OFF
finished
off
Got a POST request for OFF
Got a POST request for ON
finished
finished
Got a POST request for ON
finished
Got a POST request for OFF
finished
Got a POST request for ON
finished
Got a POST request for ON
finished
on
Got a POST request for ON
finished
Got a POST request for OFF
finished
Got a POST request for ON
finished
on
Got a POST request for OFF
finished
Got a POST request for ON
finished
on
Got a POST request for OFF
finished

View File

@@ -1,17 +0,0 @@
0 info it worked if it ends with ok
1 verbose cli [ '/usr/bin/nodejs', '/usr/bin/npm', 'start' ]
2 info using npm@1.4.21
3 info using node@v0.10.29
4 error Error: ENOENT, open '/home/chip/fanController/package.json'
5 error If you need help, you may report this *entire* log,
5 error including the npm and node versions, at:
5 error <http://github.com/npm/npm/issues>
6 error System Linux 4.3.0
7 error command "/usr/bin/nodejs" "/usr/bin/npm" "start"
8 error cwd /home/chip/fanController
9 error node -v v0.10.29
10 error npm -v 1.4.21
11 error path /home/chip/fanController/package.json
12 error code ENOENT
13 error errno 34
14 verbose exit [ 34, true ]

View File

@@ -9,22 +9,23 @@
<link href='http://fonts.googleapis.com/css?family=Ubuntu' rel='stylesheet' type='text/css'>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="apple-touch-icon" sizes="180x180" href="images/apple-touch-icon.png">
<link rel="icon" type="image/png" href="images/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="images/favicon-16x16.png" sizes="16x16">
<link rel="manifest" href="images/manifest.json">
<link rel="mask-icon" href="images/safari-pinned-tab.svg" color="#5bbad5">
<link rel="shortcut icon" href="images/favicon.ico">
<meta name="msapplication-config" content="images/browserconfig.xml">
<meta name="theme-color" content="#ffffff">
<meta content="yes" name="apple-mobile-web-app-capable" />
<meta content="black-translucent" id="status_bar" name="apple-mobile-web-app-status-bar-style" />
<link rel="apple-touch-icon" sizes="180x180" href="images/apple-touch-icon.png">
<link rel="icon" type="image/png" href="images/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="images/favicon-16x16.png" sizes="16x16">
<link rel="manifest" href="images/manifest.json">
<link rel="mask-icon" href="images/safari-pinned-tab.svg" color="#5bbad5">
<link rel="shortcut icon" href="images/favicon.ico">
<meta name="msapplication-config" content="images/browserconfig.xml">
<meta name="theme-color" content="#ffffff">
<meta content="yes" name="apple-mobile-web-app-capable" />
<meta content="black-translucent" id="status_bar" name="apple-mobile-web-app-status-bar-style" />
<script type="text/javascript">
if(!navigator.standalone && (/iphone|ipod|ipad/gi).test(navigator.platform) && (/Safari/i).test(navigator.appVersion)){
document.getElementById("status_bar").content = "black-translucent";
}
</script>
<script type="text/javascript">
if(!navigator.standalone && (/iphone|ipod|ipad/gi).test(navigator.platform) && (/Safari/i).test(navigator.appVersion)){
document.getElementById("status_bar").content = "black-translucent";
}
</script>
<style type="text/css">
html * {
font-family: 'Ubuntu', sans-serif !important;
@@ -82,11 +83,10 @@
</div>
<hr>
<div class="state_display">
<h4>Current fan state:</h4>
<span id="state">ON</span>
</div>
<div class="state_display">
<h4>Current fan state:</h4>
<span id="state">ON</span>
</div>
<hr>
<div style="text-align:center;">
@@ -104,6 +104,7 @@
</p>
<img src="images/chip.png" style="width: 60px;" />
</body>
<script type="text/javascript">
function UserAction(state) {
var xhttp = new XMLHttpRequest();
@@ -111,7 +112,7 @@
xhttp.setRequestHeader("Content-type", "application/text");
xhttp.send();
console.log(xhttp.status)
if (xhttp.status == "200") {
if (xhttp.status == "200" && xhttp.responseText != 'none') {
var stateDisplay = document.getElementById('state');
stateDisplay.innerText = xhttp.responseText.toUpperCase();
}

View File

@@ -1 +0,0 @@
off

View File

@@ -1,9 +0,0 @@
#!/usr/bin/env python3
import CHIP_IO.GPIO as GPIO #import the GPIO library
GPIO.setup("CSID0", GPIO.OUT) #set CSID0 as an output
GPIO.output("CSID0", GPIO.LOW) #set CSID0 (LED) LOW (Off)
f = open( 'public/state.txt', 'w' )
f.write('off')
f.close()

View File

@@ -1,9 +0,0 @@
#!/usr/bin/env python3
import CHIP_IO.GPIO as GPIO #import the GPIO library
GPIO.setup("CSID0", GPIO.OUT) #set CSID0 as an output
GPIO.output("CSID0", GPIO.HIGH) #set CSID0 (LED) HIGH (On)
f = open( 'public/state.txt', 'w' )
f.write('on')
f.close()

View File

@@ -1 +0,0 @@
off

51
scripts/fanController.py Executable file
View File

@@ -0,0 +1,51 @@
#!/usr/bin/env python3
import CHIP_IO.GPIO as GPIO #import the GPIO library
import sqlite3
from sys import argv
path = "scripts/fanState.db"
def updateFanstate(state):
conn = sqlite3.connect(path)
c = conn.cursor()
c.execute('UPDATE fanstate SET state='+str(state))
conn.commit()
conn.close()
def getFanstate():
conn = sqlite3.connect(path)
c = conn.cursor()
c.execute('SELECT state FROM fanstate')
state = c.fetchone()[0]
conn.close()
return state
def turnFanON():
if not getFanstate():
updateFanstate(1)
GPIO.setup("CSID0", GPIO.OUT) #set CSID0 as an output
GPIO.output("CSID0", GPIO.HIGH) #set CSID0 (LED) HIGH (On)
return '1'
def turnFanOFF():
if getFanstate():
updateFanstate(0)
GPIO.setup("CSID0", GPIO.OUT) #set CSID0 as an output
GPIO.output("CSID0", GPIO.LOW) #set CSID0 (LED) HIGH (On)
return '1'
if __name__ == '__main__':
arg = argv[1]
if (arg == 'on'):
print(turnFanON())
elif (arg == 'off'):
print(turnFanOFF())
elif (arg == 'get'):
print(getFanstate())
else:
print("Invalid input")

BIN
scripts/fanState.db Normal file

Binary file not shown.

View File

@@ -4,8 +4,6 @@ var path = require('path');
var PythonShell = require('python-shell');
app.use(express.static('public'));
var fs = require('fs');
// This responds with "Hello World" on the homepage
@@ -16,31 +14,52 @@ app.get('/', function (req, res) {
})
app.get('/state', function(req, res) {
var contents = fs.readFileSync('public/state.txt', 'utf8');
console.log(contents);
res.send(contents);
var options = {
pythonOptions: ['-u'],
args: 'get'
};
PythonShell.run('scripts/fanController.py', options, function (err, results) {
if (err) throw err;
if (results[0] == true)
res.send('on')
else
res.send('off')
});
})
// This responds a POST request for the homepage
app.post('/on', function (req, res) {
console.log("Got a POST request for ON");
var options = {
pythonOptions: ['-u'],
args: 'on'
};
PythonShell.run('pyscripts/led-csid0-on.py', function (err) {
if (err) throw err;
console.log('finished');
PythonShell.run('scripts/fanController.py', options, function (err, results) {
if (err) throw err;
if (results[0] == true)
res.send('on')
else
res.send('none')
});
res.send('on');
})
app.post('/off', function (req, res) {
console.log("Got a POST request for OFF");
var options = {
pythonOptions: ['-u'],
args: 'off'
};
PythonShell.run('pyscripts/led-csid0-off.py', function (err) {
if (err) throw err;
console.log('finished');
PythonShell.run('scripts/fanController.py', options, function (err, results) {
if (err) throw err;
if (results[0] == true)
res.send('off')
else
res.send('none')
});
res.send('off');
})
var server = app.listen(3000, function () {

View File

@@ -1,6 +0,0 @@
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8124, "10.0.0.70");
console.log('Server running at http://10.0.0.70:8124/');