Skip to content

Commit

Permalink
Add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
caiomarqs committed Mar 18, 2020
1 parent 869c0cb commit 1d580e3
Show file tree
Hide file tree
Showing 11 changed files with 407 additions and 23 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
node_modules/
*.md
node_modules/
2 changes: 1 addition & 1 deletion ArduinoProgram/MasterProgram/MasterProgram.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void loop(){
}
if (inChar == '\n') {
int sliderValue = inString.toInt();
int ledValue = map(sliderValue, 1, 100, 0, 255);
int ledValue = map(sliderValue, 0, 100, 0, 255);
if(ledValue >= 240){
ledValue = 240;
}
Expand Down
Binary file added ArduinoProgram/video_example.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
143 changes: 143 additions & 0 deletions README-ptBR.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# Socket IO - Arduino

Este projeto é um exemplo de interação entre um Arduino pela porta serial e o Socket.io.

*Veja a documentação em [Inglês](./README.md).*

<br>
<p align="center">
<a>
<img src="./ArduinoProgram/video_example.gif" width="400">
</a>
</p>

### Rode o projeto
- Instele todas as dependecias do *Node.js*:
```
npm install
```
- Rode a aplicação do server no `app.js`:
```
node app.js
```

### Configurando a aplicação

**Antes de rodar a aplicação, ligue o Arduino na porta *Serial/USB* e rode o programa `.ino` na Arduno IDE.**

Esse projeto é um modelo inicial, veja a documentção do [Socket.io](https://socket.io/docs/).

- Configurar _Express_ e _Socket io_:
```
const express = require('express')
const http = require('http')
const socketio = require('socket.io')
//Instance server
const app = express()
const server = http.createServer(app)
const io = socketio(server)
app.use(express.static('public')) //Express use public folder for static content
server.listen(8080, () => {
console.log(`Server listening on port: 8080`)
})
```

- Configurar o _Socket io_ no lado do client:
```
<script>
const socket = io()
socket.on('connect', () => {
const playerId = socket.id
console.log(`Player connected on Client with id: ${playerId}`)
})
</script>
```

- Configurar porta _Serial/USB_ e iniciar o Serial App:
```
const SerialArduinoApp = require('./serial-arduino-app')
const config = {
log: false,
port: 'COM3',
baudRate: 9600,
serialLog: true
}
const serialApp = new SerialArduinoApp(config);
```

### Exemplos de interação

#### **Enviado dados do client para o Arduino:**
- Lado do server:
```
let lastSliderValue
serialApp.sendDataToSerial() //Init for send data to serial port
//Init the io, the socket can manipule data from front-end
io.sockets.on('connection', (socket) => {
const userId = socket.id
console.log(`User connected: ${userId}`)
//Listening slider from front-end
socket.on('slider-data', (sliderValue) => {
if (lastSliderValue != sliderValue) {
serialApp.emit('send-data', sliderValue) // emit to a serialport the value recived from client
lastSliderValue = sliderValue
} else {
lastSliderValue = sliderValue
}
})
})
```
- Lado do client:
```
<input type="range" min="0" max="100" value="0" class="slider" id="rangeInput">
<script>
var slider = document.getElementById("rangeInput")
slider.oninput = function () {
socket.emit('slider-data', this.value)
}
</script>
```

#### **Enviando dados do Arduino para o client:**
- Lado do server:

```
let lastSerialData
serialApp.reciveDataToSerial() // Init to recive data from serialport
//Listening data recived from serialport
serialApp.on('serial-data', (serialData) => {
if (lastSerialData != serialData) {
io.emit('new-value', serialData) // emit to client the serial data
lastSerialData = serialData
} else {
lastSerialData = serialData
}
})
```

- Lado do client:
```
<script>
socket.on('new-value', (newValue) => {
const textOptions = { value: newValue, color: "black" }
potentiometerAnimation(textValue, textOptions, requestAnimationFrame)
})
</script>
```


143 changes: 143 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# Socket IO - Arduino

This project is an example of interaction between an Arduino through the serial port and Socket.io.

*See this document in [Portuguese](./README-ptBR.md).*

<br>
<p align="center">
<a>
<img src="./ArduinoProgram/video_example.gif" width="400">
</a>
</p>

### Run Project
- Install all *Node.js* dependencies:
```
npm install
```
- Run the server of application on `app.js`:
```
node app.js
```

### Configure Application

**Before running the application, connect the Arduino and run the program `.ino` on the Arduino IDE.**

This is a quick start, for more information see [Socket.io](https://socket.io/docs/) documentation.

- Configure _Express_ and _Socket io_:
```
const express = require('express')
const http = require('http')
const socketio = require('socket.io')
//Instance server
const app = express()
const server = http.createServer(app)
const io = socketio(server)
app.use(express.static('public')) //Express use public folder for static content
server.listen(8080, () => {
console.log(`Server listening on port: 8080`)
})
```

- Configure on the client side the _Socket io_:
```
<script>
const socket = io()
socket.on('connect', () => {
const playerId = socket.id
console.log(`Player connected on Client with id: ${playerId}`)
})
</script>
```

- Configure _Serial/USB_ port for listening and init the Serial App:
```
const SerialArduinoApp = require('./serial-arduino-app')
const config = {
log: false,
port: 'COM3',
baudRate: 9600,
serialLog: true
}
const serialApp = new SerialArduinoApp(config);
```

### Examples of interaction

#### **Send data from client to Arduino:**
- Server side:
```
let lastSliderValue
serialApp.sendDataToSerial() //Init for send data to serial port
//Init the io, the socket can manipule data from front-end
io.sockets.on('connection', (socket) => {
const userId = socket.id
console.log(`User connected: ${userId}`)
//Listening slider from front-end
socket.on('slider-data', (sliderValue) => {
if (lastSliderValue != sliderValue) {
serialApp.emit('send-data', sliderValue) // emit to a serialport the value recived from client
lastSliderValue = sliderValue
} else {
lastSliderValue = sliderValue
}
})
})
```
- Client side:
```
<input type="range" min="0" max="100" value="0" class="slider" id="rangeInput">
<script>
var slider = document.getElementById("rangeInput")
slider.oninput = function () {
socket.emit('slider-data', this.value)
}
</script>
```

#### **Send data from Arduino to client:**
- Server side:

```
let lastSerialData
serialApp.reciveDataToSerial() // Init to recive data from serialport
//Listening data recived from serialport
serialApp.on('serial-data', (serialData) => {
if (lastSerialData != serialData) {
io.emit('new-value', serialData) // emit to client the serial data
lastSerialData = serialData
} else {
lastSerialData = serialData
}
})
```

- Client side:
```
<script>
socket.on('new-value', (newValue) => {
const textOptions = { value: newValue, color: "black" }
potentiometerAnimation(textValue, textOptions, requestAnimationFrame)
})
</script>
```


8 changes: 4 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
const express = require('express')
const http = require('http')
const socketio = require('socket.io')
const SerialArduinoApp = require('./serial-arduino-app/index')
const SerialArduinoApp = require('./serial-arduino-app')

//Instancia do servidor
//Instance server
const app = express()
const server = http.createServer(app)
const io = socketio(server)
Expand All @@ -18,12 +18,12 @@ const config = {
}
const serialApp = new SerialArduinoApp(config);

app.use(express.static('public')) //Exepress use public folder for static content
app.use(express.static('public')) //Express use public folder for static content

let lastSliderValue
serialApp.sendDataToSerial() //Init for send data to serial port

//Init the io, the socket can manipule data from a front-end
//Init the io, the socket can manipule data from front-end
io.sockets.on('connection', (socket) => {
const userId = socket.id
console.log(`User connected: ${userId}`)
Expand Down
Loading

0 comments on commit 1d580e3

Please sign in to comment.