Upswift REST API - Monitor device status

This guide explains how to monitor device status using Upswift REST API, Including Python scripts.

Time to read:

3

minutes

When it comes to managing edge Linux devices, one of the key points is to make sure that the devices are up and running. For example, when deploying an IoT app which connects many endpoints together, it can be extremely useful if there was a way to easily fetch a particular device’s online/offline status; preferably through a simple API call.

In this guide, we will be taking a look at Upswift's device online status API call, which is an HTTP GET request, which the posting payload should be in the form of JSON array.


For simplicity, the script is written in Python and the prerequisite modules are json and requests. These are by default installed with every Python installation, and the scripts are cross-platform compatible.

Step 1 - Obtain the User Token

The user token can be found in the Settings section in the Upswift dashboard, by clicking on Show Account Token. This token is unique to your account and cannot be changed.


Step 2 - Obtaining the Device ID

The Device ID is needed to obtain the device online status, and it can be found in the devices section. Each device is given a unique device ID after enrollment with the platform.

Step 3 - Python script template for fetching device status

Following the official documentation for the API, the following Python script template can then be used to obtain device status from the dashboard.

Create a new file using nano <filename>.py and type in the script, and update the details accordingly. Then, press Ctrl+O and press Enter to confirm save.

import requests


#Upswift account token and device ID
user_token = #User token from Upswift settings page
device_id = #Device ID from Upswift devices page

json_content = {'device_id': user_token,
            	'user_token': device_id}

call_request = requests.get("https://api.upswift.io/v1/get_device_state", json=json_content)
call_response = json.loads(call_request.text)

print("Call response : " + str(call_response))

if call_request.status_code != 200:
	if call_request.status_code == 429:
    	error = "API limit reached"
	else:
    	error = call_response["error_message"]
	print(error)

else:
	device_status = call_response["message"]["device_status"]
	device_id = call_response["message"]["device_id"]

Step 4 - Execute the script and verify the functionality


Type in the command python <filename>.py (which was used to name the script in Step 3), and press Enter. (in the following example, the file has been saved as upswift_device_status.py)

Upon successful completion of the script, the above output should be displayed. If any error occurred, the script will print out the error message for further debugging.


For more information on the Upswift Change Device Details API, refer to the official documentation.