Permalink
Browse files

Express Framework added

Use Express framework to make the application easy to install and
maintain.
  • Loading branch information...
Branyac
Branyac committed May 25, 2015
1 parent 2c86177 commit a6c9bbeee58385d64667cc38f3c9a46a18961d94
View
@@ -19,19 +19,14 @@ Connect Intel Edison board Arduino pins to the L298N controller in this way:
## Installation
### NodeJS backend
1. Connect the Intel Edison board to a network with an static IP.
2. Go to `Smartcar` folder and open `SmartCar.xdk` with Intel XDK IoT Edition.
3. Click on `Install/Build` button of Intel XDK to fetch dependencies and compile the code.
3. Click on `Upload` to send the program to the device.
4. Click on `Upload` to send the program to the device.
### HTML + JS Frontend
1. Open `web/js/main.js` file in a text editor.
2. Replace on line 20 `url: 'http://192.168.1.109:8888/',` and put the IP of your Intel Edison board. In this case the IP is `192.168.1.109`. Don't remove the port number because it is needed to connect the frontend with the backend.
3. Use an SCP client to upload `web` folder to `/usr/lib/edison_config_tools/public` folder in the Intel Edison.
### Usage
Open a web browser and go to http://[IP_OF_EDISON_BOARD]/web/index.html. There will be 4 buttons to move the car forward (Avanzar), backward (Retroceder), turn left (A la izquierda), and turn right (A la derecha).
Open a web browser and go to http://[IP_OF_EDISON_BOARD]:8080/ There will be 4 buttons to move the car forward (Avanzar), backward (Retroceder), turn left (A la izquierda), and turn right (A la derecha).
<br />
<br />
<br />
View
Binary file not shown.
View
@@ -1,103 +1,143 @@
#!/usr/bin/env node
/**
SmartCar
By: Rafael Espejo and Sergio Monedero
2015
**/
var APP_PORT = 8080;
var http;
var url;
var mraa;
var express;
var app;
// Motor controller pins
var m_input3;
var m_input4;
var m_input5;
var m_input6;
function setup_mraa() {
mraa = require('mraa'); //require mraa
console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the console
console.log('')
function setup_requires() {
try {
console.log('Initializing libraries...');
http = require("http");
console.log('HTTP initialized');
url = require('url');
console.log('URL initialized');
mraa = require('mraa'); //require mraa
console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the console
express = require('express');
console.log('Express framework initialized');
console.log('')
} catch(err) {
console.log("ERROR: " + err.message);
}
}
function setup_webserver() {
var http = require("http");
http.createServer(function(request, response) {
try {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("SmartCar");
var url = require('url');
var motor_action = url.parse(request.url, true).query['motors_action'];
if(motor_action !== undefined) {
response.write("motors_action=" + motor_action);
start_motors(motor_action);
} else {
response.write("no motors_action");
}
response.end();
} catch(err) {
console.log("ERROR: " + err.message);
}
}).listen(8888);
function setup_app() {
try {
app = express();
app.use(express.static(__dirname + '/public'));
app.use('/backend/doAction', doAction);
var server = app.listen(APP_PORT, function () {
var host = server.address().address;
var port = server.address().port;
console.log('intel-edison-smartcar listening at http://%s:%s', host, port);
});
} catch(err) {
console.log("ERROR: " + err.message);
}
}
function setup_arduino_pins() {
m_input3 = new mraa.Gpio(5);
m_input3.dir(mraa.DIR_OUT);
m_input4 = new mraa.Gpio(4);
m_input4.dir(mraa.DIR_OUT);
m_input5 = new mraa.Gpio(6);
m_input5.dir(mraa.DIR_OUT);
m_input6 = new mraa.Gpio(7);
m_input6.dir(mraa.DIR_OUT);
var myDigitalPin5 = new mraa.Gpio(13); //setup digital read on Digital pin #5 (D5)
myDigitalPin5.dir(mraa.DIR_OUT); //set the gpio direction to output
myDigitalPin5.write(1); //set the digital pin to high (1)
try {
m_input3 = new mraa.Gpio(5);
m_input3.dir(mraa.DIR_OUT);
m_input4 = new mraa.Gpio(4);
m_input4.dir(mraa.DIR_OUT);
m_input5 = new mraa.Gpio(6);
m_input5.dir(mraa.DIR_OUT);
m_input6 = new mraa.Gpio(7);
m_input6.dir(mraa.DIR_OUT);
var myDigitalPin5 = new mraa.Gpio(13); //setup digital read on Digital pin #5 (D5)
myDigitalPin5.dir(mraa.DIR_OUT); //set the gpio direction to output
myDigitalPin5.write(1); //set the digital pin to high (1)
} catch(err) {
console.log("ERROR: " + err.message);
}
}
function doAction(request, response) {
try {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("SmartCar");
var motor_action = url.parse(request.url, true).query['motors_action'];
if(typeof motor_action !== 'undefined') {
response.write("motors_action=" + motor_action);
start_motors(motor_action);
} else {
response.write("no motors_action");
}
response.end();
} catch(err) {
console.log("ERROR: " + err.message);
}
}
function main() {
setup_mraa();
setup_requires();
setup_arduino_pins();
setup_webserver();
setup_app();
}
function start_motors(action) {
if(action == 'L') {
console.log('Motors: Turn left.')
console.log('Motors: Turn left.');
m_input4.write(1);
m_input3.write(0);
m_input6.write(0);
m_input5.write(1);
} else if (action == 'R') {
console.log('Motors: Turn right.')
console.log('Motors: Turn right.');
m_input4.write(0);
m_input3.write(1);
m_input6.write(1);
m_input5.write(0);
} else if (action == 'B') {
console.log('Motors: Go backward.')
console.log('Motors: Go backward.');
m_input4.write(1);
m_input3.write(0);
m_input6.write(1);
m_input5.write(0);
} else if (action == 'F') {
console.log('Motors: Go forward.')
console.log('Motors: Go forward.');
m_input4.write(0);
m_input3.write(1);
m_input6.write(0);
m_input5.write(1);
}
setTimeout(function() {
console.log('Motors: Stop.')
console.log('Motors: Stop.');
m_input4.write(0);
m_input3.write(0);
m_input6.write(0);
m_input5.write(0);
}, 1000)
}, 1000);
}
main();
View
@@ -1,11 +1,26 @@
{
"name": "DigitalWrite",
"description": "",
"version": "0.0.0",
"main": "main.js",
"engines": {
"node": ">=0.10.0"
},
"dependencies": {
}
"name": "intel-edison-smartcar",
"version": "1.1.0",
"description": "",
"author": "Sergio Monedero",
"repositories": [
{
"type": "git",
"url": "https://github.com/Branyac/intel-edison-smartcar.git"
}
],
"licenses": [
{
"name": "Apache License 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0"
}
],
"homepage": "http://thingsandcode.com",
"main": "main.js",
"engines": {
"node": ">=0.10.0"
},
"dependencies": {
"express": "4.10.1"
}
}
File renamed without changes.
@@ -3,7 +3,6 @@
<link rel="stylesheet" href="css/default.css" />
<script type="text/javascript" src="js/main.js"></script>
<script type="text/javascript" src="js/lib/jquery.js"></script>
</head>
<body>
<button onclick="sendMotorAction('F');">Avanzar</button><button onclick ="sendMotorAction('B');">Retroceder</button><br />
View
@@ -0,0 +1,11 @@
function sendMotorAction(action) {
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "/backend/doAction?motors_action=" + action, true);
xmlhttp.send();
}
View

This file was deleted.

Oops, something went wrong.
View

This file was deleted.

Oops, something went wrong.

0 comments on commit a6c9bbe

Please sign in to comment.