Working with Tuya Cloud API: Introduction for beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to be talking about cloud and i know this is evil resurrected but keep on watching you might learn something new [Music] hey guys i went shopping couple of days ago well i didn't go anywhere i've opened aliexpress and i found this this is a really cool e-ink display with the temperature and humidity and also luminosity sensor inside so i thought it would be perfect for my home automation what i didn't check was the interface and believing that i'm buying a zigbee version i ended up getting a bluetooth one right about the same time sour sent me a message introducing the cloud api and asking me to make a video about it so while still have to wait another 2-3 weeks for the zigbee version of this sensor to arrive so i could play with it i thought that might be a good opportunity to talk more about two-year cloud api and i know exactly what you're thinking you're not interested in cloud because well cloud is evil and the only way forward with diy automation is obviously hosting everything yourself and if you follow this channel for long enough you know that i this is exactly what i'm doing and the most of the services that i'm using are hosted on the raspberry pi on this raspberry pi to be precise behind me so why am i talking about two-year api and why i want to show you how to work with two api tuya is big and i know it might not seem like it in the west but i'm going to throw some numbers at you so they have over 324 000 developers for hardware and software that's the immense amount of people working with toya already and that means that tui can offer you right now over 310 000 different skus and yes that's a lot of different smart sockets that you can offer portuga does it in over 1 000 and 100 different categories and in terms of traffic they handle 122 millions ai voice requests a day and 84 billion cloud requests a day so we can say they're pretty big right so why am i telling you all this because well just like me you might be missing out on the really good device's hard device all right now if you want to go diy path you've got three options option one well you can limit yourself in terms of hardware design and look only at the hardware manufacturers that caters to diy users like me or you well it's not ideal if you want the latest and the greatest gadgets option two you may wait until your favorite gadget is hacked by someone more knowledgeable and well described online how to perform such arc so you could use it in your home automation that usually involves complicated process and that delay and option three is something we're gonna talk about today which is taking control of the latest and greatest devices using api and this is where tuia comes in handy because thanks to the cloud api you actually can connect to a lot of different devices whether it's via bluetooth protocol zigbee wi-fi etc and it doesn't mean that you have to switch over from your established home assistant instance on node red it can be complimentary until something better comes your way and there is a one more thing that no one from you know diy automation enthusiasts want to admit we add drop in a consumer's ocean which means it's nice to see manufacturers catering to us but we're not making them a bulk of income so if you found a device like myself that you really want to play with and it's hardly impossible to get it hacked for now or you just don't want to wait working with api might be the solution to integrate it in your home automation until you find a better way to connect to it and today i'm going to show you how to get started with api how to get started with cloud connectivity from 2ya and how to use postman to make your job much easier because tuia actually took care of a lot of things to make that process quite simple in this video i'm going to show you how to extract data from a sensor like this and also how to check the status and enable controls over fingerboards something that twiya also sent me so i could showcase you their cloud api they're cute these fingerbots from adaproxx are well small robots to press the buttons something that is very similar to switchbot robots you can check out the video in there and yeah they work over bluetooth as well they have a usb type c charging and they're quite small and cute so they're perfect to trigger small lights and switches they work over bluetooth so i also have a corresponding hub to connect these bots into the wi-fi and then to to a cloud api the hub apart from being automation station he also has a infrared blaster inside so you can use that to trigger tvs or hifi stations as for sensor i've mentioned that this is a bluetooth sensor which measures temperature humidity and luminescence you also have a really cute e-ink display which is quite responsive it has ability to respond to readings every four seconds and update the display which is quite nifty because you don't have to look at your app or mobile phone to tell what the temperature is like and thanks to the built-in luminosity you can use that as a trigger and respond to different light levels in your room enough of the talk let's get to action first you obviously have to register with tuia iot platform and you probably want to download postman postman is optional however it's a perfect tool to actually experiment with api really quickly without writing a single piece of code so i strongly recommend you to do it because you can use that knowledge to transfer it to a different cloud services and different apis that you come across in the future to make it easy i've included the links in the description of this video and additional information in my article and also linked in there also as a bonus to you i included a special link that if you use it you'll get extra balance to your developer account if you want to get more out of your account but don't worry guys in regards to your personal home automation there isn't more than enough quota to keep you going for a long time and as we're working with to your cloud api you probably want to download to your app on your phone and register your account because we're going to use that to send your devices from the app to your developer account so you can interact with them to get started with toya first obviously register your account now you can have a free trial addition and that's gonna be plenty for your use case navigate to cloud and start a new project by clicking create now i already have two and the one i'm using is home automation this is what's your basically cloud workspaces you'll be given a set of credentials with your client id and secret keep those safe because you're going to need them to really create and obtain authentication token i'll show you how to do it in a second but first let's go through a quick overview now at first you're not going to see any of the devices in here i'll show you how to add them before you do that navigate to api and enable api you can use go to authorize to do so so this is the one i have enabled by default follow that link and i'll give you options to enable them once you have them enabled you want to link your devices now you need a two-year account for this so click click devices by app and click add app account it will create a qr code open to your app on your phone and scan this code and there you have it you are authorized within seconds you should see a device list now to see it on your toy account you have to swipe switch back to tsuya apps account select your register account user id and the correct region and you'll see all the devices that are linked to this account and now you can start working with these now on this page itself you'll also see the device ids the product ids which are different for each product and some general information addition additionally you can also get standard function set and state set that is available for each device so that's a very useful in terms of obtaining information about commands and predicting what sort of data you can obtain from individual devices this is all also explained in a very elaborate developer documentation now there's a plenty of different information in here and most of it is over my head but don't worry what we actually need is well explained and located in a cloud development if you navigate to cloud development you'll also be given a quick start guide api user guide and standard instruction set which we're going to be using now for this you probably want to use postman and if you're interested just skip ahead to node red and if you don't want to learn more about how api and rest request works but i would strongly recommend you to stay with me for the postman overview because it's an excellent tool to learn how to work with apis and requests and how to make your life much easier especially that to your cloud prepared json files to work with so in a documentation and in the associated article of mine you'll find the links to download json files for postman this means that you can simply import using the import function your collection of sample requests that you can test your cloud connection with now that's the first thing that you have to import the second thing that you have to import are set of environmental and variables now these will need to be selected in here and filled in as required i already cover where to find all that information in my article as well but briefly uh if you see at the va if you look at the variable names you'll see that they are very similar to the variables uh the data you have provided on the device list and in a project overview additional information you'll also find in linked devices like your user id so fill those in as required as this is how postman works it will basically use that set of the variables to fill in the gaps provided in the collections so for example when we have for example gets the function list you'll notice that it will ask you for variable url and it will in this case get functions for all devices that start with dj in terms of their type name now in order to use that you have to select the correct environment and for this i'm using two year environment and that environment has all my details filled in to work with sdk and cloud api you need to first authenticate yourself to your does that by sending an encrypt message with your credentials to generate access token this access token is only valid for a specific amount of times usually couple of hours or more depending on this configuration and you can reset this and refresh it using refresh token action now to try out providing you filled in your url with a specific region i'm using europe you can simply send that request you'll see your access token and everything generated at the bottom and those are valid credentials should you use your use them now with your api call they will work just fine now a really nice thing that i like about postman is the fact that it will generate most of the code that you're using for your individual programming languages in here so you can select what kind of language you're working with in my case in node-red is a javascript jquery and it'll provide me with pretty much everything i need to send that request that's really handy and it's a good benefit of working with postman now that i have the access token i can start actually pulling information from my account so in order to receive that information i need to get the device list by user id i already specified the user id the ui id in my variables just make sure you're selecting them in a correct environment so all i have to do is just send that request once the request is processed you'll see the json file that i have in here and it will list all the devices i have associated with my account with some basic values statuses etc you can copy and paste that in a notepad plus for better understanding on resize this or generate that as a different text type you can see i have a light in here which is specified id so i can grab this id and i'll show you how to manually change the id for a quick lookup so let's say i want to get information about this device i'm selecting get information and you'll see that i have to provide device id i can either modify that device id or variable or simply delete this enter my device id and send the request now on send i'll receive all the information about my device only and this is information about this specific device now this information will contain information about its state information about some values that are being applied and possible different values that i can apply to this device now if i want to receive information about the device for example like a sensor that has a data points then i will use git device data points instead and it works in a very similar way in this case it provides me with the same information however if that was for example a device like a sensor it would provide me with a history of the sensor later if i want to send the commands then i would have to generate a command for the information and send it and that's being handled by post send device commands you'll see that i need device id and then commands to specify so if i paste the device id in this case and send this you will see that the command is not supported because i have not specified correct command for this action you'll see my data is for switch not for the light and i've turned it to true which is not correct standard so how do you know what to send well we have to refer back to the documentation now if you navigate to documents and then go to standard instruction set it will give you an overview of what you can send in for example lightning category you'll notice that it contains some of the information about lights if you click on the standard instruction set it will give you how to information how to handle the lights and what kind of information you should send in order to send them correctly also there will be instructions with examples that you can use so if you want to set the different values and the commands should look like that but for this tutorial i only wanted to switch it on so let's do this i'm gonna copy this this sample code it's gonna be switch led and set it to true now i have to change that data structure to reflect this i can do it in a body section of the request you'll see all i have to do is just select this delete it paste new one and send the request and now the lights are on those are located in the kitchen so we won't be able to see it but as i can see from the result and the successor it responded well now that you are more familiar with postman and how to find information in documentation and the cloud platform let's switch over to node-red and i'll show you how to recreate this in node thread so you could actually use it in your home automation don't panic i know it looks like a lot but it's actually quite simple so let's start with authentication we need to get access token and i know what your question is how do you get an access token well you need a timestamp then this is a settings node that you'll have to fill in in a section onstart you'll have to provide all the information that it asks you to but then it will use that information to actually generate the data needed to be encrypted and sent to to your cloud so in this case we're taking a client id and time once we've got this we're going to encrypt that using this node in here so if you go to manage palette you'll see that there's a crypto.js dynamic and we're using sha256 and then we make well then we use the secret and from the account from the tui account to encrypt the message and construct our header that header then is obviously set and sent via get to that url and the note on the url i'm in europe so i'm using european version if you have a different locale region just use the one that you use on your to your account now that we have all that and we send it and then there's a function node that's gonna basically create a variable that we can access later with access token and refresh token and you probably ask what is refresh token well access token has a span and if this expires then you want to send the refresh token to refresh access token and give us a new refresh token and you can do that by running this second one it's pretty much everything the same except this runs automatically once a day and refreshes the uh the data and writes new access token and the refresh token so we can process all our usual requests for data and for commands so let's get to exciting bits first we probably want a device list so again it's the same thing we need a timestamp we're going to prep our encryption encrypted and then get our request constructed this time we're going to use access token so once you have your header constructed you'll notice that we sending a message the url that requires user id which was previously set in this first prep encryption start method so this is very important that you set that user id and then once the request is done for every device that you have associated with your account i'm creating a flow variable and it's going to be accessible here so you can see i have lots of the devices in here and each one has a device id that you can use later to get device status so let's do that to get the device status again time stop encryption then we need to use the device id to specify where we're getting the status from and then compose our request we're just going to use headers and it's method get and then it uses that device id in url to get that information so when you click click for example this is for my switch sorry fingerbot you'll notice that i get fingerboard information and the status of it it's going to be in here with a different settings etc so now that i have it down that you know how to do it with all that metadata let's talk about data points what's the difference is data points are only that status section here so if you for example have a sensor and you're not interested in any of that you can simply ask for the data points and if i do that you'll notice that on my name temperature sensor i've got data points so i have a brightness i have a battery i have current temperature humidity etc so this is the difference between the data points and the status and lastly let's talk about controlling things thing so for controlling it's a very similar experience it's just uh except for the device id there is also a command that we have to set and this command just paste a json in here which you want to send and that's going to be constructed and sent as a payload uh when making your url request obviously your url request is going to be post and we're going to be using the device id so if you want to turn it was on if i remember correctly if you want to turn it off you're gonna run the second command you could hear it in the background where i run my switch to set to false and now if i gonna run my status it should respond that the actual status for this device is false so let's take a look there you have it my switch is false now if i want to turn on and run that request again you'll notice that my switch should be on and there you have it that's how you control your devices in non-thread now if you want all of that you don't want to write it yourself and then head to my website and download it at the end of the article you'll have this flow accessible for you if you're asking me it's all about flexibility i am a big supporter of having your services stored locally so you don't have to share that information with third party but i'm not strictly against using cloud after all i'm using alexa and google home devices at home so it's not like my environment is cloud free the only question that you should answer yourself is is it beneficial to your situation if the answer is yes well maybe it's not worth being stubborn about not using cloud because cloud is evil and actually taking advantage of whether it's out there to see your needs i would like to thank tuia for sponsoring this video and making the cloud api quite accessible thanks to their work and documentation it's actually quite easy to work with and wherever you're gonna learn and take out from this video can be applicable to different services so i hope you enjoyed this tutorial and if you do have any questions you can find a little bit more information in our article associated with this video link in the description of course or just ask me in the comments section as for now guys you do know that i do not have a posting schedule so if you want to follow me and find out what's next well you know how youtube works definitely not going to explain that to you but what i'm going to say i have a bunch of social media that should really check out because i post some cool stuff that is not available elsewhere so thanks so much for watching and see you next time bye [Music] you
Info
Channel: NotEnoughTECH
Views: 23,097
Rating: undefined out of 5
Keywords: NotEnoughTECH, Tuya, Cloud API, REST, Home Automation
Id: qVZJyHJsF9A
Channel Id: undefined
Length: 24min 0sec (1440 seconds)
Published: Mon Jul 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.