English
English
简体中文
Contact Us
Register
Log In
layoutIndex

Device Control

Last Updated on : 2022-03-03 06:47:03

Smart devices have gained ground in many sites and industries, but how to deliver secure and flexible permission management has become a challenge. In the old days, application scenarios are pretty simple, and a mobile app was used to control only one smart device. However, nowadays, more and more IoT devices are serving smart scenes. Each mobile app is used to control a host of smart devices.

In specific scenarios, certain devices are controlled by multiple users. For example, furniture-like smart power strips can be switched on or off by all site members. In this case, the following requirements must be met: a mobile app can be used to control multiple devices. Multiple users can be authorized to mutually control multiple devices. Therefore, group management and smart scenes are designed to achieve these purposes.

This topic describes how to achieve device control.

Device DPs

  • The dps property of NSDictionary type for the class TuyaSmartDeviceModel defines the current device status. The status is known as one or more data points (DPs).

  • In each dictionary of dps, key matches dpId of a DP, and dpValue matches the value of the DP.

    You can check the definitions of DPs for a product on the Tuya IoT Development Platform. The following figure shows a list of DPs.

    Device Control

    For more information, see Product Functions.

Command format

Send a control command to a device in the following format:

{
	"(dpId)":"(dpValue)"
}

The following sample code shows the DPs 1, 4, 5, 6, and 15 of a light product that is created on the Tuya IoT Development Platform:

ObjC:

- (void)publishDps {
  // self.device = [TuyaSmartDevice deviceWithDeviceId:@"your_device_id"];

    NSDictionary *dps;
    // The DP of Boolean type with the `dpId` value of `1`. Command: switches on the light.
    dps = @{@"1": @(YES)};

        // The DP of String type with the `dpId` value of `4`. Command: sets the red, green, and blue (RGB) value to `ff5500`.
    dps = @{@"4": @"ff5500"};

        // The DP of Enumeration type with the `dpId` value of `5`. Command: sets the lighting level to `2`.
    dps = @{@"5": @"2"};

        // The DP of Value type with the `dpId` value of `6`. Command: sets the temperature value to `20`.
    dps = @{@"6": @(20)};

        // The DP of Raw type with the `dpId` value of `15`. Command: passes through the infrared data `1122`.
    dps = @{@"15": @"1122"};

        // Sends multiple DPs in a command.
    dps = @{@"1": @(YES), @"4": @(ff5500)};

    [self.device publishDps:dps success:^{
        NSLog(@"publishDps success");

                //The DP is sent and the callback of status reporting is implemented with the `deviceDpsUpdate` method.

    } failure:^(NSError *error) {
        NSLog(@"publishDps failure: %@", error);
    }];

}

Swift:

func publishDps() {
    var dps = [String : Any]()

    // For more information about the DPs, see the function definitions of the product.
    device?.publishDps(dps, success: {
         print("publishDps success")

                //The DP is sent and the callback of status reporting is implemented with the `deviceDpsUpdate` method.
    }, failure: { (error) in
        if let e = error {
            print("publishDps failure: \(e)")
        }
    })
}

Data types must be correct when DPs are sent. For example,

  • a DP of Value type can be sent in the format of {“2”: 25} rather than {“2”: “25”}.
  • A byte array of Raw type is a hexadecimal string with an even number of digits. In this case, the array can be sent in the format of {“1”: “011f”} rather than {“1”: “11f”}.

Device control

Sends DPs to a device to change the device status or features and control the device.

API description

Devices can be controlled automatically, over a local area network (LAN), or in the cloud. If the LAN is connected, control over the LAN is recommended. Otherwise, control in the cloud is recommended.

  • Control over the LAN:

    [self.device publishDps:dps mode:TYDevicePublishModeLocal success:^{
        NSLog(@"publishDps success");
    } failure:^(NSError *error) {
        NSLog(@"publishDps failure: %@", error);
    }];
    
  • Control in the cloud:

    [self.device publishDps:dps mode:TYDevicePublishModeInternet success:^{
        NSLog(@"publishDps success");
    } failure:^(NSError *error) {
        NSLog(@"publishDps failure: %@", error);
    }];
    
  • Automatic control:

    [self.device publishDps:dps mode:TYDevicePublishModeAuto success:^{
        NSLog(@"publishDps success");
    } failure:^(NSError *error) {
        NSLog(@"publishDps failure: %@", error);
    }];
    

This section contains the following topics to help you quickly understand related concepts and how-to guides: