Binding Devices to Cloud Accounts

API for binding devices to accounts is shown in Figure 10.26 and can be found at https://swaggerapis.rainmaker.espressif.com/#/User%20Node%20Association/addRemoveUserNodeMapping.

Figure 10.26. API for binding devices

To bind the device to the account, use the secret key generated in Section 10.5.3, the device ID (node_id) and the operation identifier.

PUT /v1/user/nodes/mapping Content-Type: application/json Authorization: $accesstoken { "node_id": "$node_id", "secret_key": "$secretKey", "operation": "add" }

Binding devices in Android

📝 Source code

For the source code of binding devices in Android, please refer to book-esp32c3-iot-projects/phone_app/app_android/app/src/main/java/com/espressif/cloudapi/ApiManager.java.

public void addNode(final String nodeId, String secretKey, final ApiResponseListener listener) { DeviceOperationRequest req = new DeviceOperationRequest(); req.setNodeId(nodeId); req.setSecretKey(secretKey); req.setOperation(AppConstants.KEY_OPERATION_ADD); apiInterface.addNode(AppConstants.URL_USER_NODE_MAPPING, accessToken, req).enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { //Code Omitted } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { } }); }

Binding devices in iOS

📝 Source code

For the source code of binding devices in iOS, please refer to book-esp32c3-iot-projects/phone_app/app_ios/ESPRainMaker/ESPRainMaker/Interface/Provision/SuccessViewController.swift.

@objc func sendRequestToAddDevice() { let parameters = ["user_id": User.shared.userInfo.userID, "node_id": User.shared.currentAssociationInfo!.nodeID, "secret_key": User.shared.currentAssociationInfo!.uuid, "operation": "add"] NetworkManager.shared.addDeviceToUser(parameter: parameters as![String: String]) { requestID, error in if error ! = nil, self.count > 0 { self.count = self.count - 1 DispatchQueue.main.asyncAfter(deadline: .now()) { self.perform(#selector(self.sendRequestToAddDevice), with:nil, afterDelay: 5.0) } } else { if let requestid = requestID { self.step3Indicator.stopAnimating() self.step3Image.image = UIImage(named: "checkbox_checked") self.step3Image.isHidden = false self.step4ConfirmNodeAssociation(requestID: requestid) } else { self.step3FailedWithMessage(message: error?.description ?? "Unrecognized error. Please check your internet.") } } } }

Once the device is bound to the account, users can initiate the request for remote communication.