随着工业自动化的不断发展,设备入云这门技术也越来越重要,很多人都开启了学习设备入云。然后前面几篇设备入云这一块也讲了好多了,本文呢就介绍设备入云之后自己做一个独立自主的设备客户端APP的实际项目操作。
提示:以下是本篇文章正文内容,下面案例可供参考
结合上一篇【FlexManager和阿里云MQTT通讯实例详解】,我们现在给设备做一个独立自主的APP,其通讯框架如下图所示,先一览为快:
因为阿里云设备端虚拟前面一篇已经讲了,此处略去一万字,看图:
设备端部署,创建完成后设备虚拟为rtyu,如图所示,已显示在线。
查看设备虚拟详情,如下图所示:
调试一波设备信息收发情况,如图所示:
在阿里云物联网平台配置设备APP虚拟客户端APP334,如图所示:
在andriod studio 创建项目开发设备APP,段落重要代码如图所示:
public void connectToIot() {
try {
MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
String mqttUsername = deviceName + "&" + productKey;
System.out.println("gen username : " + mqttUsername);
mqttConnectOptions.setUserName(mqttUsername);
System.out.println("show content : " + content);
System.out.println("show secret : " + deviceSecret);
String mqttPassword = "";
try {
mqttPassword = encryptHMAC("hmacsha1", content, deviceSecret);
} catch (Exception e) {
throw new RuntimeException(e);
}
System.out.println("calc password : " + mqttPassword);
mqttConnectOptions.setPassword(mqttPassword.toCharArray());
mqttConnectOptions.setAutomaticReconnect(true);
mqttConnectOptions.setCleanSession(false);
mqttConnectOptions.setMqttVersion(4);
mqttConnectOptions.setKeepAliveInterval(90);
mqttAndroidClient.connect(mqttConnectOptions, null, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
System.out.printf("connect to iot hub success.\n");
DisconnectedBufferOptions disconnectedBufferOptions = new DisconnectedBufferOptions();
disconnectedBufferOptions.setBufferEnabled(true);
disconnectedBufferOptions.setBufferSize(100);
disconnectedBufferOptions.setPersistBuffer(false);
disconnectedBufferOptions.setDeleteOldestMessages(false);
mqttAndroidClient.setBufferOpts(disconnectedBufferOptions);
subscribeToTopic();
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
System.out.printf("connect to iothub failed, exception : %s\n", exception.toString());
}
});
} catch (MqttException ex) {
ex.printStackTrace();
}
}
public void subscribeToTopic() {
try {
mqttAndroidClient.subscribe(subscriptionTopic, 0, null, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
System.out.println("subscribe topic success.");
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
System.out.println("subscribe topic failed.");
}
});
// THIS DOES NOT WORK!
mqttAndroidClient.subscribe(subscriptionTopic, 0, new IMqttMessageListener() {
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
// message Arrived!
System.out.println("Message: " + topic + " : " + new String(message.getPayload()));
}
});
} catch (MqttException ex) {
System.err.println("Exception whilst subscribing");
ex.printStackTrace();
}
}
public void messageArrived(String topic, MqttMessage message) throws Exception {
//System.out.println( message.toString());
//Toast.makeText(Jieshou.this, message.toString(),Toast.LENGTH_SHORT).show();
String mesg = new String(message.getPayload());
JsonParser jp = new JsonParser();
//将json字符串转化成json对象
JsonObject jo = jp.parse(mesg).getAsJsonObject();
//获取message对应的值
//获取address对应的值
String 手动状态 = jo.get("checkFailedData").getAsJsonObject().get("手动状态").getAsJsonObject().get("value").getAsJsonObject().get("value").getAsString();
//float cssl=Float.parseFloat(手动状态);
DI1 = 手动状态;
D1.setText(DI1);
String 自动状态 = jo.get("checkFailedData").getAsJsonObject().get("自动状态").getAsJsonObject().get("value").getAsJsonObject().get("value").getAsString();
DI2 = 自动状态;
D2.setText(DI2);
String EDI水箱液位 = jo.get("checkFailedData").getAsJsonObject().get("EDI水箱液位").getAsJsonObject().get("value").getAsJsonObject().get("value").getAsString();
AI1 = EDI水箱液位 + "cm";
A1.setText(AI1);
String 原水电导 = jo.get("checkFailedData").getAsJsonObject().get("原水电导").getAsJsonObject().get("value").getAsJsonObject().get("value").getAsString();
AI2 = 原水电导 + "us/cm";
A2.setText(AI2);
String 一级产水电导 = jo.get("checkFailedData").getAsJsonObject().get("一级产水电导").getAsJsonObject().get("value").getAsJsonObject().get("value").getAsString();
AI3 = 一级产水电导 + "us/cm";
A3.setText(AI3);
String EDI产水电阻 = jo.get("checkFailedData").getAsJsonObject().get("EDI产水电阻").getAsJsonObject().get("value").getAsJsonObject().get("value").getAsString();
AI4 = EDI产水电阻 + "MΩ.cm";
A4.setText(AI4);
String 输送电阻 = jo.get("checkFailedData").getAsJsonObject().get("输送回水电阻").getAsJsonObject().get("value").getAsJsonObject().get("value").getAsString();
AI5 = 输送电阻 + "MΩ.cm";
A5.setText(AI5);
String 超滤进水压力 = jo.get("checkFailedData").getAsJsonObject().get("超滤进水压力").getAsJsonObject().get("value").getAsJsonObject().get("value").getAsString();
AI6 = 超滤进水压力 + "Mpa";
A6.setText(AI6);
String 一级膜前压力1 = jo.get("checkFailedData").getAsJsonObject().get("一级膜前压力1").getAsJsonObject().get("value").getAsJsonObject().get("value").getAsString();
AI7 = 一级膜前压力1 + "Mpa";
A7.setText(AI7);
String 一级膜前压力2 = jo.get("checkFailedData").getAsJsonObject().get("一级膜前压力2").getAsJsonObject().get("value").getAsJsonObject().get("value").getAsString();
AI8 = 一级膜前压力2 + "Mpa";
A8.setText(AI8);
String EDI进水压力 = jo.get("checkFailedData").getAsJsonObject().get("EDI进水压力").getAsJsonObject().get("value").getAsJsonObject().get("value").getAsString();
AI9 = EDI进水压力 + "Mpa";
A9.setText(AI9);
String 输送压力 = jo.get("checkFailedData").getAsJsonObject().get("输送压力").getAsJsonObject().get("value").getAsJsonObject().get("value").getAsString();
AI10 = 输送压力 + "Mpa";
A10.setText(AI10);
String 超滤产水流量 = jo.get("checkFailedData").getAsJsonObject().get("超滤产水流量").getAsJsonObject().get("value").getAsJsonObject().get("value").getAsString();
AI11 = 超滤产水流量 + "L/h";
A11.setText(AI11);
String 一级产水流量 = jo.get("checkFailedData").getAsJsonObject().get("一级产水流量").getAsJsonObject().get("value").getAsJsonObject().get("value").getAsString();
AI12 = 一级产水流量 + "L/h";
A12.setText(AI12);
String 一级浓水流量 = jo.get("checkFailedData").getAsJsonObject().get("一级浓水流量").getAsJsonObject().get("value").getAsJsonObject().get("value").getAsString();
AI13 = 一级浓水流量 + "L/h";
A13.setText(AI13);
String EDI产水流量 = jo.get("checkFailedData").getAsJsonObject().get("EDI产水流量").getAsJsonObject().get("value").getAsJsonObject().get("value").getAsString();
AI14 = EDI产水流量 + "L/h";
A14.setText(AI14);
String 超滤进水温度 = jo.get("checkFailedData").getAsJsonObject().get("超滤进水温度").getAsJsonObject().get("value").getAsJsonObject().get("value").getAsString();
AI15 = 超滤进水温度 + "℃";
A15.setText(AI15);
String EDI产水温度 = jo.get("checkFailedData").getAsJsonObject().get("EDI产水温度").getAsJsonObject().get("value").getAsJsonObject().get("value").getAsString();
AI16 = EDI产水温度 + "℃";
A16.setText(AI16);
设备端、客户端虚拟同时完成,如图所示:
设备端详情
APP客户端详情:
以上就是今天要讲的内容,本文仅仅简单介绍了给设备做一个独立自主的APP,而完全版的物联网APP源码请到我的学习资源里边找一找。