You are on page 1of 9

AIoT training camp

Lab Guide –
O&M Feedback

Huawei Technologies Co., Ltd.


A lot 开发实验 第1页

1 Remote O&M feedback

1.1 Experiment Introduction

1.1.1 About This Experiment


Based on the commodity association analysis and regional sales forecast, we can adjust the
commodity display sequence and prices on the development board (vending machine) to
promote certain commodities by providing feedback and calling back the development board.
In the background of intelligent O&M, we hope to remotely invoke APIs to control the display
of the development board. To call an API to remotely control a device, you need to complete
device authentication, obtain the accessToken, and then use Python to send a command to
the device access service based on parameters such as the application ID and application key.
In this way, the development board is called back.

1.1.2 Experiment Objective


⚫ Master the method of remotely controlling devices using Python.

1.2 Procedure

1.2.1 Deliver a command.


步骤 1 Device authentication

When the technical support engineer server accesses the open API of the IoT platform for the
first time, the technical support engineer server needs to invoke the interface to complete
authentication and obtain the access token.
Note that the authentication API is the prerequisite for invoking other APIs. Except for the
authentication API, the subsequent requests must carry the app_key and Authorization
parameters in the request header. The value of app_key must be the same as that of appId in
the request parameter. Authorization is accessToken.
A lot 开发实验 第2页

Certificate files (client.crt and client.key) are required for device authentication. Place the
authentication files in the corresponding directory in the OBS bucket for easy reading.

Note: After the upload is complete, synchronize OBS with ModelArts.


The parameters in the authentication request are as follows:

If you forget the application ID and secret, go to HUAWEI CLOUD IoT platform to view the
application interconnection domain name (URL), port number, and protocol.
A lot 开发实验 第3页

Click Access Device Access Service. The service page is displayed.

Touch the app to access the app.


A lot 开发实验 第4页

The app ID is displayed. If you forget the secret, you can reset the secret and save it to the
local host.
The URL, port number, and application key (appID and key) are obtained.
Use the requests library to send a post request.
import requests,json # Imported package
Import OS

def readCertificate():
certFilePath = './client.crt' #Path of the configuration authentication file
certFilePath2 = './client.key'
cert = (certFilePath, certFilePath2)
return cert

payload = dict(key=' vpfqC_pEExjfFoDj0DU4Mt78ioca', secret =' RdIofxo_h4oW9IDWAGvCp8R_McUa')


#The key value and key are subject to the obtained configuration file.
payload_js=json.dumps(payload) # Note: The value must be converted to the JSON format. Otherwise,
an error will be reported.
headers = {'Content-Type': 'application/json'}#Request header information

url = 'https://iot-api.cn-north-4.myhuaweicloud.com:8743/api/v3.0/auth/tokens'
#The URL is subject to the IoT platform address. The port number is usually 8743.
r = requests.post(url, headers=headers, data=payload_js, cert=readCertificate(), verify=False):

步骤 2 Obtain the accessToken.

The response parameters are as follows:


A lot 开发实验 第5页

print('Request URL:', r.url)


print('Request Status_code:', r.status_code)
print('Request Result:', r.text)

The command output is as follows:


Request URL: HYPERLINK "https://iot-api.cn-north-4.myhuaweicloud.com:8743/api/v3.0/auth/tokens" \t
"_blank" https://iot-api.cn-north-4.myhuaweicloud.com:8743/api/v3.0/auth/tokens:
Request Status_code: 200 # If the value of Request Status_code is 200, the authentication is successful.
Request Result:
{"accessToken":"92b1a166448d6398a9e41e7aa5f39d2","tokenType":"bearer","refreshToken":"35f4c58bf1
8a82ed7578315676fe52","expiresIn":3600,"scope":"default"}:

Note that the validity period of the accessToken is 3600s. If the platform is not used for a
long time, authentication needs to be performed again.

步骤 3 Send commands to the device service.

The authorization parameter must contain Authorization and app_key. Authorization is the
obtained accessToken. The URI is in the format of https://ip:port number
/api/v3.0/devices/{deviceId}/commands. The request body contains the serviceId, method,
expireTime, and body parameters. "body":{"ioswitch":"1678923450"} indicates the content of
the request, the number sequence "1678923450" indicates the display sequence of the
offering on the developer board.
Mandatory request parameters are as follows:
A lot 开发实验 第6页

To obtain the value of deviceId, choose Device > All Devices to view the device list.

Click the current device to view device details.


A lot 开发实验 第7页

Set deviceId to the value of deviceId in the basic information and edit the post request.
headers = {"Authorization":"92b1a166448d6398a9e41e7aa5f39d2",
"app_key":"vpfqC_pEExjfFoDj0DU4Mt78ioca"}:

body = {"serviceId":"Delivery",
"method":"SET_LIGHT_ON",
"expireTime": 0,
"body":{"ioswitch":"1678923450"}} # Offering display sequence. The number indicates the offering ID.
The initial sequence is 0123456789.
url = 'https://iot-api.cn-north-4.myhuaweicloud.com:8743/api/v3.0/devices/1ebce315-1226-4066-bf5e-
494e06d77b99/commands' ##The URI format is HYPERLINK "https://ip" https://ip address:port number
/api/v3.0/devices/{deviceId}/commands.

response = requests.post(url, headers = headers, json=body, verify=False):

1.2.2 Verification
步骤 1 Output the request result.

Note: Keep the device online.


print(response.text)

The command output is as follows:


{"commandId":"7a1a66f4c6114eb482c38fa4642b04c7"}:

步骤 2 Verify the development board result.

The following figure shows the initial offering sequence.


A lot 开发实验 第8页

After the request for changing the offering placement sequence is sent, the developer board is
displayed as follows.

You might also like