Skip to content

Instantly share code, notes, and snippets.

@zefanja zefanja/index.html
Last active Jun 8, 2019

Embed
What would you like to do?
My two files for my little robot project
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Robot Project</title>
<script src="/socket.io/socket.io.js"></script>
<script>
//Damit lesen wir den Gyroskopen aus
window.addEventListener("deviceorientation", handleOrientation, true);
function handleOrientation(inEvent) {
var socket = io.connect('http://192.168.1.15');
if (inEvent.beta < 0 && inEvent.gamma > -15 && inEvent.gamma < 15)
socket.emit('forwards');
else if (inEvent.beta >= 0 && inEvent.beta <= 60 && inEvent.gamma > -15 && inEvent.gamma < 15)
socket.emit('stop');
else if (inEvent.beta > 60 && inEvent.gamma > -15 && inEvent.gamma < 15)
socket.emit('backwards');
else if (inEvent.gamma < -15)
socket.emit('turnLeft');
else if (inEvent.gamma > 15)
socket.emit('turnRight');
//Optionale Ausgabe der Werte des Gyroskopen
document.getElementById("alpha").innerHTML = inEvent.alpha;
document.getElementById("beta").innerHTML = inEvent.beta;
document.getElementById("gamma").innerHTML = inEvent.gamma;
return true;
}
</script>
</head>
<body>
<h1>Robot Project</h1>
<p id="alpha"></p>
<p id="beta"></p>
<p id="gamma"></p>
</body>
</html>
var gpio = require("./node-rpio/lib/rpio");
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(80);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
socket.on('forwards', function () {
forwards();
});
socket.on('backwards', function () {
backwards();
});
socket.on('stop', function () {
stop();
});
socket.on('turnRight', function () {
turnRight();
});
socket.on('turnLeft', function () {
turnLeft();
});
});
//Setup GPIO Pins
gpio.setmode(gpio.BOARD);
gpio.setup(13, gpio.OUT);
gpio.setup(15, gpio.OUT);
gpio.setup(16, gpio.OUT);
gpio.setup(18, gpio.OUT);
function forwards() {
gpio.output(13, 1);
gpio.output(15, 0);
gpio.output(16, 1);
gpio.output(18, 0);
}
function backwards() {
gpio.output(13, 0);
gpio.output(15, 1);
gpio.output(16, 0);
gpio.output(18, 1);
}
function turnRight() {
gpio.output(13, 0);
gpio.output(15, 0);
gpio.output(16, 1);
gpio.output(18, 0);
}
function turnLeft() {
gpio.output(13, 1);
gpio.output(15, 0);
gpio.output(16, 0);
gpio.output(18, 0);
}
function stop() {
gpio.output(13, 0);
gpio.output(15, 0);
gpio.output(16, 0);
gpio.output(18, 0);
}
process.on('SIGINT', function() {
console.log("\nGracefully shutting down from SIGINT (Ctrl+C)");
//Cleanup
gpio.cleanup();
process.exit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.