 This is part five, I believe, of this series on home automation using the Cancun Smart Plug. And if you've been watching along, great. If not, there should be an annotation on the screen to the full playlist. There's also notes and scripts in the description. You can link to my GitHub page and the projects. Just a few things I've created that we're mainly going to be looking at today. And, you know, the goal of this is this is a great little device for controlling home automation lights or whatever be able to turn them on and off remotely through your Wi-Fi or through the internet if you want. But by default when you buy this device, it's like they want you to install an app for your iPhone or Android. And not only do I not like installing unnecessary software regardless of where it is for your open source, these apps are not free and open source, so I wouldn't use them at all. Luckily, we do have SSH access and there is a web server already running on the plug. So let's go ahead and log in real quick here. And last tutorial, I got the device connected to my router rather than connecting it to a directly as if it was an access point. And it was assigned this IP address. So I'm going to SSH end just as we have all the previous tutorials. I'm going to type in the password that I changed it to. And right away off the bat, if you watch previous tutorials, you'll notice that this little welcome message is different. It has my name there, my website, a link to my GitHub project for this, which again should be in the description of this video and license this information there, which there's more of on the project on the page. So how do you change the welcome message? Well, you know, this is running OpenWRT, which there's a lot of information out there for. And as we already know, there's already VIM on this device and we can VIM ETC banner is the name of the file. It's just a plain text file. And there is the text file that I've created. Now, I did this ASCII wording here on my desktop with Figlet. And another program called Toilet, a great name, are for making fonts like this ASCII type large fonts. I have videos on those. So if you go to my YouTube page and search either Figlet or Toilet, you'll get the tutorials on that. But as I said, if we open up this link here, which opened up on another page here in the background, there we go. This is my project. And not only do I have a text file here, which is from the last tutorial about setting up your device to connect to your Wi-Fi. But I also have three folders here. And one of them is the ETC, which has my example banner file. So you can pull that if you want. So those are mainly we're going to be looking at the scripts here. So in this bin folder, there are three files on, off, and delay, which are simple little scripts I created. And I'll probably update in the future. But let's have a look at what I've created so far. I already have them on my device. So I'm going to get out of the text that are here of them by hitting colon, escape colon, Q for quit and exclamation mark. Just say I haven't made any changes. And if I VIM bin my on script, you'll see that this is a shell script. Note that the shebang line doesn't say bash says SH because there is no bash on this device. And if you watch previous tutorials, I have, if you echo into this file, the number one, it turns things on. And if you echo off or echo zero, it turns them off. So actually the same, off is basically the same exact file with that. And it just saves me the trouble of typing all that out every time I can either type on. And I know you can't see it, but I have the plug hooked up to the lighting in my office now. So the lamps on my counter over here just came on. If I type off, they turn off. The delay script is something that I'll probably improve on in the future. But if I'm leaving and I want the light to turn on an hour from now, a half an hour from now, five hours from now, this script will do that. We have this first little part just basically will echo out instructions if you don't input commands properly. But basically it's going to sleep for the number of minutes you input and then either turn the light on or off whether you put one or zero. And it's set to throw this automatically into the background. So you can run this and then exit out or whatever you would like. So if I quit out of that and I type in delay with no parameters, it's going to give me that message on how to use it. So I can say delay, I'll say one because it's going by minutes and I'll say one saying in one minute, turn it on. And it says switch will activate one minute. Now I can exit out and that script is still running in the background. Unless the plug loses power and restarts in a minute, that light will come on. If I go back into the plug, I can again clear the screen here, delay, I can say one zero and a minute from now the light will turn off. So in less than a minute it's going to turn on and then about 50 seconds from now it's going to turn off again. I'll let you know when that happens. It should be happening any moment now. So that's what that script does. Really a cron job might be good if you want the lights to come on at a certain time or not. And cron is installed on this plug already. So you could use crontab-e to set up your stuff but you also have to make sure the process is running. I haven't really messed with that yet. So that might be a good thing to look into that I'm not really going to go over in this tutorial. If you want the light to come on, the light just came on by the way. If you want the light to come on and off at certain times of the day, crontab might be a good option. You just have to get the service running in the background there. So those are the three little shell scripts I created. Now let's get into the meat and potatoes and the light just went off. Okay, the WWW. As we said, there's already a web server running on this device but there's nothing in the home, the root web directory. So I added some stuff. I added in previous tutorials, we created an on and off CGI but what I did was now is I created a toggle. I'm not sure if I went over that in a previous tutorial but basically if I go here and I type in the IP address of my plug it gives me the root directory for the web server which is the WWW folder in the root directory. If I go into CGI bin, it says forbidden because you're not supposed to be able to see scripts like this but I can type in toggle.cgi when I hit enter. The light just came on, it gives me the message that the light is on. If I refresh that, the light goes off. If I refresh it again, the light goes on, it goes off and it gives me the output message here each time. What I've added here is this switch HTML. By default when you go here, it just says switch but this button, if I click it, it tells me, the light turns on and it tells me the light is on. If I click it again, it goes off. Basically it's just in the background calling that toggle. If I hit F12 here, I'm in Chrome, I can see my console here and if I hit the button you can see the output message. I have it go to the console as well and if we go to network, you can see each time I press it it calls that toggle CGI. Right now I can open up browser on my desktop as we're doing or laptop or my phone or tablet. I can go to this web page running on the device itself and I can toggle the light on and off and if I forward ports on my router I can now turn these lights on and off from anywhere in the world as long as I have internet access. So that script, if we control you here to look at the code, very simple, very little JavaScript. It's using jQuery so it loads up jQuery from the internet. You could put it on the plug but I'm trying to save room on the plug since there's not very much space on there. And it says when the document's ready when we click a button that has the idea switch it's going to run this function which is just basically calling that toggle event and then the response from that website which is the words either the light is on or the light is off it prints to the console and it also changes the text on the button. Down here we have our HTML our main body of our HTML and you can see that it's just a hreference, sorry hyperlink reference with an idea of switch and then I gave it some classes because I grabbed some CSS that I just generated from a website that generates buttons to customize a little bit to make it kind of a full screen thing and that's it. So majority of almost, you know, 75, 80% of this code is actually just CSS to make the button look like this so when I hover over it it looks like I'm hovering over it and when I click it it kind of moves up and down like I'm clicking it. The actual programming is just a couple of lines and the actual, you know, HTML it's just basically one line inside the body. Very, very simple and again this is all on this project page if we go into the www folder here so you can clone this project and you can s-copy it to the device or you can get, well, you can't get it I was hoping to be able to get it straight to the device but unfortunately WGet that's built into Busybox which is a slimmed down version of WGet does not support SSL and GitHub even if you remove the S from the HTTPS it still forwards to HTTPS so it tries to encrypt the connection which is great but this version of WGet doesn't support that so you can clone the project to your desktop and then use SSH or SCP to copy it over or you can come onto the device and grab it with an FTP server whatever you get those scripts to your device you can also just copy and paste them into the shell but that will give you these functions and I'll probably add more to these scripts because I'm using this on a daily basis to turn my lights on and off so as I get more of these plugs which I've already ordered another one because I've loved it so much I'll probably make an interface that runs all of them and also this interface doesn't necessarily even have to be on the device I could have it running on my web server my films by Chris web server and just have it redirect to my house at home I can obviously password protect it so I'm going to open it up to the entire world but yeah, the interface is there as well as my few little scripts to make it easy to turn things on and off that's all you have to do once these scripts are installed obviously once you copy them over make sure they're executable with ChangeMod plus X and the name of the script once you do that once you're good to go on and off, we'll turn things on and off and that's it so yeah, check out the links in the description you'll get links to the notes for this the project with all my scripts as well as a reference to a PDF or not a PDF but a website that actually has a bit more information on playing with this plug some stuff I haven't gone into that's a good reference material as always, I hope that you enjoyed this video hope you visit my website filmsbychris.com that's Chris at the K there should be a link to that in the description as well and if you enjoyed this series be sure to think about becoming a supporter over at patreon.com patreon.com forward slash mail x1000 even a few bucks a month helps me continue doing these videos so thank you for watching and I hope that you have a great day okay, this is an introduction to filmsbychris.com I'm Chris, that's Chris at the K that's me right there my daughter Ember and my wife Jennifer we pretty much live in the swamps of Florida I'm a firefighter by day as well as by night we work long hours but that's not why you're here you're here about the videos I put up on YouTube these videos are mainly about computers and programming which means most of my videos look something like this and if that's what you're interested in great, if not, that's alright I do videos on other topics too such as video editing special effects photo editing 3D design and music creation if you are one of my viewers and you enjoy my videos my Patreon page is a place where you can go to help support my videos so I ask that you take the time to go to my Patreon page and look at different levels of rewards you can receive for different levels of backing there should be a link in the description of this video if you are watching it on YouTube otherwise you can visit patreon.com forward slash metalx1000 and I thank you for your time and your support have a great day