The Blinking Light, a Button and a little bit of socket.io
It seems like we are getting into some kind of a series here. It started with using a Raspberry Pi as a Docker host and then we became more interactive with a blinking light connected to the GPIO pins of the RPi. Here is the whole series so far:
- [“Docker for Your Pocket”]({% post_url 2015-11-25-docker-for-your-pocket %})
- [“Docker for Your Pocket - Streamlined”]({% post_url 2015-12-05-docker-for-your-pocket-streamlined %})
- [“Hypriot, wiringPI and a Blinking Light”]({% post_url 2015-12-06-hypriot-wiringpi-and-a-blinking-light %})
- [“The Blinking Light - Javascript Edition”]({% post_url 2016-01-13-blinking-light-javascript-edition %})
Now, I had some new objectives:
- Being able to start and stop the blinking light with a physical button.
- To have a web frontend that shows if blinkig mode is enabled or disabled.
- Being able to start and stop the blinking light with a button in the web frontend.
The “onoff”-library that I was already using made it quite easy to add the physical button, an example is already there on their project page.
The cool thing when using the “onoff”-library, you do not need a while loop in your code to query the pin state. Incoming GPIO will trigger a callback. That’s all.
This is my wiring-diagram:
You can download the Fritzing from here.
I have divided the development in two stages and tagged my code accordingly.
Release “2016-03-18” scope:
- Physical button can activate and deactivate the blinking.
- blinker should just run and ignore any exceptions, also ignore any timeouts from the GPIO service.
- Better prototyping with docker volumes mapped to host.
- Refactored how Docker was using the Node package.json.
Release “2016-04-12” scope:
- Extended frontend with a button.
- Frontend connects to backend via Websockets.
- Activate and deactivate the blinking with the button.
- Backend sends its status to frontend via socket.io.
There is a very nice question on Stackoverflow about a minimal socket.io example. In case you are not familiar with socket.io, check it out. It helped me so much getting up to speed.
So far, the projects proves to accumulate more and more technologies. For now, we have the following:
- Raspberry PI
- Docker
- NodeJS, Express
- socket.io
Of course there are some open items that I probably will cover in the future.
- Backend address is hardcoded and other code optimizations.
- Do I really need a separate port address for socket.io?
- Store data in a MongoDB.
Done for today!