Xiaobai learns the IOT simulation device [CCP protocol] message queue reporting

The first two articles introduced the RPC&RRPC communication in the CCP protocol, because the CCP protocol is divided into two parts: RPC&RRPC and PUB&SUB. The full names of PUB and SUB are PUBLISH and SUBSCRIBE, respectively. The Chinese name is called publish and subscribe. In a popular understanding, I think it can be like when we subscribe to newspapers or magazines when we were young. Publishing means that as an author, we are allowed to publish articles in newspapers and magazines on a regular or irregular basis. Subscribing means that our readers regularly receive publications of newspapers and magazines or can read a certain column. In the category of Ali IOT, newspapers and magazines are like this big platform of Ali IOT, each TOPIC is similar to a certain newspaper, magazine or a column, and our readers or writers are a certain category in this IOT ecosystem A specific device under the product. This should be easier to understand. For example, if a device is interested in a certain TOPIC, it must first apply to the server to subscribe to this TOPIC, then if the information about this TOPIC is updated in the future, the server will send it to the subscription. of this device. And if a device intends to let other devices or servers know about its status or related information, it can put it on the relevant TOPIC by publishing it. This time, we will first introduce the specific implementation of the release [PUB].


  On Alibaba IOT, TOPIC generally defines three TOPICs by default: GET, UPDATE, and ERROR, which correspond to receiving information, reporting information, and reporting error exceptions. Of course, we can also create new device-specific TOPICs. According to the explanation of the three TOPICs, we can also guess that their communication direction or authority should be subscribe, publish, publish. Viewing through the IOT console is indeed the case:




according to the architecture of the Alibaba IOT platform, the device needs to use the MNS message service to publish information to the server, so we must first activate the message service, and there are 18 major items under the product item on the Alibaba Cloud homepage. Select the message service in the application service in the category product, which is the bottom category in the first column, and then click Activate Now.




On the server side, we also need to do some configuration. The message service is divided into two types: queue and TOPIC. Let's take a look at how the queue operates. In the server subscription column of the console page, click Configure, then select Device Information Report, and then save. After configuration, remember the name of the queue on the right, it is best to copy it to Notepad, because we will use it later This value: The








next step is to get the ENDPOINT of the message queue. On the console page of the message queue, click the queue column, click Get ENDPOINT, and copy the link in the first line in the pop-up information box;
after the relevant preparation process is completed , we can start modifying the code. Modify the device side first.
Modify the demo.c file in the aliyun-iot-device-sdk-c-2017-01-24\examples\linux\ccp directory:
add line 273
strncpy(subscribe.topics[0], TOPIC_GET, sizeof(subscribe.topics[ 0]) - 1);
to

strncpy(subscribe.topics[0], TOPIC_UPDATE, sizeof(subscribe.topics[0]) - 1); change the publish.payload = (unsigned char *)"{\"
on line 287
message\":\"hello,world.\"}";The
content is changed to hello,
queue.publish.payload = (unsigned char *)"{\"message\":\"hello,queue.\"}";
Then run make clean on the command line;
Look at the results, we can receive the information sent by our device.






Next , we modify the code on the server side so that the server can also receive and display the messages sent by the device:
run wget on the server http://docs-aliyun.cn -hangzhou.oss.aliyun-inc.com/assets/attach/32381/cn_zh/1496733042815/aliyun-mns-php-sdk-1.3.5.zip,
then unzip: unzip aliyun-mns-php-sdk-1.3.5 .zip,
enter the example directory: cd php_sdk\Samples\Queue
CreateQueueAndSendMessage.php This file has 5 steps to create a queue, send a message, receive a message, delete a message and delete the queue, because we now do the function of receiving messages first, remove the sending message Step 2, then enter our AK, SK and ENDPOINT around line 103.

$accessId = "";
$accessKey = "";
$endPoint = ""; Change
to the name of our queue around line 25
$queueName = "CreateQueueAndSendMessageExample"; Change
to:
$queueName = "aliyun-iot-1000188705";


            $jd=json_decode($body);
            $topic = "topic is :".$jd->topic;
            $payload = "payload is :".base64_decode($jd->payload);
            echo $topic."<br> ";
            echo $payload."<br>";


Save and exit, then run it in the browser, then run the device simulation program, and see the results of the browser. Since the php file in this example receives a message within 30 seconds, it is enough for our device to send a message.








Attached is the relevant code:
lesson3.rar (4 K) Downloads: 1
Timelapse video:
http://www.xunyun17.xyz/aliiot/lesson3/lesson3.mp4

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326350590&siteId=291194637