AdSense

Showing posts with label IoT RASPBERRY PI. Show all posts
Showing posts with label IoT RASPBERRY PI. Show all posts

Sunday, August 20, 2017

IBM Watson IoT (3): marble maze game with Raspberry Pi / Sense Hat and Node-RED


IBM WATSON IoT (3): MARBLE MAZE GAME WITH RASPBERRY PI / SENSE HAT AND NODE-RED

- Layout for this exercise


1 - Introduction

- The goal of this exercise is to create a marble maze game using the Motion events properties (accelerometer, gyroscope, magenotmeter compass) and the 8x8 LED matrix provided by the Sense HAT module added on top of a Raspberry Pi device.

- The marble maze game consists of 3 types of colored and lighted dots: green (walls that cannot be traversed), red (one dot, what moves trying to find the 'goal') and blue (one dot, being the 'goal') . 

- The rest of the dots (unlighted) in the matrix are the paths where the red dot moves along.

- The red dot moves trying to reach the position of the dot blue, at which point the goal of the game is achieved.

Once the goal of the game is achieved a new layout and goal position is automatically generated.

The Raspberry Pi device is connected to the Watson IoT platform where it sends events such as the final position (XY coordinates) of the blue dot each time the goal is achieved.

- This video shows how the game works:





2 - Creating a Node-RED flow

- The Node-RED flow consists of 4 nodes.

1) Sense Hat Output



- This node is edited for using Motion events:




2) Sense HAT Input



- No special edition for this node:














3) Function



- The node function generates the layout, the green walls, the red and blue dots, as well as collects the movement of the Raspberry Pi sensors:




- JavaScript code of the function:

// New layout and goal position

function updateGoal() {
 do {
 goalX = Math.floor(Math.random()*7)+1;
 goalY = Math.floor(Math.random()*7)+1;
 } while(checkWall(goalX,goalY));

 flow.set('goalX',goalX);
 flow.set('goalY',goalY); 
}

// Generating walls

function generateWalls() {
 walls = [];
 for (var i=0;i<10;i++) {
 var wx = 0;
 var wy = 0;
 do {
 wx = Math.floor(Math.random()*8);
 wy = Math.floor(Math.random()*8);
 } while(wx === x && wy === y);
 walls.push({x:wx,y:wy});
 }
 flow.set('walls',walls);
}

// Checking collides with the walls

function checkWall(x,y) {
 for (var i=0;i<walls.length;i++) {
 if (walls[i].x === x && walls[i].y === y) {
 return true;
 }
 }
 return false;
}

// Retrieve positions

var x = context.get('x');
var y = context.get('y');

// Retrieve the current goal position from flow context

var goalX = flow.get('goalX');
var goalY = flow.get('goalY');

// Retrieve the current walls from flow context

var walls = flow.get('walls');

// Generate new walls if needed

if (!walls) {
 generateWalls();
}

// Generate a new goal if needed

if (isNaN(goalX) || isNaN(goalY)) {
 updateGoal();
}

var moved = false;
var ox = x;
var oy = y;

// Initializing the player position

if (isNaN(x) || isNaN(y)) {
 ox = 3;
 oy = 3;
 x = 3;
 y = 3;
 moved = true;
} else {

 // Retrieve the current roll and pitch from the message arriving from the SenseHAT

 var roll = msg.payload.orientation.roll;
 var pitch = msg.payload.orientation.pitch;

 // Move the player based on tilt, ensuring it stays within the bounds of the screen

 var sensitivity = 7;
 if (roll > sensitivity && roll < 90) {
 y += 1;
 moved = true;
 if (y > 6) { y = 7; }
 } else if (roll < 360-sensitivity && roll > 270) {
 y -= 1;
 moved = true;
 if (y < 1) { y = 0; }
 }
 if (pitch > sensitivity && pitch < 90) {
 x -= 1;
 moved = true;
 if (x < 1) { x = 0; }
 } else if (pitch < 360-sensitivity && pitch > 270) {
 x += 1;
 moved = true;
 if (x > 6) { x = 7; }
 }

 // If the resultant position hits a wall, go back to where it started

 if (checkWall(x,y)) {
 x = ox;
 y = oy;
 }
}

// Store the new player position

context.set('x',x);
context.set('y',y);

