Arduino LED from Martin P on Vimeo.
Make a web server with Websockets for Raspberry Pi. via instructables
in this instructable, I’ll show you how to create a lightweight and incredibly responsive web server with WebSockets.
I’ll use it to control a LED using an Arduino but the concept can be applied to many other projects.
Since this project does not use any on-board peripherals, it will work with just about any computer, but running it on a low power machine like the Raspberry PI makes sense for continuous operation.
Why use Node.Js or WebSockets?
There are a few tutorials on the web that show how to use a Raspberry Pi for home automation, but many use php and simple http requests to send data back to the server. This is fine for simply switching on and off some lights but quickly reaches it’s limitations when you want to run code server-side or if you want bidirectional communication.Node.Js allows you to write programs in JavaScript and the vast amount of community-made libraries enables you to write very intricate programs in just a few lines of code.
Websockets have a few advantages over simple http requests:
Speed: A normal http request has to establish a connection before any transactions can happen which takes a lot of time. A websocket is always open and ready to send or receive data.
This means that the lag can be as low as your ping, so just a millisecond or two in most cases
Bidirectional: Websockets allow data to be sent in both directions, this also means that the server can trigger events in the client.
Each Friday is PiDay here at Adafruit! Be sure to check out our posts, tutorials and new Raspberry Pi related products. Adafruit has the largest and best selection of Raspberry Pi accessories and all the code & tutorials to get you up and running in no time!