Tuya Smart: Smart Home Automation

Tuya Smart: Smart Home Automation © CC BY-NC-SA

N this tutorial, I will share how to set up and use a smart switch using the Tuya IoT platform.

  • 499 views
  • 0 comments
  • 3 respects

Components and supplies

Apps and online services

About this project

In search of new IoT platforms similar to Blynk and RemoteMe, I stumbled upon Tuya. An IoT platform for industrial products. In this tutorial, I will share how to set up and use a smart switch using the Tuya IoT platform. So without any further delay, let's get started.

Supplies

Hardware Requirements:

Software Requirement:

Making a Device on Tuya.

First things first, we need to create an account on Tuya’s IoT platform. To do that, head over to Tuya.comand sign up to create your account. Once you have created an account, log into it and follow the steps given below.

1. You will be greeted by an overview page. On the top, you will notice a “Create” button. This click on it to create a new device.

2. Then in the Standard category, select Electrical and then select Switch. As we are making a smart switch here.

3. Now select the Custom solution and click on the available Switch.

4. Now fill in the product information.

  • First, give your product a unique name.
  • The product model is optional. You can leave it empty.
  • The protocol is the communication method. As we are using ESP8266, select WiFi (WiFi and Bluetooth for ESP32).

5. As soon as you click create, a popup will appear for selecting switch functions. Here you can select the number of switches you need for your application. As we are going to toggle only one LED/Relay, select one switch and click ok.

6. You will notice 6 tabs namely Function Definition, Device Panel, Hardware Development, Product Configuration, Device Debugging, and Testing Service.We will skip the device panel for now as we will be using Tuya’s smartphone app to make the panel.

7. Moving further, you first need to get a token to activate your device, to do that copy the PID and send it to dev@tuya.com. Make sure you send the mail from the email account you used to register with the Tuya platform.

With that being done, all the device setup is now ready. It’s now time to activate the device through Tuya PMS.

Activation on Tuya PMS.

You will receive a token from the developers from Tuya. Copy this token, and head over to the Tuya PMS site, and create an account there. Once an account is created, log in to your account.

Next on the Sidebar go to Production Management >> Work Order Management >> Activation Code Verification. Paste the token here and click on Confirm.

You will get a popup of successful activation like follows.

Now download the TYDA appfor burning the Firmware.

Burning Firmware on ESP8266.

Now it’s time to work with the code. But don’t worry, you don’t have to be an expert at it. Follow these steps and you should be good to go

1. Download and unzip the Tuya burning authorization package. You will find the following two files in it.

2. First, install the CP210x driver as it will allow your ESP boards to communicate with your PC. Then install the TYDA application. Just like any software, follow the onscreen instructions.

3. After successfully installing the TYDA app, launch it and you will be greeted with a home screen that might be in Chinese. But don’t worry, it is very easy to switch languages. Refer to the image below.

4. Now that the app is set in English, first, go to File >> Setup and check of the baud rates are set as below.

5. After that, click on the Enter token button. Here make sure that the download firmware box is checked and in the workstation dropdown, you have selected “Burning Authorization”.

Finally, enter the token received over mail and click ok.

6. The next step is to connect your ESP8266 board (in this case Wemos D1 mini) to the computer using a USB cable. Now click on the COM port menu and select the COM port available for your ESP.

If you have multiple COM ports and don’t know which one to use, just disconnect the ESP board and restart the TYDA app. Now check the list of COM ports and then connect the ESP board to see which new port appears. This will be the Port you need to burn firmware.

7. Click on the Run button on the top right. And wait for a few seconds. This might take a couple of minutes sometimes. Do not unplug the board from your computer until you get a success message.

After a successful burning process, you should see the Yellow colour of the TYDA app turn green indicating the burning process has been completed. Now unplug your ESP and move to the Arduino.

Programming the Arduino.

Now comes the Arduino part, many of you might be familiar with the Arduino IDE, but for beginners, Arduino IDE is software used to develop and burn Codes into an Arduino microcontroller.

