Raspberry Pi + Socket.IO + React = Pixel Art π€β¨π¨
Adventures in trying out new hardware, code, and having fun in a personal project
By Stephanie Nemeth
Raspberry Pi + Socket.IO + React
= Pixel Art π€β¨π¨
Adventures in trying out new hardware, code, and having fun in a personal project
stephanie.lol
Stephanie Nemeth @stephaniecodes
Hi, I'm Stephanie.
* I built a fiber optic dress once.
[self-taught] Full-stack Ruby on Rails developer
Regularly host a coffee + code small code group βοΈ+π»
Co-organized Stupid Hackathon Amsterdam π©π
Currently looking for a new [Ruby, React, JS] job π
Overview
What I built
Why I had this idea
Build highlights
Demo!
Lessons learned
Final thoughts
What It Does
Your browser does not support the video tag.
It all started with a visit to the Stedelijk Museum...
Jean Tinguely
http://www.flickriver.com/photos/46774986@N02/17028829322/
Self-Destructing Art
I wanted something ephemeral, that would pass like a falling star and, most importantly, that would be impossible for museums to reabsorb. I didn't want it to be 'museumised'. The work had to pass by, make people dream and talk, and that would be all, the next day nothing would be left, everything would go back to the garbage bins."
- Jean Tinguely
What I dreamed of
Reality Check
Inexperienced building hardware π
Forgotten everything I learned in Physics about circuitry π©
Want to focus on the software not necessarily hardware π€
Must-have: write the frontend in React & React Native π€
Optimize for success rather than discouragement π
Solution: Unicorn HAT!
64-Led board (H ardware A ttached on T op)
No soldering! π
Has an established Python library! πͺ
My Main Requirement
Raspberry Pi must be accessible from the app anywhere, not just localhost!
How does it send messages?
Socket.IO makes communication a breeze
Enables real-time bidirectional event-based communication
Works on every platform, browser or device πͺ
Uses the WebSocket protocol with long polling as a fallback option
Has client-side libraries for Python and React! π
Setting Up the Server
var server = http.createServer(app);
var io = require('socket.io').listen(server);
io.on('connection', function(socket){
console.log("CLIENT CONNECTED π ");
socket.on('stateChanged', function(state){
io.emit("updateState", state);
});
});
Setting Up the Pi Client
from socketIO_client import SocketIO
socketIO = SocketIO('https://light-art.herokuapp.com')
def main():
# Listen
socketIO.on('connect', connect)
# Receive a message from a react client!
socketIO.on('updateState', updateState)
Setting Up React Client
import React, { Component } from 'react'
import io from 'socket.io-client'
const socket = io('https://light-art.herokuapp.com', {})
class App extends Component {
...
sendMessage = (status, data) => {
socket.emit('stateChanged',
{message: "Light Design Submitted",
squares: this.state.squares })
}
}
Let's demo this thing!! πβ¨πΎπ¨π©π¦
Try it out!
React App:
React Native App:
Lessons Learned: Raspberry Pi
It's really easy to get started building with a Pi!
I was intimidated for a long time by Pis and Arduinos. Ugh, how do I choose?!
Recommendation: A Pi starter kit makes starting a breeze. π
Includes everything to get started: power supply, pi, & pre-formatted SD card
Check out Pimoroni (that's where I got my stuff) ππ―πΆ
Lessons Learned: Raspberry Pi
Wanna do more than light a single LED but intimidated by soldering or wiring or capacitors straight away?
Look into HATs! Easily plugs into your Pi's GPIO and usually has an established python library available!
There's all kinds of HATs...
Lessons Learned: React & React Native
These frameworks ARE NOT "write once, run anywhere."
These frameworks ARE "learn once, write anywhere.β
Less pre-built OSS components available for React Native β οΈ
e.g. I had to build my own color picker for React Native app
Wanna try React Native? Check out ExponentJS !
User friendly, easy to share and view your app on your own device
Don't have to open Xcode or Android Studio, or write any native code.
Final Thoughts π€
I had never written any Python or React Native code before this project.
I had never built anything using WebSockets or Socket.IO before.
I had never built an Express server before.
I had never written for hardware before.
I had never combined so many languages into one project.
Despite all these "nevers ", I managed to pull off a cool project using a Pi. π
Don't let what you think are your insufficiencies hold you back in tackling the stuff you really wanna build-- find a way to optimize for your own success.
Thank you!
Now go build something useless. π©π π
stephanie.lol
Stephanie Nemeth @stephaniecodes