Part 4 - Bluemix and the Internet of (50000 small) Things: Completing the Pi software


We now have the parts installed, but need to pull them together, capturing the sensor data. First, to enable the streaming and node-RED services, you should copy the following files from GitHub. If you followed the easy way to install, you can skip this part (or go back and run that!).

sudo cp node-redd /etc/init.d
sudo chmod +x /etc/init.d/node-redd
sudo update-rc.d node-redd defaults
sudo cp mjpg-streamerd /etc/init.d
sudo chmod +x /etc/init.d/mjpg-streamerd
sudo update-rc.d mjpg-streamerd defaults
sudo reboot

After booting, you should be able to see the outputs from both the streamer service and node-RED. For the video stream, visit http://<RaspberryIPAddresss>:10088 - this will display a page like this:





For node-RED, go to  http://<RaspberryIPAddresss>:1880




At this point, you're ready to write your node-RED flow. To speed this up, you can copy the contents of the pi.flow file in GitHub to the clipboard, and selecting import from the node-red menu:



This opens a popup window on the page where you can paste the flow. Drag the flow to where you can see it and click. The flow should now display as:



Let's look at each node in turn. Double-click on the inject node on the left and it displays:


It simply states that every 10 minutes it'll send a blank message into the next node. In other words, it will trigger capture of data from the sensors once every ten minutes.

The DS18B20 node contains:


This has a blank Topic field, which means to this node that it should return values for all sensors of this type.

The DHT22 node contains:

The Topic is set to the ID we want to use for this sensor, and it uses the WiringPi numbering scheme pin 2 to capture data.

The node that does all the work is the function node - this contains JavaScript to generate the JSON that we want to capture:



Rather than explain the entire JavaScript, I'll just point out a couple of parts of the code.
  • In the top of the script, it generates a timestamp to indicate when the data was captured. We could capture the time from a number of different places, but this just seemed like a good idea at the time.
  • The function node generates two outputs, the entry for the temperature and the entry for the humidity. Obviously, this latter is only available from the DHT22, otherwise it's simply set to NULL.
  • The function finally returns  an array of two messages, corresponding to the two outputs.
OK, let's play with our flow. First click Deploy on the top right, to make sure your flow is running. Then click the tab of the inject node on the left, and it will send a message to trigger the reading of the sensors, displaying the output via the Debug node, where you'll see something like:


As you can see, this shows two items for the DHT22 (temperature and humidity) and one for the DS18B20 sensor (temperature only). Both temperature and humidity readings are similar in structure which will make processing them quite straightforward. This is quite interesting, but really there's not much we can do with this. Instead, we're going to send this to the Internet Of Things service on Bluemix.

Connecting to Bluemix

At this point, you have to have a Bluemix account - you can get a trial account. Even if you continue to use Bluemix after the trial period, development experiments use so few resources that your credit card bill is unlikely to give you a shock. Anyway, the first step is to log into Bluemix and create a new application. Click on Catalog on the top of the page, and click on the Node-RED Starter boilerpate:


Provide a name and click Create. Your application is now created and deployed on Bluemix - no, it's not particularly difficult! Once it's created, click on it to expand its contents. Your application contains two services - Cloudant NoSQL DB and Monitoring and Analytics, and we'll add a third - click on Add a Service or API and select Internet of Things (at the time of writing, it's on the bottom of the page). Make sure it's associated with the correct application and then click Create. You'll be asked to restage the application - do so.

Now that the application is running, we need to tell it all about our Raspberrry Pi. Click on the Internet of Things service and it opens the Internet of Things Foundation - click on Launch Dashboard:


Now we need to tell it about our devices - click on Devices and then on Add Device. Specify raspberrypi as the device type and provide your MAC address as mentioned in the last part of this series:


Click on Continue and it will display information for the device: it's very important to copy this to somewhere safe.


On the next page, you'll see your device: it will say that it's unavailable, mainly because Bluemix hasn't received anything from it yet. So our next step is to tell our Pi to send data to Bluemix.

Go back to the Node-RED flow on the Pi, and replace the Debug node at the end of the flow with a MQTT node so that the flow now looks like this:

Double-click on the mqtt node and click the edit button to the right of the Broker field. Then fill in the broker config as:


Set the fields as:
  • Broker: <org>.messaging.internetofthings.ibmcloud.com
  • Client ID: d:<org>:<type>:<id>
  • Username: use-token-auth
  • Password: <auth-token>
All of these are fields from the information displayed for the device that you have saved (see above). Click Update (or Add for the first time) and then complete the MQTT out node:


The Topic should be set to iot-2/evt/status/fmt/json - you can leave the QoS and Retain fields blank if you wish. Click OK and then click on Deploy. After a few seconds, the mqtt node should be tagged as connected:



If you now look at the Internet of Things Foundation page, you'll see that the Pi has started reporting:


Now that we are sending data to Bluemix, and capturing it in our IoT service, the next step is to do something with the information. We'll cover that in the next installation.


Comments