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.
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.
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.
First, let's wire up the WeatherBot, as shown in the following diagram:
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:
button
object.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!
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!
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!