這期內(nèi)容當中小編將會給大家?guī)碛嘘P(guān)如何解析LoRaWAN設備數(shù)據(jù)及開源MQTT SDK設備端模擬,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
為四子王等地區(qū)用戶提供了全套網(wǎng)頁設計制作服務,及四子王網(wǎng)站建設行業(yè)解決方案。主營業(yè)務為成都網(wǎng)站設計、做網(wǎng)站、四子王網(wǎng)站設計,以傳統(tǒng)方式定制建設網(wǎng)站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
LoRaWAN設備與物聯(lián)網(wǎng)平臺的通信數(shù)據(jù)格式為透傳/自定義,因此需要使用數(shù)據(jù)解析腳本,解析上下行數(shù)據(jù)。本文主要以阿里云官方文檔LoRaWAN設備數(shù)據(jù)解析為基礎(chǔ),基于開源MQTT SDK,實現(xiàn)完整的: 設備<->云端消息鏈路測試。
前期準備
1、創(chuàng)建產(chǎn)品,因為這邊沒有入網(wǎng)憑證,使用WiFi聯(lián)網(wǎng)方式,數(shù)據(jù)格式:透傳/自定義:
cdn.com/597e368e382cffc50012a9ecfd3e0a0db2deb00c.png">
2、添加物模型,可以直接參考官方文檔說明逐個添加,這里提供對應物模型的完整文本,可以copy內(nèi)容到本地自己創(chuàng)建的:model.json文件,然后物聯(lián)網(wǎng)平臺管理控制臺直接導入:
{ "schema":"https://iotx-tsl.oss-ap-southeast-1.aliyuncs.com/schema.json", "profile":{ "productKey":"********">
3、添加腳本并測試,腳本使用官方附錄:示例腳本即可,測試正常后注意點擊提交。
虛擬設備調(diào)試
5、在線發(fā)送
7、二進制數(shù)據(jù)Base64編碼(對應截圖中使用的AAEC的計算方法)
import sun.misc.BASE64Encoder; import java.io.IOException; public class ByteToBase64 { public static void main(String[] args) throws IOException { String data = "000102"; // 待轉(zhuǎn)換的十六進制數(shù)據(jù)對應的字符串 byte[] bytes = hexToByteArray(data); String base64Str = getBase64String(bytes); System.out.println("base64Str: ">
8、設備端代碼
import com.alibaba.taro.AliyunIoTSignUtil; import org.eclipse.paho.client.mqttv3.*; import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; import sun.misc.BASE64Encoder; import java.io.IOException; import java.util.HashMap; import java.util.Map; // 透傳類設備測試 public class IoTDemoPubSubDemo { public static String productKey = "********"; public static String deviceName = "device2"; public static String deviceSecret = "*********"; public static String regionId = "cn-shanghai"; // 物模型-屬性上報topic private static String pubTopic = "/sys/" + productKey + "/" + deviceName + "/thing/model/up_raw"; // 物模型-訂閱屬性Topic private static String subTopic = "/sys/" + productKey + "/" + deviceName + "/thing/model/down_raw"; private static MqttClient mqttClient; public static void main(String [] args){ initAliyunIoTClient(); // 初始化Client // ScheduledExecutorService scheduledThreadPool = new ScheduledThreadPoolExecutor(1, // new ThreadFactoryBuilder().setNameFormat("thread-runner-%d").build()); // // scheduledThreadPool.scheduleAtFixedRate(()->postDeviceProperties(), 10,10, TimeUnit.SECONDS); // 匯報屬性 postDeviceProperties(); try { mqttClient.subscribe(subTopic); // 訂閱Topic } catch (MqttException e) { System.out.println("error:" + e.getMessage()); e.printStackTrace(); } // 設置訂閱監(jiān)聽 mqttClient.setCallback(new MqttCallback() { @Override public void connectionLost(Throwable throwable) { System.out.println("connection Lost"); } @Override public void messageArrived(String s, MqttMessage mqttMessage) throws Exception { System.out.println("Sub message"); System.out.println("Topic : " + s); System.out.println("16進制形式輸出:"); System.out.println(bytes2hex(mqttMessage.getPayload())); System.out.println("10進制形式輸出:"); byte[] bytes = mqttMessage.getPayload(); for (byte t:bytes) { System.out.print(t + " "); } } @Override public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { } }); } /** * 初始化 Client 對象 */ private static void initAliyunIoTClient() { try { // 構(gòu)造連接需要的參數(shù) String clientId = "java" + System.currentTimeMillis(); Map<String, String> params = new HashMap<>(16); params.put("productKey", productKey); params.put("deviceName", deviceName); params.put("clientId", clientId); String timestamp = String.valueOf(System.currentTimeMillis()); params.put("timestamp", timestamp); // cn-shanghai String targetServer = "tcp://" + productKey + ".iot-as-mqtt."+regionId+".aliyuncs.com:1883"; String mqttclientId = clientId + "|securemode=3,signmethod=hmacsha1,timestamp=" + timestamp + "|"; String mqttUsername = deviceName + "&" + productKey; String mqttPassword = AliyunIoTSignUtil.sign(params, deviceSecret, "hmacsha1"); connectMqtt(targetServer, mqttclientId, mqttUsername, mqttPassword); } catch (Exception e) { System.out.println("initAliyunIoTClient error " + e.getMessage()); } } public static void connectMqtt(String url, String clientId, String mqttUsername, String mqttPassword) throws Exception { MemoryPersistence persistence = new MemoryPersistence(); mqttClient = new MqttClient(url, clientId, persistence); MqttConnectOptions connOpts = new MqttConnectOptions(); // MQTT 3.1.1 connOpts.setMqttVersion(4); connOpts.setAutomaticReconnect(false); connOpts.setCleanSession(true); connOpts.setUserName(mqttUsername); connOpts.setPassword(mqttPassword.toCharArray()); connOpts.setKeepAliveInterval(60); mqttClient.connect(connOpts); } /** * 匯報屬性 */ private static void postDeviceProperties() { try { //上報數(shù)據(jù) //高級版 物模型-屬性上報payload System.out.println("上報屬性值"); String hexString = "000111"; byte[] payLoad = hexToByteArray(hexString); MqttMessage message = new MqttMessage(payLoad); message.setQos(0); mqttClient.publish(pubTopic, message); } catch (Exception e) { System.out.println(e.getMessage()); } } // 十進制byte[] 轉(zhuǎn)16進制 String public static String bytes2hex(byte[] bytes) { StringBuilder sb = new StringBuilder(); String tmp = null; for (byte b : bytes) { // 將每個字節(jié)與0xFF進行與運算,然后轉(zhuǎn)化為10進制,然后借助于Integer再轉(zhuǎn)化為16進制 tmp = Integer.toHexString(0xFF & b); if (tmp.length() == 1) { tmp = "0" + tmp; } sb.append(tmp); } return sb.toString(); } /** * hex字符串轉(zhuǎn)byte數(shù)組 * @param inHex 待轉(zhuǎn)換的Hex字符串 * @return 轉(zhuǎn)換后的byte數(shù)組結(jié)果 */ public static byte[] hexToByteArray(String inHex){ int hexlen = inHex.length(); byte[] result; if (hexlen % 2 == 1){ //奇數(shù) hexlen++; result = new byte[(hexlen/2)]; inHex="0"+inHex; }else { //偶數(shù) result = new byte[(hexlen/2)]; } int j=0; for (int i = 0; i < hexlen; i+=2){ result[j]=hexToByte(inHex.substring(i,i+2)); j++; } return result; } /** * Hex字符串轉(zhuǎn)byte * @param inHex 待轉(zhuǎn)換的Hex字符串 * @return 轉(zhuǎn)換后的byte */ public static byte hexToByte(String inHex) { return (byte) Integer.parseInt(inHex, 16); } }
9、設備運行狀態(tài)
10、在線調(diào)試服務調(diào)用
{ "MaxTemp": 50, "MinTemp": 8, "MaxHumi": 90, "MinHumi": 10 }
11、設備端下行消息監(jiān)聽
上報屬性值 Sub message Topic : /sys/********/device2/thing/model/down_raw 16進制形式輸出: 5d0a000332085a0a 10進制形式輸出: 93 10 0 3 50 8 90 10
12、數(shù)據(jù)腳本解析
上述就是小編為大家分享的如何解析LoRaWAN設備數(shù)據(jù)及開源MQTT SDK設備端模擬了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享文章:如何解析LoRaWAN設備數(shù)據(jù)及開源MQTTSDK設備端模擬
本文來源:http://jinyejixie.com/article2/jjhcic.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導航、定制網(wǎng)站、微信公眾號、企業(yè)建站、網(wǎng)站內(nèi)鏈、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)