Device Control

Last Updated on : 2022-03-03 07:19:18download

With the popularity of IoT devices, how to safely and flexibly manage the control permissions of the devices has become more complicated. In the past simple application scenarios, the control APP only needs to control one device. However, as the Internet of Things devices owned by the family become more abundant, the control APP needs to control multiple devices at the same time. In addition, some terminal devices also need to be controlled by multiple people.

For example, smart power strips can be turned on or off by all family members. Therefore, permission management problems arise. One APP can control multiple devices, or multiple users can control multiple devices with each other. As a result, concepts such as group management and smart scenes emerged.

This topic introduces the content about device control.

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).
Each key in the dps dictionary refers to a dpId of a DP, and Value refers to the dpValue of the DP. The dpValue is the value of the DP.

Refer to the DPs of the product in Tuya IoT platform > Product > Development > Function Definition for the definition of DPs for products. See the following example:

Device Control

The control instructions shall be sent in the format given below:

{"<dpId>":"<dpValue>"}

According to the definition of DPs for the product in the backend, the following sample code is applied.

Examples

Objc:

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

  NSDictionary *dps;

  // Set bool dp value to true
	dps = @{@"1": @(YES)};

  // Set string dp value to "ff5500"
	dps = @{@"4": @"ff5500"};

  // Set enum dp value to "Medium"
	dps = @{@"5": @"Medium"};

  // Set number dp value to 20
	dps = @{@"6": @(20)};

  // Set byte dp value to "1122"
	dps = @{@"15": @"1122"};

  // Send multiple dp values together
	dps = @{@"1": @(YES), @"4": @"ff5500"};

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

    // Publish dp success. device state change will be reported from deviceDpsUpdate delegate callback.

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

}

Swift:

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

    // dps: Please refers to the specific product definition

    device?.publishDps(dps, success: {
 	    print("publishDps success")

      // Publish dp success. device state change will be reported from deviceDpsUpdate delegate callback.
    }, failure: { (error) in
        if let e = error {
            print("publishDps failure: \(e)")
        }
    })
}

Note:

  • Special attention shall be paid to the type of data in sending the control commands. For example, the data type of DPs shall be value, and the @{@"2": @(25)} instead of @{@"2": @"25"} shall be sent for the control command.
  • In the transparent transmission, the byte string shall be the string format, and all letters shall be in the lower case, and the string must have even bits. The correct format shall be: @{@"1": @"011f"} instead of @{@"1": @"11f"}

To check more details about DPs, choose Configure in Platforms > Function Definition > Custom Functions.

Device control

Device control supports three kinds of channel control, LAN control, cloud control, and automatic mode (if LAN is online, first go LAN control, LAN is not online, go cloud control)

LAN Control

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

Cloud Control

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

Auto Mode

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