ElixirConf 2019 - Breaking Into Nerves: How to Use Your Elixir... - Jacqueline Manzi

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] Oh quick show of hands how many people here have done anything with a Raspberry Pi or a Drano or anything like that it's a lot of hands okay how many of you have done anything with nerves cool so not a lot of hands great that's exactly what I was looking for hopefully this won't be too boring for the rest of you my name is Jacqueline Mansi I work at a company called MUX and at work at MUX I work on a lick sir on our API layer I also write a lot of JavaScript and react for the front-end so I'm a full-stack developer I don't really find myself doing a lot of lower-level things and certainly not anything that might be considered embedded programming so this talk is really just about how somebody like me who knows how to code and knows how to write a lick sir can approach something as scary as nervous what is nerves well if you go to the official website it'll say it's three things it's a platform it's a framework and it's tooling it's a really a minimal version of build root Linux that was directly to the beam it's a framework which is it's a bunch of libraries that allow you to write a lick sir for embedded devices and it provides really powerful command-line tools that allow you to manage builds update firmware and configure your devices etc cool so what you're saying is I can write a lick sir for embedded devices yes great so how do we get started well first I would suggest picking a project I would start out with something really simple and easy I decided to go with a security camera because I thought it would be really easy to build also because my neighborhood is really interesting and I should probably have one so you've picked your project let's say you've gone with a security camera now you have to figure out what kind of boards you're going to use so this is a list of the officially supported boards for nerves you'll see there's a lot of raspberry PI's and a lot of beaglebones all of these are great words some of them are just older than others some of them are smaller and have fewer resources like the Raspberry Pi zero or the pocket beagle you might choose those if you have a project that requires a really small chip so how do you decide between the Raspberry Pi and the Beagle bone from what I've read I've only we experimented with the Raspberry Pi but they're very similar boards they were both designed and created to be learning devices they're actually can be considered many computers in the sense that they are capable of running fully functional operating systems the real difference is that the Raspberry Pi is gonna be a little bit cheaper than the BeagleBone it's also considered more of like a media hub I know that the newer Raspberry Pi 4 can support like 4k dual 4k which is pretty crazy the pocket beagle is gonna be a little bit more powerful it has more pins and it is more expensive if this is your first nerves project or your first embedded project in general I would probably suggest going with one of the newer versions of the Raspberry Pi model the Raspberry Pi 3 B plus or the Raspberry Pi 4 just because the Raspberry Pi is a more popular board you're gonna have a lot more resources available to you on the Internet if things go wrong I went with the Raspberry Pi 3 B+ because the Raspberry Pi 4 was not out yet I probably would have bought with that but also because it supports all the things I would need to build a security camera and it's pretty cheap for $35 well so we have our Raspberry Pi looks like this we have our PI cam it's like this it's like a really small camera that's made specifically for the Raspberry Pi we have our micro SD card which we're going to need for our Raspberry Pi that's what we're gonna boot our system on - we have our SD car SD card writer and reader we have our power supply to power our Raspberry Pi and we have an Ethernet cable to connect to our Raspberry Pi so that we can connect to it and we have dongles all over the place because Apple hates us ok so now that you have everything it's time to install nerves on your computer or our hosts I'm not going to go deep into the installation instructions because the docs do a really good job of that but nerves really requires three things it requires Erling elixir and a few tools for packaging the firmware so let's say that you've successfully installed nerves on your host now it's time to create create your new nerve zap and nerve surprise a really great tool for this if you say mix nerves new hello nerves it will generate a new nerves application for you if you and something else just change hello nerves to something else and it will generate something for you it'll look really similar to other elixir applications you've used you'll notice that there is a config here that is a general config and there is a target config and that's gonna be for your raspberry pi when we refer to target we're gonna be referring to our device and in this case it's our raspberry pi we're gonna allow it to fetch it and install its dependencies yes everything was successful great cool okay so let's open up that target configuration and you'll lose a section in here that talking about SSH keys and this is really important because it's how we're gonna be able to get into our target so what this is doing is it's just looking in your default dot SSH directory for your default keys and it's going to bundle those up into your firmware so that you can authenticate your host to your target if you want to generate your own SSH key specifically for your target go ahead and do that but remember to update it here great the other important piece is how you're gonna connect to your target your raspberry pi I went with wire Ethernet so I'm going with it zero and the domain is I mean nerves that logo local and the node name is gonna be PI if you have another board you can probably connect in other ways the Raspberry Pi 3 B+ you can do wired or wireless Ethernet great ok so we've successfully configured everything for our target it's time now to build and burn the firmware to our SD card what we're gonna do is set our environment variable to be mixed target equals RPI 3 if you have a different target that's where you would put it there since I'm using RPI 3 that's that's what I'd used and then mix firmware it up burn and what this is going to do it's going to build and bundle our firmware and then burn it to our sdcard cool it's signifying that I have the right environment lots of building is happening and then it's successfully built and bundled our firmware it's also discovered our SD card there's probably a long list of other things in here I just included the first one so yes please burn to my SD card success a wait a second do I need to do that every time I wanted to play any code that seems freely nope just once which is cool so all I have to do is type mixed firmware gen script that's going to generate an upload script that after I've done my original burn I just need to say mix firmware we're just gonna bundle our firmware and then when you upload it's gonna upload over SSH that new bundle with your new code directly to your PI okay so now that we have our sdcard burned we're gonna put it into our device we're gonna plug our power supply in we're gonna connect our ethernet to our computer and then we need to make sure that we are connected we're just going to do a simple ping so we're gonna ping nerves out local and if we get bytes back we know that we have successfully connected to our device great now we know we can SSH in so I'm gonna SSH in to PI my no name at nerves that local the domain and if everything is successful we boot directly to IEX we're in let's take a look around ooh what's this toolshed thing all right I'm just going to show you for real alright so I am assist into my device I'm gonna look at the help options for toolshed a lot of these should seem pretty familiar to you if config we got ping we've got we can reboot apparently we have uptime we've got tree weather all right let's check that out it is partly cloudy and 87 degrees in Denver awesome toolshed is a great command line tool to help you with stuff on your on your system for nerves ok well the weather is pretty cool but let's actually do something useful like build a security camera so I know that there's a lot of libraries for the Raspberry Pi camera the PI cam but they're mostly in Python so are there any elixir libraries for the PI cam yes actually there is a really good one under it looks revision pi cam so let's check that out cool so what's this it's an elixir library use capture em JPEG video on a Raspberry Pi using a camera module awesome oh cool we have some examples let's look at the PI cam http1 what's this it's example application for streaming MJ MJPEG video over HTTP using PI cam all right so let's pretend that we just implemented this alright that's pretty cool there are my fingers there are you guys this is actually pretty impressive the quality is really good and the frame rate is fast and things look good I'm really impressed by this library so back to our slides so we implemented that example project that's pretty neat do you think I could use this for basic motion detection well let's see what's doing inside of the streamer file ok cool so umm what it's doing is it's taking a frame which is a JPEG from the library and then it's sending it to the browser so that there's a persistent TCP connection from the server to the browser and then it's just sending these constant stream of JPEGs over chunked encoding to the browser and so we get this really cool little fast JPEG stream here ok so what can we do with that if anything what if I compare the previous frame that JPEG frame that I just showed you to the current frame and then look to see if there are any differences and that might indicate motion so I'm going to implement a really basic gen server to do that it's going to have a a handle cast callback that's going to take a detect motion message it's also going to receive an image binary with it that image binary is gonna be that JPEG I just showed you there's going to be a little bit of state that we're having our gen server it's gonna be the count value which is just our binary image that we're going to convert into a list of numbers here and then we're going to sum them all together right and that will be our count and so the idea is that we're going to compare the current account to the previous account to percentage that greater or less than and if if there is we have we have motion right this seems really basic and simple and it could never work I pretty sure I need like OpenCV or something right so I'm going to use bring lager which is a really great logging tool for nerves I'm gonna attach to it so you notice that here I'm logging if I see movement with that count value and see what I can't believe that worked holy crap I did not expect that to work well that was cool but what's cooler would be is if I can use that motion detection to start streaming video to the cloud because it doesn't really matter if I'm I'm JPEG streaming to my browser right do you think I can do that with that constant stream of JPEGs well I work at a video company so I work with a bunch of video engineers I don't know much about video myself but I'll just ask them hey guys can i livestream a bunch of JPEGs lots of laughter lol no use ffmpeg great you could be like me and spend way too much time trying to prove them wrong you truly cannot livestream JPEGs just let you know so ffmpeg is that on my nerves build well as I mentioned before nerves is a really lean minimal version of the built route Linux and probably does not have it but let's find out so system about find executable on my PI ffmpeg nil yeah I know okay so this seems really scary and daunting to me building my own custom nerve system but once I started doing it and looking at the docs it was actually pretty simple and nerfs provides you with really good tooling for this so we're to create your own custom nerve system off of an existing one I'm gonna create one off of my Raspberry Pi 3 you clone the existing nervous system branch and then you're gonna need something like you're gonna need docker or be on a linux environment because we are gonna be using route I did this both using docker on my Mac OS X and on my a booting machine and both both of them worked really well and it was really easy to do so if you have docker installed it's pretty easy and another thing to note is that pulling the initial image if you're using docker isn't it might take some time and then building it with build root is going to take around 30 minutes or so depending on your build and your machine I'm not going to go into all the details of building a custom nerve system but it's super simple and I had a lot of fun doing it here's an example screenshot of what you will see when you are using build route to make your own system it's gonna give you a category of options to look through and here's where you're going to look through all the stuff you want to include in your new build you can actually also include some games this is me including chocolate doom and then spending way too much time playing it and getting distracted but what I really wanted was to go into target packages into the audio and video application section and find ffmpeg I found it selected it included it with my build and then went through all of the instructions to build my own custom nerve system okay let's pretend we all did that now when we go back to our system because we've now included our custom RPI 3 system and we're not using the the one that is available through nerves system to find executable ffmpeg hey it's there ok so it's in our user bin directory great ok great so we have ffmpeg on our bill but that is a command-line tool that I can't really access with an elixir right like how do I do that well how does the elixir vision library do it they're using ports cool what's a port well a port is a mechanism to start operating assistant processes external to the Erlang VM and communicate with them via message passionate so you can actually create a new port process and then it's going to manage the communication between that process that is living under operating system in our case can be ffmpeg if we look at what they do in the elixir vision library in PI cam this is a really well-written library by the way if you I suggest going through it it's just like it's very nice but what it's doing is has this function called spawn port and it is finding the executable for for RasPi JPEGs if trees their library and it is opening a port for that executable and so they have a port that they also save to their state here which I will show you later is very good for us so let's try and do that okay so here's what mine looks like and I don't know if anybody has used ffmpeg in here before but it is a command-line tool that involves a series of really confusing arguments and this is what mine ended up look like and what it's doing is it's taking video straight from the board and it's streaming it to an rtmp stream live stream that I created so I basically just followed what they did in the elixir vision PI cam library and I'm spawning a new port for ffmpeg alright so let's get to what the code for that is gonna look like I'm going in my agenda over that I showed you before I'm gonna create a call back handle info call back that takes a message allows streaming and all it's doing is updating my gen server state to say stream allowed is true and then this is the function I showed you previously the handle cast one where it's getting that detect motion message and it's receiving that binary image before we weren't doing anything with stream at lab but now we are and if stream is allowed in our state what we're actually going to do is kill that that PI cam process one that we were getting our JPEGs from and the reason for that is you actually can't have two processes accessing the the PI cam camera at the same time so I can't have my ffmpeg streaming at the same time that the raspberry pi PI cam library is getting those JPEGs so that's the reasoning for me doing that stream allowed here's the kill I can't function and before I showed you in the PI cam library how it was really useful that they were exposing their port to us because what I can do is find the pit of the PI cam camera that's their gen server get that pit and then get the state of it by saying sis don't get state which gives me that port and then and this is a little hacky but and I've had it work consistently for me so I'm sure there are better ways to do this but I'm gonna hard kill that process and the reason I'm doing that is because if you kill a port it's not guaranteed that the operating system executable process is going to die with it so I'm just gonna hard kill it and I'm gonna find that operating system paid by that port so I get it and then I kill it saying system command kill - kill with that port pid' and then I'm going to send myself a message saying that it's time to spawn that rtmp ffmpeg port process hears that so I'm gonna log myself a message saying attempting to open our TMP port I'm also going to do something fun and set myself a text message using Twilio saying that movement was detected and my stream has started and then I'm gonna do spawn our TMP port which is that function I showed you earlier with the really long ffmpeg command-line arguments and then since I don't want this to happen all the time I'm just gonna say a stream loud Falls so if that all works I should get a text message and see my live stream so let's try that over real and I know it's a really bad idea to demo things live but we're gonna do it anyway okay so we're gonna find the process bid and then we're going to send our Gen server a message to allow streaming all right so we should be allowing streaming now on movements there it is attempting to open an rtmp port did I get a text message I did Twilio says your trial account says movement was detected and your stream has started cool and you'll notice here that the other process timed out because I killed it I hard killed that I can port process okay let's see that live stream not to be a corporate shill I just work on this product so it was like the thing I was most familiar with but I just use mocks but you can use anything else a live stream with we see that we did actually connect let's check it out we have an asset that's preparing and it's ready and there is our live stream a little bit late and see we'll catch up cool okay now I probably have a lot of time left but I have more slides okay so wow you made that look really easy did you have a hard time or make any mistakes I had a really hard time and I made a lot of mistakes and a lot of it was just like really I don't want to say stupid mistakes but like things that seem stupid to me now but there are no stupid mistakes especially when you're first learning things I looked quite a bit on the slack channel the elixir slack channel for nerves and they are really great the nerves team they're just full of really nice people that are there for you and they really want to help you out so I suggest if you are your nerves hop on the slack channel hop on the nerve size channel and ask questions because they're gonna be there I made a really silly bad mistake that I can laugh about now but at the time it wasn't that funny I yeah I was fooling around with myths and Jen servers I was actually gonna include that in my talk but yeah it was my work machine too I was going to include the NIF's and Jen's to everything in my talk but I became a little bit more complicated and I wanted this to be more like a big dinner friendly talk so when I was messing around with this I was creating make files and I was like commenting out a bunch of stuff and I went to go make food or something I came back and ran my project which of course ran the makefile and in that make file the only rule that was uncommented was clean RM dash RF prefix slash star and prefix was also comin up so I deleted all of my applications likely I stopped it in time so it was just my applications anyway that is the talk come find me if you want to chat more about elixir or video games or anything at all check out that slack channel and you can send me a message on twitter my handle is crypto pattern which 2011 million thought was a great handle not so much now me it's like a bad tattoo that's it thanks guys [Applause]
Info
Channel: ElixirConf
Views: 4,067
Rating: 4.9652176 out of 5
Keywords:
Id: IU8epH8FAI4
Channel Id: undefined
Length: 24min 4sec (1444 seconds)
Published: Thu Aug 29 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.