 Okay, let's go ahead and get started and I think that the rest of the folks can trickle in and catch the context as we go. This is the making tweet monkey session, so I think the title is distinct enough. You probably didn't get confused about where you're at. I am Code Foster. I go by Code Foster online. My name is Jeremy Foster. I'm a software engineer at Microsoft and my job is working with the community, with developers in the community. I'm up in the Seattle area. Actually, Portland is one of my jurisdictions, so I'm kind of over Seattle and Portland and Boise and this is a great part of the world to be in. This is kind of a fun place. I really like all three of those communities for their own unique contributions to development. Making tweet monkey as a topic is a bit of a unique one for this conference. I think I actually submitted a bunch of sessions and this is the one that they chose, so I guess we're talking about tweet monkey. It's kind of a fun project. I normally use tweet monkey for younger and beginning crowds, but if you're like me, whenever I'm working in a new space with either a new platform or a new paradigm or whatever, I love the really, really simple projects that help me to just really grasp the concepts. I designed the tweet monkey project so that it was fun, so that I'm going to be able to talk to newbies about it and it's going to kind of pull them in. Easy thing to climb on top of, but it also involves a lot of different concepts in the IoT, in the embedded space essentially, and really invites new and beginner developers into IoT and gets their imagination working. The fact that I'm working with a monkey here is completely incidental. It could be most any device that you find around. So when I talk to these student groups in, like to say, as young as middle school, I ask them what kinds of devices they have in their rooms that take batteries. And if it takes batteries, it's an electronic device and it's something that we could control. So that's fun. The slides that you're going to see today are just basically placeholder slides. Everything's going to be referencing a GitHub repository. And so if you just know where my GitHub repository is, which is easy to remember because it's github.com slash code foster, if you go to slash tweet monkey, that's the root of all of this. And all of my docs are up there, the step by step for the entire project, as well as all the code that you can run on the project, as well as a list of everything that you might want to buy if you wanted to put together a tweet monkey of your own or say you have kids that want to do that. So you don't really need to save the slides. They're just a transition point. Keep us kind of on track. Who am I? Like I said, I'm code foster. You can find me online at codefoster.com and also on Twitter at code foster. I have a presence on github and Upverter and probably all of the other social areas. But as many times as I could, I try to have code foster so that I'm nice and consistent. Now in the interest of open source software and being open, I just wanted to relay that I myself am quite open and you're welcome to just send me a direct email jeremy.foster at microsoft.com. There's my phone number. Feel free to call me. I've never had somebody take me up on that. But I'm actually very open. There's my social security number and here's my private SSH key, so have at it. There you go. Yeah, like I said, you can follow along with this on github.com slash code foster slash tweet monkey and here are the steps that we're going to go through. Step one, you're going to have to buy some stuff. This is an IOT project, a maker project that involves materials. It's inconvenient when I am kind of newer to the space in IOT. I actually have schooling in electronics, but then I've spent most of my career in software. And the interesting thing about software is that you could basically take your laptop anywhere, plug in. As long as you have internet, you can do your whole job, right? Well, enter the IOT hobbyist space, the maker movement, and now all of a sudden we have things again. And now when I go to a community event like a hackathon or something, I've got to bring a toolbox with resistors in it, because there's no getting around that, right? We're in the hardware space. And so when you do a project like this, you're going to need to buy stuff. You're going to need to acquire stuff. It's a pain in the butt. It's also one of the magics of the maker movement in the IOT hobbyist area. When I go into a student group, I can show them whatever I want. I could have a fire-breathing dragon on the screen, and they're not impressed. They just saw one on a TV program last night, okay? But if I have a robot in front of them or a monkey with clanging symbols, they're like, wow, that's amazing. They just absolutely love it. So, yeah, there's stuff attached to projects like this, and that's bad and good. Step two, we're going to have to take this physical thing, this monkey in this case. We're going to have to modify it and somehow. In this case, we've got to find out how it works, intercept a circuit, and facilitate communicating with this monkey by way of a logical electronic circuit. So we'll look at that. Step three, we're going to have to create this circuit, this circuit that is compatible with our existing electronic circuit, with this thing that we're hacking, this hardware hack. So we'll look at a very simple circuit. So you can see already how we're implementing these various concepts. You can get an introduction to hacking some hardware as well as some electronics design, some circuit design, as well as working with IoT devices. So we're going to be working with a Raspberry Pi. I actually had this entire talk done on an Intel Edison as well, and that works extremely well. It's just that it's a higher price point. So whenever somebody wants to get into this, and I have to tell them it's going to cost $130 to buy the whole kit, it's a little bit counterproductive. So the Raspberry Pies are a bit cheaper, and I'm just going to kind of default on that device. So we'll learn how to set that up. We'll also learn how to sign up for Twitter, because this involves a modern API, a modern social API, and namely Twitter. Relatively easy to work with. It's one of those APIs, though, that if you have to talk to it using like a C library, like wiring, or if you have to talk to it from low level, it's a pain in the butt, because you've got all kinds of exchanges of keys and tokens and authentication that has to happen, and nobody wants to do that in C libraries. In fact, if you're working on certain devices, you may not even be capable of it. They may not have the stacks where they're able to communicate with Twitter. But since we're using an embedded Linux system, Raspberry Pi, we have Node.js, and we've reached what I like to call the node threshold of glory. If you have a device that's capable of installing and running Node, which even the Raspberry Pi hasn't had it for ever, it really only entered in somewhere in v0.12 of Node, the ability to run v8 on an ARM processor. But we have it now, and all the later versions of it have it. If you reach the node threshold of glory, then all of a sudden you're opened up to literally hundreds of thousands of packages out there that you can install and run on your device. So, very exciting because somebody's already written your code for you, and we like it when somebody's already written our code. Then we'll write the code. Actually, since you just cloned a repo by going to codefoster slash tweetmonkey on GitHub, you have the code. But you can configure the code and reverse engineer the code, make sure you understand what's going on with it, and then probably modify it so that your solution does something a bit unique. It's not exactly the same as tweetmonkey. And then we've got to deploy that code out to the device and run the program. So we've got to do the ops part of the equation. Now, before I dive into this, any questions that were a small group so you guys can feel free to steer me, and maybe if you ask questions now it'll determine a little bit of the content of the presentation. How much of this do I get the kids to do? I usually engineer a workshop for the audience, and if I know they're going to be very young kids, and I have devices that already have the code on them, if I just plug them in, plug in the tweetmonkey and send a tweet, it's going to work. So I don't have to fret about whether or not they're actually going to get the deploy process right. And I'll still take them through the steps so they feel like they've done the whole thing. It's just that it works a hundred percent of the time. So there are the most fun part of this for the kids, I think, is the modification of the monkey and the completing of the circuit. The hands-on part. They're very tactile. The code is like something we're always trying to get our kids into, you know, because it's a good field, frankly, for them to be involved in. It's a real mind career. Because I don't think it's ideal for this. I mean, of course, being at Microsoft, you might think that I would just use it by default, but maybe that's the way we used to work. But at Microsoft, we're not so concerned necessarily with that. We want you to have the right tool for the job, and in this case, using Raspbian is it's kind of a native operating system on the Raspberry Pi. It's something that we feel like audiences, like the students that I'm talking to, I want them to learn that. I want them to learn that, that version of it. And certainly, you know, I can show them how to use Windows 10 IoT Core to do this as well. IoT Core has some pretty interesting stuff in it. When you work with UWP applications, they're very easy because they're very predictable and homogenous on the device, so they're very easy for the device to manage. You can go to a web interface and say, set this as the default app, and now whenever the thing reboots, it starts that app. Well, that's not a no-brainer in Linux, right? How do you do that? You've got to go create a system deservice, you know, to start up whenever the machine boots up. I think you guys probably know a lot more about that than I do. But it's not the simplest thing in the world. It doesn't always work, and then you've got to find some log file, and then you're buried in log files, and sometimes it's just, you know, bang your head against a wall. And also on Windows 10 IoT Core, a UWP can actually host a Node.js console application. So all of this node stuff that I do at the console, I can do that inside of a Windows 10 UWP app as well. So I really like the way that they've architected it, and I think that the operating system makes tons of sense whenever you are A, trying to create an application that's shared across multiple devices, because that same code base can be used on that Raspberry Pi as well as on a HoloLens and on your website, and on a Windows desktop app. I mean it's really phenomenal that we have so much code sharing. So if you're going to share the code, then that's one really good reason. Another really good reason to do it is if you're creating any sort of a user interface, especially like a media server or something like that, because it's very good at doing UI. You have your choice of a full XAML UI or a full HTML UI, and you've got a lot of really good controls, media playback controls, and things like that. I'm more of a command line geek, and so I do the Linux-y stuff and the node stuff, and I leave it to my colleagues to do more of the .NET stuff. So any other questions? I was going to say too on the kids doing this, the younger folks doing this, the plugging the circuit together is a lot like a puzzle, you know, just figuring, I'll show you, I actually have not just a schematic, which is, you know, great if you're an electronics engineer, but if you're a kid what you really want is just a diagram of the devices being plugged into the breadboard, and trying to translate that and then do it in real life is a really fun practice, reading resistor codes and orienting components and things like that. Okay, step one, buy stuff. So in order to show you this, I'm going to jump you out to the repo. The repo, like I said, is at GitHub, CodeFoster, TweetMonkey, and there's just an introduction here, I mean, I intend to get a image up on this page so that it looks a little bit more welcoming, but if you jump into the docs folder or click here on read the docs, it's going to take you into another readme file that's broken down into these seven steps for putting this together. And my MiFi is apparently a little on the slow side, but I guess we'll live. And so here we are, number one, buy the stuff to modify the monkey and so on. If there are any problems with these docs that you find, I would love either just an issue or a pull request even, that would be wonderful. I kind of want to work on this with others. So here's how you buy the stuff. I put links to Amazon and DigiKey, which I've found are the best suppliers for things, and that's what you need. Now this, as I mentioned here, sums up to $84, which is still a bit of an entry point, but that gives you a number of, quite a bit of redundancy. You know, you get like six mini breadboards and a lot of wire and a whole bunch of jumpers, and so it's giving you more than just one thing, and so certainly if you wanted to create four or five of these with a small group of folks, it's not going to cost 84 times four or five. But this is where you go to buy everything. The monkeys are on here. There's the monkey, and it's a kind of a crapshoot finding the monkeys. Sometimes you'll find one supplier on Amazon that has 40 of them, and sometimes the quantities seem to be low. So I did an event at the Intel IDF conference at the Moscone Center in San Francisco, and we did 60 monkeys, which I don't know if it was one of you I was telling us just before, but 60 of these monkeys is pretty creepy. To be staring back at these 60 plastic faces is just enough to give you the heebie-jeebies. So, but anyway, I actually had to go to like three or four different suppliers in order to find enough monkeys for that one, but you'll find them on there eventually. So that's pretty straightforward, just a matter of clicking order on all of that. I find that if you, there's one purchase that I made that I think is really helpful, especially when you're doing this like at events or something, like if you're going to go to a kid's school or if you're going to do this with others. This is a Leatherman Squirt ES4 pocket knife thingy, multi-tool, and it's actually not just pliers up here. This is wire strippers and then the nose of it does give you just a little bit of grip, a little bit of pliers, and then wire cutters back there too. So really great for working with with wires and wiring and then you kind of have most of the basic tools back here as well, including full-on scissors, which I find very handy as well. So this has been an awesome tool for whatever it is, like $30 or something, and I use that all the time, and TSA has yet to take it away from me, but we'll see about that. I suppose I need to knock on wood. Okay, so modifying the monkey. Oops, I accidentally just hit back and didn't mean to. I'll hit forward. Modifying the monkey is a matter of taking this thing that this monkey, I'll go ahead and take my should have had this out for you already, but this is tweet monkey number two, and the monkey is a, like I said, kind of an off-the-shelf toy, and the first time you get this item that you want to modify using this type of thing, you've got to just check it out and see how it's made. This has a little battery bay at the bottom, and it takes two AA batteries, and they're oriented like they normally are one plus up, one with the minus up, and in case you didn't figure this out 100 years ago, what they're actually doing there is they're putting those two batteries in series, right? The negative of one's touching the positive of the other, and the battery bay door here has contacts for both of those, and then a little switcher is on the door, so we're just making contact there and completing that circuit, and so I know that obviously I can close that gap any means I want, and it's going to complete that circuit. My first iteration of tweet monkey involved drilling through the door here with some fine drill bit, running the wires through that and then soldering them, just a real quick tax solder to these little metal points here. Worked wonderfully, but kids in soldering irons are, you know, they're not necessarily good fellows, so the better way is on the side of the battery bay, it's totally convenient, there's a nice little hole in it, and you can just run the wire in through that hole, run it up and then electrical tape it to the lead of the battery, to the end of the battery, the contact, and then it doesn't necessarily feel like it's going to hold it that well, but by the time you close the door on it and lock it, it holds those contacts on there very well. If you do it this way, if you actually solder it, it's great because you still have use of the switch. If you do it this way, you've you've covered that up, and so now the switch doesn't work anymore, but if you touch these two wires together, that makes the monkey anime, okay? So if I take these two wires, touch them together, I'm completing the circuit, I'm doing the same thing that I was with that switch, so we know now that all I really have to do is somehow logically connect these two wires together, but I have to consider the fact that whenever I do, I'm going to be carrying some current, right? So we have to know what kind of a device this is, well I did a little surgery, I actually took the monkey apart, he basically consists of a DC motor and the two batteries, that's it. The DC motor mechanically drives a bunch of stuff in there, there's a little squeaker that it mechanically squeezes to go ah, ah, ah, ah, and his legs go up and down and his arms go in and out, that's all just mechanics, the only thing in there is a little DC motor, and so when I connect those together it's got to spin that motor. Now DC motors are relatively high power usage, it takes something like 350 milliamps to run that motor, that's relatively high and that's the type of thing you don't want to drive with your IOT board, right? If you want to keep your IOT board that is, it might work for a few milliseconds but that would be about it. So we've got to figure out a circuit and we'll go ahead and move on to the next concept here, we've got to figure out a circuit that's going to allow us to logically connect these together, and I went through a few iterations here, can you guys think of any ways in which we could complete a circuit logically? Transistor, that's right, that was one of my approaches, a relay would do it, yeah. Relay was my first attempt, transistor was my second attempt, my third and final attempt is a MOSFET. So the relay is a no-brainer, it's a very mechanical solution, you give it enough of a signal and it goes, okay cool, I'll put this metal together and that'll complete a circuit, we can use that for all kinds of things. I actually bought a relay and installed it under my stove because I've got this stove that's the old microvolt and all I really needed to do is kind of touch these two wires together so I installed a relay and it just basically touches those two wires together and turns my stove on. So now my modern thermostat works with my old microvolt stove because I've just got a relay under there doing this monkey work for me. So the relay works fine but it ends up being kind of a big and mechanical solution depending on the relay that you buy. There's a bit of a click whenever it engages, click click and it just didn't feel like the best thing. I wanted something that was a little bit more streamlined plus I wanted to learn what was the best circuit for here. So I tried a transistor, well the problem with a transistor is that a transistor is essentially a current driven device. It's a device that when you give it gate current it translates that into a current that that falls across the sink and source. Well that's pretty good except that it still takes a certain amount of current to drive it. It's not a huge amount but it takes a certain amount of current and I'm really just talking about logic here. I'm not talking about like an amplifier where I have to amplify a gate current to a source current. I'm talking about just logically turning on a flow of current and logically turning it off. And one of my colleagues recommended a MOSFET so that it would be perfect and in fact it was. A MOSFET is a lot like a transistor except it actually uses an IC internally, an integrated circuit that has a field effect such that when it gets just enough gate voltage that field internally collapses and allows current to flow from source to drain. And it doesn't take any or hardly any amount of gate current going from the gate to the drain because it's voltage based as long as it gets that 0.7 or whatever it is volts. In this case this is a logic level MOSFET so it's going to take like the 3.3 volts that the Raspberry Pi is going to output on its GPIO pins. But as soon as it gets that voltage it's just going to turn it on and it's going to stay on until I take that off. So MOSFET ended up being a really good solution for this. Now before I show you this diagram I'm going to show you a couple of one other diagram. This is the actual circuit diagram. This is the one I do not show middle schoolers if I'm doing this for them. But I think for many of you this will make sense. How many of you understand electronic circuits? So this makes good sense already. So I modeled the monkey here by basically just putting a motor and then here's one battery and here's the other battery and I've got the negative end of one and the positive end of the other and I'm basically intercepting that area. The existing switch as long as you do the solder thing is still there so you can close it. But our switch is this MOSFET. And this MOSFET has of course it's got a common ground but then it's got a gate resistor here, a current limiting resistor on the gate to make sure that I don't overload this thing. And then do you guys know what this resistor is here for? No. I mean it kind of acts that way a little bit but in a MOSFET circuit this is pretty common. This is a gate drain. This is to make sure that if I take my signal off of here that the any voltage on here goes away. This is a very big resistor 33k. It really doesn't matter what size it is. It just needs to be large enough that it's not going to change appreciably your voltage level at the gate. So we use a very large resistor and this makes sure that as soon as I take my pin low and say monkey turn off all that current goes bye bye down to ground and it actually does turn off. Otherwise this would kind of stick high and be like wait a minute I told this guy to turn off and it would just have some residual voltage there. And then it might just turn off randomly on its own whenever it finally drained through natural means. Okay so that's the basic circuit but then it's nice to look at this diagram. So I jump directly to here for the students. They see the Raspberry Pi sitting in front of them. They see the monkey and they've got two leads coming off of the monkey and then they go oh good this is all I have to do is plug this in just like this. And so we've got the MOSFET going in here we've got three different values of resistors and we've got an LED and that's it for components. So they get to learn a little bit about the components the resistors the LEDs the MOSFETs and then how to plug all these wires in how to interface with the Raspberry Pi pins and so on and so forth. So relatively simple any questions on the circuit? Okay the next step is prepping your Raspberry Pi. Now this step is the one wherein there be dragons because we're talking about a system where it has a lot of complicated software on it. It's got an entire Linux distribution on it. It's got a node stack on it and it's got your code on it. And things can go wrong and oftentimes do. I've run into various situations with where I found out oh man this node module they changed the version and so that had this part broken I needed to do this and this had me up last night until almost midnight and I had to drive here from Seattle this morning. So I got very little sleep because of this step right here. It was actually a stupid problem it doesn't end up being an extremely difficult process but and I imagine that this is going to be the simplest part for folks like yourself that are at conference like this but what we need to do is we need to install an operating system like I said I always recommend Raspbian and I always recommend Raspbian Lite because I don't feel like on an embedded system I really need all that UI that's at least not for this project that's not what we're what we're using and I don't like to have to tell kids that they've got to have a monitor and a keyboard and a mouse and get the whole thing set up and it's an administrative nightmare because we've got to swap the pies out or carry a ton of monitors or whatever and so I've got this really great flow for provisioning these devices very easily without using a monitor keyboard or mouse at all and the way that we do that is like once again I might be just preaching to the choir here but we basically just plug them in over ethernet and the Raspbian Lite does mdns by default and raspberry pies are called raspberry pie by default so as long as you make sure you're on the same network or you can even plug it directly into your pc and I feel like an idiot because I just learned this a few months ago that modern network interface controllers are smart enough to determine that you're in a crossover scenario you don't have to have separate crossover cables I came from the days where you did so I thought I can't plug that directly in it's not a crossover cable but apparently that's gone and you could just use patch cables for everything and I love that so you just plug it into the directly or to the same network and then you can ping raspberry pie dot local and it's going to discover it and it's going to find its IP address for you or you can even ssh directly to raspberry pie dot local perfect from there you can train it on your wi-fi network you can give it a better host name one that makes sense and one that avoids having 13 raspberry pie named devices in the room but those are all the kinds of things that you need to do with the operating system I walk you through the the raspy config and what what are some good things to do set your locale and your keyboard layout make sure you turn on i2c in case you want to use that one of the workshops I do involves using the raspberry pie camera to take a picture and send that to azure cognitive services and it will tell you what's in that picture so that's always really fun because I can point it at a table with a bunch of stuff on it and it'll say I see a red apple and a can of coke and it looks like we're indoors it looks like a hotel it's able to determine a bunch of stuff with some really amazing machine learning models that are built into cognitive services well if you want to do that and you don't want to spend two hours trying to figure out why your camera is not working it's because it's in the raspy config you have to turn on the camera module does it sound like I've run into that problem before I have you go into your wpa supplicant I won't even insult your intelligence by assuming that you don't know how to do this type of thing in Linux but for somebody like me who works at Microsoft it's just remarkable to know how to do that it's not really not so much anymore it is important and this is what ended up keeping me up until midnight last night if you do raspy in light you do not get wiring pie and at least the versions of raspy IO which is the adapter that I'm using in node to talk to the GPIO the at least the version that I'm using here requires the wiring pie package to be installed on the device so the way I finally figured this out was after countless efforts I installed the full version of raspy in and I inspected the packages that were installed and I saw wiring pie in there and I thought I remembered seeing something like that in my error messages and lo and behold after an install of simply wiring pie it worked and oh yeah that's fine okay and then you need to install node JS now the means here of installing node JS is the simplest that I currently recommend which is just doing a W get unstuffing that and putting it in your user local bin and then setting up some soft links to node this has been an absolutely bullet proof means of installing node on a device and it's actually not a bad way to switch versions as well because you can instead of just putting this in user local node you can put that in user local node slash version 7.5.0 or whatever version you're interested in and you can hold multiple versions and then you can switch between the versions by just changing your soft links so that's a pretty good solution there is a cross platform node version selector called nvs that's a really cool package you guys have probably if you've done anything with node you've probably heard of nvm nvm is good but it only works on some operating systems it doesn't work on windows and then there's a version on windows that doesn't work on linux nvs works everywhere and it has a relatively sane syntax whereas some of the other ones the syntax is really weird so I really like nvs I just haven't fully tested it yet so I'm not I'm still recommending this method all right and then you just then you have node installed test it and make sure that node is working and then we look at the next one and I don't have my follow on link created yet but the next one is essentially writing the code now in order to show you the code I'm going to jump over to my actual repository in my IDE and my IDE is visual studio code free cross platform IDE works great for all kinds of code bases it's especially adept at node and that's as you can probably tell my sweet spot it's usually faster than this of course when I'm trying to show off normally it takes less than a second for node to open or for code to open and that took ridiculously long but that's life okay now here is the code base it is this is the whole repo there's the docs and the node modules and the github and all this stuff all the code is under index j s and I want to I want to zoom out here not that you'll be able to read it but just so you can see that this is not a huge amount of code okay now this code is talking to twitter doing a search and looking for a kind of a constant search it's actually letting twitter search for us it's not doing the search ourselves and then whenever there's any kind of whenever there's a tweet that meets certain qualifications it is turning the monkey on and then it's waiting two seconds and turning the monkey off and it's also managing the little led light on there and so it's encouraging to me that that amount of logic that that amount of functionality can happen in this little code and this includes you know a lot of comments and kind of a relatively verbose syntax to make it easy for folks to learn so that's fun so let's look through this code just a little bit now the first thing that we're doing is we are requiring johnny five and raspy i o now I can go down to this board and I'm going to jump into here I'm going to ssh to using the pie user I just left it at the default user cf pie three dot local and I'm going to take you into sys class and then into gpio and has anybody ever worked with the gpio pins on a raspberry pi okay so you can do it directly in here you can do things like you can take pin 16 or like in this case we'll say 24 because that's the one with the light and I can send that out to export and echo thank you okay well why not maybe I don't need to do yeah there you go I didn't need it thank you though okay so now I have this gpio 24 and I can say echo I don't remember whether I need quotes or not out to gpio 24 direction and now I can say echo one to gpio 24 value and my light comes on over here did you guys see the light come on and I'll go ahead and turn that off again there we go so that's the the way that raspian decided to implement this subsystem this microcontroller that's built into their platform but of course that's extended into the various frameworks and one of those is into node j s now I think that what I need to do is unexport hopefully hopefully that worked and it's not going to block me now okay so let's go look at this code so johnny five and raspio work together to give me the ability at a high level to talk to my gpio and raspio is like the connector for this board but johnny five works with lots of different boards so if you want to work with an Edison you would just swap out raspio for the Edison connector Twitter is a node module it's one of the like 400,000 node packages that allows me to talk to the streaming API at Twitter and I'm just recognizing that I'm not I'm a little bit I've got about 13 minutes left so I'm going to speed up just a little bit and it gives me a really really easy way to do something that is it's kind of impressive everything that's happening I basically just go to Twitter and sign up for my API keys I put those here and then I can with a relatively terse syntax I can sign up for tweets but we'll get there in just a second first thing I want to do is I want to define a board and I want that board to be a Raspberry Pi so that's just a little bit of ceremony we have to do when we're using johnny five and now here I'm defining my Twitter keys as some environment variables so you need to make sure that those are on there by the way if you're using visual studio code you've got a really nice facility here you've got a launch file that you don't normally check in to your github repo and this launch file has launch configurations and this configuration here can include a bunch of environment variables and so now if I just hit f5 in visual studio code this is what it's going to do it's going to run the index.js using node and it's going to configure these environment variables for me so for that run it's going to use those as environment variables and I could set up different runs there so that's pretty slick okay so I want whenever the monkey gets a tweet I want him to come on and and I don't want to get too annoyed so I want him to turn back off after two seconds and wait for another one so we'll set 2000 milliseconds for our on duration and in this case we'll set the search term to be tweet monkey now for the duration of this conference it's been open iot so if you guys were in the expo hall and you heard tweet monkey going off it was because somebody was tweeting about open iot which is going to happen and event like this okay and then kind of like in jQuery how you say don't do this until the document is ready here we're going to say don't do all of this until the board is ready you can see that I'm using a nice es6 syntax in my javascript here because this is using the latest version of node so it's very much capable of that and then I like giving nice friendly names to my actual pins here GPIO 23 and 24 you can look at a raspberry pi pin map to see which pins those are but I want 23 to be the pin that goes to my monkey and turns the monkey on and pin 24 to be the one that goes to the light so that later on I can say monkey high or light on that one's not showing but notice that the monkey is a pin and the light is an LED they're really doing the same thing but in Johnny five you get to specify a driver and that determines what functions you call on it so the calls make sense it makes sense to take a digital pin and set it higher low but if you have something that you know is a light then it makes more sense to say something like light on or light off or you also get things like light blink or light stop things like that okay so let's break this down now assume I've got all my keys correct and I'm signed into the Twitter API the board is ready so I define a couple of pins great now I can just say light on or monkey high and I can control those pins but when do I want those to happen well I need to do this call into the Twitter client so client stream I'm interested in filtering the statuses the status is the text of a tweets the 140 characters so I need to filter the statuses this is just the way their API works and then I pass in this search term so this is that hashtag in this case tweet monkey I pass in that search term and I get a stream and with that stream I can wire up some events so the stream on data is what happens whenever a tweet occurs so this is gorgeous because this means that I'm defining a function and I'm kind of sending that function to Twitter and saying would you call this on my machine whenever you get a tweet that matches these criteria now I'm not polling Twitter and searching tweets and parsing them I'm not doing any of that funny business I'm just saying call me when you get one with this in it because Twitter knows how to search Twitter right far better than I do so Twitter searches Twitter I can also handle the error case but whenever I do get data it gives me a tweet and I can say first of all let's console log that a tweet was received from this person and here's what they said but also let's set the monkey high that sets the digital pin 23 to a value of 3.3 volts or a logical one those pins have built in divider built in pull down resistors so they're by default going to be zero volts unless you tell it high and then it's going to be up and by the way the pull down resistor is by default you can change that with software and make it be a default of high and then you have to pull it down programmatically and then I do a set timeout for the on duration so two in two seconds call monkey low you know so in two seconds it's going to turn the monkey back down low if I get any errors just tell me what the error is and then board on warn gets called whenever this process gets terminated or anything goes wrong with it if it's a board closing event then I want to make sure that the monkey's off and the light is off so I don't get stuck in a weird case where the application gets exited but the light's still on or the monkey's still going because it hadn't turned off yet that's really it for the code it's really really simple code the most difficult concepts here are the calling of this client stream and the defining of what happens whenever data lands those might be new concepts for you you may also not have seen even if you've seen some javascript may not have had time to come up to speed on the latest ES6 syntax and seen this fat arrow function syntax but I love it and so I use it and I'm trying to encourage people to get used to it even though I know it's a little bit a little bit of a tax so any questions on the code? all right we've got about six minutes to get this code to the device and actually call it so at a conference like this this is going to be child's play for you guys I basically just go into my tweet monkey and I do either an scp or an rsync I like doing an rsync normally and that does it I'm just going to skip over that so we can see something more interesting because you got it's already out there on the on the pie and you guys have probably seen that at least four times in your life so if I go into tweet monkey and show everything I have a node modules folder but if you just scp that out all you really need is the index.js and the package.json because the index.js is all the code and the package.json is the definition of this node package and which dependencies it has it defines things like I need johnny5 I need raspio and I need twitter and so if you just if you just scp index.js and package.json out there that's a nice fast way to deploy and then from here you need to do an npm install and when you do that you get your node modules all fleshed out everything's there and then you can call it except you can't because raspberry pie demands that you are elevated in order to access the GPIO and so you need to sudo that but if you sudo that then your environment variables are different and so you need to sudo with a big E node this then I'll open my twitter client now you see the light came on that light basically just means yeah we're listening and this this REPL here is a feature of johnny5 you can actually talk directly to the device run commands against the device straight from this REPL so it's kind of nice I saw a nice illustration at a local event where a guy was using johnny5 to control a drone a drone mid-flight and from right here he would say you know left three and thing it was pretty pretty impressive just using that REPL okay so I'm going to let's see what am I going to do I'm going to jump over to my twitter client and you guys can do the same it's looking for tweet monkey so showing off the amazing wonderful brilliant tweet monkey at open iot as we speak it's all that right two seconds and he's done all right and if you get a classroom full of middle schoolers with twitter clients this is just an absolute hoot questions hi frequency yeah I knew you were going to ask that because there's always that one guy so what he's talking about is if you look at my code I'm turning on the monkey here and then I'm two seconds later I'm turning it off but it's very possible to get another high signal before this one's actually been called and so you run into a timing thing you caught me I could I could fix it but then that would make my code more complicated and and this uh this makes more sense for the younger ones it's a feature that's absolutely right it's a feature okay thanks everybody for joining me