Contents


Overview

Skill Level: Beginner

This recipe will help you to create an IoT solution for available parking lot detection. Device can switch un/available mode automatically based on the space occupation.

Ingredients

  • The raspberry pi 3 borad
  • 16 GB microSD card
  • A magnetic sendor (HMC5883L)
  • A diffused 10-mm LED
  • A BlueMix account

Step-by-step

  1. Prepare your raspberry pi

    • Install Raspbian on your raspberry pi 3
      • Download¬†Raspbian image(Raspbian Jessie with Pixel) and extract to you local folder.
      • Download and install the Etcher SD card burner utility.
      • Flash to install Raspbian to the microSD card by using Etcher utility.
    • Connect the LED to Pi
    • Connect Raspberry Pi 3 to the network

    device

  2. Create an Internet of Things Platform service

    Go to the Bluemix portal to create a Watson IoT Platform

    create-iot-platform

    • Launch the dashboard of the newly created Watson IoT platform

    launch-dashboard

     

     

  3. Register Raspberry Pi to the Watson IoT Platform

    • In the IoT platform dashboard, click DEVICES tab -> Device Types tab -> +Create Type button to create a new device type for your raspberry pi.

    create-device-type

    create-device-type-2

    create-device-type3

    Observe that a new device type is added in the IBM Watson IoT Platform Organization. At this step, you have successfully created a device type in IBM Watson Platform organization.

    create-device-type-4

    • To register a device, in the IBM Watson Platform dashboard, click Browse tab and then +Add Device button as shown below:

    add-device-1

    • Follow the instructions to fill in required information for your device to complete the registration. Finally you will get the device credential information when your registration successfully.

    add-device-3

               For more detail, you can also reference this recipe to register a device in your Watson IoT Platform.

    • ¬†Generate API Key for cloud foundry Apps connectivity in Watson IoT Platform.
      • Click the “APPS” tab from the left panel of the dashboard.
      • Click “Generate API Key” on the right top corner of the page.¬†genreate-api-key1
      • Please DO NOT forget to copy the API Key and Authentication Token string before move to next step.
      • Provide comment information and click “Generate” button to generate the key
  4. Install Node-RED on your device

    • Apply raspbian updates for your Raspberry Pi 3
      sudo apt update
      sudo apt full-upgrade
    • Install node.js 6.x version (not suggest to install the latest version, which is 7.0.0)
      curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
      sudo apt install nodejs
    • You can used the following command to confirm your node.js version
      node -v
    • Install Node-RED:
      sudo npm install -g --unsafe-perm node-red
    • Start your Node-RED:
      node-red
    • Once Node-RED is running – point a browser to the machine where Node-RED is running. One way to find the IP address of the Pi is to use the command
      hostname -I

      Then browse to http://{the-ip-address-returned}:1880/ use the Node-RED editor.

    • (Optional) Set up autostart on boot. Please follow the “Starting Node-RED on boot” section in this article to enable autostart for your Node-RED.
  5. Connecting Raspberry Pi by using Node-RED

    • After installed Node-RED on Raspberry Pi, we still need to install the node for supporting IBM Watson IoT Platfrom.
      • Go to Node-RED installation folder, in my case is “/usr/lib/node_modules/node-red”
      • Run the following command in the user directory of you Node-RED install:
        sudo npm install node-red-contrib-ibm-watson-iot
      • Restart the Node-RED and refresh your browser which connected to the Node-RED editor, if you see the Watson IoT inout and output nodes showing up then it is ready to move to next step.

                        WIoT_input_node    WIoT_output_node

    • Download the flowing node.js files and put in local folder (e.g. /home/pi/parkinglot) on your Raspberry Pi, these scripts will be used in the Node-RED flows in your device:
    • Install the required module to run the above script on your Raspberry Pi.
    sudo npm install hmc5883l-pi
    sudo npm install wiring-pi
    • Make sure the I2C intrface is enable on your Raspberry Pi. You can go to Raspberry Pi Configration -> Interfaces -> I2C to have a check. If you set the I2C interface as enable, reboot is required to take effect.

              Enable-I2C-Interface

    • Add a flow to the Raspberry Node-RED editor (http://{your-raspberrypi-hostname}:1880) to read magnetic data from sensor, and send to Bluemix Watson IoT Platform serviec you've created before.
      • In the Node-RED editor, which you have opened in the browser (either on Raspberry Pi or on the host system), click on Menu ‚Äď > Import -> Clipboard.¬†¬†Import-flow-1
        • Copy the json from this¬†link and paste it in the clipboard.
        • Click on Ok button then place the flow in the work place.¬†device-node-red-flow
        • Click on Deploy button to start.
        • Config the “Device Event” output node to connect to the Watson IoT Platform.
        • Double click the “Device Event”¬†output node.read-magnetic-data-flow-2
        • Select connect as Registered, and then select edit button in Credentials.¬†
        • create-device-type-2
        • Fill in the Organization, Device Type, Device ID, Auth Token and then click on the Add button. All of these information can be found on dashboard of your Bluemix Watson IoT Platform.¬†
        • add-flow-for-app-3
        • Config the “cmd receiver” input node to use the credential you've just added in the above step.¬†
        • Click Ok button
        • Click Deploy button to start.

                   

  6. Create a Node-RED application for Watson IoT Platform

    In this case, we need a Node-RED application to receive events from device and send commands to the device. The device event contains the magnetic data which will be used to calculate whether this parking slot is occupied. The application is responsible for the calculation and send back a signal/command to the Raspberry Pi device if the parking slot status is changed (from occupoed to available, or from available to occupied).

    • Login Bluemix portal with your own account, navigate to the catalog page, input “Node-RED” in the search box and click the Node-RED Starter application on the search result.

    create-node-red-app-1

    • Provide the name to create your Node-RED application and click “Create” button. Bluemix will create a Node.js application with a default cloudant DB for you. You need to wait a few minutes for the app to get fully started.

    create-node-red-app-2

    • ¬†Connect the Node-RED application to Watson IoT Platform.
      • Go to Overview page of your application
      • Click the “Connections” tab from the left panel
      • Click the “Connect Existing” button on the right top corner.¬†create-node-red-app-3
      • Choose the Watson IoT Platfrom service you've created in previours step to bind to. Then you will be asked to restage your application. Click “Restage” and wait until the application back to running status to finish this step.
      • Navigate “Overview” page and click “View App” ¬†button to the application URL.¬†create-node-red-app-4
      • Click “Go to your Node-RED flow editor” button to the Node-RED editor of you application.¬†
  7. Add a Node-RED flow for application to receive/send message.

    1. In the Node-RED editor, click on Menu ‚Äď> Import -> Clipboard, Copy the json content from this link¬†to your clipboard and click “import” button to import the flow from this json.

    add-flow-for-app-1

    The “calculation” function node contains the algorithm to detect whether a parking lot is occupied by a car. It is written in Node.js and referece to this paper. You can double click the “calculation” function node to view the node.js implementation in our solution.

        calculate1

    2. Click both “IBM IoT” input and output node to set up the API key information of the Watson IoT Platform.

    • Choose “API Key” as authentication type and choose to add new ibmiot config for API Key.add-flow-for-app-2
    • Click the edit button to configure API key, you need to provide the API key and API token you got when you add a API key in previours step. Click “Add” button to finish this step.add-flow-for-app-3
    • Click “Deploy” to apply this flow.

    Note: At this stage, the device, application and Watson IoT Platform is wiring together through Node-RED flows.

  8. Walk through the E2E flow

    Initialize:

    1. Make sure your HMC5883l sensor is in a fixed position.
    2. Click the trigger button on the timestamp node in your application Node-RED editor to initialize the data.
    3. You can add some debug node on your Node-RED flow and view massge pass through the flow.
    4. The LED is turned off at initial stage and no object occupied near the sensor.

    Senario:

    1. Move a magnetic object (such as you mobile phone) to near by or cover the sensor
    2. The LED will be turned on to indicate an “occupied” status after a few seconds for a stable magnetic field.
    3. Move the test object away and wait a few seconds.
    4. The LED will be turned off to indicate an “available” status.

1 comment on"Parking IoT solution using Watson IoT with Node-RED"

  1. […] can try assembling an algorithm in Node-RED in this hands-on recipe: Parking IoT solution (Step […]

Join The Discussion