SlideShare uma empresa Scribd logo
1 de 84
Baixar para ler offline
ACENDA A SUA LUZ
COM JS
+ um despacito
@ ABCDev - São Caetano do Sul 22/09/18
CAIQUE MITSUOKA Sorocabano
Ruby / JS / Elixir
1ª vez em São Caetano do Sul
Eu sou legal no twitter, segue lá
Sommelier de litrão
twitter.com/caiquemitsuoka
github.com/caiquemitsuoka
O despacito
O PROBLEMA
Preguiça
+
Vontade de aprender
=
💡
A IDEIA
👧 + 📱 = 💡
Colocar internet na 💡.
Uma 💡 é um led.
Essa é a história de como
acender um led com JS.
O PLANO
Enviar o comando pela internet
para o Raspberry Pi
e acender a luz
Pra mim C
é C de chato.
Node tem release oficial
para ARM
Node + Rasp = ♥
github.com/CaiqueMitsuoka/thomas-light
github.com/CaiqueMitsuoka/thomas-light-server
Socket @ Server
const SocketServer = {
setupClient: () => {
SocketServer.server.on('connection', (client) => {
client.on(
'register',
SocketServer.handleRegister(client)
).on(
'disconnect',
SocketServer.handleDisconnect(client)
)
})
},
...
}
Socket @ Server
const SocketServer = {
...
handleRegister: (client) => {
return (data) => {
client.lightId = getMd5(data)
SocketServer.lights[client.lightId] = {
client: client
}
client.emit('registered', client.lightId)
}
},
...
}
Socket @ Server
const SocketServer = {
setupClient: () => {
SocketServer.server.on('connection', (client) => {
client.on(
'register',
SocketServer.handleRegister(client)
).on(
'disconnect',
SocketServer.handleDisconnect(client)
)
})
},
...
}
Socket @ Server
const SocketServer = {
...
handleDisconnect: (client) => {
return () => {
delete SocketServer.lights[client.lightId]
}
}
}
const lightsController = {
edit: (request, response) =>
lights[request.params.id].client.emit('status.update')
response.redirect('/')
}
}
Controller @ Server
const socket =
require('socket.io-client')(process.env.SERVER_URL);
const LightLighter = require('./light-lighter/index')
socket.on('connect', () => {
socket.emit('register', process.env.ID)
})
.on('status.update', () => {
toggleMainLight()
})
Socket @ Client
const state = {
mainLight: false
}
const toggleMainLight = () => {
if(state.mainLight) {
LightLighter.off()
} else {
LightLighter.on()
}
state.mainLight = !state.mainLight
}
Toggle @ Client
Light Lighter
O Raspberry funciona
com 3,3v e 5v
Uma lampada comum
funciona com
110v
110v + rasp = 🔥
Não podemos acender
a 💡
com energia do rasp
Relay
Eu não entendi como
funciona o Relay na real
Mas a parte boa é que vc
não precisa! Vc só
precisa entender o
suficiente
const gpio = require('rpi-gpio')
const lightLighter = {
io4: 7,
off: () => {
gpio.destroy();
},
on: () => {
gpio.setup(lightLighter.io4, gpio.DIR_OUT, console.log)
}
}
LightLighter @ Pi
É realmente só isso
Foi assim que eu
coloquei internet em
uma lâmpada
Mas o que é a internet,
se não vocês <3
thomaslight.herokuapp.com
Internet
of
É muito
simples
fazer IoT
Faça algo
que você
gosta
Faça uma PoC
Faça código
para você
Faz um IoT daora ai
e me mostra.
Não faz IoT
quem não quer
E o despacito?
Acender uma
💡
é legal
Eu queria
ser RGB
O PLANO
Conectar uma
fita de leds
RGB
Mas não
rolou
Aquela
quinta-feira
Qualquer coisa
que emite luz
satisfaz meus
requisitos
Mas só
acender é
chato
Eu sou
altamente
influenciável
Guitar Hero
+
Internet
=
Vamo tenta
faze um negócio
doido
http://thomaslight.herokuapp.com/
guitar
const socket =
require('socket.io-client')(process.env.SERVER_URL);
const VigilantGuitar = require('./vigilant-guitar')
VigilantGuitar.init()
VigilantGuitar.start((color) => {
socket.emit('guitar.press', color)
})
Socket @ Client
const gp = require('gamepad')
const ACTIONS = {
0:'yellow',
1:'green',
2:'red',
3:'blue',
4:'orange',
}
VigilantGuitar @ Client
module.exports = VigilantGuitar = {
init: () => {
gp.init()
VigilantGuitar.CONTROLLERS = []
for (var i = 0, l = gp.numDevices(); i < l; i++) {
VigilantGuitar.CONTROLLERS
.push(gp.deviceAtIndex(i))
}
...
VigilantGuitar @ Client
...
VigilantGuitar.controller =
VigilantGuitar.CONTROLLERS
.find((item) => {
return item.description.includes('Guitar')
})
},
VigilantGuitar @ Client
start: (callback) => {
VigilantGuitar.buttonSetup(callback)
setInterval(gp.processEvents, 50)
},
VigilantGuitar @ Client
buttonSetup: (callback) => {
gp.on('down', (id, num) => {
if(id === VigilantGuitar.controller.deviceID){
callback(ACTIONS[num])
}
})
}
VigilantGuitar @ Client
client.on('panel.connection',
SocketServer.handlePanelConnect(client))
.on('guitar.press',
SocketServer.handleGuitarPress(client))
Socket @ Server
const SocketServer = {
...
handlePanelConnect: (client) => {
return () => {
client.join('panels')
}
},
...
}
Socket @ Server
client.on('panel.connection',
SocketServer.handlePanelConnect(client))
.on('guitar.press',
SocketServer.handleGuitarPress(client))
Socket @ Server
const SocketServer = {
...
handleGuitarPress: (_client) => {
return (button) => {
SocketServer.server
.to('panels')
.emit(`guitar.${button}`)
}
},
...
}
Socket @ Server
<body>
<div data-panel></div>
</body>
HTML @ /guitar
var colors = {
green: '#12580d',
...
}
var socket = io()
var changeColor = function(colorName) {
return () => {
document.querySelector('[data-panel]')
.style.backgroundColor =
colors[colorName]
}
}
Script @ /guitar
socket.emit('panel.connection')
socket.on('guitar.green',
changeColor('green'))
socket.on('guitar.red', changeColor('red'))
socket.on('guitar.yellow',
changeColor('yellow'))
socket.on('guitar.blue',
changeColor('blue'))
socket.on('guitar.orange',
changeColor('orange'))
Script @ /guitar
Minha ‘guitarra’
Ta conectada com
Vc agora ♥
É isso ai
Valeu! ☭

Mais conteúdo relacionado

Destaque

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Destaque (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Acenda a sua luz com Javascript