IoT With HTML5 Game Engine And Node.JS

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.
 
 
Why would I spend extra effort on the above system?
 
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?
  1. 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.
  2. 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,
  1. r = require('rethinkdb')  
  2. var five = require("johnny-five");  
  3. var board = new five.Board();  
  4. r.connect({  
  5.     host: 'localhost',  
  6.     port: 28015  
  7. }, function (err, conn) {  
  8.     if (err) throw err;  
  9.   
  10.     r.db('test').table('snakegame').delete().run(conn, function (err, res) {  
  11.         if (err) throw err;  
  12.         console.log(res);  
  13.   
  14.         board.on("ready"function () {  
  15.             var joystick = new five.Joystick({  
  16.                 pins: ["A0""A1"],  
  17.                 invertY: true  
  18.             });  
  19.   
  20.             joystick.on("change"function () {  
  21.                 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)) {  
  22.   
  23.                     console.log("Joystick");  
  24.                     console.log("  x : "this.x);  
  25.                     console.log("  y : "this.y);  
  26.                     console.log("--------------------------------------");  
  27.   
  28.                     r.table('snakegame').insert({  
  29.                         date: new Date(),  
  30.                         x: this.x,  
  31.                         y: this.y  
  32.                     }).run(conn, function (err, res) {  
  33.                         if (err) throw err;  
  34.                         console.log(res);  
  35.                     });  
  36.                 }  
  37.   
  38.             });  
  39.         });  
  40.     });  
  41. });  
Developing Game.js file to take navigation info from server.js. Server.js will produce us a web method to take data from Rethinkdb.I prefer phaser.io for developing games.
  1. jQuery.ajax({  
  2.             url: 'http://localhost:8081/sample',  
  3.             success: function (result) {  
  4.                 console.log('x: ' + result.x);  
  5.                 console.log('y: ' + result.y);  
  6.   
  7.                 var isaction = false;  
  8.                 cursors.right.isDown = false;  
  9.                 cursors.left.isDown = false;  
  10.                 cursors.up.isDown = false;  
  11.                 cursors.down.isDown = false;  
  12.                 if (result.x < 0 && result.y == 0) {  
  13.   
  14.                     console.log('left');  
  15.                     cursors.left.isDown = true;  
  16.                     isaction = true;  
  17.                 } else if (result.x > 0 && result.y == 0) {  
  18.   
  19.                     console.log('right');  
  20.                     cursors.right.isDown = true;  
  21.                     isaction = true;  
  22.   
  23.                 } else if (result.y < 0 && result.x == 0) {  
  24.                     console.log('up');  
  25.                     cursors.up.isDown = true;  
  26.                     isaction = true;  
  27.   
  28.                 } else if (result.y > 0 && result.x == 0) {  
  29.                     console.log('down');  
  30.                     cursors.down.isDown = true;  
  31.                     isaction = true;  
  32.   
  33.                 }  
 
When we look at the database, all the action about x-y coordinates will be saved inside of the Rethinkdb database. So in this method server.js can easily access db contend by using Reql.
 
 
At the end of this article,
 
We have used 3 js files,
  1. Game.js
  2. Server.js
  3. Joystick.js
Game.js
 
Game.js aims to catch your movement when you change the position of joystick up and down OR right or left. 
 
Server.js
 
It is a kind of a bridge between game and game database. After joystick.js saving data inside of the RethinkDb, Server.js can take this data from Rethinkdb.
 
Joystick.js
 
It has a continuous integration with Rethinkdb. Always save Snake position in the NoSQL database by calling Rethinkdb connection codes.
 
As a Result, we focus on the continuous integration of IoT with the web environment. Of course, I prefer Node.js to get a successful result because Node.js has good features for web and IoT hybrid applications. The main teaching of the article is that it is easy to achieve a distributed system by developing small node.js application. This article also proves to us that Node.js is a good choice for the IoT world because Industry 4.0 needs scalability and flexibility.