Configure a Raspberry Pi and connect it to the cloud!
This video will work for Raspberry Pi
You just received your 
Raspberry Pi,
and you want to connect it to 
cloud!
In this video, you will use 
windows 7
to install the Raspbian OS 
on an SD Micro card.
You will them configure the 
Raspberry Pi with the 
Raspbian OS
Using Node-red, an open 
source development 
environment,
which is preinstalled on you 
Raspberry Pi
you will create the application
which 
reads the CPU temperature of 
your device,
but more importantly, reports 
this data to the IBM cloud!
You will see how to access the 
Cloud platform
and visualize the data you are 
sending
Let’s get started!
For this task, you need your 
Raspberry Pi,
an SD Micro card with adapter
which will host the OS for the 
Pi,
An Ethernet cable to connect to 
the internet.
You could use a wireless 
connection,
but that requires a little more 
configuration on your Pi.
A monitor and an HDMI cable 
to connected to the Pi
A USB keyboard and mouse
A compute to configure the SD 
card
And power supplies for 
everything
The first step is to install the 
Raspbian OS on an SD Micro 
card.
This work will be done on the 
computer.
To do this, you need to 
download the latest Raspbian
and a tool to help you write the 
image on the SD Micro.
I am using the 
Win32DiskImager.
Un-compress the image 
using… 7zip for example.
And install the 
Win32DiskImager.
OK.. lets get the OS on the SD 
card
Put the SD micro in an SD 
adapter and
plug it in the SD card port on 
your computer;
if you don’t have an SD port 
use a USB adapter.
My SD card is in my D:\ drive.
Start Win32DiskImager
Point it to the Raspbian image 
you extracted,
and tell it which drive hosts the 
SD card.
Remember that this operation 
will overwrite the card,
so make sure you are using the 
correct drive.
Do not click Read, because 
that will take the image form 
your SD card
and overwrite the Raspbian 
image.
Click Write.
This will replace the SD card 
content with the image 
specified,
in this case, the latest 
Raspbian.
This process takes a few 
minutes,
but eventually, if all goes well,
a success message is 
displayed.
This is the file system on the 
SD card
once the install is complete.
Once the SD card is imaged,
You are ready to configure the 
Raspberry Pi.
I connected my Raspberry Pi to 
a screen
using an HDMI cable,
and to the internet using an 
Ethernet cable.
Insert the SD card micro in the 
Raspberry Pi,
and power it up!
The primary desktop is 
displayed when boot is 
complete.
Click on the terminal icon 
to open the terminal window
From the terminal window start 
the Raspberry Pi
Software configuration tool by 
typing sudo raspi-config
and pressing enter
The Raspberry Pi software 
configuration tool
provides a straight-forward way 
of configure your Raspberry Pi.
The Expand filesystem is 
selected.
Press enter to expand the 
Filesystem
If this is successful, you will get 
a confirmation message.
Press enter to continue.
Using the up/down arrow keys,
select the Change User 
Password option and
change your password from 
the default.
Use the Internationalisation 
Options,
to Set up regional language.
Use the space-bar to select 
one or more languages.
Set the default local.
Set the timezone.
From the advanced options, 
Set a host name for your pi
It is good to also Enable I2C, 
which is a multi-device bus
used to connect devices like 
SenseHat to your Pi.
Reboot the device so these 
changes to take effect.
Once the device boots, login 
with your new credentials.
Update the Pi firmware and 
reboot again
Then, use the Update 
command to checks
which updates are available for 
your pi
Use the upgrade command to 
download
and installs updates after the 
update.
And the autoremove command 
to clean up old items no longer 
needed.
You just completed the basic 
configuration…
Lets move on the fun part!
Use the clear command to get 
a clean screen.
Get the IP address of the 
device by using the
hostname –I command.
You will need this when 
working in Nod-red
Send a command to your pi, to 
see its temperature.
[vcgencmd measure_temp]
You will use this command 
again from within node-red.
Notice the output of the 
command.
Start note red.
Soon, the device is connected.
To create the node-red 
program,
you could work directly on your 
pi,
using the browser like 
IceWeasel
Alternatively, you can use a 
webbroser on your desktop
and connect to your device.
In your webbrowser, on your 
computer,
type the :1880.
This is the port that node-red 
listens to.
Node red comes with some 
nodes that
connect to the IoT Platform, 
like Input [Watson IoT]
and output [Watson IoT]
Lets build a flow.
From the advanced section, 
drag the exec node
in the flow.
In the info tab on the right, 
notice the details of this node
Double-click the node to edit it.
In the command section add 
the vcgencmd command
uncheck the msd.payload
add the measure_temp 
parameter
And name the node; example 
getCPUtemp.
This is the name that will 
appear in the flow.
Press OK to close the context 
window.
Add a debug node from the 
output section
to display the results of the 
exec node in the debug 
window.
Add an inject node from the 
input section,
to tell the system when to take 
the temperature.
Connect the flow.
Deploy the runtime
Click on the debug tab on the 
right.
Click on the inject node, and 
notice the message in the 
debug tab.
To get a message on the 
debug winow,
you have to assure that the 
debug node is active.
This is great
Now let’s output this to the 
cloud.
From the output section, drag
the Watson IoT node onto the 
flow.
In the Info tab, node-red 
describes the way
Watson expects the data.
So we have to change the 
string to the format Watson 
wants.
From the function section,
drag the function node onto the 
flow.
Double-click to edit the node
write some java-script
[ msg.payload={'d' : 
{'temp':msg.payload.replace("te
mp=", "").replace("'C\n","")}}
return msg;  ]
to take the message object 
and create a payload which
prunes the incoming payload 
leaving only the number.
Once it reformats the message, 
it turns it,
sending it to the next node.
Let’s connect this function 
node to the debug
to see what it looks like.
I am not limited to one input 
and output from the nodes...
simply connect what you need.
Re-deply for the changes to 
take effect.
Click on the inject node.
Notice the format is what we 
need it to be.
Now connect the function to 
the Watson IoT node,
which will send this output to 
the cloud.
And to make the data more 
dramatic,
lets send this data every three 
seconds.
ReDeploy
Double-click the watson IoT 
node,
and click this button to connect 
it to the cloud.
Congratulations!
You are looking at your devices 
CPU temperature,
reported every three seconds.
You just saw how easy it is to use services provided by the IBM Watson IoT Platform
to connect to the cloud.
Now register for IBM Bluemix, to access the IOT Platform services within Bluemix,
and rapidly compose and extend application
that take advantage of data and analytics
form your connected devices and sensors.
I hope you found this useful.   Have fun exploring!