if (moved) {
 var eventMsg = null;

 // If the player is on the goal, generate a new set of walls and goal position

 if (x === goalX && y === goalY) {
 generateWalls();
 updateGoal();
 // Blank the display
 msg.payload = "*,*,off,";
 eventMsg = {
 payload: {
 x: goalX,
 y: goalY
 }
 };
 } else {

 // Blank the old position of the player

 msg.payload = ox+","+oy+",off,";
 }

 // Draw each of the walls

 for (var i =0;i<walls.length;i++) {
 msg.payload += walls[i].x+","+walls[i].y+",green,";
 }

// Draw the goal and player position

 msg.payload += goalX+","+goalY+",blue,"+x+","+y+",red";

 // Pass  the message to the Sense HAT Out node

 return [msg,eventMsg];
}

// Nothing has moved so no need to update screen - return nothing

return null;



4) Watson IoT



- The Watson IoT node is connected as a Device with the Quickstart service, what it is enough for the purpose of this exercise:




- Deploying the flow:










- Clicking the Quickstart icon:










- The Quickstart service offers information about the XY coordinates of the blue dot, located at the 8x8 LED matrix of the Sense HAT module, every time the goal is achieved:












Tuesday, August 15, 2017

IBM Watson IoT (2): connecting a Raspberry Pi as a Registered Device with Node-RED


IBM WATSON IoT (2): CONNECTING A RASPBERRY PI AS A REGISTERED DEVICE WITH NODE-RED


- Layout for this exercise:





* To complete this exercise it is assumed the previous existence of an account on the IBM Bluemix platform


* This exercise is based in the previous exercise flow:


https://dgmsp.blogspot.com/2017/08/ibm-watson-iot-1-connecting-raspberry.html




1 - Creating an IBM Watson IoT Platform organization


- In this exercise, and unlike the previous one with Quickstart, the Raspberry Pi connects to the IoT platform providing credentials and registration of the device.

- Creating a private organization space that requires authentication and registration for all devices and applications to use in that organization. - Each organization is defined by a 6-character ID, as we will see in short.

- The first step consists of the login process into the IBM Bluemix account. Let's notice that IBM Watson IoT is a service provided by the IBM Bluemix cloud platform:







- Going to the Internet of Things Platform:






- Creating an IBM Watson IoT  service:





- Clicking Launch:






- Entering the IBM Watson organization space:






2 - Creating a Device Type 


- Devices connected to the IBM Watson IoT Platform are associated with a Device Type, what are intended to be groups of devices sharing common characteristics. 


- Going to Devices:





- Browse and Create Type:









- Creating a "device type" (be careful not confusing with "gateway type"):





- Entering Name and Description:





- Defining the template is optional. In this case Manufacturer and Model are defined:







- Metadata is optional:






3 - Adding and registering a device to the the IBM Watson IoT Platform


- Choosing the Device Type:








- The Security step allows to enter an optional token, or otherwise it can be auto generated.  


- In this case we choose an authentication token  auto generated by the service, what will be displayed and the end of the registration process:









- Adding the device RBP_1:





- After adding the device the auto generated Device Credentials are displayed. It is important to write down all this information because in case of loss there is no possible recovery:





- Clicking the X, the Security step is ended:






4 - Generating API Keys


- Some API Keys must be generated to enable the connectivity. Going to Apps, clicking Generate API Key:









- API Keys are generated to allow the connection:



















5 - Connecting the Raspberry Pi as a registered Device


- At this point a device RBP_1 has been created, added and registered to the IBM Watson IoT platform, and also some API Keys for enabling the connectivity have been generated.


- To connect the Raspberry Pi device (in this case as Registeredaccording to the previous exercise flow:


https://dgmsp.blogspot.com/2017/08/ibm-watson-iot-1-connecting-raspberry.html


- The Watson IoT node must be edited as Registered:





- Clicking the Credentials icon:     





- Entering the credentials (Organization ID, Device Type, Device ID, Authentication Token) obtained at point 2 of this exercise:







- Deploying:





- Eventually the connection is achieved:






6 - Receiving data from the Raspberry Pi device at the Watson IoT platform


- Now the RBP_1 is connected:






- The Connection Log shows the sucssessfull connection:





- Events are generated every 5 seconds, displaying the last received CPU temperature from the RBP_1 device: