TA的每日心情 | 奋斗 2023-7-8 16:17 |
---|
签到天数: 971 天 连续签到: 1 天 [LV.10]以坛为家III
|
在 钛极小龟 MQTT 的教程多次提到MQTT本地服务器 的启动与配置,其实不明白那东西是怎么来的,出自哪里?
后面找到使用mosquitto 创建mqtt服务器 与 使用Eclipse Paho实现客户端的连接 的方法进行实现
mosquitto 官方地址 http://mosquitto.org/
Eclipse Paho 官网 https://www.eclipse.org/paho/
mosquitto 的启动
Eclipse Paho 的设置
其他的按照教程操作
- package tikit.t600.esp8266b;
- import java.io.IOException;
- import tijos.framework.devicecenter.TiGPIO;
- import tijos.framework.networkcenter.dns.TiDNS;
- import tijos.framework.networkcenter.mqtt.MqttClient;
- import tijos.framework.networkcenter.mqtt.MqttClientListener;
- import tijos.framework.networkcenter.mqtt.MqttConnectOptions;
- import tijos.framework.networkcenter.mqtt.MqttException;
- import tijos.framework.platform.wlan.TiWiFi;
- import tijos.framework.transducer.relay.TiRelay1CH;
- import tijos.framework.util.json.JSONObject;
- import tijos.framework.util.logging.Logger;
- /**
- *
- * MQTT Client 例程, 在运行此例程时请确保MQTT Server地址及用户名密码正确
- *
- * @author TiJOS
- */
- /**
- * MQTT 事件监听
- *
- */
- class MqttEventLister implements MqttClientListener
- {
-
- @Override
- public void connectComplete(Object userContext, boolean reconnect) {
- Logger.info("MqttEventLister","connectComplete");
-
- }
- @Override
- public void connectionLost(Object userContext) {
- Logger.info("MqttEventLister","connectionLost");
-
- }
-
- @Override
- public void onMqttConnectFailure(Object userContext, int cause) {
- Logger.info("MqttEventLister","onMqttConnectFailure cause = " + cause);
-
- }
- @Override
- public void onMqttConnectSuccess(Object userContext) {
- Logger.info("MqttEventLister","onMqttConnectSuccess");
-
- }
-
- @Override
- public void messageArrived(Object userContext, String topic, byte[] payload) {
- Logger.info("MqttEventLister","messageArrived topic = " + topic + new String(payload));
- try {
- TiRelay1CH relay1ch = (TiRelay1CH)userContext;
- //JSON格式获取开关值
- JSONObject newObj = new JSONObject(new String(payload));
- int num = newObj.getInt("switch");
- //如果订阅为1则打开开关,订阅值为零关闭开关
- if(num == 1 ) {
- relay1ch.turnOn();
- }else {
- relay1ch.turnOff();
- }
- } catch (IOException e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- @Override
- public void publishCompleted(Object userContext, int msgId, String topic, int result) {
- Logger.info("MqttEventLister","publishCompleted topic = " + topic + " result = " + result + "msgid = " + msgId);
-
- }
- @Override
- public void subscribeCompleted(Object userContext, int msgId,String topic, int result) {
- Logger.info("MqttEventLister","subscribeCompleted topic = " + topic + " result " + result + "msgid = " + msgId);
-
- }
- @Override
- public void unsubscribeCompleted(Object userContext, int msgId, String topic, int result) {
- Logger.info("MqttEventLister","unsubscribeCompleted topic = " + topic + "result " + result + "msgid = " + msgId);
- }
- }
- public class MqttClientDemo {
- public static void main(String args[]) {
-
- try{
- //启动WLAN及DNS
- TiWiFi.getInstance().startup(10);
- TiDNS.getInstance().startup();
- }
- catch(IOException ex)
- {
- ex.printStackTrace();
- return ;
- }
- //MQTT Server 地址
- final String broker = "tcp://192.168.0.4:1883";
- //MQTT Server 用户名
- final String username = "demo";
- //MQTT Server 密码
- final String password = "tijos";
- //ClientID
- final String clientId = "mqtt_test_java_tijos";
-
- //MQTT连接设置
- MqttConnectOptions connOpts = new MqttConnectOptions();
- connOpts.setUserName(username);
- connOpts.setPassword(password);
- //允许自动重新连接
- connOpts.setAutomaticReconnect(true);
- MqttClient mqttClient = new MqttClient(broker, clientId);
-
- int qos = 1;
- try {
- // GPIO资源分配,GPIO0的PIN2
- TiGPIO gpio0 = TiGPIO.open(0, 2);
- // GPIO总线资源与继电器对象绑定
- TiRelay1CH relay1ch = new TiRelay1CH(gpio0, 2);
- //订阅监听
- mqttClient.SetMqttClientListener(new MqttEventLister());
-
- //连接MQTT服务器
- mqttClient.connect(connOpts, relay1ch);
-
- //主题topic
- String topic = "/SmartSocket/12345678/cmd";
- String pubtopic = "/SmartSocket/12345678/data";
-
- //订阅topic
- int msgId = mqttClient.subscribe(topic, qos);
- Logger.info("MQTTClientDemo", "Subscribe to topic: " + topic + " msgid = " + msgId);
-
- while(true)
- {
- if((relay1ch.isTurnedOn()?1:0)==1) {
- //发布
- msgId = mqttClient.publish(pubtopic, "{switch:1}".getBytes(), qos, false);
- Logger.info("MQTTClientDemo", "Topic " + topic + "Publish message: " + "{switch:1}" + " msgid = " + msgId);
- }else {
- msgId = mqttClient.publish(pubtopic, "{switch:0}".getBytes(), qos, false);
- Logger.info("MQTTClientDemo", "Topic " + topic + "Publish message: " + "{switch:0}" + " msgid = " + msgId);
- }
-
- Thread.sleep(2000);
- }
- } catch(Exception ex) {
-
- ex.printStackTrace();
- }
- finally
- {
- try {
- //关闭MQTT Server
- mqttClient.close();
- } catch (MqttException e) {
- /*ignore*/
- e.printStackTrace();
- }
- }
- }
- }
复制代码
//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 设置相同即可以
|
|