1. Download and install Arduino IDEif you don’t have it already

2. After installing the IDE, you need to install a library by Tuya. It is very easy to install a library in Arduino IDE. First, open the IDE and click on Tools >> Manage Libraries. This will open a pop-up window, in this window type in the search bar “Tuya” and hit enter.

Install the library marked above. This will take just a few seconds, after it is installed, close the window and restart the IDE.

3. Now click on File >> Example >> Tuya_WiFi_MCU_SDK >> Start. This will open a pre-built sketch.

4. Next, connect your Arduino UNO to the PC. If you are using a clone board, make sure you install the CH341 USB driver. Then click on Tools and make sure you have selected the right port.

5. When the port is selected, you can now click on the upload button on the top left side below the menu indicated by the right arrow. Wait patiently as the uploading process completes, after successful upload, you will see the message “uploaded” at the bottom left.

With that, the Arduino is programmed and ready to be used. The next step is to make the right connections

Connections.

The connections for this project are very simple. All you will need is 4 male-to-male jumper wires, ESP8266, and Arduino UNO. Follow the connections given in the diagram below. But make sure you disconnect the boards before.

After the connections are made, let’s proceed to the app which will control the LED.

Making the App.

First head over to the Play Storeor App Storeand download the Tuya Smart app. After the app is installed you should be greeted with the following screen.

1. Click on Add Device button. This will take you to the next page wherewe will create a switch. Here click on Add Device button.

2. In the switches section, Select Switch(Wi-Fi).

3. Next, enter your WiFi credentials here. Make sure it is a 2.4GHz network. 5Ghz does NOT work.

4. Now for this step, Power the Arduino and connect Pin 7 to GND for 3-4Sec.This will put the board in pairing mode. You will see the onboard LED blink rapidly. Now click on the“Confirm the indicator is blinking” Radio button and click on Next.Now for this step, Power the Arduino and connect Pin 7 to GND for 3-4Sec. This will put the board in pairing mode. You will see the onboard LED blink rapidly. Now click on "Next".

5. Now the device adding will take a couple of minutes. So wait patiently for the process to complete. Once the connection is established. Click on Done.

6. The final look of the app will be like this. You can toggle the LED using the button at the bottom.

Step 7: Testing and Conclusion.

The device works seamlessly without any lags or delays. What you can do now is replace the LED with a relay to control appliances.

In the future, I will share more projects based on this amazing platform and maybe design a commercial product with it. If you like this tutorial, check more projects on ProjectHub.in

If you are looking for custom projects, product design, PCB design, manufacturing, or sourcing. Contact us at Contact@projecthub.in

Code

Arduino SketchArduino
/*
 * @FileName: start.ino
 * @Author: Tuya
 * @Email: 
 * @LastEditors: Tuya
 * @Date: 2021-04-10 11:24:27
 * @LastEditTime: 2021-04-28 19:48:31
 * @Copyright: HANGZHOU TUYA INFORMATION TECHNOLOGY CO.,LTD
 * @Company: http://www.tuya.com
 * @Description: This demo is based on the Arduino UNO, and the LEDs on the UNO board are controlled by the Tuya Smart App. 
 *               Enter network connection mode when Pin7 to GND.
 * @Github:https://github.com/tuya/tuya-wifi-mcu-sdk-arduino-library
 */

#include <TuyaWifi.h>
#include <SoftwareSerial.h>

TuyaWifi my_device;


/* Current LED status */
unsigned char led_state = 0;
/* Connect network button pin */
int key_pin = 7;

/* Data point define */
#define DPID_SWITCH 20

/* Stores all DPs and their types. PS: array[][0]:dpid, array[][1]:dp type. 
 *                                     dp type(TuyaDefs.h) : DP_TYPE_RAW, DP_TYPE_BOOL, DP_TYPE_VALUE, DP_TYPE_STRING, DP_TYPE_ENUM, DP_TYPE_BITMAP
*/
unsigned char dp_array[][2] =
{
  {DPID_SWITCH, DP_TYPE_BOOL},
};

