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:
- Getting Node-RED running on your Raspberry Pi and Sense HAT
- Creating a simple game using the 8×8 LED matrix and motions sensors
- Registering your Raspberry Pi as a device on the Watson IoT platform
- Publishing events from the device to the Watson IoT platform
- Creating an Application access token on Watson IoT platform
- Creating a Bluemix application that can receive events from the device
- Sending commands to the device from your Bluemix application
- Handling commands on your Raspberry Pi
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:
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
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.
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.
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
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.
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
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.
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.
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