Xiaobai learns IOT simulation device RRPC command issuance

RPC communication can report messages from the device to the server, or send commands to the device through the server. This kind of communication is also called RRPC. This time, we will introduce and practice RRPC command communication;
send commands through the server to simulate device instructions Light switch to test RRPC communication.
1. Since Alibaba Cloud's PHP SDK is used, first download the PHP sdk:
git clone https://github.com/aliyun/aliyun-openapi-php-sdk.git


2. Write the PHP file for the user interface, one for Provide the user to choose whether to turn the light on or off;

<html><meta charset='UTF-8'><title>Xiaobai learns IOT</title><p><center><head>RRPC command delivery</head> <body bgcolor='gray'><hr>
<?php
echo "</p><br><p>LED control:<br>";
$str = $_SERVER["QUERY_STRING"];
echo "<form action=\"led.php\">
Select type:<br/>
<input type='radio' name='led' value='ON' ><img src='./ledon.jpg'/>



echo "</p></center>";
?>
</body></head></center></p></html>


3. Write a PHP file that controls the lights on and off, here The core and iot directories in the PHP SDK are used. The file code is as follows, which specifically means to receive the indicator light control information from the interface, and then configure the AK, SK, product KEY, and device name, and encode the control information through BASE64. Then send it to the device, and finally receive the feedback information from the device and display it on the web page. :

<html><meta charset='UTF-8'><title>Xiaobai learns IOT</title><p><center><head>RRPC command issued</head><body bgcolor='gray'> <hr>
<?php


echo "</p><br><p>LED control:<br>";
$str = $_SERVER["QUERY_STRING"];


if($str == "led=ON") {
    echo "<img src='./ledon.jpg'/><br>";
    $led = "ON";
}
if($str == "




include_once 'aliyun-openapi-php-sdk/aliyun-php-sdk-core/AcsRequest.php';
include_once 'aliyun-openapi-php-sdk/aliyun-php-sdk-core/AcsResponse.php';
include_once 'aliyun-openapi-php-sdk/aliyun-php-sdk-core/RpcAcsRequest.php';
include_once 'aliyun-openapi-php-sdk/aliyun-php-sdk-core/IAcsClient.php';
include_once 'aliyun-openapi-php-sdk/aliyun-php-sdk-core/DefaultAcsClient.php';
include_once 'aliyun-openapi-php-sdk/aliyun-php-sdk-core/Autoloader/Autoloader.php';


include_once 'aliyun-openapi-php-sdk/aliyun-php-sdk-iot /Request/V20160104/RevertRpcRequest.php';
$iClientProfile = DefaultProfile::getProfile("cn-shenzhen",
"你自己的AK", "你自己的SK");
$client = new DefaultAcsClient($iClientProfile);
$request = new Iot\Request\V20160530\RevertRpcRequest();
$request->setProductKey(product KEY);
$request->setDeviceName("device name");//device name
$request->setTimeOut(5000); / /Timeout time, in milliseconds. If the device does not respond after this time, it will return "TIMEOUT"
$request->setRpcContent(base64_encode($led));
//The data pushed to the device. The data requires binary data to be BASE64 encoded.( The example is the encoded value of "helloworld")
$response = $client->getAcsResponse($request);
//print_r("\r\n");
//print_r($response->RpcCode);
if(isset ($response->RpcCode)){
if($response->RpcCode=='SUCCESS'){
    echo 'The operation succeeded.';
}else{
    echo 'The operation failed: '.$response->RpcCode;
}
}
echo " </p></center> ";
?>
</body></head></center></p></html>




4. Finally, you need to run the sample program on the simulated device side to run normally:
this is to view the device side, you can see that the ON information has been received; the


attachment is the relevant coding file:
lesson2-web.rar (7 K) Downloads: 0



timelapse Address :
http://www.xunyun17.xyz/aliiot/lesson2/IOT-RRPC.mp4

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326563993&siteId=291194637