当前位置:首页 » 云服务器 » 如何发送到mqtt服务器

如何发送到mqtt服务器

发布时间: 2022-06-21 00:16:41

‘壹’ 怎么将消息发送到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有很好地理解。

‘贰’ 设备将数据发送到MQTT服务器,那服务器怎么数据转发到后台系统

MQTT直接存到后台数据库里,或交给消息中间件处理

‘叁’ apollo服务器mqtt单片机怎么往上发报文

MQTT+Mina的路过,我的思路是上行走http,下行接收走mqtt,以此实现A->B。 Publish包的消息体payload要进行扩展,你说的topic拓展我觉得意义不大

‘肆’ 在云服务器上搭建了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的报文。

‘伍’ 怎么搭建esp8266智能家居mqtt服务器

存储读取到的ESP8266的信息,然后用print发送到串口上去。 如果你的串口监视器打开了,就可以看到信息了。

热点内容
wincc图形编译在哪里 发布:2024-09-17 03:58:26 浏览:977
androidubuntu 发布:2024-09-17 03:50:27 浏览:701
识梦源码 发布:2024-09-17 03:50:18 浏览:26
诺基亚密码忘了打什么电话 发布:2024-09-17 03:27:09 浏览:555
树深度优先算法 发布:2024-09-17 03:26:58 浏览:472
跳转页源码 发布:2024-09-17 03:13:05 浏览:543
html文件上传表单 发布:2024-09-17 03:08:02 浏览:785
聊天软件编程 发布:2024-09-17 03:00:07 浏览:726
linuxoracle安装路径 发布:2024-09-17 01:57:29 浏览:688
两个安卓手机照片怎么同步 发布:2024-09-17 01:51:53 浏览:207