Contents


Overview

Skill Level: Any Skill Level

Beginner

IntroductionThe Sense HAT is an accessory for the Raspberry Pi that provides an 8×8 LED matrix, a variety of motion and environmental sensors and a five-button joystick. This combination makes it a device that can be used both as a sensor that generates events and one that can provide distinctive feedback to the user. It […]

Ingredients

  • A Raspberry Pi running Raspbian Jessie
  • A Sense HAT
  • Bluemix Account and Watson IoT platform organization

Step-by-step

  1. Introduction

    The Sense HAT is an accessory for the Raspberry Pi that provides an 8×8 LED matrix, a variety of motion and environmental sensors and a five-button joystick. This combination makes it a device that can be used both as a sensor that generates events and one that can provide distinctive feedback to the user. It is an ideal device for getting started with the Internet of Things.

    This recipe shows how to create a simple game on the Sense HAT that connects to the Watson IoT platform to share events and receives commands back from an application running in Bluemix. It provides a starting point for creating your own applications connected to the Watson IoT platform.

    The recipe takes you through the following steps:

    1. Getting Node-RED running on your Raspberry Pi and Sense HAT
    2. Creating a simple game using the 8×8 LED matrix and motions sensors
    3. Registering your Raspberry Pi as a device on the Watson IoT platform
    4. Publishing events from the device to the Watson IoT platform
    5. Creating an Application access token on Watson IoT platform
    6. Creating a Bluemix application that can receive events from the device
    7. Sending commands to the device from your Bluemix application
    8. Handling commands on your Raspberry Pi
  2. Getting Node-RED running on your Raspberry Pi and Sense HAT

    Node-RED comes pre-installed on the Raspbian Jessie distribution. This includes nodes for interacting with the Sense HAT as well as nodes for connecting to the Watson IoT platform.

    This recipe requires the very latest version of the Node-RED package to be installed. To ensure you have the latest version, run the following commands:

    $ sudo apt-get update
    $ sudo apt-get install nodered

     

    You can then start Node-RED by running the following command:

    $ node-red-start

     

    Once started, you can open the Node-RED editor in your web browser by pointing it at http://ip-address-of-your-pi:1880. You can find the IP address of your Raspberry Pi by running the command:

    $ hostname -I 

     

    If you want Node-RED to run automatically each time the Raspberry Pi is turned on, run the following command:

    $ sudo systemctl enable nodered.service
  3. Creating a simple game using the Sense HAT 8×8 LED matrix and motions sensors

    To begin with, we're going to create a simple marble-maze game that uses the tilt sensors and LED display of the Sense HAT. This will consist of a Node-RED flow with a Sense HAT Input node emitting motion events, a Function node to do something with the events and finally a Sense HAT Output node to update the LED display.

    1. Add a Sense HAT Input node and configure it to only send Motion events.

    2. Open the Import Nodes dialog (Cmd/Ctrl-i or MenuImportClipboard) and paste in the JSON from here. This will add a Function node to handle the motion events.

    3. Add a Sense HAT Output node.

    4. Wire the three nodes together and deploy the flow

     

    Once deployed, as you tilt the Raspberry Pi, you can move the red dot, the 'marble', around the display. The aim is to move the marble over the blue dot, the 'goal'. The green dots are walls to make it more interesting. When the marble hits the goal, it generates a new set of walls and moves the goal.

     

    With the basic game in place, the next step is to get the Raspberry Pi connected to the Watson IoT platform and sending events.

  4. Registering your Raspberry Pi as a device on the Watson IoT platform

    In order to send events to the Watson IoT platform you must first register your Raspberry Pi as a device on the platform.

    The recipe available here takes you through the necessary steps to register your device. Once complete, you will be presented with the device credentials as shown here:

     

    Make a note of them as you will need them in the following steps. Note that the Authentication Token is not recoverable – if you don't make a note of it at this stage you will need to re-register the device to generate a new token.

  5. Publishing events from the device to the Watson IoT platform

    With the device now registered, this step modifies the existing flow to send an event to the Watson IoT platform whenever the goal is hit.

    1. Delete the existing Function node from the flow and import the node from here in its place.

    The Function has been updated to add a second output from which a message is emitted when the marble hits the goal.

    2. Add a Watson IoT Output node and configure it as follows:

    • Connect as Device in Registered mode
    • Add a new wiot-credentials node and give it the credentials obtained in the previous step
    • Set the Event type to 'goal'

    3. Wire the second output of the Function node to the Watson IoT Output node and deploy the new flow

     

     

  6. Creating a Bluemix application that can receive events from the device

     

    With events now being sent to Watson IoT, the next step is to create an application in Bluemix to receive the events.

    1. In the Bluemix catalog, select the Node-RED Starter under the Boilerplate category

    2. Create a new instance, giving your application a unique name. After a few minutes you will be taken to your applications Overview page

    3. From the overview page, connect (bind) your Internet of Things Platform service instance to your application

    4. You will then be able to access the Node-RED editor by opening http://<your-application-name>.mybluemix.net/red

     

    With your Node-RED instance running and connected to the IoT Platform, you can create a simple flow to start receiving the device events.

    1. Add an ibmiot input node and configure it to use Bluemix Service authentication. Set the Device Type field to match the type you used to register your device with. Leave the Device Id and Event fields set to all

    2. Add a Debug node wired to the output of the ibmiot node and hit the Deploy button

    Once deployed, open the Debug sidebar in the Node-RED editor. On the Raspberry Pi, tilt the board so the red marble hits the blue goal. When it hits the goal you should see a message appear in the Debug sidebar:

    The event contains the x and y positions of the goal that was hit.

     

  7. Sending commands to the device from your Bluemix application

    The next task is to send commands back to the device in response to the events it sends.

    For this recipe, we will send back to the device a new maze layout and update the location of the goal.

    1. On your Bluemix Node-RED instance, import the Function node from here and wire it to the existing ibmiot input node.

    2. Add an ibmiot output node wired to the output of the Function node. In the configuration of the node, set the authentication mode to Bluemix Service and the Device Type and Id properties to match that of your registered device. The Command Type should be set to maze.

    3. Deploy the updates

     

  8. Handling commands on your Raspberry Pi

    The final task is to update the flows on the Raspberry Pi to handle the commands sent to it.

    1. On your Raspberry Pi Node-RED instance, add an Watson IoT Input node. Configure the node to connect as a Device, using the credentials you added previously and set to receive all commands.

    2. Import the Change node from here and wire it to the Watson IoT Input node.

    This node updates the various context properties used by the flow using the values received in the command message.

    3. Delete the existing Function node from the flow and import the node from here in its place.

    This updated function includes code to refresh the display when a command is received. It also resets the display if the Sense HAT joystick is pressed – useful if the randomly generated walls block the goal.

    4. To enable the Sense HAT joystick, edit the Sense HAT Input node and tick the box for Joystick events.

    5. Restore the wiring to the Function and wire in the newly added nodes as follows:

     

    6. Deploy the updates.

    At this point, you have a simple game on the Raspberry Pi and Sense HAT that, whenever you hit the goal, sends an event via the Watson IoT Platform to an application running in Bluemix that responds with a command containing a new maze and goal.

  9. Next steps

    The recipe has demonstrated how you can connect your Raspberry Pi and Sense HAT to the Watson IoT Platform. It took a self-contained application – the original game from the first step – and move parts of it into the cloud. By processing the sensor data on the Raspberry Pi, it only sends meaningful events to the cloud.

     

    Next Steps

    There are many possible extensions to this recipe you may want to try out. For example:

    • Keep a score of how long it takes to complete each maze
    • Create a web page served by Node-RED in Bluemix that displays realtime stats about the game
    • Add a second Raspberry Pi and make the game multi-player

     

