Bluemix Mobile and Internet of Things

Hello and Welcome.
In this Lab we’re going to use Bluemix to create an application that tracks movement in a Internet of Things sensor – in our case a smartphone using a cunning little Web app – and then raises alarms whenever the phone is moved.

We can then decide to do whatever we like with the motion alerts. Send SMS messages, Mobile Push notifications, emails to warn someone – just like we did in this earlier lab – but the difference it we don’t need any special sensors and devices – just a regular smartphone.

In this lab – for the sake for variety – we’ll alert people by generating a tweet.

Let’s get started.

  • Log into Bluemix.
  • Click on the Catalog link at the top.

1

  • Find the Node-Red Starter service in the Boilerplates section near the top and click on it.

2

The Node-Red Starter will give us a really cool visual programming tool, that’s great for rapid prototyping of apps. The tool will generate Javascript apps that run on Node.js.

Bluemix’s Node-Red Starter service will deploy a Node.js runtime, a Cloudant NoSQL database and a monitoring service all in one click.

  • Give your Node-Red service a name and hit create.

 4

Bluemix will now create and deploy your Node.js runtime, Cloudant NoSQL database in the cloud. That was easy!

  • Now click on the big red button to open up the Node-Red editor.

5

  • Now add an IBM Internet of Things node to the canvas.

1

We’ll configure this node a little later.

  • Meanwhile add a Debug node and wire this to the Internet of Things node.

2

  • Now go to your smartphone. Make sure you have a good Internet connection – ideally Wi-Fi.
  • Open a Web browser on your phone.
  • Navigate to phonesensor.mybluemix.net

IMPORTANT – On Android, you’ll need to use Chrome. The built-in browser won’t work.

You should see this screen and the Web app should automatically connect.

phone

Now go back to your Bluemix Node-Red screen.

Double click on the IBM Internet of Things node to configure it.

  • Set the Authentication to Quickstart
  • Carefully enter the Device ID number displayed on your phone into the Device ID box.

3

Now Deploy the app and switch to the Debug console.

14

You should see a steady flow of debug messages.

4

The phonesensor.mybluemix.net Web app is capturing data from your smartphone’s internal sensors and using the Internet of Things protocol MQTT to transmit this information in JSON format to Bluemix.

The accelerometer in your smartphone is capturing the orientation of the phone and how much motion.

Try rotating and shaking your phone to see the values change.

Now let’s write some Javascript to decide what to do when the phone moves significantly.

  • Add a Function node to the canvas.
  • Copy and paste the following Javascript into your Function node

if (parseFloat(msg.payload.d.accelY) > 2) {

randomstring = Math.random().toString().substring(0,5);

msg.payload = "YOU CAN'T TOUCH THIS!! @BenM4nn @Bluemix @Node-Red @IBMIoT http://wp.me/pER69-7Y " + randomstring;
} else return;
return msg;

This code will detect any motion where the accelerometer moves more than +2 in the Y-axis. This setting should detect someone picking up the phone, but it not too sensitive that it’ll trigger a false alarm when the phone is sat on the table.

It also puts a random number at the end of the status message. This helps bypass the duplication checks on Twitter.

13

  • Wire the IBM Internet of Things node into the Function node.
  • And wire the Function node into the Debug node.

7

Now Deploy the app and switch to the Debug console.

14

Rather than the steady flow of sensor events coming through, we’ll now only see debug messages if the phone is moved.

Test it by grabbing your phone. You should see debug messages appear telling you to put the phone back down!

8

Cool. So we can generate motion events from our phone and process these in Bluemix with Node-Red.

Finally let’s send a tweet whenever our phone is touched!

First though a warning!

Warning – Make sure you don’t forget to wire your output through a Delay node! Otherwise you’ll post loads of tweets each time your phone is touched! The sensor generates loads of messages per second!

tshirt

Ok – Let’s add that Delay node so we don’t forget later!

  • Find the Delay node and add it to the Node-Red canvas.
  • Wire the Delay node so that the Function node output also feeds into it. We can leave the Debug node wired too.

9

  • Double click on the Delay node to configure it.
  • Change the Action to “Limit rate to”…

