Last Updated on : 2023-05-25 06:23:46
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 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 ThingSmartDeviceModel
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.
For more information, see Product Functions.
Send a control command 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:
Objective-C:
- (void)publishDps {
// self.device = [ThingSmartDevice deviceWithDeviceId:@"your_device_id"];
NSDictionary *dps;
// The data point of Boolean type with the `dpId` value of `1`. Feature: turns on the switch.
dps = @{@"1": @(YES)};
// The data point of String type with the `dpId` value of `4`. Feature: sets the red, green, and blue (RGB) value to `ff5500`.
dps = @{@"4": @"ff5500"};
// The data point of Enumeration type with the `dpId` value of `5`. Feature: sets the lighting level to `2`.
dps = @{@"5": @"2"};
// The data point of Value type with the `dpId` value of `6`. Feature: sets the temperature value to `20`.
dps = @{@"6": @(20)};
// The data point of Raw type with the `dpId` value of `15`. Feature: 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. Example:
{"2": 25}
rather than {"2": "25"}
.@{@"1": @"011f"}
rather than @{@"1": @"11f"}
.Sends DPs to a device to change the device status or features and control the device.
Device control supports control over the local-area network (LAN), control in the cloud, and control through an automatically selected channel. The third control method is recommended.
Control over the LAN:
Control a device over the LAN only. The device must run on a LAN.
[self.device publishDps:dps mode:ThingDevicePublishModeLocal success:^{
NSLog(@"publishDps success");
} failure:^(NSError *error) {
NSLog(@"publishDps failure: %@", error);
}];
Control in the cloud:
Control a device in the cloud only. The device must run in the cloud.
[self.device publishDps:dps mode:ThingDevicePublishModeInternet success:^{
NSLog(@"publishDps success");
} failure:^(NSError *error) {
NSLog(@"publishDps failure: %@", error);
}];
Control through an automatically selected channel:
Check the availability of channels in the sequence specified by communication.communicationModes
that is defined in the device model ThingSmartDeviceModel
, and select an appropriate channel to control the device.
[self.device publishDps:dps success:^{
NSLog(@"publishDps success");
} failure:^(NSError *error) {
NSLog(@"publishDps failure: %@", error);
}];
// or use
[self.device publishDps:dps mode:ThingDevicePublishModeAuto success:^{
NSLog(@"publishDps success");
} failure:^(NSError *error) {
NSLog(@"publishDps failure: %@", error);
}];
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback