Camera Scan Code Network Configuration

Last Updated on : 2023-06-05 02:50:00download

The smart camera’s unique QR code mode refer to QR code mode

Wired network configuration

The wired device is connected to the network, not need the name and password of the router during the network configuration. The figure below uses Zigbee wired gateway as an example to describe the wired gateway network configuration process.

Camera Scan Code Network Configuration

Discovery device

Tuya SDK provides the function of discovering wired devices. You should register the notification of the wired device to get device information. Before obtaining the device, the phone must be connected to the same network as the device.

Notification

// Notification name
// message the receiver sends observer likes: @{@"productId":productId, @"gwId":gwId}
TuyaSmartActivatorNotificationFindGatewayDevice;

Get token

Before the Wired Network Configuration, the SDK needs to obtain the network configuration Token from the Tuya Cloud. The term of validity of Token is 10 minutes, and the Token becomes invalid once the network configuration succeeds. A new Token has to be obtained if you have to reconfigure the network.

Description

- (void)getTokenWithHomeId:(long long)homeId
                   success:(TYSuccessString)success
                   failure:(TYFailureError)failure;

Parameters

Parameters Description
homeId Home Id
success Success block, return Token
failure Failure block

Example

Objc:

- (void)getToken {
	[[TuyaSmartActivator sharedInstance] getTokenWithHomeId:homeId success:^(NSString *token) {
		NSLog(@"getToken success: %@", token);
		// TODO: startConfigWiFi
	} failure:^(NSError *error) {
		NSLog(@"getToken failure: %@", error.localizedDescription);
	}];
}

Swift:

func getToken() {
    TuyaSmartActivator.sharedInstance()?.getTokenWithHomeId(homeId, success: { (token) in
        print("getToken success: \(token)")
        // TODO: startConfigWiFi
    }, failure: { (error) in
        if let e = error {
            print("getToken failure: \(e)")
        }
    })
}

Wired network configuration

Description

- (void)startConfigWiFiWithToken:(NSString *)token timeout:(NSTimeInterval)timeout

Parameters

Parameters Description
token Config Token
timeout Timeout, default 100 seconds

Example

Objc:

- (void)startConfigWiFiToken:(NSString *)token {
	// Set TuyaSmartActivator delegate,impl delegate method
	[TuyaSmartActivator sharedInstance].delegate = self;

	// Start activator
	[[TuyaSmartActivator sharedInstance] startConfigWiFiWithToken:token timeout:100];
}

#pragma mark - TuyaSmartActivatorDelegate
- (void)activator:(TuyaSmartActivator *)activator didReceiveDevice:(TuyaSmartDeviceModel *)deviceModel error:(NSError *)error {

	if (!error && deviceModel) {
		//active success
    }

    if (error) {
        //active failure
    }

}

Swift:

func startConfigWifiToken(_ token: String) {
    // Set TuyaSmartActivator delegate,impl delegate method
    TuyaSmartActivator.sharedInstance()?.delegate = self

    // Start activator
    TuyaSmartActivator.sharedInstance()?.startConfigWiFi(withToken: token, timeout: 100)
}

#pragma mark - TuyaSmartActivatorDelegate
func activator(_ activator: TuyaSmartActivator!, didReceiveDevice deviceModel: TuyaSmartDeviceModel!, error: Error!) {
    if deviceModel != nil && error == nil {
        //active success
    }

    if let e = error {
        //active failure
        print("\(e)")
    }
}

Stop network configuration

The App will continuously broadcast the network configuration information until the network configuration succeeds or the timeout is reached once the network configuration starts. The [TuyaSmartActivator stopConfigWiFi] method has to be invoked if you need to cancel the network configuration or the network configuration is completed.

Description

- (void)stopConfigWiFi;

Example

Objc:

- (void)stopConfigWifi {
	[TuyaSmartActivator sharedInstance].delegate = nil;
	[[TuyaSmartActivator sharedInstance] stopConfigWiFi];
}

Swift:

func stopConfigWifi() {
    TuyaSmartActivator.sharedInstance()?.delegate = nil
    TuyaSmartActivator.sharedInstance()?.stopConfigWiFi()
}

Sub-device configuration

Camera Scan Code Network Configuration

Start network configuration

Description

- (void)activeSubDeviceWithGwId:(NSString *)gwId timeout:(NSTimeInterval)timeout

Parameters

Parameters Description
gwId Gateway Id
timeout Timeout, default 100 seconds

Example

Objc:

- (void)activeSubDevice {
    // Set TuyaSmartActivator delegate,impl delegate method
	[TuyaSmartActivator sharedInstance].delegate = self;

	[[TuyaSmartActivator sharedInstance] activeSubDeviceWithGwId:@"your_device_id" timeout:100];
}

#pragma mark - TuyaSmartActivatorDelegate
- (void)activator:(TuyaSmartActivator *)activator didReceiveDevice:(TuyaSmartDeviceModel *)deviceModel error:(NSError *)error {

    if (!error && deviceModel) {
		//active success
    }

    if (error) {
        //active failure
    }
}

Swift:

func activeSubDevice() {
    // Set TuyaSmartActivator delegate,impl delegate method
    TuyaSmartActivator.sharedInstance()?.delegate = self

    TuyaSmartActivator.sharedInstance()?.activeSubDevice(withGwId: "your_device_id", timeout: 100)
}

#pragma mark - TuyaSmartActivatorDelegate
func activator(_ activator: TuyaSmartActivator!, didReceiveDevice deviceModel: TuyaSmartDeviceModel!, error: Error!) {
    if deviceModel != nil && error == nil {
        //active success
    }

    if let e = error {
        //active failure
        print("\(e)")
    }
}

Stop activating the sub-device

The stopActiveSubDeviceWithGwId method has to be invoked if you need to cancel the network configuration or the network configuration is completed.

Description

- (void)stopActiveSubDeviceWithGwId:(NSString *)gwId

Parameters

Parameters Description
gwId Gateway Id

Example

Objc:

- (void)stopActiveSubDevice {
  [TuyaSmartActivator sharedInstance].delegate = nil;
	[[TuyaSmartActivator sharedInstance] stopActiveSubDeviceWithGwId:@"your_device_id"];
}

Swift:

func stopActiveSubDevice() {
  	TuyaSmartActivator.sharedInstance()?.delegate = nil
    TuyaSmartActivator.sharedInstance()?.stopActiveSubDevice(withGwId: "your_device_id")
}

Scan the QR code of the device configuration

This function is only available for devices that have a QR code and are connected to the Internet.

Camera Scan Code Network Configuration

  1. Use the universal interface to get uuid

Scan the QR code of the device to obtain the url, and then obtain the uuid through the universal interface

Parameters

Parameters Description
apiName tuya.m.qrcode.parse
Version 4.0
postData @{@“code”:url}

Example

Objc:

    [self.apiRequest requestWithApiName:@"tuya.m.qrcode.parse" postData:@{@"code":url} version:@"4.0" success:^(id result) {

    } failure:^(NSError *error) {

    }];

Swift:

apiRequest .request(withApiName: "tuya.m.qrcode.parse", postData: ["code":url], version: "4.0", success: {_ in

        }, failure: { (Error) in

        })
  1. QR Code Network Configuration

Get token interface

Description


- (void)getTokenWithUUID:(NSString *)uuid
                  homeId:(long long)homeId
                 success:(TYSuccessString)success
                 failure:(TYFailureError)failure;

Example
Objc:

 [[TuyaSmartActivator sharedInstance] getTokenWithUUID:uuid homeId:homeId success:^(NSString *result) {
       // get token
    } failure:^(NSError *error) {

    }];

Swift:

        TuyaSmartActivator.sharedInstance()?.getTokenWithUUID(uuid, homeId: homeid, success: { (token) in
            print("getToken success: \(token)")
            // TODO: startConfigWiFi
        }, failure: { (error) in
            if let e = error {
                print("getToken failure: \(e)")
            }
        })

Start Network Configuration

Description

- (void)startConfigWiFi:(TYActivatorMode)mode
                   ssid:(NSString *)ssid
               password:(NSString *)password
                  token:(NSString *)token
                timeout:(NSTimeInterval)timeout;

Example
Objc:

[[TuyaSmartActivator sharedInstance] startConfigWiFi:TYActivatorModeEZ ssid:@"" password:@"" token:self.token timeout:Timeout];

Swift:

 TuyaSmartActivator.sharedInstance()?.startConfigWiFi(TYActivatorModeEZ, ssid:nil, password: nil, token: token, timeout: Timeout)

The callback of config network status update

Description

- (void)activator:(TuyaSmartActivator *)activator didReceiveDevice:(TuyaSmartDeviceModel *)deviceModel error:(NSError *)error;

Example
Objc:

[TuyaSmartActivator sharedInstance].delegate = self;

- (void)activator:(TuyaSmartActivator *)activator didReceiveDevice:(TuyaSmartDeviceModel *)deviceModel error:(NSError *)error {
      // get deviceModel
}

Swift:

func activator(_ activator: TuyaSmartActivator!, didReceiveDevice deviceModel: TuyaSmartDeviceModel!, error: Error!) {
    if deviceModel != nil && error == nil {

    }

    if let e = error {

        print("\(e)")
    }
}