Contents


Conserve water with the Internet of Things, Part 1

Build a Raspberry Pi monitoring solution on IBM Bluemix with the IoT Platform Starter, Node-RED, and Cloudant

Comments

Important: IBM Internet of Things Foundation (IoT Foundation) is now named IBM Watson IoT Platform. The Bluemix service names have also changed. This tutorial was written using previous service names and a previous version of the IBM Bluemix® interface. Given the rapid evolution of technology, some steps and illustrations may have changed. The content and images have not been updated. It is provided "as is." Given the rapid evolution of technology, some steps and illustrations may have changed as well.

The Poseidon project is a voluntary initiative that aims to reduce water usage in the world. As a first step, the project developed educational material for monitoring the water in the soil of houseplants using an app that views sensor data from a plant to understand when the plant needs watering. The objective is to build an enthusiastic community focused on finding solutions that reduce water usage by applying technologies such as the Internet of Things (IoT), cloud computing, mobile, and analytics.

In this three-part tutorial series, you'll build a low-cost plant-watering solution and find out how to work collaboratively to improve on it. The solution is based on Raspberry Pi and uses IBM Bluemix for collecting and visualizing sensor data and posting updates on your social network.

Here you can see the device in use:

Photo of the plant-monitoring device in operation
Photo of the plant-monitoring device in operation


And here's a message that the solution posted on Twitter:

Screenshot of tweet generated by the plant-monitoring system
Screenshot of tweet generated by the plant-monitoring system


The solution architecture looks like this:

Diagram of the solution design
Diagram of the solution design


The system uses:

  • A Raspberry Pi (1 or 2) with GrovePi extension board, a temperature/barometric sensor, and a soil-moisture sensor.
  • An IoT cloud broker. This free service, available on the Internet, receives the sensor data and redistributes it to all subscribed clients. You'll use the open MQTT protocol for the exchange of sensor data.
  • IBM Bluemix as a cloud platform to run the application. You'll use the Internet of Things Foundation Starter to store and visualize sensor data for desktop and mobile devices. The IoT starter uses these Bluemix services:
    • Node.js: A server-side platform for running JavaScript applications.
    • Node-RED: A service that enables you to wire together nodes (functionality) visually in a browser. You'll use Node-RED to wire incoming sensor data to a database to store the data, and to a tweet node that submits data to Twitter.
    • Cloudant: A NoSQL cloud database based on CouchDB.
  • External services such as Twitter for posting notifications when your plant needs water.

The IBM Internet of Things Foundation Starter provides the Node.js runtime and the Node-RED application and creates an instance of the Cloudant NoSQL database.

