查看: 1610|回复: 0

[原创] 【 钛极小龟】MQTT 智能插座疑惑与尝试

[复制链接]
  • TA的每日心情
    奋斗
    2023-7-8 16:17
  • 签到天数: 971 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2018-12-11 16:11:51 | 显示全部楼层 |阅读模式
    分享到:
    在 钛极小龟 MQTT 的教程多次提到MQTT本地服务器 的启动与配置,其实不明白那东西是怎么来的,出自哪里?

    后面找到使用mosquitto 创建mqtt服务器 与 使用Eclipse Paho实现客户端的连接 的方法进行实现
    mosquitto 官方地址 http://mosquitto.org/
    Eclipse Paho 官网 https://www.eclipse.org/paho/

    mosquitto  的启动
    1.png

    Eclipse Paho 的设置
    2.png
    3.png

    其他的按照教程操作
    1. package tikit.t600.esp8266b;

    2. import java.io.IOException;

    3. import tijos.framework.devicecenter.TiGPIO;
    4. import tijos.framework.networkcenter.dns.TiDNS;
    5. import tijos.framework.networkcenter.mqtt.MqttClient;
    6. import tijos.framework.networkcenter.mqtt.MqttClientListener;
    7. import tijos.framework.networkcenter.mqtt.MqttConnectOptions;
    8. import tijos.framework.networkcenter.mqtt.MqttException;
    9. import tijos.framework.platform.wlan.TiWiFi;
    10. import tijos.framework.transducer.relay.TiRelay1CH;
    11. import tijos.framework.util.json.JSONObject;
    12. import tijos.framework.util.logging.Logger;

    13. /**
    14. *
    15. * MQTT Client 例程, 在运行此例程时请确保MQTT Server地址及用户名密码正确
    16. *
    17. * @author TiJOS
    18. */

    19. /**
    20. * MQTT 事件监听
    21. *
    22. */
    23. class MqttEventLister implements MqttClientListener
    24. {
    25.        
    26.         @Override
    27.         public void connectComplete(Object userContext, boolean reconnect) {
    28.                 Logger.info("MqttEventLister","connectComplete");
    29.                
    30.         }

    31.         @Override
    32.         public void connectionLost(Object userContext) {
    33.                 Logger.info("MqttEventLister","connectionLost");
    34.                
    35.         }
    36.        
    37.         @Override
    38.         public void onMqttConnectFailure(Object userContext, int cause) {
    39.                 Logger.info("MqttEventLister","onMqttConnectFailure cause = " + cause);
    40.                
    41.         }

    42.         @Override
    43.         public void onMqttConnectSuccess(Object userContext) {
    44.                 Logger.info("MqttEventLister","onMqttConnectSuccess");
    45.                
    46.         }
    47.        

    48.         @Override
    49.         public void messageArrived(Object userContext, String topic, byte[] payload) {
    50.                 Logger.info("MqttEventLister","messageArrived topic = " + topic + new String(payload));
    51.                 try {
    52.                         TiRelay1CH relay1ch = (TiRelay1CH)userContext;
    53.                         //JSON格式获取开关值
    54.                         JSONObject newObj = new JSONObject(new String(payload));
    55.                         int num = newObj.getInt("switch");
    56.                         //如果订阅为1则打开开关,订阅值为零关闭开关
    57.                         if(num == 1 ) {
    58.                                 relay1ch.turnOn();
    59.                         }else {
    60.                                 relay1ch.turnOff();
    61.                         }
    62.                 } catch (IOException e) {
    63.                         // TODO: handle exception
    64.                         e.printStackTrace();
    65.                 }
    66.         }

    67.         @Override
    68.         public void publishCompleted(Object userContext, int msgId, String topic, int result) {
    69.                 Logger.info("MqttEventLister","publishCompleted topic = " + topic + " result = " + result + "msgid = " + msgId);
    70.        
    71.         }

    72.         @Override
    73.         public void subscribeCompleted(Object userContext, int msgId,String topic, int result) {
    74.                 Logger.info("MqttEventLister","subscribeCompleted topic = " + topic + " result " + result + "msgid = " + msgId);
    75.        
    76.         }

    77.         @Override
    78.         public void unsubscribeCompleted(Object userContext, int msgId, String topic, int result) {
    79.                 Logger.info("MqttEventLister","unsubscribeCompleted topic = " + topic + "result " + result + "msgid = " + msgId);

    80.         }

    81. }

    82. public class MqttClientDemo {

    83.         public static void main(String args[]) {
    84.                
    85.                 try{
    86.                 //启动WLAN及DNS
    87.                 TiWiFi.getInstance().startup(10);
    88.                 TiDNS.getInstance().startup();
    89.                 }
    90.                 catch(IOException ex)
    91.                 {
    92.                         ex.printStackTrace();
    93.                         return ;
    94.                 }
    95.                 //MQTT Server 地址
    96.                 final String broker       = "tcp://192.168.0.4:1883";
    97.                 //MQTT Server 用户名
    98.         final String username     = "demo";
    99.       //MQTT Server 密码
    100.         final String password     = "tijos";

    101.         //ClientID
    102.         final String clientId     = "mqtt_test_java_tijos";
    103.                 
    104.                 //MQTT连接设置
    105.             MqttConnectOptions connOpts = new MqttConnectOptions();
    106.             connOpts.setUserName(username);
    107.             connOpts.setPassword(password);
    108.             //允许自动重新连接
    109.             connOpts.setAutomaticReconnect(true);

    110.             MqttClient mqttClient = new MqttClient(broker, clientId);
    111.       
    112.             int qos = 1;

    113.                 try {
    114.                         // GPIO资源分配,GPIO0的PIN2
    115.                         TiGPIO gpio0 = TiGPIO.open(0, 2);
    116.                         // GPIO总线资源与继电器对象绑定
    117.                         TiRelay1CH relay1ch = new TiRelay1CH(gpio0, 2);
    118.                         //订阅监听
    119.                         mqttClient.SetMqttClientListener(new MqttEventLister());
    120.        
    121.                         //连接MQTT服务器
    122.                     mqttClient.connect(connOpts, relay1ch);
    123.                    
    124.                     //主题topic
    125.                     String topic        = "/SmartSocket/12345678/cmd";
    126.                     String pubtopic     = "/SmartSocket/12345678/data";
    127.                    
    128.                     //订阅topic
    129.                     int msgId = mqttClient.subscribe(topic, qos);
    130.                     Logger.info("MQTTClientDemo", "Subscribe to topic: " + topic + " msgid = " + msgId);
    131.                 

    132.                     while(true)
    133.                     {   
    134.                         if((relay1ch.isTurnedOn()?1:0)==1) {
    135.                                 //发布
    136.                                 msgId = mqttClient.publish(pubtopic, "{switch:1}".getBytes(), qos, false);
    137.                                 Logger.info("MQTTClientDemo", "Topic " + topic + "Publish message: " + "{switch:1}"  + " msgid = " + msgId);
    138.                         }else {
    139.                                 msgId = mqttClient.publish(pubtopic, "{switch:0}".getBytes(), qos, false);
    140.                                 Logger.info("MQTTClientDemo", "Topic " + topic + "Publish message: " + "{switch:0}"  + " msgid = " + msgId);
    141.                         }
    142.                         
    143.                             Thread.sleep(2000);
    144.                    }

    145.                 } catch(Exception ex) {
    146.                        
    147.                     ex.printStackTrace();
    148.                 }
    149.                 finally
    150.                 {       
    151.                         try {
    152.                                 //关闭MQTT Server
    153.                                         mqttClient.close();
    154.                                 } catch (MqttException e) {
    155.                                         /*ignore*/
    156.                                         e.printStackTrace();
    157.                                 }
    158.                 }
    159.             }
    160. }
    复制代码


    //MQTT Server 地址
    final String broker       = "tcp://192.168.0.4:1883";
    //MQTT Server 用户名
    final String username     = "demo";
    //MQTT Server 密码
    final String password     = "tijos";


    //主题topic
    String topic        = "/SmartSocket/12345678/cmd";
    String pubtopic     = "/SmartSocket/12345678/data";


    Eclipse Paho 设置相同即可以

    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则



    手机版|小黑屋|与非网

    GMT+8, 2024-4-23 20:14 , Processed in 0.114967 second(s), 15 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.