On Github ajfisher / jsiot-workshop-slides
DDD, August 8, 2015
Andrew Fisher @ajfisher
Press 'S' for full notes
Hi, my name is Andrew Fisher and I'm an interaction developer and I also build hardware using JavaScript. Our workshop today is going to go for the two sessions, so just under two hours and the aim is that by the end of it all of you will have gone away having built something that interacts with the physical world using JS.(CC) Flickr ⣫⣤⣇⣤
Now, for those that might know a bit about nodebots or have been to a nodebots session before, whilst we're using the nodebots stack (and I'll get into that in a moment), we won't be building full scale robots today due to not much time. As such I felt that given there's some other topics at DDD around things like APIs, Microservices and IoT that this would be a great opportunity to show you that not only can you build robots with JS, but you can build IoT devices too and build them quickly and easily. So if you were here for the robotics and you're a bit disappointed we're not making battle bots this morning you've still got some time to go check out Troy's security workshop or learn about APIs.(CC) ajfisher
Okay so let's get cracking. We'll start with some basic electronics. Enough to get you by. Who here as done any electronics recently? A handful okay, so we'll quickly touch on the simple bits you need to know for today and where to find out more.(C) Tinkernow
The easiest way to think about electronics is like water and pipes. The wires are pipes, the electrons or electricity is like water going through the pipes. Voltage then is like the pressure. So if you imagine I have a big tank or I turn on a mains tap there's a lot of pressure. So that's like having a powerful battery or plugging in the powerpoint. This is measured in volts. All the stuff we'll use today is 5V which you can run from USB on your computer so won't kill you or really probably blow anything up. Current is the rate of flow of the electricity through the circuit. This is measured in Amps. If you have a big motor attached to the circuit you can imagine it would need more energy to turn it than a little LED. As such we say it draws a lot of current - thus makes a lot of flow of electricity. Finally we have resistance. Think of this like the width of the pipe. If it's narrow maybe you can't draw much electricity through it but if it's fat then you can draw a lot. Some components may draw too much current so you use a resistor to restrict the flow.(CC) Phil Farugia
Okay - that's great, that's how electronics works in a circuit how do we affect it? So we typically use a microcontroller. A microcontroller is like a really old computer which you program in C and you can interact very directly with the circuit you wire up to it. Today, the micro we'll use is called an Arduino which is open source hardware and very inexpensive. You can program it in C but you can interact with it in JS and we'll come to that in a moment.(CC) ajfisher
So when you are prototyping it's a real drag to solder all the time. As such we use one of these, it's called a breadboard and it allows you to plug wires into it and make connections. It's not as secure as soldering but good enough and allows you to make mistakes.(CC) ajfisher
Now a microcontroller is just a tiny little computer, you need to plug things into it to do meaningful stuff. So the things we'll be playing around with today are things like this. LEDs emit light. They come in different colours and one of them is even an RGB one which we'll use in the example. Note that LEDs are polar so the long leg is the anode or the positive side.(CC) ajfisher
Next up we have motors, and specifically servos which are used to move things normally thorugh 180° using a digital signal.(CC) ajfisher
We'll be using a temperature sensor like this to record the temperature here in the space and use it. These are very useful as they are inexpensive and remarkably accurate.(CC) ajfisher
And finally we have a light sensor which we'll use to measure light intensity.(CC) Flickr hiperbolica
Okay so we're all software peeps so that's enough hardware to get you going. So how do we get JS working with this microcontroller that I said a moment ago can only use C.(C) Joanne Daudier
Johnny Five was started by this guy - Rick Waldron, and there are now about 30 core project members, nearly 100 contributors and over 2000 commits to the codebase in the last couple of years. It's a very active and expanding project and we're always looking for more contributors to help out. Johnny Five is a hardware abstraction framework so instead of writing code that is specific to a chip you can talk about components that behave in different ways and leave the implementation details up to the people who write the board level interfaces whereupon you can then use it.Glasses (C) Andy Gelme | Image (CC) Matthew Bergman
We're going to build some things in a second but here's a couple of things that people have been making which I thought I'd show you in order to hopefully inspire some further thinking on this front.Skirt (C) Kassandra Perch | Image (CC) Matthew Bergman
This skirt, made by Kassandra Perch is fully contained running javascript on a little board embedded into it. It has an accelerometer and as you move around the LEDs inside it light up different colour. So you can even use JavaScript in your clothing!!(C) Adrian Catalan
Here is an example of using nodeJS to make a physical game. This is made by Adrian Catalan and uses nodebots and node-pixel to make a tetris game on LED panels.github.com/ajfisher/dddworkshop
If you haven't already, please make sure you have downloaded or cloned this repo as we're about to get cracking in it for the rest of the session. Each of the 3 exercises we'll do are in folders 1, 2 and 3 respectively in this repo. There's a readme in every folder, please follow along with that if you don't keep up for whatever reason.(CC) Flickr Daniel Novta
Okay so enough theory, it's time to get stuck in. The first exercise we are going to do is to just make sure you understand the stack and have it all working. Hardware often doesn't have a screen so hello world is blinking an LED.var five = require("johnny-five"); var board = new five.Board(); board.on("ready", function() { var led = five.Led({pin:13}); led.blink(1000); });Now we have that, we need to talk to it from Johnny Five. You can see the code in the repo. Explain code here.
node 1_hello_world/led.jsNow run node and you should get a nice blinking LED.
var five = require("johnny-five"); var Twitter = require("twitter"); var twitter_creds = require ("./access.js"); var board = new five.Board(); var led; var keyword = "robot"; if (process.argv[2] == undefined) { console.log("Using keyword 'robot', pass a keyword next time"); } else { keyword = process.argv[2]; console.log("Tracking keyword: %s", keyword); } board.on("ready", function() { led = new five.Led({pin: 9}); }); var client = new Twitter({ consumer_key: twitter_creds.TWITTER_CONSUMER_KEY, consumer_secret: twitter_creds.TWITTER_CONSUMER_SECRET, access_token_key: twitter_creds.TWITTER_ACCESS_TOKEN_KEY, access_token_secret: twitter_creds.TWITTER_ACCESS_TOKEN_SECRET, }); client.stream('statuses/filter', {track: keyword }, function(stream) { stream.on("data", function(tweet) { if (board.isReady) { led.fade({ easing: "linear", duration: 1000, cuePoints: [0, 0.7, 1], keyFrames: [0, 255, 0], }); console.log("---"); console.log(tweet.text); } }); stream.on("error", function(error) { console.log(error); }); });This is the twitter example as it's pretty tiny. As you can see it creates a board does the twitter authentication and then basically tracks a keyword and every time the data event fires, it then does a fade animation which lasts a second and fades the LED in from off to on and off again. So we'll spend the next 30 minutes playing with these so choose one you want to do. Twitter and Gmail are good if you use those services as you'll need to auth them. Weather is good if you don't. Build the circuits then play with them a bit and see what you can do.
// examples
Okay, now we're going to flip the focus around and look at how we connect a thing and push data to the internet. Obviously a huge part of IoT is getting data out of the environment.DDD, August 8, 2015
Andrew Fisher @ajfisher