[MUSIC]
Hi everyone, my name is Patrick, and I'm a web developer from Sydney.
I've recently been looking into using JavaScript to control devices such as
Arduinos, the Elite Emotion, Ninja Blocks
and hopefully very soon this, Pebble watch.
Today I'm gonna be going over the
absolute basics of controlling an Arduino using Node.
We're gonna cover connecting up to a
computer, getting Node to communicate with the
Arduino, and hopefully, by the very end, blinking this LED light on and off.
I'll be sticking to absolute basics here, so we're
not going to be doing anything too advanced, hopefully.
In future tutorials, I'll go over this and it gets a little bit more complicated.
But, for this one, we're gonna show you how to get started.
So, let's do that and get started.
What is an Arduino?
A lot of people have read the name online, or they've heard it
mentioned, but they've never actually seen one in action, so this is it.
It's a single board micro-controller that's
open source which basically means that you
can control electronics using a platform that anyone can build and tinker with.
The people at Arduino are lovely enough to have open sourced it, which
means that anybody can make their own Arduino boards that really want to.
This one here, in particular, is the Arduino Uno.
It's one of a variety, of different models
that the lovely people at Arduino have released.
You can have a whole range of components
connected to it, too, like LED light bulbs, sensors,
and even shields, which are things you kind of
stack on top of Arduinos to add more functionality.
This one in particular is a MIDI, shield which has
the ability for, to play sounds, like a MIDI Keyboard does.
Node.js is a platform that takes
JavaScripts, a scripting language most commonly used
for web pages, unless you're writing up network applications such as web servers.
We'll be using it today, to run a web server that'll talk to our Aduino.
If you haven't used Node.js before, get started by
heading over to Node.js.org, which I'm showing on screen here.
Click that Install button and get it installed
and running on your computer, so that you'll
be able to continue and follow along with the example code that I'll be putting up.
[BLANK_AUDIO]
So why Node.js and Arduinos?
Why link these two technologies together?
Well, one of the strengths of JavaScript is that there are quite a few APIs
out there, which you can join in different ways to make ideas come to life.
Want to develop a robot that takes
instructions from your Twitter followers, or a
coffee machine that makes you a different
style of coffee, depending on the weather outside?
If you want to add a Leap Motion as an input
device, or feed in data from your Jawbone or FitBit fitness products.
The possibilities are going all the time.
And JavaScript is becoming an easy to use bridge between technologies,
because we've got a whole bunch of APIs we've got access to.
And what is Johnny-Five?
Johnny-Five, is one of those open APIs that makes JavaScript such
a valuable language to be developing this sort of thing in.
It allows you to control your Arduino using
very similar functions to those that you'd use
if you were programming it just using the
Arduino platform that comes with the Arduino itself.
We'll be doing a lot of this stuff in the
tutorial that it's explaining here, so don't worry too much.
But you can see, there's a lot of cool
things that people have started to make with it.
Wireless Nodebot Kinnect controlled robot arm, and LCD Running Man.
So, you know, the possibilities get pretty exciting.
Okay, to get started we'll need to connect our, our Arduino to our computer.
So, we'll do this by connecting the USB port to our Arduino.
It serves as both the power to the Arduino,
and the way to upload software to our Arduino.
As you can see, once you plug it in, you'll
have the on lights flicker on, which is a good sign.
In this demo today, I'll be doing a really
basic example of turning this LED light on and off.
I've already got the jumper leads in where they should be.
So if you need to pause this to be able to follow along, feel free to
pause it now, and put the jumper leads in, in the way that I've got them.
Connecting up the Arduino is pretty simple.
Connect up the green one here, to pin 13 on your Arduino.
Then connect up the red one to the 5 volt pin.
And finally we connect up the black one, to the ground pin.
Doesn't really matter about colors, you can
use whatever color jumper wire you'd like.
Now that that's all done, we're ready to go into the software part
of the tutorial with what code we've gotta run, and where to talk to our Arduino.
Node application is extremely simple.
We've only got two files, an index JavaScript file and a package Jason file.
If you are new to Node the package Jason file is what we use to provide any details
about the application, and what other Node modules are required for that application.
So, to start with, I'll go over this packaged
up Jason file, and show you what's going on.
So, here we've got what the name of
our application is in this case, Node in Johnny-Five.
Can name this whatever you'd like, totally up to you, but keep it all to one word.
It doesn't seem to like spaces.
It'll start complaining at you, so definitely stick to one word.
We've got a version number.
Once again, you can pretty much use any version
number you'd like, really, for simple basic test applications.
I've called it 0.0.1, because really this application is very,
very early stages and isn't going to be doing too much.
So, I'm keeping it at a very early version.
Then the very important thing, which will definitely prevent
our application working with Arduinos is our dependency section here.
This is where we say what other modules we need,
running and in our Node application for this to work.
So, we only need Johnny-Five, which is the module I was showing earlier.
And this here is the version.
So, in this case, we want the latest Johnny-Five version of that module.
Basically, we're doing such simple stuff, just basic turning
an LED light on and off, not likely to change.
So, we're gonna assume that, in every future version of
Johnny-Five, it'll be compatible with what we're gonna be doing here.
So, there's no need to really be, specific on which version we're wanting.
Now we'll go on to the index JavaScript file.
Our index JavaScript file contains all
of the functionality, but the node application.
So, this pretty much is our note application
in 19 lines, two of which are Console logs.
So, very, very small application here.
I'll go through it step by step and
explain what everything is and why it's there.
Firstly, we've got our five variable.
Our five variable just requires the Johnny-Five module.
Once again, the reason that we've got it here as well as in the package Jason file,
is that we're assigning all of the different objects and functionality.
That we gain from the Johnny-Five module into this five
variable so we can access it throughout the node application.
An example of that is our next line here, which is board.
Our board variable, is creating a new board
object which is part of the Johnny-Five module.
So we're looking into the Johnny-Five object here, that
we've required here, and we're grabbing the board object.
Which is all the part of the Jonny-Five module.
We don't have to worry about any of this.
We don't have to really create any of this.
It's all been there.
Thanks to the geniuses who have come before us.
Next up, we are declaring a few more variables that we use later on,
so we are declaring the LED variable, and we are declaring toggle state variable.
I'll explain these a bit later on.
[BLANK_AUDIO]
To begin, look at this here actually.
This is an important one because this is what
will come up first, when we run our application.
It's just a console log saying waiting for device to connect.
What that means is that, that will come up first, as soon as the known application
is starts up just to let us know that the new application is at least running.
But we don't know yet whether our Arduino has
connected up, and whether we can talk to it.
What then happens, is, using this baud variable here that we created.
Which is a Johnny-Five board object.
We say, on ready, which means, when we've got
the event of ready fired from the board, which means.
The board's pretty faster, sends any sort of commands and requests to it.
We run this function right here, and this function
starts off with a console log saying board ready.
This will just let us know, if we're watching the console while
our node application is running, that the board is ready to receive.
Our request.
It also is a good way to devote whether, or not the board is connected properly.
You are doing a board.
Next up, we use this LED variable that we declared earlier, and
we create a new LED, once again part of the Johnny-Five module.
We're basically just letting it know that we've got an LED.
At pin 13 which is what we plugged into earlier.
Then we get to the JavaScript bit which is, we're setting an interval using the
JavaScript function which many of you might
be used to if you are JavaScript gurus.
Basically set interval means that we're gonna use this function here, toggle LED.
Every 200 milliseconds, and you can play around with this
number if you'd like later on, just to experiment, and change
how often the LED light gonna toggle on and off,
but basically we're gonna say, run this function every 200 milliseconds.
What isn't that function well, the function here has.
Three lines and one life line which is just there for user reading.
Basically we use this variable here, toggle state,
which we set up earlier to be false.
And each time toggle LED runs, will change the value of
toggle state from true to false or from false to true.
So, it basically just changes it the opposite of what it was.
Then, if total state is true, we turn our LED light on, otherwise turn it off.
So, basically, we keep changing toggle state on and off, on and off,
in turn, turning our LED light on and off and on and off.
[BLANK_AUDIO]
You've.
You're following this now and kind of,
picturing the function running in your head.
If you'll notice there is nothing that turns off
this LED, so it will theoretically go on forever continually
turning on and off, on and off until we either
disconnect our Arduino or we turn off this node application.
And I'll be showing you how to do that to.
Theoretically, you could also include some other functionality, in the
error to turn off the, Intel by clearing Intel if you'd
like to but in this tutorial to keep things simple,
we're just gonna give it to the basics I'm showing you.
This is how you can control the Arduino from Node, and
then you can add as much functionality as you'd like to.
On top of this and have a bit of fun.
Now that we have those two files up there and ready to run our Node application, we
do have to make sure those dependencies we mentioned, are there and available to run.
So, what I've done is I've opened up a tunnel, and I've gone to
the same folder that we put those two files in to prove that to you.
I'll just show you there.
So, you've got the index JavaScript file, and our package JSON file.
So.
How you would usually run a node application is just Node index.js.
And we're gonna attempt to run it.
And what we see here is that is says, cannot find Module Johnny-Five.
Which is fair enough.
We've told it in the Index JavaScript file.
That there's gonna be a node module called Johnny-Five and it's gone and looked for
it and can't find and it is now very upset at us for lying to it.
So, we need to get it installed.
To install it you can use an incredible package manager called MPM.
MPM just runs through your package.jason file,
and installs any dependencies that you've got listed.
If you don't have MPM on your machine just do a
quick Google search for MPM, and install it on your computer.
It's quite a simple process, so I won't cover it here.
So basically, once you've got MPM installed, you type in MPM space install.
And, it will go through looking through our package Jason file.
It also does a nice bunch of warning us, saying we
don't have a few of those that we could have had.
Description I think and a few other ones.
So, basically, it's gone through and installed
our modules here so we've got all the
Johnny-Five Modules, and all this other stuff
that it deems necessary to run our application.
So now, I'm going
to clear this, but before we run our actual.
Application we're gonna need to set up our Arduino, so
that it's able to receive communication from our Arduino application.
To get this communication working between our Arduino and the computer,
we just need to upload a bit of code to our Arduino.
Arduino calls these bits of code sketches.
Which you upload and then install onto the Arduino and then the Arduino knows
what it needs to do, In our case we don't need the Arduino to do too much.
Most of our functionality is in Node, all we need to do is install
a bit of code to ensure it understands how to communicate with the computer.
First off, before we install it, if you haven't used the Arduino software
before, double check that your serial port is pointing to the correct USB port.
You might need to do a bit of trial, and error with
this one if you're not sure which USB port is the correct one.
But in my case, it was this TTY USB modem.
Do a bit of trial and error to check which will work
if you are having any issues with uploading your code to your Arduino.
Definitely check that setting right there.
I've also got the board set up to
be Arduino set up to whichever board [INAUDIBLE].
Then top load the sketch.
We're going to go to file, examples.
And then you'll have this on yours too
Fermata, we want the standard Fermata, sketch here, and
this as it says here is a generic protocol
for communicating with micro controls, from software on computer.
That is convenient, because that's exactly what we want to do.
We want to communicate from our notes software
to our micro control, which is our Arduino.
So, to upload this to our Arduino, make sure you've
got it plugged into your computer still, and click upload.
It'll compile the sketch.
It'll upload it onto the Arduino.
And then in a few seconds it should be done.
There we go.
So, it's done uploading, which mean's you've now got
the standard Fermata software or a standard Fermata sketch.
On your Arduino, which means we're ready now, to move on to the
next very exciting bit of running our Node code and communicating with that Arduino.
We've got our two files set up.
Our index JavaScript which has all the code for our Node application.
We've installed all our dependencies, using MPM.
We've also got our Arduino connected and
running the standard Fermata, sketch code; which means
it's now ready to accept all commands that will come to it from our computer.
So, all that's left is to be in our terminal
and type in Node index.js, and when we run this,
we've got our, waiting for device to connect, message along
with a few messages that come from the Johnny-Five Module.
It's found our serial port, which is the
USB port that we've got the Arduino connected to.
Your one might be very different to this.
Doesn't really matter.
The main important thing, is that our console log
that we set up, which said, board ready, has appeared.
And if you turn to face your LED light, you'll see that it's now flashing.
Which is lovely.
It means that our code is working.
We're sending our commands to the 13th pin, telling
it to turn an LED light on and off.
And so it's turning on and off, and on and off, on and off.
Just as we requested it to.
The only important question is how do you get it to turn off?
How do you stop it from constantly running?
This note application.
If we go back to the terminal here, what you've gotta do is just press Ctrl C and
then Ctrl C again and it'll close the board and it'll stop that light blinking.
If you timed it correctly, the light'll be off.
If you time it slightly differently, the light will stay on.
And that's okay, really.
It's not a big deal.
If you want to completely turn it off, you can just unplug the
USB from the computer, and it will turn off all power to your Arduino.
It won't harm it in any way.
It's just a quick way of getting it to.
Stop doing what it was doing that you really didn't want it to do.
And that, my friends, is how you turn a LED light on and off, and on and off
in an infinite loop using node Johnny-Five and Arduino.
If you followed along, you should now have an
LED light, bending to your node application as well.
Good work.
This is the basics.
There's a lot more to do from here.
If you've got any questions, or if there's anything that
I've missed, feel free to get in touch with me.
My name once again is Patrick Catanzariti.
I am on Twitter at thatpatrickguy or I've got a website Patcat.me.
So, feel free to get in touch, if you make anything really cool with Arduinos
following up from this tutorial, definitely get
in touch as well, we'd love to see.
Thanks for taking the time, to watch, and I hope you learned something new.
See you.