Changing Device Status

The app allows users to change device name, power on/off status, brightness, hue, and saturation. This section will explain how to implement this function with the example of changing brightness and on/off status.

The API to change the status of a device is shown in Figure 10.31 and can be found at https://swaggerapis.rainmaker.espressif.com/#/Node%20Parameter%20Operations/updatenodestate.

Figure 10.31. API to change device status
PUT /v1/user/nodes/params
Authorization: $accesstoken

[
    {
        "node_id": "string",
        "payload": {
            "Light": {
                "brightness": 100,
                "output": true
            },
            "Switch": {
                "output": true
            }
        }
    }
]

Among the returned fields, node_id represents the device's unique identifier; Light represents the brightness of the device, and brightness is the specific value; Switch indicates the on/off status of the device.

Changing device status in Android

📝 Source code

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

public void updateParamValue(final String nodeId, JsonObject body, 
                            final ApiResponseListener listener) {

    apiInterface.updateParamValue(AppConstants.URL_USER_NODES_PARAMS, 
                                accessToken, 
                                nodeId, 
                                body).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) {
            t.printStackTrace();
            listener.onNetworkFailure(new Exception(t));
        }
    });
}

Changing device status in iOS

📝 Source code

For the source code of changing device status in iOS, please refer to book-esp32c3-iot-projects/phone_app/app_ios/ESPRainMaker/ESPRainMaker/AWSCognito/ESPAPIManager.swift.

func setDeviceParam(nodeID: String?, parameter: [String: Any],
                    completionHandler: ((ESPCloudResponseStatus) -> Void)? = nil) {
    NotificationCenter.default.post(Notification(name: Notification.Name
                                (Constants.paramUpdateNotification)))
    if let nodeid = nodeID {
        ESPExtendUserSessionWorker().checkUserSession(){accessToken, error in
            if let token = accessToken {
                let url = Constants.setParam + "?nodeid=" + nodeid
                let headers: HTTPHeaders = ["Content-Type":
                                            "application/json", 
                                            "Authorization": token]
                self.session.request(url, method: .put, 
                                    parameters: parameter, 
                                    encoding: ESPCustomJsonEncoder.default,
                                    headers: headers).responseJSON {response in
                    //Code Omitted
                }
            } else {
                let _ = self.validatedRefreshToken(error: error)
            }
        }
    }
}

The code above allows users to change the status of a specific device. So far, we have implemented the functions of account registration, account login, device scanning, device connection, device provisioning, device binding, and remote control on RainMaker.