mirror of
https://github.com/s00500/nodeMessageBoard.git
synced 2024-12-03 12:51:27 +00:00
Added server and Interface structure
This commit is contained in:
parent
f4b1da0e1f
commit
76cc6c6d6a
47
app.js
47
app.js
@ -1,22 +1,32 @@
|
||||
console.log("hello world\r");
|
||||
var server = require("./server");
|
||||
var router = require("./route");
|
||||
var requestHandlers = require("./requestHandlers");
|
||||
|
||||
var http = require("http");
|
||||
var server = http.createServer(function(request, response) {
|
||||
response.writeHead(200, {"Content-Type": "text/html"});
|
||||
response.write("<!DOCTYPE 'html'>");
|
||||
response.write("<html>");
|
||||
response.write("<head>");
|
||||
response.write("<title>Hello World Page</title>");
|
||||
response.write("</head>");
|
||||
response.write("<body>");
|
||||
response.write("Hello World!");
|
||||
response.write("</body>");
|
||||
response.write("</html>");
|
||||
response.end();
|
||||
var debug = false;
|
||||
|
||||
var handle = {}
|
||||
handle["/"] = requestHandlers.sendInterface;
|
||||
handle["/interface"] = requestHandlers.sendInterface;
|
||||
|
||||
server.start(router.route,handle,debug);
|
||||
|
||||
|
||||
|
||||
|
||||
// old app.js
|
||||
/*
|
||||
console.log("hello world\r");
|
||||
var port = 3000;
|
||||
var express = require('express')
|
||||
var app = express()
|
||||
|
||||
app.get('/', function (req, res) {
|
||||
res.send('Hello World')
|
||||
});
|
||||
|
||||
server.listen(5000);
|
||||
console.log("Server is listening");
|
||||
app.listen(port, function () {
|
||||
console.log('Example app listening on port '+port+'!');
|
||||
});
|
||||
|
||||
var serialport = require("serialport");
|
||||
var SerialPort = serialport.SerialPort;
|
||||
@ -30,7 +40,12 @@ serialPort.on("open", function () {
|
||||
console.log('open');
|
||||
serialPort.on("data", function (data) {
|
||||
console.log("here: "+data);
|
||||
|
||||
app.get('/', function (req, res) {
|
||||
res.send(data)
|
||||
})
|
||||
});
|
||||
|
||||
serialPort.write("AT+CPIN?\r");
|
||||
});
|
||||
*/
|
||||
|
173
board.html
Normal file
173
board.html
Normal file
@ -0,0 +1,173 @@
|
||||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
|
||||
<style>
|
||||
body {
|
||||
margin: 0px;
|
||||
padding: 0px;
|
||||
}
|
||||
#rData{
|
||||
float:left;
|
||||
margin-left:100px;
|
||||
margin-right:auto;
|
||||
width:470px;
|
||||
}
|
||||
#sData{
|
||||
float: left;
|
||||
margin-left:100px;
|
||||
margin-right:auto;
|
||||
width:470px;
|
||||
}
|
||||
h2{
|
||||
text-align:center;
|
||||
}
|
||||
#myCanvas {
|
||||
border: 2px dashed grey;
|
||||
}
|
||||
#btnHolder, #sliderTxt{
|
||||
text-align:center;
|
||||
}
|
||||
</style>
|
||||
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
|
||||
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
|
||||
<script src="/socket.io/socket.io.js"></script>
|
||||
<script>
|
||||
// canvas request for all browsers
|
||||
window.requestAnimFrame = (function(callback) {
|
||||
return window.requestAnimationFrame ||
|
||||
window.webkitRequestAnimationFrame ||
|
||||
window.mozRequestAnimationFrame ||
|
||||
window.oRequestAnimationFrame ||
|
||||
window.msRequestAnimationFrame ||
|
||||
function(callback) {
|
||||
window.setTimeout(callback, 1000 / 30); // 30 frames per second
|
||||
};
|
||||
})();
|
||||
|
||||
var iosocket;
|
||||
var pollOneH = 0;
|
||||
var poll1;
|
||||
var text;
|
||||
var potValue;
|
||||
var prevPotValue;
|
||||
//var onOff = false;
|
||||
var toggleVal = 0;
|
||||
|
||||
function animation(poll1,text)
|
||||
{
|
||||
var canvas = document.getElementById("myCanvas");
|
||||
var content = canvas.getContext("2d");
|
||||
|
||||
// clear canvas
|
||||
content.clearRect(0, 0, 460, 540);
|
||||
|
||||
content.fillStyle = 'black';
|
||||
content.textAlign = 'center';
|
||||
content.font = '20pt Calibri';
|
||||
|
||||
// make the wobbely values stop
|
||||
if(pollOneH*2 > prevPotValue+2 || pollOneH*2 < prevPotValue-2){
|
||||
prevPotValue = potValue;
|
||||
potValue = pollOneH*2;
|
||||
}
|
||||
|
||||
content.fillText('Potmeter value: ' + potValue, text.x, text.y);
|
||||
|
||||
// render graph
|
||||
content.fillStyle = 'orange';
|
||||
content.fillRect(poll1.x,(poll1.y-poll1.h),poll1.w,poll1.h);
|
||||
|
||||
content.fill();
|
||||
|
||||
// request new frame
|
||||
requestAnimFrame(function() {
|
||||
|
||||
if(poll1.h < pollOneH){
|
||||
poll1.h += (pollOneH - poll1.h)*.15;
|
||||
}
|
||||
else if(poll1.h > pollOneH){
|
||||
poll1.h -= (poll1.h - pollOneH)*.15;
|
||||
}
|
||||
text.y = (poll1.y - poll1.h) - 5;
|
||||
animation(poll1,text);
|
||||
});
|
||||
}
|
||||
|
||||
function initSocketIO()
|
||||
{
|
||||
iosocket = io.connect();
|
||||
iosocket.on('onconnection', function(value) {
|
||||
pollOneH = value.pollOneValue/2; // recieve start poll value from server
|
||||
initPoll();
|
||||
initButton();
|
||||
initSlider();
|
||||
|
||||
// recieve changed values by other client from server
|
||||
iosocket.on('updateData', function (recievedData) {
|
||||
pollOneH = recievedData.pollOneValue/2; // recieve start poll value from server
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function initPoll()
|
||||
{
|
||||
poll1 = {
|
||||
x: 10,
|
||||
y: 540,
|
||||
w: 440,
|
||||
h: 0
|
||||
}
|
||||
text = {
|
||||
x:poll1.w/2,
|
||||
y:100
|
||||
}
|
||||
potValue = pollOneH*2;
|
||||
prevPotValue = potValue;
|
||||
animation(poll1,text);
|
||||
}
|
||||
|
||||
function initButton()
|
||||
{
|
||||
$( "#check" ).button();
|
||||
}
|
||||
|
||||
function initSlider()
|
||||
{
|
||||
$( "#slider" ).slider({
|
||||
min:0,
|
||||
max:255,
|
||||
change: function(event,ui) {
|
||||
iosocket.emit('sliderval',ui.value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
window.onload = function() {
|
||||
initSocketIO();
|
||||
};
|
||||
|
||||
$(document).ready(function() {
|
||||
$('#check').click(function() {
|
||||
toggleVal += 1;
|
||||
toggleVal %= 2;
|
||||
iosocket.emit('buttonval',toggleVal);
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="rData">
|
||||
<h2>Data from Arduino</h2>
|
||||
<canvas id="myCanvas" width="460" height="540"></canvas>
|
||||
</div>
|
||||
<div id="sData">
|
||||
<h2>Data to Arduino</h2>
|
||||
<div id="btnHolder">
|
||||
<input type="checkbox" id="check" value="toggle"/><label for="check">Toggle LED</label>
|
||||
</div>
|
||||
<p id="sliderTxt">LED Dimmer</p>
|
||||
<div id="slider"></div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
20
package.json
Normal file
20
package.json
Normal file
@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "nodemessageboard",
|
||||
"version": "1.0.0",
|
||||
"description": "A SMS Messageboard built using node and express",
|
||||
"main": "app.js",
|
||||
"dependencies": {
|
||||
"serialport": "^2.0.6",
|
||||
"socket.io": "^1.4.5"
|
||||
},
|
||||
"devDependencies": {},
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "."
|
||||
},
|
||||
"author": "Lukas Bachschwell",
|
||||
"license": "ISC"
|
||||
}
|
15
requestHandlers.js
Normal file
15
requestHandlers.js
Normal file
@ -0,0 +1,15 @@
|
||||
// functions that will be executed when
|
||||
// typeoff handle[pathname] === a function in requestHandlers.
|
||||
// the handle and function are discribed in index.js
|
||||
|
||||
var fs = require('fs'),
|
||||
server = require('./server');
|
||||
|
||||
function sendInterface(response) {
|
||||
console.log("Request handler 'interface' was called.");
|
||||
response.writeHead(200, {"Content-Type": "text/html"});
|
||||
var html = fs.readFileSync(__dirname + "board.html")
|
||||
response.end(html);
|
||||
}
|
||||
|
||||
exports.sendInterface = sendInterface;
|
18
route.js
Normal file
18
route.js
Normal file
@ -0,0 +1,18 @@
|
||||
function route(handle, pathname,response,request,debug) {
|
||||
console.log("About to route a request for " + pathname);
|
||||
//typeof probes the data type of handle[pathname]. So if
|
||||
//handle[pathname] is a function (in both type and value)
|
||||
//,then run that function.
|
||||
if (typeof handle[pathname] === 'function') {
|
||||
return handle[pathname](response,request);
|
||||
} else {
|
||||
if(debug == true){
|
||||
console.log("No request handler found for " + pathname);
|
||||
}
|
||||
response.writeHead(404, {"Content-Type": "text/plain"});
|
||||
response.write("404 Not found");
|
||||
response.end();
|
||||
}
|
||||
}
|
||||
|
||||
exports.route = route;
|
93
server.js
Normal file
93
server.js
Normal file
@ -0,0 +1,93 @@
|
||||
var fs = require('fs'),
|
||||
http = require('http'),
|
||||
socketio = require('socket.io'),
|
||||
url = require("url"),
|
||||
serialport = require("serialport");
|
||||
|
||||
var SerialPort = serialport.SerialPort;
|
||||
|
||||
var socketServer;
|
||||
var serialPort;
|
||||
var portName = '/dev/tty.usbmodemFD121'; //change this to your Arduino port
|
||||
var sendData = "";
|
||||
|
||||
// handle contains locations to browse to (vote and poll); pathnames.
|
||||
function startServer(route,handle,debug)
|
||||
{
|
||||
// on request event
|
||||
function onRequest(request, response) {
|
||||
// parse the requested url into pathname. pathname will be compared
|
||||
// in route.js to handle (var content), if it matches the a page will
|
||||
// come up. Otherwise a 404 will be given.
|
||||
var pathname = url.parse(request.url).pathname;
|
||||
console.log("Request for " + pathname + " received");
|
||||
var content = route(handle,pathname,response,request,debug);
|
||||
}
|
||||
|
||||
var httpServer = http.createServer(onRequest).listen(1337, function(){
|
||||
console.log("Listening at: http://localhost:1337");
|
||||
console.log("Server is up");
|
||||
});
|
||||
serialListener(debug);
|
||||
initSocketIO(httpServer,debug);
|
||||
}
|
||||
|
||||
function initSocketIO(httpServer,debug)
|
||||
{
|
||||
socketServer = socketio.listen(httpServer);
|
||||
if(debug == false){
|
||||
socketServer.set('log level', 1); // socket IO debug off
|
||||
}
|
||||
socketServer.on('connection', function (socket) {
|
||||
console.log("user connected");
|
||||
socket.emit('onconnection', {pollOneValue:sendData});
|
||||
socketServer.on('update', function(data) {
|
||||
socket.emit('updateData',{pollOneValue:data});
|
||||
});
|
||||
socket.on('buttonval', function(data) {
|
||||
serialPort.write('AT\r');
|
||||
console.log('sending:..');
|
||||
});
|
||||
socket.on('sliderval', function(data) {
|
||||
serialPort.write(data + 'P');
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
// Listen to serial port
|
||||
function serialListener(debug)
|
||||
{
|
||||
var receivedData = "";
|
||||
serialPort = new SerialPort(portName, {
|
||||
baudrate: 19200,
|
||||
parser: serialport.parsers.readline("\n")
|
||||
});
|
||||
|
||||
serialPort.on("open", function () {
|
||||
console.log('opened serial communication');
|
||||
|
||||
// Listens to incoming data
|
||||
serialPort.on('data', function(data) {
|
||||
//receivedData += data.toString();
|
||||
//if (receivedData .indexOf('E') >= 0 && receivedData .indexOf('B') >= 0) {
|
||||
sendData = '50'; //.substring(receivedData .indexOf('B') + 1, receivedData .indexOf('E'));
|
||||
receivedData = '';
|
||||
console.log('server event happening..' + data + '.\r');
|
||||
//}
|
||||
// send the incoming data to browser with websockets.
|
||||
socketServer.emit('update', sendData);
|
||||
});
|
||||
//setTimeout(sendPin,500);
|
||||
serialPort.write('AT+CPIN=3797\r');
|
||||
console.log("Sent Pincode...");
|
||||
});
|
||||
}
|
||||
|
||||
function sendPin(){
|
||||
serialPort.write('AT+CPIN=3797\r');
|
||||
console.log("Sent Pincode...");
|
||||
}
|
||||
|
||||
|
||||
exports.start = startServer;
|
Loading…
Reference in New Issue
Block a user