Introduction
In this article, I will tell you how to use node.js as a bridge between an HTML 5 game and IoT by using Johnnyfive.io and Phaser.io. There are a lot of samples on the internet about Arduino and its features. But I intend to show new concepts of industry 4.0. Using IoT and Machine learning technologies, a distributed system is created which is called industry 4.0.
I will show how to develop the industry 4.0 system by using IoT, NoSQL, and Node.js Web services. All of them can be hosted on remote servers but connected over the internet. Let’s start.

Because as I mentioned before, my intention is the creation of a distributed system. What is a distributed system? A distributed system is a model in which components located on networked computers communicate and coordinate their actions by passing messages. The components interact with each other in order to achieve a common goal. That’s exactly true.
Arduino can be burned or you can decide to change Arduino because of the new coming IoT technologies. Maybe your boss can decide to keep the road with Raspberry PI so new things can be inserted in the above system. But everything should work perfectly. Node.js can give us this ability because in my point of view software should be independent of hardware. They have to survive inside of their ecosystem. They are responsible for passing messages without any interruption. This is the basic need for creating a distributed system.
What will I need to achieve the above cheat sheet?
-
All online games need a server that will be developed by node.js also. Because the snakegame will be controlled by a player using a Sparkfun joystick. This joystick will control your snake by sending x-y coordinates.
-
We will need a joystick.js to send data to Rethinkdb. Rethinkdb is a kind of NoSQL database.
Let’s start to host joystick.js,

At the end of hosting Joystick.js,
- r = require('rethinkdb')
- var five = require("johnny-five");
- var board = new five.Board();
- r.connect({
- host: 'localhost',
- port: 28015
- }, function (err, conn) {
- if (err) throw err;
- r.db('test').table('snakegame').delete().run(conn, function (err, res) {
- if (err) throw err;
- console.log(res);
- board.on("ready", function () {
- var joystick = new five.Joystick({
- pins: ["A0", "A1"],
- invertY: true
- });
- joystick.on("change", function () {
- if ((this.x == 0 && this.y == 1) || (this.x == 1 && this.y == 0) || (this.x == 0 && this.y == -1) || (this.x < -0.90 && this.y == 0)) {
- console.log("Joystick");
- console.log(" x : ", this.x);
- console.log(" y : ", this.y);
- console.log("--------------------------------------");
- r.table('snakegame').insert({
- date: new Date(),
- x: this.x,
- y: this.y
- }).run(conn, function (err, res) {
- if (err) throw err;
- console.log(res);
- });
- }
- });
- });
- });
- });




Hadshana KamalanathanPosted Jul 21, 2018, 11:33 AM
Thanks for sharing
Rajiv LochanPosted Aug 25, 2017, 9:41 AM
Nice article really appreciate !!! I am also starting my personal blog for IoT please have a look at https://goo.gl/FZ7kuQ and let me know if you have any feedback for me.