Integrate with Message Service

Discussing technical aspects of Yun development products, including IoT Core and other cloud service APIs, data analysis products, etc.


Post Reply
developer.newbee
Posts: 8

Tuya's message service is customized and modified based on open source Pulsar. The Pulsar SDK for Java is provided. This topic describes how to use Tuya's Pulsar SDK to receive messages.

Prerequisite

  • Download the GitHub demo: Pulsar SDK.
  • Install the development environment: IntelliJ IDEA.
  • Download the Smart Life app to receive the test messages.
  • At least one cloud project is created. For more information, see Manage Projects.
  • Prepare a Tuya-enabled smart device, such as a smart light, and link the device with your cloud project. For more information, see Link Devices.

Step 1: Set up a development environment

1. Open the source code project with IntelliJ IDEA. Click Open to find the downloaded and decompressed file in the previous step.

Image

2. Modify parameters in open-mq-sdk/src/main/java/com.tuya.open.sdk/mq/MqConsumer.java and modify the Java project as a test environment. For more information about the parameters, see mq/MqEnv.java.

Code: Select all

   private MqEnv    env = MqEnv.TEST;

:::info

  • MqEnv.TEST represents the test environment.
  • MqEnv.PROD represents the production environment.

Image

3. Modify parameters in open-mq-sdk/src/main/java/com.tuya.open.sdk/example/ConsumerExample.java.

Code: Select all

   String url = MqConfigs.CN_SERVER_URL;
   String accessId = "xt*****ff**n1****8ufo";
   String accessKey = "479bcb7345******582d9c******4ef7";
ParameterDescription
urlThe endpoint. Choose the endpoints according to your location.<ul><li> CN_SERVER_URL: China, pulsar+ssl://mqe.tuyacn.com:7285/</li><li> US_SERVER_URL: America, pulsar+ssl://mqe.tuyaus.com:7285/</li><li> EU_SERVER_URL: Europe, pulsar+ssl://mqe.tuyaeu.com:7285/</li><li> IND_SERVER_URL: India, pulsar+ssl://mqe.tuyain.com:7285/</li></ul>For more information about the parameters, see mq/MqConfigs.java.
accessIdEnter the value of Access ID in the Authorization Key section of the cloud project on the Tuya IoT Development Platform.
accessKeyEnter the value of Access Secret in the Authorization Key section of the cloud project on the Tuya IoT Development Platform.

Image

Step 2: Receive messages in the test environment

1. Configure a test device.

  1. On the Message Service page of the cloud project, enable the message service.


    Image

  2. After the subscription status shows Enabled, click the Test Environment tab.

    Image

  3. Enter the linked device ID in the search box, click the magnifier icon, and then double-click the selected device.

    Image

  4. If a linked device is specified as a test device, the test device will report messages to the test channel for you to debug device features.

2. On the IntelliJ IDEA, right-click the ConsumerExample.java file and select Run to run the program.

Image

3. Control the device with the Smart Life app.

  1. Turn on the test light.
  2. Adjust the brightness of the light.

4. Automatically get the push messages on IntelliJ IDEA.

Image

Step 3: Configure the production environment message push rule

For details, please visit Manage Message Service.

Step 4: Parse the messages

You can parse the messages you received in the following format. For more information about message types, see Message Types.

Parameter nameData typeDescription
protocolIntegerThe protocol number.
pvStringThe version of the specified communication protocol.
tLongThe timestamp.
signStringThe signature.
dataStringThe data body.

Code: Select all

{
   "protocol": 4,
   "pv": "2.0",
   "t": 146052438362,
   "data":"4FDEE3FE59Fxxxx***",
   "sign": "58285279b5b5790c7d917dxxxxx***"
}

Best practice

After you use Tuya's Pulsar SDK for Java to receive messages, you can further explore:

Troubleshooting

Failed to authenticate

  • Cause analysis: This error occurs because the subscriber cannot be identified.

  • Solution:

    • If the ACCESS_ID or ACCESS_KEY is incorrect, you can check whether the ACCESS_ID key pair configured for the cloud project above is correct.

    • If you have not subscribed to the message service for the region the client is connected to, see Message Service.

      Image

Subscription does not exist

  • Cause analysis: This error occurs because the subscription information does not exist.
  • Solution: Go to the Tuya IoT Development Platform to check whether the subscription name exists. Tuya provides a subscription name by default, and the naming rule is {AccessId}-sub. For example, 57e9trwr********s38-sub. However, you need to go to the Tuya IoT Development Platform to create a custom subscription name. For more information about specific steps, see Enable Message Service.

Image

Subscription is of different type

  • Full error message: Failed to subscribe 57e9trwr********s38/out/event with 11 partitions Subscription is of different type.

  • Cause analysis: This error occurs because a consumer of another subscription type already exists. Consumers of multiple subscription types cannot coexist.

  • Solution: To modify the subscription type of a consumer, you must close all consumers and start them again, or change a subscription name.

Image

Post Reply