Transferring IoT Terminal Device Data using Message Queues

Configuring Server Subscription from the Alibaba Cloud IoT Console

Create a Product

Add a Device Under the Product

Configure Server Subscription under the Product

{
"payload": "Base64 Encode data",
"messagetype": "status",
"messageid": 996000000000000001,
"topic": "the Topic of a specific device",
"timestamp": 1526450324
}

Device-side development

Get IoT SDK for Node.js

Write Device-side Application Code

/**
* package.json add dependency:"aliyun-iot-mqtt": "0.0.4"
*/
const mqtt = require('aliyun-iot-mqtt');

//Device triplets
const options = {
productKey: "product",
deviceName: "device",
deviceSecret: "secret",
regionId: "cn-shanghai"
};

//device and cloud establish connection, device connected
const client = mqtt.getAliyunIotMqttClient(options);

//topic
const topic = `${options.productKey}/${options.deviceName}/update`;
const data = {
temperature: Math.floor((Math.random()*20)+10),
humidity: Math.floor((Math.random()*100)+20),
};
//specified topic publishes data to cloud
client.publish(topic, JSON.stringify(data));

Run the Simulation Device Script

$node iot-mns.js

Device Messages in a Message Queue

Use MNS

<dependencies>
<dependency>
<groupId>com.aliyun.mns</groupId>
<artifactId>aliyun-sdk-mns</artifactId>
<version>1.1.8</version>
<classifier>jar-with-dependencies</classifier>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.42</version>
</dependency>
</dependencies>
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.mns.client.CloudAccount;
import com.aliyun.mns.client.CloudQueue;
import com.aliyun.mns.client.MNSClient;
import com.aliyun.mns.model.Message;
import org.apache.commons.codec.binary.Base64;

public class ComsumerDemo {

public static String accessKeyId = "AK";
public static String accessKeySecret = "AK secret";
public static String endpoint = "mns public network endpoint";

public static void main(String[] args) {
CloudAccount account = new CloudAccount(accessKeyId,accessKeySecret,endpoint);
MNSClient client = account.getMNSClient();
//Get message queue
CloudQueue queue = client.getQueueRef("aliyun-iot-a1jnUEKYhw4");

while (true) {
Message popMsg = queue.popMessage(10);
if (popMsg != null) {
System.out.println("message id: " + popMsg.getMessageId());
System.out.println("message body: \n" + decodeBase64(popMsg.getMessageBodyAsString()));
//Delete messages
queue.deleteMessage(popMsg.getReceiptHandle());
}
}
}

public static String decodeBase64(String jsonString) {
try {
JSONObject json = JSON.parseObject(jsonString);
String payload = new String(Base64.decodeBase64(json.getString("payload")));
json.put("payload", JSON.parseObject(payload));
return json.toJSONString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

Run the application

message id: E2CF179AD5686386-2-16367878417-200000009
message body:
{
"payload": {
"lastTime": "2018-05-16 13:58:44.413",
"clientIp": "42.120.74.246",
"time": "2018-05-16 13:58:44.427",
"productKey": "a1jnUEKYhw4",
"deviceName": "suw8umOqgJ72yCADerZp",
"status": "online"
},
"messagetype": "status",
"messageid": 996631012001751041,
"timestamp": 1526450324
}


message id: "656A4F66B391367-1-1636787AAC0-20000000C"
message body:
{
"payload": {
"temperature": 14,
"humidity": 116
},
"messagetype": "upload",
"topic": "/a1jnUEKYhw4/suw8umOqgJ72yCADerZp/update",
"messageid": 996631053735047169,
"timestamp": 1526450334
}

message id: E2CF179AD5686386-2-1636787F5F1-20000000A
message body:
{
"payload": {
"lastTime": "2018-05-16 13:59:04.381",
"time": "2018-05-16 13:59:13.571",
"productKey": "a1jnUEKYhw4",
"deviceName": "suw8umOqgJ72yCADerZp",
"status": "offline"
},
"messagetype": "status",
"messageid": 996631134240534528,
"timestamp": 1526450353
}

Source Code

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store