当前位置:首页 » 数据仓库 » 阿里云mqttqos如何配置
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

阿里云mqttqos如何配置

发布时间: 2022-05-18 04:30:55

⑴ mosquitto.conf 怎么配置

1、下载mosquitto安装文件(openssl-1.0.0.tar.gz这里为只读topicread主题useruserName2这里为可读可写topic主题topic#(或+)表示可以读写任何主题到这里用户密码及权限已配置完成,订阅和发布的时候加上用户名及密码即可验证:例如:订阅client=newMqttClient("tcp://127.0.0.1:1883","java_client0000000000");//回调处理类Mybackcallback=newMyback();client.setCallback(callback);//创建连接可选项信息MqttConnectOptionsconOptions=newMqttConnectOptions();conOptions.setCleanSession(false);conOptions.setUserName("userName");conOptions.setPassword("pwd".toCharArray());//连接brokerclient.connect(conOptions);client.subscribe("主题");}发布:MqttClientclient=newMqttClient("tcp://127.0.0.1:1883","mqttserver-pub");MqttTopictopic=client.getTopic("主题");MqttMessagemessage=newMqttMessage(topic.getName().getBytes());message.setQos(1);MqttConnectOptionsoptions=newMqttConnectOptions();options.setUserName("userName");options.setPassword("pwd".toCharArray());client.connect(options);topic.publish(message);}即可验证!

⑵ 如何采用mqtt协议实现android消息推送

使用一个代理服务器message broker,客户端client连接上这个服务器,然后告诉服务器,可以接收哪些类型的消息,同时client也可以发布自己的消息,这些消息根据协议的内容,可以别的client获取。这样就实现了消息推送。
消息推送是通过一定的技术标准或协议,在互联网上通过定期传送用户需要的信息来减少信息过载的一项新技术。
如果想要使用消息推送,推荐使用深圳极光的消息推送系统。深圳极光是国内首个为移动应用开发者提供专业、高效的消息推送服务的产品。品牌成长的过程,就是与客户肩并肩迈向成功的过程。极光将以市场为导向,以创新为动力,以技术为支持,不断用心努力,为每一位尊贵的客户提供极致的服务。

⑶ 想问一下,如何用MQTT协议搭建一个物联网空调的阿里云服务器,谢谢

MQTT推荐使用EMQ,来自国人开发的产品

⑷ mqttkit qos1的话怎么办

前面的几个字母
你要解释清楚
不然我们不好回答

⑸ 如何采用MQTT协议实现android消息推送

MQTT是一项消息传递技术,由IBM再2001年发布。

总结一下,机制就是使用一个代理服务器messagebroker,
客户端client连接上这个服务器,然后告诉服务器说,我可以接收哪些类型的消息,
同时,client也可以发布自己的消息,这些消息根据协议的内容,可以被其他client获取。
只要手机客户端,连上服务器,然后就可以接收和发布消息了,不用自己写socket什么了,

低带宽,低耗电量,代码量也少,很简单吧。

package com.pig.test.mqtt;

import com.ibm.mqtt.MqttClient;

import
com.ibm.mqtt.MqttException;
import com.ibm.mqtt.MqttSimpleCallback;

public class SubscribeClient {
private final static String
CONNECTION_STRING = "tcp://192.168.1.60:1883";
private final static boolean
CLEAN_START = true;
private final static short KEEP_ALIVE =
30;//低耗网络,但是又需要及时获取数据,心跳30s
private final static String CLIENT_ID =
"client1";
private final static String[] TOPICS =
{
"Test/TestTopics/Topic1",
"Test/TestTopics/Topic2",
"Test/TestTopics/Topic3",
"toku/client1"
};
private
final static int[] QOS_VALUES = {0, 0, 2,
0};

//////////////////
private MqttClient mqttClient =
null;

public SubscribeClient(String i){
try {
mqttClient =
new MqttClient(CONNECTION_STRING);
SimpleCallbackHandler
simpleCallbackHandler = new
SimpleCallbackHandler();
mqttClient.registerSimpleHandler(simpleCallbackHandler);//注册接收消息方法
mqttClient.connect(CLIENT_ID+i,
CLEAN_START, KEEP_ALIVE);
mqttClient.subscribe(TOPICS,
QOS_VALUES);//订阅接主题

/**
*
完成订阅后,可以增加心跳,保持网络通畅,也可以发布自己的消息
*/
mqttClient.publish(PUBLISH_TOPICS, "keepalive".getBytes(), QOS_VALUES[0],

true);

} catch (MqttException e) {
// TODO Auto-generated
catch block
e.printStackTrace();
}
}

/**
* 简单回调函数,处理client接收到的主题消息
* @author pig
*

*/
class SimpleCallbackHandler implements MqttSimpleCallback{

/**
* 当客户机和broker意外断开时触发
* 可以再此处理重新订阅

*/
@Override
public void connectionLost() throws Exception {
//
TODO Auto-generated method
stub
System.out.println("客户机和broker已经断开");
}

/**
* 客户端订阅消息后,该方法负责回调接收处理消息
*/
@Override
public void
publishArrived(String topicName, byte[] payload, int Qos, boolean retained)
throws Exception {
// TODO Auto-generated method
stub
System.out.println("订阅主题: " +
topicName);
System.out.println("消息数据: " + new
String(payload));
System.out.println("消息级别(0,1,2): " +
Qos);
System.out.println("是否是实时发送的消息(false=实时,true=服务器上保留的最后消息): " +
retained);
}

}

/**
* 高级回调
* @author pig

*
*/
class AdvancedCallbackHandler implements MqttSimpleCallback{

@Override
public void connectionLost() throws Exception {
//
TODO Auto-generated method stub

}

@Override
public void publishArrived(String arg0, byte[] arg1, int
arg2,
boolean arg3) throws Exception {
// TODO Auto-generated
method stub

}

}

/**
* @param args

*/
public static void main(String[] args) {
// TODO Auto-generated
method stub
new SubscribeClient("" + i);

}

}

broker服务器,MQTT的jar包,记得下载啊,没有就消息我咯~

到这里,如果完成IBM的MQTT协议实现push消息的实例的,
都会有个问题,好像没考虑到安全问题,如果客户端连上来作乱怎么办呢?

上面用的broker时rsmb的,mqtt的简单服务器。
IBM已经推出了MQTT V3.1版本,已经加入了安全验证机制,不要怕啦。

⑹ 如何使用 NB-Iot + Arino 通过MQTT协议上传数据至阿里云平台

有很多通信模块只有TCP功能,没有MQTT功能,比如WIFI,W5500等模块,还有一些NBIOT模块,但是又想连接阿里云物联网平台,官方提供了操作系统,需要自己移植,很麻烦,比较难看得懂。就在想有没有一些简单一定的方法。
心想MQTT是基于TCP的,能否使用TCP转MQTT?因此就想使用TCP协议然后转MQTT协议连接阿里云物联网平台,经过试验证明是可以的。

首先我们先分析一下如何登陆接入Onenet平台。

先从它数据格式开始分析。首先我们要从后台取出三个信息,我们以这个为例。

我们把产品ID,设备名称,设备秘钥,简称三要素 (具体是什么看你自己的设备)

其实阿里云物联网平台的MQTT协议用的就是标准的,不过它加入了自己的认证方式。

MQTT协议需要上传四个参数,报活时间,clientID,用户名,密码。

那么阿里云的就在clientID,用户名,密码做了手脚。

clientID比较长,按照一定的格式

用户名:设备名和秘钥组成

密码:使用了加密串进行了加密,有sha1或者MD5加密方式

下面我们来介绍一下

MQTT接入都是发十六进制的数据。

么我们发送的时候就是这样子的一串数据

0x74 0x00 0x04 0x4d 0x51 0x54 0x54 0x04 0xC0 0078 0033 0x61 0x62 0x63 0x7c 0x73 0x65 0x63 0x75 0x72 0x65 0x6d 0x6f 0x64 0x65 0x3d 0x33 0x2c 0x73 0x69 0x67

0x6e 0x6d 0x65 0x74 0x68 0x6f 0x64 0x3d 0x68 0x6d 0x61 0x63 0x73 0x68 0x61 0x31 0x2c 0x74 0x69 0x6d 0x65 0x73 0x74 0x61 0x6d 0x70 0x3d 0x31 0x32 0x30 0x7c 0009

0x35 0x36 0x37 0x38 0x26 0x31 0x32 0x33 0x34 0028 0x32 0x32 0x32 0x37 0x35 0x30 0x44 0x45 0x44 0x46 0x45 0x34 0x46 0x37 0x37 0x34 0x30 0x30 0x32 0x45 0x45 0x38 0x37 0x45 0x45 0x44 0x32 0x39 0x43 0x46 0x44 0x30 0x36 0x33 0x38 0x43 0x35 0x46 0x36 0x36

十六进制解释

数据长度:0x74

协议数据长度 0x00 0x04

协议类型: 0x4d 0x51 0x54 0x54

协议数据: 0x04 0xC0

keepAlive数据:0078

ClientID长度:0033

ClientID: 0x61 0x62 0x63 0x7c 0x73 0x65 0x63 0x75 0x72 0x65 0x6d 0x6f 0x64 0x65 0x3d 0x33 0x2c 0x73 0x69 0x67 0x6e 0x6d 0x65 0x74 0x68 0x6f 0x64 0x3d 0x68 0x6d 0x61 0x63 0x73 0x68 0x61 0x31 0x2c 0x74 0x69 0x6d 0x65 0x73 0x74 0x61 0x6d 0x70 0x3d 0x31 0x32 0x30 0x7c

用户名:0009

用户名: 0x35 0x36 0x37 0x38 0x26 0x31 0x32 0x33 0x34

密码长度:0028

密码: 0x32 0x32 0x32 0x37 0x35 0x30 0x44 0x45 0x44 0x46 0x45 0x34 0x46 0x37 0x37 0x34 0x30 0x30 0x32 0x45 0x45 0x38 0x37 0x45 0x45 0x44 0x32 0x39 0x43 0x46 0x44 0x30 0x36 0x33 0x38 0x43 0x35 0x46 0x36 0x36复制代码上面的就是连接服务器的连接包

下面呢,我们来做个发布包(上传数据到服务器)

0x30 0x1D 0009 2f7379732f706f7374 0x7b 0x70 0x61 0x72 0x61 0x6d 0x73 0x3a 0x7b 0x74 0x65 0x6d 0x70 0x3a 0x31 0x30 0x7d 0x7d

十六进制数据解释

数据头:0x30

数据长度:0x1D

TopicName数据长度:0009

TopicName数据内容:2f7379732f706f7374

主体json数据: 0x7b 0x70 0x61 0x72 0x61 0x6d 0x73 0x3a 0x7b 0x74 0x65 0x6d 0x70 0x3a 0x31 0x30 0x7d 0x7d复制代码以上就是连接阿里云的数据包格式及发布数据的格式,由于时间问题没有做订阅的数据包分析,下一次更新订阅的内容。

⑺ mosquitto 参数怎么配置

1.
retry_interval
当QoS为1或2的消息已经被发送后,mosquitto在一段时间内仍未接收到客户端的反馈消息,将重新发送消息。
默认为20秒
2.sys_interval
每隔一段时间将更新$SYS层级话题的状态,其中包含着proker的状态信息。
默认为10秒
3.store_clean_interval
表示间隔多长时间将不再被使用的消息销毁掉。该值越小,使用的内存就会越小但会需要更多的处理时间。
如果设置为0,表示不被
使用的消息将会及时销毁。
默认为10秒
4.
pid_file
默认为/var/run/mosquitto.pid
5.user

设置mosquitto启动用户
6.max_inflight_messages
表示允许多大数量的QoS为1或2消息被同时进行传输处理。这些消息包括正在进行握手的消息和进行重新发送的消息。默认为20个,
如果设置为0,表示不设限制;如果为1,则会确保消息被顺序处理。
7.max_queued_messages
表示允许多大数量的QoS为1或2消息在队列中进行排队。
默认为100个
8.max_connections
设置最大的连接数
-1表示不限制
9.autosave_interval
表示当开启持久化设置时,间隔多少时间mosquitto会把内存中的消息保存到磁盘中。默认为30分钟,当设置为0时,只有mosquitto
关闭的时候才会写的磁盘中。
10.persistence
设置为true时,所有的连接,订阅和消息数据都会被保存到磁盘的mosquitto.db文件中。当mosquitto重启的时候,它会从mosquitto.db文件中
重新加载数据。
11.persistence_location
默认为/var/lib/mosquitto/
12.log_dest

设置日志的输出目的地
可以是:stdout
stderr
syslog
topic

如果输出到某个文件的话可以这样设置log_dest
file
/var/log/mosquitto.log
要赋予对mosquitto.log文件的读写权限。
13.log_type
日志类型:debug,
error,
warning,
notice,
information,
subscribe,
unsubscribe,
websockets,
none,
all
14.log_timestamp

是否记录日志时间
15.clientid_prefixes
设置只有clientId以某个前缀开始的客户端才允许连接到mosquitto
broker.
16.allow_plicate_messages
如果一个客户端订阅了多个topic时,设置是否允许接收重复的消息。比如订阅了foo/#

foo/+/baz。
17.autosave_on_changes
If
true,
mosquitto
will
count
the
number
of
subscription
changes,
retained
messages
received
and
queued
messages
and
if
the
total
exceeds
autosave_interval
then
the
in-memory
database
will
be
saved
to
disk.
If
false,
mosquitto
will
save
the
in-memory
database
to
disk
by
treating
autosave_interval
as
a
time
in
seconds
18.persistent_client_expiration
持久订阅的过期设置。
对于将clean
session设置为false的持久订阅客户端,如果在一定的时间段里面没有重新连接mosquitto将会被移除。
这并不是一个标准的配置项,因为对于MQTT协议来说所有的持久订阅应该是永远有效的。
如:persistent_client_expiration
2mpersistent_client_expiration
14dpersistent_client_expiration
1y
h:小时d:天m:月y:年
19.queue_qos0_messages是否将QoS为0的消息计算到max_queued_messages参数中

更多相关内容可参考资料http://www.viiboo.cn

⑻ 在云服务器上搭建了mqtt,为什么手机连接不上mqtt,要怎么做才能连接上求求大神帮忙

MQTT协议是广泛应用的物联网协议,使用测试MQTT协议需要MQTT的代理。有两种方法使用MQTT服务,一是租用现成的MQTT服务器,如阿里云,网络云,华为云等公用的云平台提供的MQTT服务,使用公用的MQTT服务器的好处是省事,但如果仅仅用于测试学习还需要注册帐号,灵活性差些,有的平台还需要付费。另一方法是自己使用开源的MQTT组件来搭建。
MQTT服务器非常多,如apache的ActiveMQ,emtqqd,HiveMQ,Emitter,Mosquitto,Moquette等等。
这里介绍的是用轻量级的mosquitto开源项目来搭建一个属于自己的MQTT服务器。
第一步:需要安装一台linux主机,这不多介绍,可以使用真机安装也可以使用虚拟机安装。如果仅仅是自己测试使用都可以。
第二步:下载mosquitto需要的依赖
sudo apt-get install libssl-devsudo apt-get install uuid-devsudo apt-get install cmake

第三步:下载mosquitto并解压,现在mosquitto官网最新的版本是1.5.1
tar xzvf mosquitto-1.5.1.tar.gz
第四步:编译
cd mosquitto-1.5.1/
make
make install
第五步:启动mosquitto
./mosquitto -v
1535473957: mosquitto version 1.5.1 starting
1535473957: Using default config.
1535473957: Opening ipv4 listen socket on port 1883.
1535473957: Opening ipv6 listen socket on port 1883.
这时候mosquitto就会以默认的参数启动。如果需要带配置文件可以修改配置文件mosquitto.conf,
启动时候加上参数 -c,
./mosquitto -c mosquitto.conf
可以看到,mosquitto监听的端口为1883.
这时候我们的MQTT服务器就搭建好了。可找一个mqtt客户端来测试一下。
先发布一个主题“home/garden/fountain/2”
内容是“hello world”
这时候在mosquitto会打印出下面的log
535474247: New connection from 192.168.1.105 on port 1883.
1535474247: New client connected from 192.168.1.105 as MQTT_FX_Client (c1, k60).
1535474247: No will message specified.
1535474247: Sending CONNACK to MQTT_FX_Client (0, 0)
1535474307: Received PINGREQ from MQTT_FX_Client
1535474307: Sending PINGRESP to MQTT_FX_Client
1535474339: Received PUBLISH from MQTT_FX_Client (d0, q0, r0, m0, 'home/garden/fountain/2', ... (12 bytes))
1535474367: Received PINGREQ from MQTT_FX_Client
1535474367: Sending PINGRESP to MQTT_FX_Client

订阅主题“home/garden/fountain/2”

可以看到收到了自己发布的消息。
用wireshark抓包
可以看到抓到了一个MQTT的publish的报文。

⑼ 怎么将消息发送到mqtt代理服务器

通过Cocoa Pods添加MQTTKit

MQTTKit在github上链接https://github.com/NormanLeeIOS/MQTTKit#send-a-message,down下来。

cd到工程目录,输入pod install,用xcode打开工程的打开xcworkspace扩展名的文件。

如果不是MQTTKit存在更新的版本,则输入pod update。

新建一个MQTT的服务请求

NSString *clientID = ...
MQTTClient *client = [[MQTTClient alloc] initWithClientId:clientID];

发送消息,每次发送消息包括目标host和本地MQTT消息.具体MQTT格式消息见代码。这里Host可以是Server的IP,不需要host表解析。

// connect to the MQTT server
[self.client connectToHost:@"iot.eclipse.org"
completionHandler:^(NSUInteger code) {
if (code == ConnectionAccepted) {
// when the client is connected, send a MQTT message
[self.client publishString:@"Hello, MQTT"
toTopic:@"/MQTTKit/example"
withQos:AtMostOnce
retain:NO
completionHandler:^(int mid) {
NSLog(@"message has been delivered");
}];
}
}];

订阅主题并接受MQTT格式的消息,这部分在viewdidload中实现。

// define the handler that will be called when MQTT messages are received by the client
[self.client setMessageHandler:^(MQTTMessage *message) {
NSString *text = [message.payloadString];
NSLog(@"received message %@", text);
}];

// connect the MQTT client
[self.client connectToHost:@"iot.eclipse.org"
completionHandler:^(MQTTConnectionReturnCode code) {
if (code == ConnectionAccepted) {
// when the client is connected, subscribe to the topic to receive message.
[self.client subscribe:@"/MQTTKit/example"
withCompletionHandler:nil];
}
}];

断开连接

[self.client :^(NSUInteger code) {
// The client is disconnected when this completion handler is called
NSLog(@"MQTT client is disconnected");
}];

整个连接建立、发送消息、接受消息、断开连接都是通过Block的消息机制来实现,因此需要对block有很好地理解。

⑽ windows系统下的mosquitto 怎么配置

1、 下载mosquitto安装文件(http://mosquitto.org/files/source/)
2、 找到相应系统的安装文件安装,如果不想做任何设置直接在服务里启动就行。
3、配置文件
如果需要配置一些用户名、密码、用户权限的参数,则需要修改安装目录下的mosquitto.conf文件
下面来说说我用到的一些参数吧:
①用户密码: #password_file pwfile.example 后面跟着是用户密码配置文件,需写上绝对路径并且路径不带空格
②创建用户密码:打开doc窗口,进入mosquitto安装目录,运行mosquitto_passwd -c pwfile.example userName 回车,然后输入密码(密码输入两遍后,在该文件里会自动加密密码)
生成的文件内容格式例如:
userName:$6$Ls7JYQTdn9xagJJ2$/WArx/SAtFRKlvKKnHRCUg==
userName2:$6$bymgVcrtj+7wj8mR$+zmAxnOybqJvrBZboxX1XXPnz/TKZwz9aKQJ72zJym5A=
③如果想再增加用户,则执行mosquitto_passwd -u pwfile.example userName2即可
④用户权限:#acl_file aclfile.example 后面跟着是用户权限配置文件,需写上绝对路径并且路径
文件内容格式为:
user userName

/etc/ld.so.conf.d
mosquitto.conf
/usr/local/lib/python2.6/site-packages ( mosquitto.py )
/usr/local/bin
vi /etc/sysconfig/iptables
/usr/local/src/mosquitto-1.1.3/lib/python
make install
ldconfig
不改config.mk里面的东西
需要安装
yum -y install patch make gcc gcc-c++ gcc-g77 flex bison

centos5.6下 yum -y install gcc automake autoconf libtool make
yum -y install openssl openssl-devel vim-minimal

http://www.openssl.org/source/openssl-1.0.0.tar.gz
这里为只读
topic read 主题
user userName2
这里为可读可写
topic 主题
topic #(或+)表示可以读写任何主题
到这里用户密码及权限已配置完成,订阅和发布的时候加上用户名及密码即可验证:
例如:订阅
client = new MqttClient("tcp://127.0.0.1:1883","java_client0000000000");
// 回调处理类
Myback callback = new Myback();
client.setCallback(callback);
// 创建连接可选项信息
MqttConnectOptions conOptions = new MqttConnectOptions();
conOptions.setCleanSession(false);
conOptions.setUserName("userName");
conOptions.setPassword("pwd".toCharArray());
// 连接broker
client.connect(conOptions);
client.subscribe("主题");
}
发布:
MqttClient client = new MqttClient("tcp://127.0.0.1:1883","mqttserver-pub");
MqttTopic topic = client.getTopic("主题");
MqttMessage message = new MqttMessage(topic.getName().getBytes());
message.setQos(1);
MqttConnectOptions options = new MqttConnectOptions();
options.setUserName("userName");
options.setPassword("pwd".toCharArray());
client.connect(options);
topic.publish(message);
}
即可验证!