Connecting NodeBots to the Web

Bots are really cool on their own—collecting data, outputting that data using colors, text, and even images! We can only do so much when our bots only talk to themselves. However, because of the Node platform that we build our NodeBots on, talking to web services and using Internet data in our projects is really simple. How so? Well, all you have to remember about NodeBots code is the following.

It's just a Node Server!

Implementing data retrieval and third-party APIs in our NodeBots is easy—especially when, thanks to npm, the modules that interface with all our favorite APIs are right at our fingertips.

Anything that you can install on your computer from npm, you can use with your NodeBots. For instance, I have a wearable that pulls colors from tweets—I use Twitter and the color modules from npm to make this happen smoothly!

For our first example, we're going to build a relatively simple bot, hardware-wise. It'll have a button and a temperature sensor. However, we're going to connect this bot to the Internet and have it collect weather data and use Twilio to text us this data.

Using Twilio

In order to get started, you'll have to follow the instructions at http://twilio.github.io/twilio-node/ to get your own account, phone number, and API keys. Once you've done this, you'll need to save the keys and phone number for use in our project code.

Building the WeatherBot

First, let's wire up the WeatherBot, as shown in the following diagram:

Building the WeatherBot

The Arduino WeatherBot schematic

Once you've wired it all together and got your Twilio API keys and phone number, it's time to start coding.

Let's take a look at the steps that we need to take:

  1. We need to establish our weather and Twilio services, create our board, and start the board.
  2. Then, when the board is ready, we need to create a temperature sensor and a button object.
  3. When the temperature sensor updates, we need to update a variable with the current indoor temperature.
  4. When the button is pressed, we need to go get the outside weather data, and combining this with our indoor weather data, we have to send a text message using our new Twilio client.

So we'll need the following in a file called arduino-weatherbot.js:

var five = require('johnny-five'),
// we'll use weather-js for the weather
var weather = require('weather-js'),
// and Twilio so send our text message
var twilio = require('twilio')(YOUR_ACCOUNT_SID, YOUR_AUTH_TOKEN)

var board = new five.Board();

board.on('ready', function(){
  var button = new five.Button(2);
  var temp = new five.Temperature({
    pin: 'A0',
    controller: 'TMP36' // Make sure you use the controller proper for your sensor!
  });

  var currentTemp = undefined; // we'll stash the temp sensor data here

  button.on('press', function(){
    console.log('Inside: ' + currentTemp + ' degrees F'),
    weather.find({ search: 'Austin, TX', degreeType: 'F' }, function(err, data){
      console.log('Outside: ' + data[0].current.temperature + ' degrees F'),
      twilio.sendMessage({
          to: YOUR_PHONE_NUMBER,
          from: YOUR_TWILIO_NUMBER,
          body: 'Inside: ' + currentTemp + ' degrees F 
 Outside: ' + data[0].current.temperature + ' degrees F'
      }, function(err, responseData) { 
        if(!err){ 
          console.log('Success!'),
        } else {
          cosole.log(err); // we need to catch any Twilio errors
        }
      });
    })
  })

  temp.on('change', function(err, data){
    currentTemp = data.F; //stash the temp data when it changes!
  });
});

Once you've got all this, keep in mind what you need to change: YOUR_ACCOUNT_SID and YOUR_AUTH_TOKEN need to be replaced with the keys you received from the Twilio API, YOUR_PHONE_NUMBER needs to be replaced with a number you can text, and YOUR_TWILIO_NUMBER needs to be replaced with the number you received from Twilio. Finally, you should change Austin, TX to your location for more accurate data!

Using the TextBot

Now, grab your cell phone and run the code:

node arduino-weatherbot.js

Once you've pressed the button, wait a bit, and you should see a text message in your phone's inbox!

Using the TextBot

The text message from my WeatherBot!

You've now connected your NodeBots to two online services: weather-js uses Yahoo weather and Twilio!

We've done a lot with our Arduino Uno, and it's been great, but let's talk about moving on to other microcontrollers and how easy this can be with Johnny-Five!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset