Tuya IoT with ESP8266 & Arduino

TUYA WITH ESP8266

In this project, we are going to make an IoT-connected device using the tuya IoT platform and ESP8266 module.

What is Tuya IoT

Tuya is a leading global IoT Cloud Platform that connects the intelligent needs of brands, OEMs, developers, and retail chains. The platform provides developers with a one-stop IoT PaaS-level solution that contains hardware development tools, global cloud services, and smart business platform development, Tuya offering comprehensive ecosystem empowerment from technology to marketing channels to build the world’s leading IoT Cloud Platform.

Read more

Hardware required

ComponentsAmazonamazonAliExpressali expressBanggoodbanggood
Arduino
ESP8266
Jumper Wire
Bread Board
Affiliate links

Softwares required

SoftwaresDownload
Arduino IDEdownload icon
Tuya firmware burnerdownload icon
Tuya Smart app (Android)download icon
Tuya Smart app (IOS)download icon

Accounts needed

WebsiteSignup Link
Tuya Smart PMShttps://pms.tuya.com/en/reg
Tuya IoThttps://auth.tuya.com/register

Let’s build it!

Step 1: Create a product and get the PID

  • Signup or login to tuya IoT from https://iot.tuya.com/
  • Then click on “Product” and choose “Development“.
  • From “Product” page, click on the “Create” button.
  • Now click on the “Lighting” tab and then select “Light Source”.
  • Then select “Custom Solution” and choose “Light Source“.
  • Now enter a name for your product, check the option “WI-FI” under protocols.
  • After this process, the system will ask you to configure the default device control functions. Leave these 3 functions as default and click ok.
  • Copy and save your PID.

Step 2: Getting and Activating token in smart PMS

  • Send the PID of the device we just created in the tuya IoT platform and the Tuya Account email ID to devops@tuya.com. 
  • After some time(may be take some long hours) you will receive your token code in reply to the email.
  • Now Signup or login into pms.tuya.com.
  • After login click on Production Manage -> Work order Manage -> Activation Code.
  • Now paste the Token we received in the email to the form in “Activation Code” page and click confirm.
  • Now a popup will appear saying “Production certification confirmation“.
  • That’s it now the Token is activated.

Step 3: Burning authorizations for ESP8266

  1. Download and install the Tuya firmware burner from here.
  2. Then login with using your PMS account detail and then click Online Login. You can choose Automatic Login and Remember The Password if needed.
  3. Then from the menu File -> Setup under Basic Settings you must set up Burning Baud to 921600 and click Ok.
  4. Now click Enter Token and select Token from the Popup window.
  5. Paste your Token ID to the provided field, select Firmware Download check box and from the Select work station dropdown list select Burning Authorization.
  6. Click OK.
  7. Now connect the ESP8266 board to your computer and choose the right com port.
  8. Now click RUN. The burning process will take few seconds, wait for it to finish and a “Success” message will be displayed.

Circuit

Follow the below circuit diagram to connect your ESP8266 with Arduino.

Note: Here I’m using NodeMUC but any other ESP8266 development boards will work

tuya iot esp8266 arduino
ESP8266ArduinoMomentary Push Button
TXRX
RXTX
GNDGNDterminal 1
Digital Pin 7terminal 2
VIN5v

Arduino Programing

For making things easier for beginners in this tutorial we are going to use an example code provided by the TUYA_WIFI_MCU_SDK library for Arduino.

Sign up and join the Tuya Developer Arduino Beta Test

Installing the TUYA_WIFI_MCU_SDK library

  1. Open your Arduino IDE.
  2. From menus go to Sketch -> Include library -> Manage Libraries.
  3. When the dialog box apears search for TUYA_WIFI_MCU_SDK & click install.

We need to change the PID given in the example code to the PID of our device which we created from the tuya IoT platform. Find the below line in the code and change the PID & also we need to change the MCU version.

Note: Check your MCU version in the tuya firmware burning app.

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

Note: While uploading the code remove the RX connection from Arduino.

After making the changes upload the code to your Arduino, don’t forget to choose the right board and port. If you are using Arduino Nano also choose the right bootloader version