8 comments on"Connecting a Sense HAT to Watson IoT using Node-RED"

  1. ManfredvanderVoort May 21, 2016

    Thanks for sharing this. Nice recipe to show people some elements of the IoT with a simple yet attractive game.

  2. The JSON provided in Step 3 doesn’t seem to be valid. When I try to validate the JSON, I get the following error:

    Error: Parse error on line 1:
    …layn msg.payload = “*,*,off,”;n } else {
    ———————–^
    Expecting ‘EOF’, ‘}’, ‘:’, ‘,’, ‘]’, got ‘undefined’

    • Thanks for raising this. It appears the recent updates to the recipes site has broken our ability to include JSON examples directly in the recipe. I have moved them over to github and updated the recipe to link to them. Apologies for the inconvenience, but thank you again for spotting it.

  3. I get the following error when I add the Marble Maze function to Node-Red:
    function:(error)
    ReferenceError: n is not defined (line 44, col 1)

    Any idea how I can troubleshoot this error?

    Thank you in advance!

    Rob

    • Hi Rob,
      thanks for spotting this. I have updated the code linked to for the node to fix it.

      If you edit the Function node, you’ll see a rogue ‘n’ at the start of line 44. If you delete that character and redeploy, you should be back on track.

      Nick

  4. Hey Nick. Thanks for creating this recipe. I was successful with everything up steps 7 and 8 on getting the Raspberry Pi to respond to commands from Bluemix. The walls are never drawn, the goal disappears after being captured, the red “ball” does not move to another location and the goal only appears upon pressing the joystick button. After observing several debug outputs I added to the flows, it appears that the Raspberry Pi is not receiving anything from Bluemix. I’m using a RaspPi 3 from home behind a U-Verse residential gateway. Suggestions? Firewall? NAT? My device registration not working? –John.

  5. Hi, I am having trouble getting data from the senseHAT board. It shows as ‘disconnected’ on Node-RED, while in console, I get the following message:

    pi : TTY=unknown ; PWD=/home/pi ; USER=root ; COMMAND=/usr/bin/python -u /usr/lib/node_modules/node-red-pi-sense-hat/sensehat.py
    pam_unix(sudo:session): session opened for user root by (uid=0)
    pam_unix(sudo:session): session closed for user root

    Note that I have already installed senseHAT with this command:
    npm install node-red-node-pi-sense-hat

    Can you please advise?

  6. M2MSupport June 18, 2017

    Hi Nick,
    I’m using Raspberry Pi 3 with Sense HAT board. Raspberry is powered up with LEDs on the Sense HAT board lit. Now I added ‘Sense HAT’ input node and configured to emit all events. I see that the ‘Sense HAT’ input node connected for less than 2 seconds and it disconnects immediately. Any suggestions of what the issue could be and how to debug this?

    -Surya

Join The Discussion