Brew a Coffee With Python - Tuya Smart IOT Platform Walkthrough

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody and welcome to another youtube video so in today's video i'm going to be showing you how i wrote a python script that can brew me a coffee using the internet of things now the internet of things is a bunch of things devices sensors motors whatever they may be that are connected to the internet and that can be controlled by system or software so in this case i have this little device it's called a fingerbot and i'll try to get in focus here so you can see it it has this one mechanical part and motion so if i press the button here you can see it should go down and then come back up so i've actually attached this to my coffee maker connected it to the toya smart iot platform which is actually the sponsor of this video and then use some python code to control this so i'm going to show you exactly how this works in this video i'll also be showing you how to connect some other smart devices that are iot enabled like a light strip i have a light strip that's actually in my door frame and i'll show you kind of how to change the colors and all that anyways this is a very cool video you don't even have to have these devices to follow along you can actually create a virtual version of them and everything i'm going to show you here is completely free so with that said let's go ahead and get started [Music] so i'm on the computer now we're going to go ahead and get started the first thing i want to do though is just explain to you more in depth what the internet of things is so the internet of things describes the network of physical objects the things that are embedded with sensors software and other technologies for the purpose of connecting and exchanging data with other devices and systems over the internet so these devices could be ordinary household objects or sophisticated industry tools and a complete iot system could include devices connected to the cloud software deployed in a cloud environment apps and web user interfaces and all these things would connect together using the cloud and using the internet of things to actually you know control different devices and achieve some task so that leads me now to what the toyota smart iot platform is so this platform is designed for developers to make iot easier the internet of things easier so the platform has kind of two main goals the first goal is to connect hardware to the cloud to make it iot enabled to allow you to do that and the second goal is to allow you to utilize connected devices and build cloud sas so software as a service based on those connected devices and cloud services so the tui iot platform provides tools to free developers from everything below the app layer which allows developers to focus on their application and product development without distraction now i've been using this platform i messed around with it before this video it's very easy to use very intuitive and you're going to see as we go through here it's pretty simple to actually connect these physical devices and start controlling them from software like in our case python scripts so now what i want to do is bring us off the computer quickly and i want to show you the physical devices that i'm using here just give you a quick walk through of kind of how they work and then we'll hop back on here we're going to have to make an account on the toya platform and once we've kind of done all of the initial setup and authorization then we'll hop into the python code and i'll show you how to connect these physical devices and how to actually control them so as i was mentioning i'm going to show you the physical components that i'm going to be using here and just kind of walk you through how they work so this here is the fingerbot that i showed you at the beginning of the tutorial come on camera focus in here has that one mechanical movement piece which is this arm right here you can kind of see it all i'll demo it going up and down now you can control this from python code from the iot to go any percent up and down that you want and to either stay down or stay up or whatever now this is actually a wireless device it does have a charging port at the back and then obviously an on and an off switch but you don't need to have this plugged in for it to work it's wireless now the way that this actually connects to the internet is by using this now this is not a wireless device if this will focus in the camera here this does need to be plugged in okay so there you go so this here is a bluetooth bridge that connects the fingerbot with it okay so this needs to be plugged in it has a little hdmi port right here comes with a cable and a usb port and you just need to plug this in somewhere so that it has power and then you need to actually connect it to an iphone app which i'm going to show you in a second now it also has kind of this on button at the back come on focus up here and that's what you do to put this into pairing mode so i'll show you how that works later but i just want to show you that what i'm using is the bridge so this kind of like bluetooth gateway and then i am using the fingerbot i'll leave links to both of these in the description down below and then the last thing i'll show you is that the fingerbot this little plastic piece right here if we can focus in this comes off you can kind of see where it slides out and you can replace it with other things like this so for example this is a ring i also have something that has like a little sticky pad on it that goes on there it's very very small you can kind of see it and then i have a few other ones i have a little shorter like one that goes in here and then this is the uh there's a longer one that goes in here as well there's a little accessory package oops just bump the mic there that you can get to put into the fingerprint so those are the two devices that i'm using to get the coffee brewing aspect of this video working however tulia has over 310 000 skus of different products within 1100 different categories and they service over 220 countries so there's a ton of different products that work you can buy these products on amazon you can get all kinds of other products that connect with tui and what i'm going to show you here is not just specific to the finger bot the controls for it will be specific but following along with this will show you how to connect any other product as well so i apologize for all of the detailed explanation of everything here before we get started just want to make sure you guys are on track you know the hardware i'm using you know the basics of how it works now we can start actually creating a tui account connecting our devices and then eventually getting into the python code all right so we're back on the computer and now we're going to get started with the actual setup steps the first thing we need to do is create an account with tuia so what we're going to do is go to the iot platform so i'm going to press that button from this website all of the links here will be in the description and we need to sign up so i'm going to sign up and create an account once i'm back i will log into that account and then i will continue from there all right so i've just created my account and you can see the first thing i'm brought to here is whether or not i'm an individual developer or an organization now in this case i'm an individual developer so what i'm going to do is press set now and just select individual developers so i am an individual developer you don't need to select this right away you can select this later in the setup process but it's simple so i'm going to do it now okay so now that we've done that what we're going to do is go to this cloud tab here and we're going to go to development now the first thing that we really need to do is we need to actually create a new cloud development project but before we can do that we need to select what edition of toya smart iot we're going to use so you can see here there's a ton of different plans we're just going to go with the trial edition this is completely free so let's go ahead and do that all right so i'm just going to go through this process here so i select trial edition i don't need to add any notes i'm going to buy that don't worry you don't need to put in any credit card information it's completely free i'm gonna press submit order and then i should have the trial edition as my plan okay so there we go i now have the trial edition so what i'm gonna do is go back to the iot platform here and i am going to now go back to cloud and go to development all right so now that we're on this page you can see we're using the trial edition and the first thing we're going to do is create a cloud project so for the name what i'm going to do is enter coffee because that's kind of the main thing that we're trying to do here i could add a description i'm not going to add one for right now for the industry i'm going to go with smart home because this is something that's in my home but you could select the appropriate industry and then for the deployment method sorry development method not deployment method we're going to go with custom now you can read the detailed descriptions if you want but in our case custom is just the best that's the one that we're going to use and then the availability zone is going to be all of the areas in which we want this to be able to operate so in our case i want this to work in america that's because i'm located in canada so i'm going to select america but if you wanted this to work in other countries other regions as well then you would select china europe india whatever you guys know you can select the correct correct area and in this case actually i'll go with eastern america because i'm kind of in the east coast of america as well so yeah we'll go with those two all right so now that i have that i'm going to press create and this should create the project for me all right so after we do that it brings us to this page that says authorize api services now there's a bunch of different apis that we can use within the toya cloud iot platform but we need to authorize them so there's already seven that are added to our project here we have authorization industrial general device control a bunch of other ones you don't have to really understand what they are but make sure you leave these in here we do need to add one more though before we go any further now the one that i'm looking for here is industry project client service api so let's look for that industry client industry project client service okay this is the one that i want so i'm going to press that right there and it brings it over here and there's a few other ones that i want as well one that i want is lbs this is location based services so i'm going to add that in and then i also want the weather service now you'll see why we're going to use this in a second but pretty much i'm going to have my light strip adjust its color based on the temperature outside and so i just want to add these in right now so we don't need to do it later all right so i'm going to go ahead and press authorize so now that we've completed the authorization we're brought to this project configuration page now this is where we need to make an asset now an asset is something that's going to be accessible by a user we're going to set up that user right here and this asset is going to contain all of the different devices that a specific user can control so it's very important that you remember the phone number or email and the password that you set here so in this case for the asset name what i'm going to do is i am going to set it as room a you can set this as really anything that you want actually i'm going to change mine i'll just set it as a main asset and then for the account type you can choose either email or phone number it doesn't matter but you need to remember what it is that you put in here so in this case i'm going to go with phone number i'm going to fill in these two fields and then go to the next step because obviously i don't want you to see my phone number or my password so i've just completed that last form there and now i'm brought to a page that looks like this where i have my project which is coffee and then i have my access id slash my client id and my access secret slash client secret now these are two important pieces of information don't worry they won't go away you can get access to them later we're going to need these when we actually start making api calls okay so the first thing we're going to do here now that we've created our asset is we're going to add some devices so we're going to go to devices and this is where we're going to connect our bridge so that bluetooth bridge and then our fingerbot and then obviously in this case we have a light strip so i'll connect the light strip as well but in your situation you might just have one single device to connect so in an asset what you're going to do is press add device and then notice there's two options here you have add device with iot device management app and then you have add virtual device so if you're following along with this and you don't have a physical device what you can do is press add virtual device and then you can select device and add it virtually this simply simulates the physical device so if you're testing out development or something or you want to follow along with this tutorial you can do that without having to own the product now in this case i have a physical product so the way i'm going to do this is with the iot device management app now notice it brings up a qr code here so what i actually need to do is go to my mobile device and i need to scan this qr code and then follow along with the steps so now we're going to move on to a recording from my phone so i'm going to start recording on my iphone and follow along with these steps so i've just scanned the qr code and you can see here it's bringing me now to download this app it says iot device management so i'm going to go ahead and press download now when i download this it's going to redirect me to download an app called test flight so i need to download this test flight app first and then i can actually join the beta because this is a beta app which is the iot device management app so i'm going to go and view this in the app store i'm going to go ahead and download that app and then once that app is downloaded i'll be right back so as you can see now i've downloaded the test flight app so what i'm going to do is start testing with the iot device management app so when i do this it brings me to test flight i'm just going to allow it to send me notifications i'm going to press continue and then see it shows the iot device management app so i'm going to go ahead and install that and then once that's installed this will just show up as an icon on your home screen you can press on that and then we'll follow along from there all right so i'm now in the iot device management app notice it says from the developer gives me the fact that this is kind of a beta application so i'm going to go ahead and press next i can share my feedback with the developer okay that's fine and then here we go we have the creator iot platform account so what this is asking me to do is create a cloud project which is what we actually just did authorize the api products we've done that already configure the project we've done that and then finally navigate to the project qr code which we just did so now what we can do is press get started so now what i can do is scan the qr code that's in front of me here when i scan that qr code it associates that with my project so notice it now says coffee and then i want to sign into my account with the username and password so let me sign in and then i'll continue alright so i've just signed in and to clarify here the sign in information that you're going to use is the one that you created when you made your asset so you're kind of signing into that asset right you're not signing in to the account you made in the very first step of the video you're signing in with the credentials you just created when we were in that cloud project setup so i've signed in now i allowed the app to have access to all the things that want to have access to it wanted the wi-fi bluetooth it needs access to those things so make sure you accept that and now what i'm going to do is press into this main asset so when i press into this main asset you're going to see it says no assets found and it will also say that we have no devices now it's asking me if i would like to let it use bluetooth so let's press yes and now what i'm going to do is start connecting my devices so to connect a device you press add device and then what you're going to do is click pair device now you should be able to automatically find the device if you're close enough to it now in this case we are using this kind of wi-fi bridge that's the first thing that we're going to need to connect so make sure that's plugged in and turned on and that you're close to that so once that's on what you should be able to do is go on auto scan here and it should actually be able to find this device for you alright so the bluetooth bridge has officially been connected you can see it says device added successfully so i'm going to go ahead and press done now what i want to do is actually connect a device to this bluetooth bridge so i'm going to go ahead and turn on my fingerbot which remember is wireless and it should start pairing and actually be able to find this bluetooth gateway so give it a second and let's see if we can find this device alright so my fingerbot has connected this did take a few minutes to actually show up the way i got it to connect was i turned my fingerbot on then i went to my bluetooth gateway and i pressed the button on it that put it into pairing mode there was a blue light that started flashing and then my fingerbot connected to it i didn't need to do anything with the app and the finger bot for it to connect it just automatically connects to the bridge and then it showed up here now for some reason your fingerbot is not connecting try turning the fingerbot on and off five times rapidly that is supposed to put it into connection mode although mine just automatically connected now you obviously can look at the fingerbot documentation and the bluetooth bridge documentation to see how to get it to connect but mine connected so we're all good we will continue all right so you can see the fingerbot is here so what i'm going to do is press on the three dots and i'm just going to show you that this is kind of the interface that you get so in this user interface that you see here you can control the fingerbot and mess with a few of the settings for it so just notice i have mine beside me so you should hear it if i turn it on you can hear that the sound actually happens because well the mechanical thing is moving this is working it is connected so now what i want to do is bring us back to the computer because since we've done this our device is now connected to our assets so within the main asset you'll see that we have this bridge and we have this fingerbot so now we'll actually be able to see these from our computer from the iot toya platform so let's go back to the computer and check this out so i've gone back to the platform here you can see i'm in my coffee project and now both the fingerbot and the bluetooth bridge have connected now i did need to refresh the page for this to happen but they are both connected now one thing to note if you do turn the fingerbot off since it is a wireless device you can just turn it off if you want it's no longer going to be connected right it's going to have the status offline but as soon as you turn it back on it will automatically connect you won't have to go through those same steps again so now that we've done that with the app we don't need to deal with the app anymore we've already connected the device we can now continue working strictly from the computer so our fingerbot and our bluetooth bridge are connected we now see them on here and we can actually start writing some python code so the two things that we're going to want here related to these devices is the device id and actually we just want the device id of the fingerbot because there's nothing we really need to control related to the bluetooth bridge that's just the way that we're going to be able to control the fingerprint okay so what we need to do if we want to actually start writing some code here is we need to open up a python file and we need to install the toya sdk for python so let's go ahead and do that alright so i've just opened up a command prompt because i'm on windows if you are on mac or linux you're going to open up terminal and what you're going to do is type in the following command you want pip 3 install toya hyphen iot hyphen pi hyphen sdk for some of you pip 3 won't work if that's the case you want to try pip and if pip doesn't work what you're going to want to try is python and then hyphen m pip install toya iot hyphen pi hyphen sdk if that doesn't work try this so add the pip 3 and finally if that doesn't work you're going to try python 3 hyphen m pip 3 install and lastly remove the 3 here on the pip and then try that these are just all the different variations of the command that you can try that hopefully will work for you so in this case we'll go pip 3 install toya iot pi sdk you can see i already have this installed and so the requirement is already satisfied for you however it should have actually installed and now you have the sdk installed so what we can do is start actually working with our devices and with the toya sdk so i'm going to close the terminal or command prompt here i'm going to go into a new python file so i'm going to work in sublime text feel free to work in whatever editor that you want and now we're going to have to do is import the toya sdk and then start actually connecting to our project and then being able to control our iot devices so the first thing we need to do here is import the toya sdk so we're going to say from and then toya underscore iot import and then this is going to be toya open api like that next we're going to say from requests import get you'll see why we need this in a minute and sorry this should be request now this module you may need to install as well for some reason i keep thinking that it's uh it's built in but you may need to install this with pip so you would do the same thing we did before pip install and then requests like that so if that doesn't work for you just make sure you install it with pip in the same way that you installed the sdk previously then we are going to oops import random let me just fix this here and these last two modules so random and requests we're going to use later okay so now that we have that we need to fill in the following information so i'm just going to copy some code in here like this so we need to fill in our access id our access key our endpoint our username and our password now the access id and the access key are going to come from that main page of your project that i showed you previously in the video when we were setting up the project from the toya iot platform then this endpoint is going to be dependent on the region that you're using in this case i'm using the us region and so us works but if you're using a different region you're gonna have to have a different kind of last two characters here and you can go to this link which i'll leave in the description and i'll go to now to show you uh where you can actually find the correct endpoint so let's open up this link here and you can see that the endpoints are right here so this is the china endpoint this is the america endpoint the american azure endpoint europe europe ms india so on and so forth okay so that is where you find the endpoint from notice that we're using the u.s one okay so we need to fill in that endpoint then we need our username and password and the username and password are going to be the same username and password you used when you signed into that app on your phone so the username and password are kind of the second username and password that we put in the one that we used when we were setting up our project not when we were creating our original account lastly we're going to need our fingerbot device id the device id is the id that we saw from the toya platform that i said to pay attention to so let's go here this is the device id that we need so i'm actually going to copy this device id and paste it here now in my case i don't really care if you see my device id but you shouldn't share this device id this should be something private as well as all of these four pieces of information so what i'm going to do is kind of write a modified python script that allows me to hide this piece of information from you but for you guys if you want to keep it simple you can just paste them in here and let me just show you where those pieces of information are so if we go back here and we go to overview you should see that we have our when this loads our client id and then our client secret uh and our access secret so the client id again it's fine if you see that for me but you guys shouldn't make that available to everyone and then the access key is something that i do need to keep hidden okay so i'm going to fill in this piece of information then i'll be right back all right so i've modified this python code now such that i'm storing that secret information in a secret json file reading in that file and then just using the appropriate key value pairs here to be able to actually access this information you don't have to understand the modification that i made just so that i hide this information from you while i am working now what we are going to do is we are actually going to connect to the toya platform so what we're going to do is say open api is equal to and then this is going to be toya with a capital t if i could type properly open api and we're going to pass the endpoint we want to connect to our access id and our access key then what we are going to do is log in to the api so we're going to say open api dot login we're going to log in with our username and our password so once we've done this if we run the program we shouldn't see any errors assuming this information is correct and we have the correct endpoint if you do see an error then you need to double check this information information sorry and make sure it's all correct so let's run this notice it just says finished we didn't get any errors that is good that means we are all good okay so now what we need to do is we need to be able to control our fingerboard so the simplest way to do this is to send a post request to a specific endpoint on the api now i'm going to show you how we can actually find this endpoint and how we can find what information we should send to this endpoint so we're going to go back to our web browser here we're going to go to the toya iot platform and we're going to go to the api explorer now the api explorer allows us to explore all of the different api calls that we can make and all the different commands that we can send to control different devices that we have so actually sorry before we go to this i need to go back and i need to go to my devices and i need to copy the device id of my fingerbot because we're going to need that so let's get the device id of the fingerbot here let's go back to the explorer and what we're going to do is go here on the left side and we're going to find the industrial general device and then this should be control okay so you're going to look for industrial general device control then you're going to look for get the instruction set of the device and what you're going to do is paste in here the device id now what this is going to do is send a request to an endpoint that will give you all of the information about the device that you need so let's submit the request and notice it shows you the request that was sent then it gives you the result so it says the category of this device is the following these are the different commands that you can send to this device so we have a timer command we have a mode command we have an arm down percent a click sustain time and arm up percent and then we have the switch now the switch is the one that we are going to focus on but if you look here it shows you how to use all of these different commands so it says the code for the command is this the name is this the type is this and this is the values that were expected in this case the only two acceptable values are click and switch we have arm down percent this accepts an integer the minimum is 51 the maximum is 100 percent you know you can go through and read how all of these work so now that we've done this and we know the commands that we can send what we can do is actually send one of these commands and test it and see if it works before we start writing the code so we can go to send commands here we can paste in our device id we can paste in the code which was switch and the value which in this case will be true or false so what i'm going to do is submit this request and then as you can likely hear the fingerbot is actually going on and off because well we told it to switch and when you type switch really what that means is go down and come back up it changes depending on the mode that the fingerbot is in but that's the basics when you send this you should see that the fingerbot reacts to this command and then you get a result that says true and success now notice if i try to send say like 34 here as the value and i submit this request we get a error code it says the type is incorrect and it tells me that this was false this was not successful so you will get valid error messages if the commands you're sending are not correct all right so what i'm going to do now is i am going to go back to my code here so i'm now going to show you how we can do this from code rather than doing it from the api explorer so what we need to do here is we need to write the following open api dot post because we're going to send a post request and then i need to find the url that i want to actually send this post request to so the way i do that is i can go to the api explorer and i can look at this sample post request here so notice it says https openapi.tulya us.com and then this is the part that i'm looking for v1 iot hyphen 3 devices this is the url of my device and then i have slash commands so what i want to do is copy this extension of the url so kind of the path right off of the base url i don't want the base url and i want to paste that in a string right here now what i also need to do is just make sure i have this leading slash here and i'm actually going to replace this so the hard-coded device id with the variable that i have which is fingerbot device id and then i'm going to turn this into an f string such that this automatically gets embedded in string if you're not familiar with f strings these are available in python version 3.6 and above and they allow you to embed a variable inside of a string inside of curly braces so now what i can do is send a post request to this url right here and then what i need to send is some commands and the commands that i'm going to send are going to be in the format that was specified from the api explorer so what i can do is i'm going to make a variable here called commands and this is going to be equal to a dictionary this is kind of going to represent json and this is going to be commands colon and then a list of the commands that i want to send now the command that i want to send is the following i want to send the code which is going to be uh switch and then i want to send the value which is going to be true all right so these are the commands that i want to send this is the format in which you send the commands you can find that from the documentation which i'll link in the description and then what you do is you simply put this right here command so just make sure it follows this format commands you then have a list and inside of here you have a dictionary with the commands that you want to send all right so what i'm going to do now is i am going to save and i'm going to run this code and see if i get any errors or if this is working all right so after running this code nothing was happening and i was curious why nothing was happening especially why we weren't getting any error messages and the reason for that is first of all we're not printing out the result of this command or sorry of this request and also we have the wrong type here for our command i put in the string true i need to actually put in the boolean value true like that anyways what i'm going to do is show you how we can actually see what the result of this is so what we can do is just say result is equal to that and then we can simply print out the results like this we actually see what the result of this request is so let's go ahead and try this now and see if it works this time okay so it says result true success true and that means the fingerbot should indeed go off so let's run this one more time and let's see so there you go the fingerbot has gone off it is indeed working and if you want to look at the other ways to control the fingerbot then what you can do is go back to that api explorer and look at the different commands however now what i'm going to show you how to do is connect the light strip and then i'm going to show you how we can control the light strip based on the weather so this is simply controlling the fingerbot now we'll do something that allows us to control a light strip that one's a little bit more fun and i almost forgot in terms of actually making this work in terms of brewing coffee or you know having some user input before it triggers the fingerbot what you could do is something like this x equals input you know press enter to brew and then you could run this program you would press enter and then after you press enter would actually run this command and then assuming you have this hooked up to the coffee machine they do come with little adhesive pads that you can use to stick it to something that's what i've done to stick it to my coffee machine then you would press enter and it would well start brewing the coffee and i'll actually show you a video of that happening right now alright so now as promised i'm going to show you how to control a light strip so i've mounted a light strip around the trim of the entrance to my closet i'll show kind of some brief photos or videos of what that looks like right now but regardless you're going to connect the light strip in the same way that you connected the fingerbot so you're going to plug it in make sure it's on and then you're going to go to the same app that we were in before you're going to go to auto scan it's going to find the light strip for you you're going to connect the light strip and then it will be added to your toya platform account so i'm going to go and connect my light strip and then i will show you how we can control it alright so you can see that my rgb strip is now connected it's associated with the main asset and this is its device id so in a similar fashion to what we did before i'm going to look at the different commands that i can send to this light strip so let's go to the get instruction set let's pass the id of this light strip and let's submit this request all right so notice these are all the different commands that we have we have switch led we have temp value we have colored data and the one that i want to look at is colored data now the reason i want to look at this one is because i want to be able to change the color of my light strip based on the temperature outside so what i'm going to do is have it so this script is actually going to figure out where i'm located determine the temperature there and then change the color of my light strip based on the temperature outside so you can customize this in any way that you want i'll just show you basically kind of how this works but notice that you actually update the temperature or the color of this led strip using hsv not rgb so hsv is i think it's hue saturation and something else although i could be completely wrong on what that stands for my editors will will pop up what hsv means on the screen in case i'm wrong there regardless we are going to use the color data code and we're going to send a json object that has hsv as the three attributes or kind of arguments in it so what i'm going to do now is i am going to go here i'm going to grab the device id and i'm going to go back to sublime text and i'm just going to remove some of this code here so i'm going to comment this out because i no longer need this and i'm going to change or make a new variable actually we'll call this light strip underscore device underscore id and i'll make this equal to the device id of my light strip now i'm going to show you how we can control the light strip which is very similar to this in fact what i can actually do is copy this code i probably should have just left it here so i'll copy this comment it out and now the code that i want to send is not going to be switch it's going to be cover in the canadian way underscore i think it was data let's go back and check that let's see here it is it is color data okay great and then for the value i want to send a json object that has h colon something comma s colon something comma v colon something and we'll fill in those values in a minute okay and then rather than the fingerbot device id i'm going to send the light strip device id okay so now i need to pick some colors that i want to send so let's just send h of like 200 s of let's just go with 100 and v will go with 100. i'm not sure what color that's going to be but we can just send this and test and see if it works all right so now just like we were able to control the fingerbot this should control the light strip for us obviously you're just gonna have to take my word for it that the light strip is working i don't have a camera like pointed at that right now that's recording so just trust me but let's run this and let's see what we get now okay so it says result true success true and the light strip has actually changed colors now one thing i'll quickly show you here is how we can turn the light strip on and off because if the light strip is off and you change the color well it's not really going to do anything right so let's do this so let's go here and let's change from color data to i think it's switch underscore led i believe that's the value and then for value here i'm going to pass true okay uh and we need a list i guess i deleted all that okay so let's fix that and now we will just send this command again and then we will change the color all right so let's run this and hopefully this time the light strip will turn on and then it will change color and yes it did perfect okay so everything is working with that now what we want to do is make it so that we change the color based on the weather and when we need to figure out the weather so in order to actually determine the weather what we first need to know is the longitude and latitude of our current location now unfortunately there's no way for us to just grab that information easily what we actually need to do in the following order is we need to get our public ip address send our public ip address to toya it will then return to us the longitude and latitude of that public ip address then we will use the longitudinal attitude to get the weather of our current area so that is what we need to do i'm going to show you how we can do that so the first thing we need to do is get our public ip now this is why we imported the request module because we're going to send a request to a website they can just tell us what this public ip is it's very simple to do this so we're going to say public underscore ip is equal to and this is going to be get and we are going to retrieve from the following website https colon slash api dot and this is ipfy and then we're going to say dot txt now this will return to you your public ip address do not share this with anyone i am not going to share my public ip address with you because well i don't want you to know where i live because obviously you can access the longitude and latitude from that next what we are going to do is we are going to set up a function i'm just going to call this get weather and this function will return to us what the weather is so i'm just going to clear these commands i'm actually going to clear this one here and just move this one down a bit so we have some space and inside of here i'm going to start writing what we need to actually get our location so let's go to the website here and let's go to our api explorer and i'm going to show you how we can find the url that we need to send the request to to get our location so notice we have this lbs here this is location based services so location based services if we press on this here it should give us the different things that we can use to get our location so in this case we want to get our location based on our ip so we'll go get ip location and then let's just send a random public ip so let's say random public ip okay random ipgenerator so let's just generate the ip okay let's grab this and let's just put this in here and let's see what this request structure looks like okay so there you go it gives us a result it gives us the continent code the country the latitude and the longitude so this is what the request will look like or sorry the result will look like and this is the endpoint that we want to send this request to and this is the format that we want to send it so i'm going to copy all of this right here and i'm going to paste this as my location underscore url so location underscore url is going to be equal to this now what i'm going to do is make this an f string and for where it says ip equals i'm going to replace this with my public underscore ip so now what we should actually be able to do is send a request to this url and get our longitude and latitude based on whatever our public ip is so what i can do now is i can grab our location so i can say that the location is equal to open api and this is going to be dot get this time not get not dot post because we're just sending a get request and we're going to send this to the location url and we're going to grab from this the result now since i don't want to print this out because i don't want you to see what my location is i'm just going to show you from here notice the first key in the result here is called result that's what we want and then inside of result we want the latitude and the longitude so what i'm going to do is i'm going to say that my lat and lawn is equal to location at and then this is latitude i think i spelt that correctly and then location at and then elongated like that now it's very possible that i spelt this wrong longitude i think it's just that actually uh let's check my spelling because i'm not very confident here okay latitude let's copy that and longitude okay looks like i spelt them correctly okay so let's paste that okay all good all right so now i have my latitude and longitude so now that i have the latitude and longitude what i want to do is get the weather so now i need to show you how we get the weather so let's go back to the api explorer this time we're going to go to our weather service not the lbs and we're going to get the current weather notice we can also get the seven day or seven get seventh weather forecast i guess that's supposed to be seven day weather forecast all right so here we can pass a latitude and longitude and it gives us the weather so let's just generate a random latitude and longitude uh okay i guess i can just put them in i can go like you know 80.00 and 80.00 okay so let's just submit that and let's see what we get all right so this is what we get we get a result that has air quality coordinate and then the current weather now we're looking for current weather and we want to know the temperature so what we're going to look for is result current weather and then temperature those are the keys that we need to access from the result and let's look at the api call here so notice this is the 2.0 api that we're calling not the 1.0 so that's important so i'm going to copy this just like we did for the previous one and now i'm going to say the weather underscore url is equal to an f string and we're going to replace the latitude and longitude numbers with lat and long from the step previously okay now that we have that we're going to again send a get request to this url so we're going to say that the weather is equal to open api dot get this is going to be our weather url and then we want to get the result we want to get the what was it i guess it's current underscore weather and then we want the temp so let's see if that looks correct here if i go here yeah we have current weather and we have tap nice okay so now this should give us the weather so what i can do is actually return the weather and i can now comment all of this out and start our program by just calling this weather function and just printing what its result is so i'm going to print get underscore weather and just see if this is accurate and if this is working all right so let's go ahead and run this and let's see what we get so we get 25.6 that is the correct temperature where i am right now maybe that gives you guys some type of hint where i live but hopefully not too much and anyways that is the weather so what i'm going to do now is just store this in a variable so weather equals get weather if this isn't working for you what you can try doing is actually printing out the location variable right here so printing out the result and seeing what it looks like the reason i'm not printing it again is because i don't want you guys to know where i live because it shows the longitude and the latitude or what the public ip address is nice so now that we have the current weather we can actually adjust the temperature or the color of the light strip based on what this weather is so what i'm going to do is copy in some temperatures copy it some temperatures copy in some colors sorry in hsv format now i just went to an hsv color picker and came up with these different variables right here these different colors you can go do that yourself or you can just copy the source code which i will link in the description of this video anyways these are some different colors that we can use so what i'm going to do is set up kind of an if else statement structure that allows me to select a color based on the current temperature so i want it to be darker if it is colder and i want it to be more light and kind of like more red if it's warmer right so dark blue will be if it's really really cold and then red will be if it's really really warm so i'm going to say color is equal to none and i'm going to say if the weather is less than negative 20 then i want the color to be equal to dark blue alif the weather is less than negative 10 then i want the color to be equal to light blue and then i'll say l if the color sorry not the color the weather is less than zero then i want the color to be equal to white l if the color i keep saying color lft weather is less than let's go with 10 then i want the color to be orange l if the weather is less than let's go with 20 then i want the color to be equal to red okay so i think i used all my colors there dark blue light blue white orange yellow red did i miss yellow i did miss yellow okay so let's do yellow as the last one l if weather is less than 30 then color equals yellow all right so really all this is going to do is just set the temperature equal to you know the appropriate colors based on what it is so if it's less than negative 20 dark blue if it's not less than negative 20 but it's less than negative 10 light blue so on and so forth you guys get the idea all right now what i'm going to do is i'm going to send a command specifically this command right here to adjust the light strip based on whatever this color is so rather than now having the value be equal to this it will be equal to this color variable and then it should just send this request and we'll adjust the temperature or color of the light show all right so now that we have this let's test this code and see if it's working so let's run it and okay so it says this is not supported okay the reason we're getting this error is because we did not convert our weather variable to a float right now it is a string and so what i need to do is convert the return value here to a float so the float of getweather so now if i run this it should work so let's go ahead and run this and when i run this it does indeed turn the light strip on and change its cover now i know you guys can't see it so you just have to trust me and believe me but if you're running this for yourself and you do have a light strip you'll see that this does indeed work right so i think with that that is going to end this tutorial and this video hopefully this gave you a general idea of how to work with the internet of things and specifically the toya iot platform thank you again to toya for sponsoring this video they really are a great company they're super flexible when they were working with me send me a bunch of different cool products to test out and really allow me to kind of experiment with the internet of things this is such a cool thing the fact that you can just write these really simple python scripts or scripts in really any language and control physical hardware devices it's also really seamless and easy to connect them to your home network as you saw in this video and really everything we did here although it might have taken a fair amount of time because i was walking through and explaining it is super simple and probably just the easiest way you really can go about actually interfacing with hardware devices so i hope you guys enjoyed if you did make sure you leave a like make sure to check out toya from the link in the description i will see you again in another youtube video [Music]
Info
Channel: Tech With Tim
Views: 39,984
Rating: undefined out of 5
Keywords: tech with tim, coffee, brew coffee, brewing coffee, tuya, internet of things, tuya platform, tuya smart iot platform, walkthrough, the internet of things, internet of things explained, what is the internet of things, iot internet of things, programming, python, coding, internet of things (iot), hardware demo, hardware, cloud projects, creating cloud projects, connecting devices, smart home, led strips, light strips, programming led strips, sdk download, controlling smart home
Id: Jj2T4TuHRRo
Channel Id: undefined
Length: 47min 20sec (2840 seconds)
Published: Sun Aug 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.