10

  • Change the limit rate to 1 message per Minute
  • Check the box to drop (or discard) intermediate messages as we won’t need these.

11

Add a Twitter Output node (the grey terminal is on the right) to the canvas and wire the Delay node into it.

12

Now double click on the Twitter Output node. If you’ve already configured Twitter in Node-Red then you’re all set.

Otherwise follow these steps.

  • Double click on the Twitter input node you’ve created so we can configure it.

14

  • Click on the pen next to the “Log in as” box.

15

  • Click on the button to authenticate with Twitter. This will allow your Node-Red app to access Twitter using your Twitter ID.

9

  • Authorise Node-Red.
  • Now go back to the browser tab where Node-Red is running. You should see that your Twitter handle now appears in the dialog box.
  • Click Add

10

All set!

  •  Deploy your flow.

14

  • Shake your phone and you should see Debug messages coming through.

17

  • Remember to put your phone down to stop sending the messages!

Check your Twitter page, and you should see a Tweet from Bluemix!

tweets

Awesome!

So now you’ve created an Twitter-enabled Motion sensor alarm.

Remember!

Don’t forget to disable this – by deleting the connections to your Twitter node and re-deploying. Otherwise you’re going to flood your Twitter account with the same tweet. (Twitter may eventually intervene since the tweet text is the same).

Cool! Now experiment with other settings.

For example, try sticking your phone onto an object – like a chair – and see if you can configure an alert when the chair swivels or leans over.

Try different forms of alerts such as emails with the Email Output node or SMS with the Twilio Output node.

Most of all – Have fun!

Here are some instructions to help you set up your SMS Text messaging node that uses Twilio.

First, we’ll set up the SMS messaging node and check that it’s working.

Then we’ll wire the SMS messaging node into the flow we created.

Ok let’s go.

  • First find the Twilio node and drag it on the canvas.

25

  • Now double click on the Twilio node to configure it
  • Under Service choose External Service

26

  • Next to Twilio click on the pen icon.

27

Leave this screen open ready for us to set up a Twilio developer account and then we’ll paste the credentials into Node-Red here.

28

To activate your account, you’ll also need to supply your mobile number complete with the international dial code e.g. +254.

Twilio will send you an SMS with a code which you enter on the screen to finish activating your account.

Finally Twilio will assign you with a new virtual Twilio number.

 29

Important – For this demo to work in Africa, make sure you are assigned a United States virtual number i.e. one that starts with +1 as the international dialling code.

If you are assigned a number elsewhere, follow these steps to select a US number otherwise the SMS service won’t work. 

30

  • Click on the Release Number button

31

  • Choose Get It Now to get another number
  • Twilio may now generate a US number that starts in +1
  • However if it does not click on Choose your own number

32

  • Use the drop down to select United States. You may need to choose another country first to get it to select US numbers.

33

  • Choose a US number starting with +1 and click Get Started.

 

Now follow these steps to make sure you allow Twilio to send texts to your country.

 

Important – Check your text message permissions

Another common issue, is that the Twilio service doesn’t active SMS messaging to all countries by default.

Check that you can send Texts to your country as follows:

b1

  • Click on the GEOGRAPHIC PERMISSIONS tab.

b2

  • Click on the Messaging tab

b3

  • Scroll down to your country or click on the top check box to select all countries.

b4

  • Make sure your country is checked.

b5

 

34

  • Here you’ll find your AccountSID and AuthToken (click on the key icon to display this)
  • Copy and paste these two values carefully into the corresponding spaces in the Twilio node in Node-Red.
  • Copy and paste your Twilio (virtual) number into the From This is the number that texts will be sent from within Node-Red.
  • Click OK.

27

  • Now enter your own Mobile number in the SMS to This means that Twilio will send a text to your number.

36

Now test that your SMS node is working…

Wire an inject node into the SMS node and set the inject message to a string. Configure the inject node to SMS whatever string you like.

Deploy the node and press the inject node. You should shortly receive a text.

Now you can add an SMS alert – either to the phone being picked up to tell the person to put it back down, or to another phone.

Have fun!

Advertisements

One thought on “Bluemix Mobile and Internet of Things

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s