Last Updated on : 2022-02-17 05:28:52download
Smart devices have gained ground in many homes 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 home 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.
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
.
To check the DPs of a specific product, log in to the Tuya IoT Platform, and in the left-side navigation pane, choose Product > Development. Find the product to be managed and click Continue to Develop to go to the Function Definition tab.
Send a control command to a device in the following format:
{"<dpId>":"<dpValue>"}
The following example shows how to define a DP.
Example
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)")
}
})
}
@{@"2": @(25)}
rather than @{@"2": @"25"}
.@{@"1": @"011f"}
rather than @{@"1": @"11f"}
.To check the definitions of more DPs, on the Tuya IoT Platform, go to the Function Definition tab of a product and find the Custom Functions section. For more information, see Custom Functions.
Sends DPs to a device to change the device status or features and control the device.
API description
Devices can be controlled in three modes: local area network (LAN) control, cloud control, and automatic control. If a device is connected to a LAN, it is controlled over the LAN in priority. If not, it is controlled in the cloud.
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);
}];
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback