Johnny-Five and the wide world of microcontrollers

We've been using Johnny-Five for the majority of this book, but we haven't really touched on one of its best features! While the REPL and the API are definitely strong points, what really stands out about it is its wide array of supported microcontrollers.

To be up to date with what Johnny-Five supports, check out johnny-five.io/platform-support—this page, as we saw in the earlier chapters, contains all the platforms that Johnny-Five supports, and also what types of component they support.

Wrappers, which we'll use in this build, are pieces of code that translate Johnny-Five's Firmata method of communicating to other platforms that don't necessarily use Firmata. In our build, for instance, we'll be using a Particle Photon, which uses a firmware called VoodooSpark. The particle-io wrapper essentially teaches Johnny-Five how to speak VoodooSpark, so we can use the Photon with our existing code.

Let's check out how easy it is to move code by moving our Arduino Uno WeatherBot to the Particle Photon, a Wi-Fi-connected microcontroller that is available at particle.io. There are definitely some differences between our Uno and the Photon: the Photon is a $20, Wi-Fi-connected device, and it comes with free cloud services provided by Particle.

Once you've received your Photon, you have to use particle-cl to create an account and claim your Photon. Do this by plugging your Photon into a USB port, and while the status light flashes blue, install the CLI using npm and run the setup:

npm install -g particle-cli
particle setup

Once you've run the setup, you'll need your access token and device ID. To get the device ID, run the following:

particle list

Then, copy the hex identifier for the Photon you just set up. To get your access token, run the following:

particle token list

Next, copy the hex value for any nonexpired token.

Finally, we need to flash our Photon with VoodooSpark, a firmware that, like Firmata on the Uno, allows our Johnny-Five code to communicate with our Photon. You can go about this in two ways: one is to follow the instructions at https://github.com/voodootikigod/voodoospark, and the other is to use the new command-line tool, voodoospark-installer. To use the new CLI, install it as follows:

npm install -g voodoospark-installer

Then, run the following:

voodoospark

This will ask for your Particle username and password, and then give you a list of Photons to pick from. Select your new Photon, hit Enter, and it will install VoodooSpark on that Photon.

Moving our WeatherBot to the Particle Photon

First, let's look at the hardware setup for this bot. It's very similar to the Arduino Uno build, but the pins are a bit different:

Moving our WeatherBot to the Particle Photon

A WeatherBot Photon schematic

Next, we'll need to enter our project folder and install particle-io. The particle-io module is a Johnny-Five wrapper module—it tells Johnny-Five how to communicate with the Photon, because it is slightly different to communicating with the Arduino.

npm install particle-io

Now, we need to add the wrapper to our code. I suggest that you copy your original code to a new file called photon-weatherbot.js:

var five = require('johnny-five'),
// our particle-io wrapper
var Particle = require('particle-io'),
// 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 Particle({
  deviceId: YOUR_DEVICE_ID,
  token: YOUR_ACCESS_TOKEN
});

board.on('ready', function(){
  var button = new five.Button('D0'),
  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('Sucess!'),
        } 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!
  });
});

Note the changes; we need to require in our wrapper, place it in our Board object constructor, and change the pins for the button and temperature sensor.

That's it! The rest of the code will work the same, and this is true for ANY platform that Johnny-Five supports. This is one of the biggest strengths of Johnny-Five; we have an ubiquitous API to build NodeBots on tons of different platforms with very little code change. Run this, and watch it work!

Now that we've explored how to change platforms, let's look at why changing platforms can be beneficial, depending on the type of project you are working on.

Tethering and Johnny-Five

You may have noticed one limitation of our Arduino setup with Johnny-Five: we have to keep our microcontroller attached to our computer via USB and keep the Node code running on the computer in order to keep Johnny-Five running. Luckily, this isn't the case for all NodeBots on Johnny-Five. For instance, the BeagleBone Black runs Node on-board, and so, using beaglebone-s, you don't need to tether. You just run the Johnny-Five code straight on the device. This is also true for raspi-io for Raspberry Pi and tessel-io for the Tessel 2.

More and more devices will be added to Johnny-Five as time goes on, and tethering limitations will become less and less of an issue accordingly.

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

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