!= Bringing Javascript to the Realm of IoT with Node-Red - Sessions by Pusher

Bringing Javascript to the Realm of IoT with Node-Red

Dalton Scott speaking at JS Monthly London in July, 2016
Great talks, fired to your inbox 👌
No junk, no spam, just great talks. Unsubscribe any time.

About this talk

Node-RED is a tool for wiring together hardware devices, APIs and online services in new and interesting ways. In this talk, Dalton explores the reasons behind its use, its architecture, and some of the key “nodes” within it.


[00:00:07] Hi everyone, my name is Dalton and I’m here to talk to you about Node-Red. It’s a Node framework used mostly for IoT but I’m going to show you a couple of other ways that it can be used. What is Node-Red? Well, it’s a tool for wiring together hardware and APIs and can be used for IoT, webscapers, bots. It’s easy to integrate with the other systems. You don’t have to make a full jump to it, you can do it incrementally and it’s always reactive principles, so there are very few side-effects. How does it work? Well, you have a general flow, this is a simple tape machine that gets activated every time an input comes in and then in that flow you have Nodes. These Nodes do one of three things: they give it input, do some sort of function, or return an output. You can see here that you have the inject Node, which is injecting some sort of false value into your flow. You also have the debug Node which allows you to see any part of the message options, which is the state that is being transformed as it goes through the state machine. A quick warning, this is not Redux, it should not be used for front-end development and I have tried. You will waste hours on it. [00:01:36] Our first demo. Imagine it’s Friday, four o’clock and your boss calls you into his office and he says to you, “I want some way to play evil laughter in my office.” You don’t really care why, you just want to go home, but after that he says, “Unless you finish this within the next hour, you are not going home. You’re going to have to stay here until it’s finished.” To do that would require some sort of IT solution, something connected to the Wi-Fi and you have to play a .wav file, that sounds complicated and linked up to some Bluetooth speakers, that sounds even more complicated. All that it sounding like more than an hour’s work, but then it gets even worse because last weekend you went on a mountain climbing accident and unfortunately broke your arm. Not only do you have to do this near impossible task in just less than one hour, you also have only one arm. I’m going to show you how you can do it with Node-Red. I’m not sure how I’m going to do this. I’m going to pretend this arm; I can’t use it because it’s holding the microphone. [00:03:00] I hooked up an Intel Edison IoT device, it’s sitting there right behind me. I’m also connected via Bluetooth to a speaker. I’ve also created a Node-Red instance. What I’m going to do is just create a simple flow for it. If I create a new file, well, let’s make this pretty simple. Some people just press a button on the Node-Red dashboard and you want something to happen. Well, how would you get the Bluetooth to work? Certainly you would have to import some weird Node and get some sort of dependency to work. Well, luckily, Node-Red has something for this situation, it’s called “exec Node”. You can just run a command and it will be as if you’re activating the command line, and the Intel Edison has a command line command for this, it’s called “B Strimmer”. If I just get this command, with only one arm, piece it in. you can generate these commands dynamically as well. You can append whatever is in the message that has been received, you can pend the input that you’re transformed. I think I call it “evil laughter”. I’ve also loaded a .wav file with a sound of evil laughter onto this. If I deploy this, which is saving to the system, let’s see if that works. No. Debug time. [00:05:38] It’s always a shame when the demo doesn’t work for you. You think as developers we’d get the hang of it. Okay. I will skip this and see if I can come back to it in a bit. You’ve completed the task and your boss is pretty happy but he still has another thing that he wants you to do. He’s figured out a way to get in on this whole Pokémon Go craze. He wants to create a guide where you will give the name of a Pokémon and you will get a description about it back. Now, again, this seems like a long and complicated task that you won’t want finish in less than half of an hour that you have left; however, you can do this as well using Node-Red. You just set a simple API using the HTTP Nodes. These work as the ins and outs of your API. You also get a HTML Node which will parse HTML and looking for the exact element that you want and also get a HTTP request, which is just a simple HTTP request that you would expect. Then you can use the function Nodes to read the input and then pass the URLs to the HTTP request. I’ve managed to find a Wikipedia for Pokémon, which I’ll just give you in the description. I just add what you pass to the body of the poster pets for an API. All poster pets are put into message.wreck.body and I would also want to do a sub strain method because I would expect this to be inputted as Poke equals whatever the Pokémon name is. That’s sub strain. If I count the number of characters. If I could the number of characters in Poke equals, that would be the equivalent to six, I believe. [00:09:05] If we passed Poke equals Pikachu, both pass the URL, HTTP, Wikipedia/Pikachu to our HTTP request Node, which actually this needs to be. Message stopped. Then I will also set these /URLs, if I set this to /Pokeinfo and once you get your HTML back, you need to parse it. There’s something called “the HTML Node” which takes a selector and then finds that exact content. We’re on the site. A nice and simple way to do it is if you go to the description of what you want, you can see, I’m just trying to get this biology. I just inspect the element and then if I grab its unique selector, I can just paste that into the HTML Node and then create another function Node because this will be passed back as an array. I just need to set the message of that payload, which will be passed back to the user into the payload. Then I just put these up. There we go. I got the HTML content when really I only want the text. If I resubmit, you should be able to see. There you go. You can see the text content. You can see the text content that’s gotten straight from Wikipedia. In conclusion, Node-Red is great for when you want to do simple projects that you don’t really have the time for. You can get the flags by PMing me @doltonhacks with the code “JS monthly”. A note for anyone in the back, I will immediately send people. Thank you everyone.