Created
March 30, 2018 23:09
-
-
Save zr0n/b24156af0db50e1bd6c4966865d8260f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Arquivo Index.js */ | |
const express = require('express'); | |
const app = express(); | |
const http = require('http').Server(app); | |
const io = require('socket.io')(http); | |
const ArduinoControl = require('./arduino-control'); | |
app.use(express.static('public')); | |
io.on('connection', (socket) => { | |
console.log("New user connected"); | |
control = new ArduinoControl(socket, io); | |
socket.on('forward', () => control.moveForward()); | |
socket.on('backward', () => control.moveBackward()); | |
socket.on('left', () => control.turnLeft()); | |
socket.on('right', () => control.turnRight()); | |
socket.on('stop', () => control.stop()); | |
}) | |
app.get('/', (req, res, next) => { | |
res.sendFile(__dirname + '/public/index.html'); | |
}) | |
http.listen(3000,'0.0.0.0' , () => { | |
console.log("Listening on port 3000"); | |
}); | |
/** Fim Index.js */ | |
/** Arquivo arduino-control.js */ | |
class ArduinoControl{ | |
constructor(socket, io){ | |
this.socket = socket; | |
this.io = io; | |
this.status = '0'; | |
this.socket.on("statusUpdate", () => { | |
this.socket.emit(this.status); | |
}) | |
} | |
moveForward(){ | |
console.log("moving forward"); | |
this.status = '1'; | |
} | |
moveBackward(){ | |
this.status = '2'; | |
console.log("moving backward"); | |
} | |
turnLeft(){ | |
this.status = '3'; | |
console.log("moving left"); | |
} | |
turnRight(){ | |
this.status = '4'; | |
console.log("moving right"); | |
} | |
stop(){ | |
this.status = '0'; | |
console.log("stoping"); | |
} | |
} | |
module.exports = ArduinoControl; | |
/** Fim Arduino-Control.js */ | |
/** Arquivo main.js (Cliente que manda os controles para o server)*/ | |
var socket = io(); | |
socket.on('connect', () => { | |
console.log("connected"); | |
}) | |
document.querySelector('body').addEventListener('mouseup', () => { | |
socket.emit('stop'); | |
}) | |
document.querySelector('#left').addEventListener('mousedown', () => socket.emit('left')) | |
document.querySelector('#right').addEventListener('mousedown', () => socket.emit('right')) | |
document.querySelector('#up').addEventListener('mousedown', () => socket.emit('forward')) | |
document.querySelector('#down').addEventListener('mousedown', () => socket.emit('backward')) | |
/** Fim main.js */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment