• 【物联网】MATLAB通过MQTT与阿里云和本地服务器建立连接


    1、MQTT服务器:本地Ubuntu搭建

    # 安装服务器
    sudo apt install mosquitto 
    sudo apt install mosquitto_pub 
    
    # 运行mqtt服务器
    mosquitto -v
    
    # 发布名称为nihao的订阅,信息内容为helloworld
    mosquitto_pub -t nihao -m helloworld
    
    # 查看ubuntu本机ip地址
    ifconfig
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 本机IP地址
      在这里插入图片描述

    参考资料:
    https://blog.csdn.net/qq_33406883/article/details/107492604

    2、MQTT服务器:阿里云平台搭建

    • 在设备中,可以找到clientId,username,passwd,mqttHostUrl等信息
      在这里插入图片描述

    • 在日志服务中,可以看到设备的行为信息。
      在这里插入图片描述

    • 关于MQTT参数的解释
      在这里插入图片描述
      在这里插入图片描述

    3、设备:MQTT.fx虚拟设备(通信测试成功√)

    成功
    成功
    设备MQTT.fx
    连接Ubuntu服务器
    连接阿里云服务器
    • 填写信息,点击Connect连接即可。
      具体可以参考之前的:https://gwj1314.blog.csdn.net/article/details/124575451
      在这里插入图片描述

    • 经过测试可以发现,连接云端时,阿里云设备在线,且可以互发消息。
      在这里插入图片描述

    • 连接本地Ubuntu服务器时,一样显示设备在线,且可以互相发消息
      在这里插入图片描述
      在这里插入图片描述

    4、设备:MATLAB虚拟设备(正式连接)

    成功
    失败
    设备MATLAB
    连接Ubuntu服务器
    连接阿里云服务器
    地址错误
    • 首先需要下载 MQTT in MATLAB ,放在matlab目录下。
      (链接:https://www.mathworks.com/matlabcentral/fileexchange/64303-mqtt-in-matlab)
      确保包含以下文件
      在这里插入图片描述

    • 文件内容如下(mqtt内容:mqtt.m)

      function obj = mqtt(varargin)
      
      try
          obj = mqttio.Mqtt(varargin{:});
      catch errExp
          throwAsCaller(errExp);
      end
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
    • 然后,未定义变量 “com” 或类 “com.mathworks.mqttclient.client.Client”
      需要执行以下命令。

      % 未定义"com” 或类“com.mathworks.mqttclient.client.Client” 报错解决方案
      % javaaddpath("C:\Program Files\MATLAB\R2022a\toolbox\MQTT in Matlab\matlab mqtt\jar\org.eclipse.paho.client.mqttv3-1.1.0.jar")
      % javaaddpath("C:\Program Files\MATLAB\R2022a\toolbox\MQTT in Matlab\matlab mqtt\mqttasync.jar")
      
      • 1
      • 2
      • 3
    4.1 使用MATLAB与本地服务器建立连接(成功)
    • 连接文件test.m
      在这里插入图片描述

      % 建立连接
      port = 1883;
      myMQTT= mqtt("tcp://10.10.21.94", "ClientID", "d06e83e7eba941299a224b47eabd00cf", 'Port', port);
      
      % 发送消息
      % 虚拟机ubuntu发布:mosquitto_pub -t nihao -m helloworld
      topic = "GoGoGo";
      message =  '{ "id": "123","version": "1.0","params": {    "isAlarm": { "value": true}  }} ' ;
      publish(myMQTT, topic, message);
      
      % 订阅消息
      Topic2 = 'nihao';
      mySub = subscribe(myMQTT,Topic2,'callback',@showMessage,'Qos',0);
      
      % 每次收到新消息时显示主题和消息的回调函数
      function showMessage(topic,data)
        disp(data);
      end
      
      % 未定义"com” 或类“com.mathworks.mqttclient.client.Client” 报错解决方案
      % javaaddpath("C:\Program Files\MATLAB\R2022a\toolbox\MQTT in Matlab\matlab mqtt\jar\org.eclipse.paho.client.mqttv3-1.1.0.jar")
      % javaaddpath("C:\Program Files\MATLAB\R2022a\toolbox\MQTT in Matlab\matlab mqtt\mqttasync.jar")
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
    • 经测试,可以发送消息到服务器。
      在这里插入图片描述

    • 也可以从服务器接受消息。
      在这里插入图片描述

    参考资料:
    https://blog.csdn.net/weixin_47545780/article/details/117398202
    https://blog.csdn.net/liu_text/article/details/122073119

    4.2 使用MATLAB与阿里云服务器建立连接(失败)
    • 如图,将mqtt函数对应的参数修改为阿里云的参数。
      运行后得到Unable to establish connection with broker. 无法与代理建立连接。
      在这里插入图片描述
      在这里插入图片描述
    clc;
    clear all;
    close all;
    
    % D001
    brokerAddress = 'tcp://xxxx';
    port = 1883;
    clientID = 'xxx';
    username = 'xxxxx';
    
    % 建立连接
    myMQTT= mqtt(brokerAddress, "ClientID", clientID, 'Port', port, 'Username', username, "Password", password);
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 相关阅读:
    【数据结构与算法】two X 树的遍历以及功能实现
    自学网络安全(黑客)
    前端学习路线参考
    经典文献阅读之--SuMa++
    只需根据接口文档,就能轻松开发 get 和 post 请求的脚本,你会做吗?
    优雅处理返回信息状态码:Result对象在Spring Boot中的应用
    go语言包管理和变量保护
    js sm4实现加密解密
    为什么需要森林防火气象站?
    毕业设计论文 免费领取源码 11819-SSM 球鞋资讯交流平台
  • 原文地址:https://blog.csdn.net/qq_33957603/article/details/126693222