Node-RED on the Raspberry Pi

Goal: Learn how to work on the Raspberry Pi with the Sense HAT.

Tips:

  • Node-RED documentation is here.
  • You need to install npm. Search for “npm install linux”. (Or see below)
  • The browser on the Raspberry Pi is not good for using Node-RED. Therefore, run Node-RED on the Raspberry Pi, but log into it via a browser from your laptop. (Via the Pi’s IP address…)

Here are the lines to install NPM:

sudo apt-get install npm

Securing Node-RED

By default, Node-RED is accessible to anyone from the network. You should therefore first protect access, with the steps described here.. For what we do today, the steps listed under Editor & Admin API security until Generating the password hash is enough. (Use a hard-coded user.)

Your First Flow

Create a simple flow, only consisting of an inject node and a debug node. Configure all nodes so that when you click on the inject node, a text is written to the Node-RED debug console.

For the Report: Of course, include a screenshot of the flow and describe how it worked.

Node-RED for the Sense HAT

Check if these flows for the Sense HAT are already installed. Otherwise, install them via the command line.

Task: Build a simple application that uses some input from the Sense HAT (via flow rpi-sensehat in) and produces some output (via flow rpi-sensehat out).

For the Report: Of course, include a screenshot of the flow and describe how it worked.

Create a Slack Robot

Goal: You should build a very simple Slack chat robot With the API of Slack, the Sense HAT and the Raspberry Pi.

First, create a new Slack domain. (You can delete it again after this course.) It’s enough if one of you has a user, but its maybe more fun when you can experiment with several users.

  • Read Getting Started With Custom Integrations about how to create a chat robot.
  • Find the green button Set up a bot user, and login into the Slack account you created earlier.
  • Select Add Configuration…, select a name for the bot, and create it.
  • Copy the API token for later.

Milestone: You have now created a robot in Slack. It now waits for instructions. They can come from different applications that you can build in the following.

Slack Robot with Node-RED

Install these Node-RED flows for an integration with Slack. After the installation, you should get two new flows, slackbot-listen and slackbot-reply.

Tip: It is necessary to install nodes globally, with the -g option:

npm install -g node-red-contrib-slack

Combine the slackbot-listen and slackbot-reply with other nodes. Stepwise explore what you can do with this API:

  • Just send an empty reply whenever you contact the chat bot from within Slack.
  • Can you print the message sent to the chat bot to the Node-RED console?
  • Deliver the current temperature as a reply to any message.

Challenge: Try to build a (more complex) Node-RED flow so that you send the current temperature to any message that contains the word “temperature” to the chat bot. When the message contains “humidity” send the current humidity.

For the Report: Of course, include a screenshot of the flow and describe how it worked.

Alarm System with Camera

Goal: Let’s create a more advanced alarm system that detects some movement (with the PIR sensor), takes a picture (with the USB webcam) and notifies you via Slack.

For this task, we work again in Python.

Altogether, this is a difficult and complex task; you need to coordinate many functions and APIs. Therefore, try to build the application step by step, and try each function in isolation before putting everything together.

Below is a list of steps. You don’t have to do them in the given order.

Task: Read first through all steps, and try to make a sequence diagram of the final application. The diagram should show a lifeline for your application, a lifeline for the PIR sensor, the camera, the cloudinary API and the Slack API.

Task: Once you have the sequence diagram, work on each of the steps.

Step: Slack Message with Image

The Slack API allows you to display an image alongside your message. However, Slack expects that you send the URL of an image, not the image itself.

Before you try to upload your own image from the webcam, you should test how the Slack API works and provide it with an image that is accessible on the internet by a url. (You can for instance use http://lorempixel.com for that.)

Step: Taking a Picture

Find a way to capture an image with a USB camera in Python. It’s a standard USB camera, and there should be code out there that does the job.

Step: Uploading an Image

Since Slack expects the URL to an image and not the image itself, we need to upload the camera picture somewhere and make it accessible via an URL.

One way to achieve it is to use a hosting service for images, like Cloudinary. It offers an API to uplpoad images, and retrieve a URL for them. Have a look at their SDK for Python on Github. (Tip: It seems you can install it again via pip install cloudinary.)

  • Create an account at Cloudinary.
  • Install the Python SDK.
  • Upload an image and test if it is accessible in the URL via a browser.

Step: Sensing Movement

Use the PIR sensor to detect movement, as in the first week.

Finally: Putting all Together

Once you have control over all of the individual steps above, put everything together to build the complete alarm system.

For the Report: Document how it works in a series of screenshots and photos, for instance like in a comic strip.

Well done!