-
1Creating Product on TUYA
I am going to use the Tuya IoT platform to develop a smart device from scratch. The simplicity and fast response time of the Tuya IoT cloud are amazing, all you need to do is set up function definitions, design app panel, and burn authorization firmware, and your device is ready to go with Tuya Android and iOS app.
On the Tuya cloud development platform, the first thing you need to do is register using your email and create your free account. The free trial versions with a limited time period are available if you’re interested in just experiencing the platform for now. Follow these simple steps.
Cloud > Development > Create Cloud Project
Create ➡ Can’t find the category?
Enter the required product information:
Protocol ➡ Wi-Fi
Power Type ➡ Standard Power Consumption
Create custom function definitions for your sensors with the below-shown data type and properties. Select the features you want to add to your product from Standard functions or create new ones (Custom) if not supported by Standard functions. Standard functions indicate the functions provided by Tuya for their product categories. Since there are lots of built-in products, it is easy to find the functions you need. Also, you can create your specialized functions named Custom functions, depending on your project.
The data point (DP) is the abstract representation of a function, and each function has an ID and data type.
Data Point ID (DP ID): indicates the code of a data point (function). Tuya Cloud sends or receives data through data point IDs. I created four custom DP for PIR, Gas, Temp, and humidity sensors and one motor controlling DP for controlling Gas knob (you can add more functions such as timer, lighting, sound, and many more).
If you wish to add some of your own DPs and customize your project, Tuya can do that easily by customizing the DPs by clicking edit and add DP.
Select standard Tuya ESP8266 module SDK in the hardware development section. Even if you don’t, it’s okay. If you have a Standard TUYA module SDK, select the appropriate one or place an order, Tuya will deliver it.
-
2Designing our App Panel Interface
Making our own App Panel Interface is amazing, but you should look at the pre-designed App panel that Tuya provides. According to your project and selected DP, it will automatically show you a catchy App panel designs list. If you wish to make your own, then select the blank panel.
You would be able to see all your DP UI elements which you can modify. In fact, there are lots of UI available for you to drag-and-drop and make a fantastic panel. Once it’s ready, click on the release button. I have also added chart and log elements for greater data summarization.
Once your panel is released, you should see and use it by scanning through the Tuya IoT app on mobile.
Authorizing ESP8266 with Tuya IoT firmware
To use the NodeMCU ESP8266 as a Tuya product for this project, you need to flash and authorize it with the required firmware. To authorize the ESP8266, you have to send the product ID (PID) and your Tuya IoT account name (e-mail) to devops@tuya.com. After a while, they will send you a Token ID. Then, follow the steps below:
First, go to the Tuya PMS service page https://pms.tuya.com/en/login .and sign up for an account if you don’t have one. Then, go to
Production Manage ➡Work Order Management ➡ Activation Code Verification
To activate the production certificate for the product, enter the Token ID. When the Token ID is confirmed, the ESP8266 is ready to be flashed and authorized. Remember, only once ID can authorize from a single account. To check your activated Tokens, go to the Activation Code List in the Production Manage -> Work Order Management list. You can download the cloud auth token burning application from the PMS console Software Download section or Download installations package on the Google drive https://drive.google.com/file/d/1SF-rM5qDLaPJiuMHrkqFauVyRXSSHMkj/view?usp=sharing from Tuya ESP8266 guide (link in the reference section) and log in with your PMS account.
You should keep in mind a few steps while setting up TYDA software; you may face the same problem that I faced and got them corrected.
- When you download your Zip file, make sure you extract it on the desktop by creating a separate folder. Don’t try to extract it in a program file in Windows C. You can extract it anywhere but try to keep it on the desktop.
- After extracting, go to the TYDA application, run it as administrator and install the software.
- After Installing, open TYDA and log in using your PMS account details with the same email and password, or else it will show an error.
- If it’s in Chinese till login, you click the first option, and after entering TYDA, select the language change option as shown.
Solution-
Set the baud rate and test baud as shown in the above image. Also, click on Enter Token button and enter the token you got from the Tuya devops@tuya.com email reply.
Connect the NodeMCU V3 LoLin ESP8266 to the computer via a USB cable and select its port number, such as COM5. If you did not use it with the Arduino IDE or other compilers before, you might need to install its driver. Finally, click on flash to burn auth tokens. You would see a green box with success if nothing breaks.
-
3Programming and flashing Arduino
I will make this part simpler by going into details of the Tuya IoT Arduino library. First, import all required libraries to work with sensors and ESP8266 over Serial. Setup Tuya Serial connection
1. DHT11 Library: Download
2. MQ-135 Library: Download
3. Adafruit SSD1306 Library: Download
Start with defining and adding the library.
Define and store all DP ids we made on the Tuya IoT platform while creating our new device.
Define all the variables
Fill your PID from Tuya IoT console and firmware version like shown below,
In void setup( ) let us initialize all callbacks to Tuya IoT cloud,
In the loop part, we need to start the UART service to allow Arduino to receive and send data to ESP8266 over serial, and ESP8266 sends it to Tuya IoT cloud.
First, check if the device is in network mode or not and connected to the cloud; you would be able to see the status via led; if led is stable, it means it is connected to the cloud. If blinking, it means in connection mode.
Now, implement your program logic to send data to Tuya IoT cloud and App Panelmcu_dp_update(DPID, value, 1); send your data values like this if the device is connected to the cloud.
Set sensors data and send to Tuya cloud
We also have the dp_process() function but it is to be used when you want to receive commands and take control over Arduino pins, like controlling actuators, just implement a switch case and do required actions based on triggered char did value. Check the previous tutorial here on the usage of dp_process(),
At last update all values, it ensures that all your data values are updated continuously; you can try skipping mcu_dp_update(DPID, value, 1) in void loop() since we already got a dp_update_all() function running continuously to update all values but make sure those variables are global.
APP Panel Interface setup on the phone
Reference Links:
- Join Arduino Beta Test: https://pages.tuya.com/develop/ArduinoBetaTest_discord?_source=cb5548a28033aee24543cdb4f47a8b61
- Tuya Saas Development Framework: https://developer.tuya.com/en/saas-development-framework?_source=19ea467a23656a5cb2f00ca04c47c11e
- Tuya Developer Platform: https://auth.tuya.com/?from=https%3A%2F%2Fiot.tuya.com%2Fopen%2F%3Fcode%3D1234%26_source%3D7249ab79fc1429854d902ebd585699d9
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.