unsigned char pid[] = {"ma67l9sgmdyg3d2k"};
unsigned char mcu_ver[] = {"1.0.0"};

/* last time */
unsigned long last_time = 0;

void setup() 
{
  // Serial.begin(9600);
  Serial.begin(9600);

  //Initialize led port, turn off led.
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

  //Initialize networking keys.
  pinMode(key_pin, INPUT_PULLUP);

  //Enter the PID and MCU software version
  my_device.init(pid, mcu_ver);
  //incoming all DPs and their types array, DP numbers
  my_device.set_dp_cmd_total(dp_array, 1);
  //register DP download processing callback function
  my_device.dp_process_func_register(dp_process);
  //register upload all DP callback function
  my_device.dp_update_all_func_register(dp_update_all);

  last_time = millis();
}

void loop() 
{
  my_device.uart_service();

  //Enter the connection network mode when Pin7 is pressed.
  if (digitalRead(key_pin) == LOW) {
    delay(80);
    if (digitalRead(key_pin) == LOW) {
      my_device.mcu_set_wifi_mode(SMART_CONFIG);
    }
  }
  /* LED blinks when network is being connected */
  if ((my_device.mcu_get_wifi_work_state() != WIFI_LOW_POWER) && (my_device.mcu_get_wifi_work_state() != WIFI_CONN_CLOUD) && (my_device.mcu_get_wifi_work_state() != WIFI_SATE_UNKNOW)) {
    if (millis()- last_time >= 500) {
      last_time = millis();

      if (led_state == LOW) {
        led_state = HIGH;
      } else {
        led_state = LOW;
      }
      digitalWrite(LED_BUILTIN, led_state);
    }
  }

  delay(10);
}

/**
 * @description: DP download callback function.
 * @param {unsigned char} dpid
 * @param {const unsigned char} value
 * @param {unsigned short} length
 * @return {unsigned char}
 */
unsigned char dp_process(unsigned char dpid,const unsigned char value[], unsigned short length)
{
  switch(dpid) {
    case DPID_SWITCH:
      led_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (led_state) {
        //Turn on
        digitalWrite(LED_BUILTIN, HIGH);
      } else {
        //Turn off
        digitalWrite(LED_BUILTIN, LOW);
      }
      //Status changes should be reported.
      my_device.mcu_dp_update(dpid, value, length);
    break;

    default:break;
  }
  return SUCCESS;
}

/**
 * @description: Upload all DP status of the current device.
 * @param {*}
 * @return {*}
 */
void dp_update_all(void)
{
  my_device.mcu_dp_update(DPID_SWITCH, led_state, 1);
}

Schematics

Connections
Circuit eu2xmgo5ng

Comments

Similar projects you might like

Discover how to create an IoT table light with Tuya Smart

Project showcase by Diego Moreira

  • 3,332 views
  • 3 comments
  • 2 respects

DIY Smart Plug and Bulb on Tuya IoT | Adaprox Fingerbot

Project tutorial by Akarsh Agarwal and Common9899

  • 2,830 views
  • 0 comments
  • 5 respects

Octopod: Smart IoT Home/Industry Automation Project

Project tutorial by Saksham Bhutani

  • 19,639 views
  • 12 comments
  • 56 respects

Smart Home Automation

by Team Kamakshi's Smart Team

  • 10,757 views
  • 2 comments
  • 20 respects

Arduino-Powered Smart Light (Works with Amazon Echo)

Project tutorial by Tinker Project

  • 34,413 views
  • 8 comments
  • 52 respects

IOT - Smart Jar Using ESP8266, Arduino and Ultrasonic Sensor

Project tutorial by Team Smazee

  • 18,369 views
  • 5 comments
  • 21 respects
Add projectSign up / Login