AliyunIoTSDK library usage problem and alicloud error code solution

TSL model of this example:

  "schema": "",
  "profile": {
    "version": "1.0",
    "productKey": "a10rLI27mdn"
  "properties": [],
  "events": [
      "identifier": "testid",
      "name": "test",
      "type": "info",
      "required": false,
      "desc": "Event test",
      "method": "",
      "outputData": [
          "identifier": "outputid",
          "name": "output",
          "dataType": {
            "type": "bool",
            "specs": {
              "0": "shut",
              "1": "open"
  "services": [],
  "functionBlockId": "001",
  "functionBlockName": "Event 01"

Code applied in this example:

#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include <AliyunIoTSDK.h>

AliyunIoTSDK iot;
static WiFiClient espClient;

#define PRODUCT_KEY "xxxxxxx"
#define DEVICE_NAME "xxxxxxx"
#define DEVICE_SECRET "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
#define REGION_ID "cn-shanghai"

#define WIFI_SSID "xxxxxxx"
#define WIFI_PASSWD "xxxxxxx"

void wifiInit(const char *ssid, const char *passphrase)
    WiFi.begin(ssid, passphrase);
    WiFi.setAutoConnect (true);
    WiFi.setAutoReconnect (true);
    while (WiFi.status() != WL_CONNECTED)
        Serial.println("WiFi not Connect");
    Serial.println("Connected to AP");

void setup()
    // Binding property callback
    AliyunIoTSDK::bindData("testid", powerCallback);
unsigned long lastMsMain = 0;
void loop()
	if (millis() - lastMsMain >= 5000)
        lastMsMain = millis();
        // Send events to alicloud platform

1, Problems encountered and Solutions

1. The prompt does not use json format

  • After pinching a json data with the < arduinojson. H > library, pass the generated variable a to AliyunIoTSDK::sendEvent(); In the second parameter of this function, the method is as follows:
    StaticJsonDocument<200> doc;//Create a hidden json document called doc (because it doesn't need to be passed into the final function)
    StaticJsonDocument<200> doc1;//Create another json document because json data needs to be nested at multiple levels
    doc1["id"] = "123";
    doc1["version"] = "1.0";
    doc1["method"] = "";
    JsonObject myO = doc.createNestedObject("Power"); //Add an object node
    myO["value"] = "on";
    JsonObject myO1 = doc.createNestedObject("Power1"); //Add an object node
    myO1["value1"] = "23.6";
    JsonObject myO2 = doc1.createNestedObject("params");//Add an object node
    myO2["power"] = myO;
    myO2["wf"] = myO1;
    void a ;
    serializeJsonPretty(doc1, a);

Results the compilation result indicates that the data type is wrong

  • Therefore, by referring to the definition of variable types in the function in the < aliyuniotsdk. H >, it is found that the data type of the second parameter of the function is string:
  * Send event to cloud platform (with data)
  * @param eventId Event name, defined in Alibaba cloud object model
  * @param param json data in string form, for example, {"${key}":"${value}"}
  static void sendEvent(const char *eventId, const char *param);
  • So a char a is defined
    AliyunIoTSDK::sendEvent("testid",a);—— Unsuccessful
  • Define a const char *a
    AliyunIoTSDK::sendEvent("testid",a);—— Unsuccessful
  • The parameter is directly changed to doc1
    AliyunIoTSDK::sendEvent("testid",doc1);—— Unsuccessful
  • Consider using the SDK, which is too complicated -- abandon the research on json data types for the time being (we'll study it later)
  • The next day, I decided not to pass variables, and directly escaped the second parameter of the function into a string

2. Alibaba cloud 5096 error

Reasons for errors given in Alibaba cloud official documents:

There is no event defined in the object model, or the event input does not match the definition.
Note: if it is an event in the custom object model module, it is necessary to splice the custom module identifier in the format of {tsl.functionBlockId}:{tsl.event.identifier}.

  • So splice module id and event id
    AliyunIoTSDK::sendEvent("001: testid", "output": "0" "); -- 5096 problem solving

3. Alibaba cloud 6304 error

Reasons for errors given in Alibaba cloud official documents:

During TSL verification, the passed in parameter does not exist in the structure.

  • Add value (prompt that the output parameter does not exist)
    AliyunIoTSDK::sendEvent("001: testid", "001: testid", "value": {"output": "0"} "); - unsuccessful

  • Add spaces
    AliyunIoTSDK::sendEvent("001: testid", "001: testid", "value": {"output": "0"} "); - unsuccessful

  • Change the "0" string to a numeric value
    AliyunIoTSDK::sendEvent("001: testid", "001: testid", "value": {"output": \ 0} "); - unsuccessful

  • Change the output parameter name to the output parameter identifier


2, Summary

  • Learn to look up header files
  • Learn to check official documents and consider the meaning of documents word by word and sentence by sentence
  • The reported json data should correspond to the key value. The key is the identifier in the TSL object model, and the value should correspond to the set data type
  • You don't need to add the value key. You can have the real parameter key value
  • The second function parameter does not need to be added with {"id": "123", "version": "1.0", "parameters": "method": "thing. Event. 001: tested. Post"}. AliyunIoTSDK library will automatically add it (6304 indicates the possibility that the method does not exist)

Tags: IoT Alibaba Cloud

Posted on Tue, 09 Nov 2021 18:50:39 -0500 by noisyscanner