What you'll need to build your solution

  • A Bluemix account and a DevOps Services account, both linked to your IBM ID. (You'll use your DevOps Services account in Parts 2 and 3.)
  • Familiarity with Linux and Python is helpful but not required.

Required hardware

  • Raspberry Pi Model B (SKU: 800025001). (Model B+ was released later and will probably work as well.)
  • GrovePi extension board for Raspberry Pi (SKU: 830004001)
  • Grove - Barometer (High-Accuracy) (includes temperature) (SKU: 811027001)
  • Grove - Moisture Sensor (SKU: SEN92355P)
  • 2 Grove Universal 4 Pin Buckled Cables (SKU ACC90453O)
  • 4GB SD card

Optional hardware

  • Raspberry Pi Power Supply (or other mini USB power supply)
  • HDMI cable to connect the Pi to your TV or monitor
  • Ethernet network cable
  • USB mouse and keyboard for the (one-time) configuration of your Pi

Run the appGet the code

Step 1. Set up your Raspberry Pi

The Raspberry Foundation makes installation of an operating system for the Raspberry Pi as easy as possible by providing the NOOBS (New Out Of The Box) installer.

  1. Set up your Raspberry Pi so that it's ready to install the OS:
    1. Connect the Pi with the HDMI cable to a monitor or TV.
    2. Connect an Ethernet network cable.
    3. Connect your USB mouse and keyboard.
  2. Set up your Pi with the Raspian OS by following the instructions in Getting Started with NOOBS.
  3. Verify that you can connect and log in to your Pi.
  4. Do not connect the GrovePi extension board yet. Follow the instructions in Connecting the Raspberry Pi and Grove Sensors from Dexter Industries (manufacturer of the GrovePi).

The hardware is now configured. Now you need to install the script that sends the data from the sensors to a central cloud service for further processing.

Step 2. Install the Poseidon client script

You'll use MQTT— a lightweight publish/subscribe messaging protocol — to send sensor data to the IBM IoT cloud. MQTT clients are available in many programming languages from the Paho project. You'll use a Python client.

  1. Change to your home directory (cd ~) and clone the PoseidonProject Git repository:
    git clone https://hub.jazz.net/git/bbhavers/PoseidonProject
  2. Copy dependent files and modules:
    cd PoseidonProject/Tutorial1/PoseidonClient
    cp ~/GrovePi/Software/Python/grove_barometer_lib.py .
    cp ~/GrovePi/Software/Python/grovepi.py .
    Note: The Grove-Barometer sensor has been replaced with a new version since this article was written. A new driver needs to be installed and PoseidonClient.py needs to be adapted to include this new driver. See the comments on the right.
  3. Edit the config.py configuration file (by using vi or nano): Enter the name of your sensor in the clientID field and your latitude and longitude in the location field.
  4. Install the MQTT client libraries:
    sudo pip install paho-mqtt
    1. Start the code:
      sudo python PoseidonClient.py &
    2. Check the log:
      tail poseidon.log

      You should see the message that is published:
      {'pressure': 1026.64, 'altitude': 167660.83, 'temperature': 21.19, 'moisture': 0, 'timestamp': '2014-12-26T11:59:45.187758'}
      Message 1 published.
  5. Automatically start PoseidonClient when booting up:
    1. Copy the script file to the startup directory:
      sudo cp poseidon.sh /etc/init.d
    2. Change permissions:
      sudo chmod 755 /etc/init.d/poseidon.sh
    3. Test the startup script:
      sudo /etc/init.d/poseidon.sh start

      You should see the "Starting PoseidonClient" message.
    4. Test whether the script is running:
      ps -ef |grep Poseidon

      You should see two entries.
    5. Test whether the script is logging:
      tail /home/pi/PoseidonProject/Tutorial1/PoseidonClient/poseidon.log

      The log contains all data sent and is overwritten every time the client is restarted.
    6. Test stopping the script (which kills all running Python processes):
      sudo /etc/init.d/poseidon.sh stop
    7. Test that script has stopped:
      ps -ef |grep Poseidon
    The entries that you saw in step 6d should be gone now.
  6. If everything has worked so far, activate the script for automatically booting up (ignore the warning about missing LSB tags):
    sudo update-rc.d poseidon.sh defaults
  7. Test whether the script restarts at reboot:
    sudo shutdown -r now

    After boot, check with the commands you ran previously in this section.
  8. Plug your moisture sensor in the soil around your plant to start monitoring.

Your Raspberry Pi is now configured and sending data to a microbroker on the Internet. Every MQTT client that subscribes to the topic as configured in config.py can see the data that you are publishing.

Next, you'll set up a cloud environment based on Bluemix to capture this data, store it in a database, and publish the data to Twitter.

Step 3. Set up the Bluemix environment

Now you'll create an app in Bluemix that uses the Internet of Things Foundation Starter. The IoT starter provides the Node.js runtime and the Node-RED application and creates an instance of the Cloudant NoSQL database.

  1. Log in to Bluemix. In the dashboard, click the CREATE AN APP button. Find and select the Internet of Things Foundation Starter from the catalog's Boilerplates section: Screenshot of the IoT Foundation Starter in the catalog
    Screenshot of the IoT Foundation Starter in the catalog
  2. Enter the name and the hostname of the app. Leave everything else as the default and click CREATE: Screenshot of the UI for creating an IoT Foundation app
    Screenshot of the UI for creating an IoT Foundation app

    The app will start automatically when provisioning is complete.
  3. Click your app's ROUTES path to browse to the app website: Screenshot of the initial Node-RED app UI
    Screenshot of the initial Node-RED app UI
  4. Click Go to your Node-RED flow editor.
  5. In the Node-RED flow editor, you can see a default IoT flow diagram: Screenshot of the Node-RED flow editor
    Screenshot of the Node-RED flow editor

    On the left side is the list of available nodes (input and output "connectors" and functions to perform on input data or to produce an output). On the right side are the properties of the node that's selected in the worksheet.
  6. Select all nodes in the sheet (by holding down the Shift key and selecting each node) and press Delete to remove them from the sheet.

Step 4. Configure Bluemix to store data

In this step, you'll configure your Bluemix environment to receive sensor data and save it to a Cloudant database.

  1. In the Node-RED flow editor, drag the mqtt input node onto the worksheet to create a node that will receive data from the MQTT broker to which the client on the Raspberry Pi is publishing the sensor data.
  2. Double-click the mqtt input node in the sheet and enter the following settings:
    • Server: realtime.ngi.ibm.com (no client ID, and no user ID and password)
    • ClientID: Choose your own unique ID, such as MyPoseidon23.
    • Topic: /org/dutchcourage/poseidon/client/sensor
    • Name: Receive Sensor Data
    Screenshot of the dialog box for editing the mqtt input node
    Screenshot of the dialog box for editing the mqtt input node
  3. Add a debug node to the flow and connect it to the Receive Sensor Data input node. The debug node will display the received messages in the debug tab on the right side of the editor:
  4. Deploy the flow to test it. (In the upper-right side of the editor, click Deploy.)
  5. Watch the debug tab for incoming messages: Screenshot of the debug tab
    Screenshot of the debug tab

    You might want to restart the Poseidon client on your Raspberry Pi to trigger it to send new values. (By default, it is sending messages once an hour.) See Step 2 for details on restarting the client.
  6. After you successfully test that data is coming in, add this data to the Cloudant database by adding the cloudant out node to the flow, and connect it to the Receive Sensor Data node.
  7. Double-click the cloudant out node to edit its parameters:
    • Service: Select your Bluemix Cloudant instance.
    • Database: poseidonsensors
    • Only store msg.payload object?: Select the check box.
    • Name: Save to Cloudant
    Screenshot of the dialog box for editing the cloudant out node
    Screenshot of the dialog box for editing the cloudant out node
  8. Click Deploy in the editor to activate the flow and store data in the Cloudant database.
  9. To view your saved data in Cloudant, browse back to your Bluemix account. In the dashboard you can see your Cloudant instance listed in the Services section: Screenshot of the LAUNCH button
    Screenshot of the LAUNCH button

    Click the Cloudant instance, and then click LAUNCH. Now you're on the Cloudant dashboard, where you can create new databases, create replication tasks, and view and edit database entries.
  10. Click the poseidonsensors database (which was created by the Save to Cloudant node). To view the actual sensor data, move the mouse pointer over the document and click Edit doc on the top-right corner of the document: Screenshot of the poseidonsensors database
    Screenshot of the poseidonsensors database

    Now you can see the JSON data structure and contents of the document: Screenshot of the JSON contents of the poseidonsensors document
    Screenshot of the JSON contents of the poseidonsensors document

Your data is now stored in the database.

Step 5. Configure Bluemix to send tweets

Now that your Node-RED instance is up and running, you can extend the flow to publish the received MQTT messages on Twitter:

  1. Browse to the Node-RED flow editor.
  2. Click the "hamburger" button in the upper-right of the flow editor and select Import > Clipboard: Screenshot of importing data into Node-RED from the clipboard
    Screenshot of importing data into Node-RED from the clipboard
  3. Import the source code of the full Node-RED flow:
    1. In a separate browser window, browse to the PoseidonProject code repository and select Tutorial1/Nodered/NodeRedFlow.txt.
    2. Copy the contents of NodeRedFlow.txt to the clipboard.
    3. Go back to the Node-RED editor. Paste the clipboard contents into the text box and click Ok: Screenshot of the pasted data
      Screenshot of the pasted data
    4. Place the flow somewhere on the worksheet.
  4. Connect the new nodes to your existing Node-RED flow:
    1. Remove from the imported flow the nodes that you already created (Receive Sensor Data, Save to Cloudant, and debug).
    2. Wire the Receive Sensor Data node to the (newly imported) JSON to JSOBJECT node.
    Your Node-RED flow should now look like this: Screenshot of the full Node-RED flow
    Screenshot of the full Node-RED flow
  5. Double-click the Tweet Sensor Values node, and click the pen icon to authenticate your Twitter account with Node-RED: Screenshot of the dialog box for authenticating with Node-RED
    Screenshot of the dialog box for authenticating with Node-RED
  6. Click Deploy to save the changes.
  7. Stop and start the Poseidon client on your Raspberry Pi to initiate the resending of data, and watch your Twitter account for a message that reports the sensor data: Screenshot of sensor-data tweet
    Screenshot of sensor-data tweet

You are now sending your data to Twitter.

Conclusion to Part 1

In this tutorial, you learned how to set up a Raspberry Pi with sensors to monitor soil moisture, barometric pressure, and temperature. You configured the client to send data via MQTT to the IBM IoT cloud broker. With Bluemix, you captured this data, stored it in a Cloudant database, and published the data on Twitter. You learned how to create an end-to-end flow from sensor to sharing data on social media, and you can now start to extend the solution's functionality — for example, with mobile apps and by analyzing the data to forecast when the plant needs water.

Now, proceed to Part 2 to find out how to secure your Node-RED instance and autodeploy changes to the app. And then in Part 3, you'll enhance the solution by building a client app that displays sensor data as a graph.


Downloadable resources


Related topics


Comments

Sign in or register to add and subscribe to comments.

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Cloud computing, Web development, Open source, Internet of Things
ArticleID=992902
ArticleTitle=Conserve water with the Internet of Things, Part 1
publish-date=12162014