MQTT连接问题

MQTT连接问题,mqtt,mosquitto,Mqtt,Mosquitto,我正在尝试使用MQTT构建物联网家庭网络,但在第一个障碍时失败了。我要绕圈子 我在Pi上运行mosquitto,但无法从Arduino连接到它。我可以从运行在windows笔记本电脑上的MQTT.fx进行连接,尽管是在同一局域网上 Arduino代码如下:我已经使用test.mosquitto.org服务器地址(85.119.83.194)和Pi的本地地址(192.168.1.200)运行了这个程序。MOSQUITO连接,但无法直接连接到Pi 我的路由器有问题吗???欢迎任何建议 #includ

我正在尝试使用MQTT构建物联网家庭网络,但在第一个障碍时失败了。我要绕圈子

我在Pi上运行mosquitto,但无法从Arduino连接到它。我可以从运行在windows笔记本电脑上的MQTT.fx进行连接,尽管是在同一局域网上

Arduino代码如下:我已经使用test.mosquitto.org服务器地址(85.119.83.194)和Pi的本地地址(192.168.1.200)运行了这个程序。MOSQUITO连接,但无法直接连接到Pi

我的路由器有问题吗???欢迎任何建议

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.
byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
byte server[] = { 192, 168, 1, 200};
byte ip[]     = { 192, 168, 1, 61 };

void callback(char* topic, byte* payload, unsigned int length) {
  // handle message arrived
  Serial.println("Message received");
}

EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);

void setup()
{
   Ethernet.begin(mac, ip);
   Serial.begin(9600);
   Serial.println("Ethernet Begin");

   if (client.connect("arduinoClient")) {
        Serial.println("Client connected");
        client.subscribe("/notification/turnlighton");
   }
   else{
          Serial.println("Client not connected"); 
   }
}

void loop()
{
  client.loop();
}
#包括
#包括
#包括
//使用适合您的网络的值更新这些值。
字节mac[]={0xDE,0xED,0xBA,0xFE,0xFE,0xED};
字节服务器[]={192,168,1200};
字节ip[]={192,168,1,61};
无效回调(字符*主题,字节*有效负载,无符号整数长度){
//处理消息到达
Serial.println(“收到消息”);
}
EthernetClient;
PubSubClient客户端(服务器,1883,回调,ethClient);
无效设置()
{
以太网开始(mac,ip);
Serial.begin(9600);
Serial.println(“以太网开始”);
if(client.connect(“arduinoClient”)){
Serial.println(“客户端连接”);
订阅(“/notification/turnlighton”);
}
否则{
Serial.println(“客户端未连接”);
}
}
void循环()
{
client.loop();
}
配置文件:

# Config file for mosquitto
#
# See mosquitto.conf(5) for more information.
#
# Default values are shown, uncomment to change.
#
# Use the # character to indicate a comment, but only if it is the 
# very first character on the line.

# =================================================================
# General configuration
# =================================================================

# Time in seconds to wait before resending an outgoing QoS=1 or 
# QoS=2 message.
#retry_interval 20

# Time in seconds between updates of the $SYS tree.
#sys_interval 10

# Time in seconds between cleaning the internal message store of 
# unreferenced messages. Lower values will result in lower memory 
# usage but more processor time, higher values will have the 
# opposite effect.
# Setting a value of 0 means the unreferenced messages will be 
# disposed of as quickly as possible.
#store_clean_interval 10

# Write process id to a file. Default is a blank string which means 
# a pid file shouldn't be written.
# This should be set to /var/run/mosquitto.pid if mosquitto is
# being run automatically on boot with an init script and 
# start-stop-daemon or similar.
#pid_file

# When run as root, drop privileges to this user and its primary 
# group.
# Leave blank to stay as root, but this is not recommended.
# If run as a non-root user, this setting has no effect.
# Note that on Windows this has no effect and so mosquitto should 
# be started by the user you wish it to run as.
#user mosquitto

# The maximum number of QoS 1 and 2 messages currently inflight per 
# client.
# This includes messages that are partway through handshakes and 
# those that are being retried. Defaults to 10. Set to 0 for no 
# maximum. Setting to 1 will guarantee in-order delivery of QoS 1 
# and 2 messages.
#max_inflight_messages 10

# The maximum number of QoS 1 and 2 messages to hold in a queue 
# above those that are currently in-flight.  Defaults to 100. Set 
# to 0 for no maximum (not recommended).
#max_queued_messages 100

# =================================================================
# Default listener
# =================================================================

# IP address/hostname to bind the default listener to. If not
# given, the default listener will not be bound to a specific 
# address and so will be accessible to all network interfaces.
# bind_address ip-address/host name
#bind_address

# Port to use for the default listener.
#port 1883

# The maximum number of client connections to allow. This is 
# a per listener setting.
# Default is -1, which means unlimited connections.
# Note that other process limits mean that unlimited connections 
# are not really possible. Typically the default maximum number of 
# connections possible is around 1024.
#max_connections -1

# =================================================================
# Extra listeners
# =================================================================

# Listen on a port/ip address combination. By using this variable 
# multiple times, mosquitto can listen on more than one port. If 
# this variable is used and neither bind_address nor port given, 
# then the default listener will not be started.
# The port number to listen on must be given. Optionally, an ip 
# address or host name may be supplied as a second argument. In 
# this case, mosquitto will attempt to bind the listener to that 
# address and so restrict access to the associated network and 
# interface. By default, mosquitto will listen on all interfaces.
# listener port-number [ip address/host name]
#listener

# The maximum number of client connections to allow. This is 
# a per listener setting.
# Default is -1, which means unlimited connections.
# Note that other process limits mean that unlimited connections 
# are not really possible. Typically the default maximum number of 
# connections possible is around 1024.
#max_connections -1

# The listener can be restricted to operating within a topic hierarchy using
# the mount_point option. This is achieved be prefixing the mount_point string
# to all topics for any clients connected to this listener. This prefixing only
# happens internally to the broker; the client will not see the prefix.
#mount_point

# =================================================================
# Persistence
# =================================================================

# If persistence is enabled, save the in-memory database to disk 
# every autosave_interval seconds. If set to 0, the persistence 
# database will only be written when mosquitto exits.
# Note that writing of the persistence database can be forced by 
# sending mosquitto a SIGUSR1 signal.
#autosave_interval 1800

# Save persistent message data to disk (true/false).
# This saves information about all messages, including 
# subscriptions, currently in-flight messages and retained 
# messages.
# retained_persistence is a synonym for this option.
persistence true

# The filename to use for the persistent database, not including 
# the path.
#persistence_file mosquitto.db

# Location for persistent database. Must include trailing /
# Default is an empty string (current directory).
# Set to /var/lib/mosquitto/ if running as a proper service.
persistence_location /var/lib/mosquitto/

# =================================================================
# Logging
# =================================================================

# Places to log to. Use multiple log_dest lines for multiple 
# logging destinations.
# Possible destinations are: stdout stderr syslog topic
# stdout and stderr log to the console on the named output.
# syslog uses the userspace syslog facility which usually ends up 
# in /var/log/messages or similar.
# topic logs to the broker topic '$SYS/broker/log/<severity>', 
# where severity is one of D, E, W, N, I which are debug, error, 
# warning, notice and information.
# Use "log_dest none" if you wish to disable logging.
log_dest none

# Types of messages to log. Use multiple log_type lines for logging
# multiple types of messages.
# Possible types are: debug, error, warning, notice, information, 
# none.
# Note that debug type messages are for decoding the incoming 
# network packets.
# They are not logged in syslog.
#log_type error
#log_type warning
#log_type notice
#log_type information

# If set to true, client connection and disconnection messages will be included
# in the log.
#connection_messages true

# If set to true, add a timestamp value to each log message.
#log_timestamp true

# =================================================================
# Security
# =================================================================

# If set, only clients that have a matching prefix on their 
# clientid will be allowed to connect to the broker. By default, 
# all clients may connect.
# For example, setting "secure-" here would mean a client "secure-
# client" could connect but another with clientid "mqtt" couldn't.
#clientid_prefixes

# Boolean value that determines whether clients that connect 
# without providing a username are allowed to connect. If set to 
# false then a password file should be created (see the 
# password_file option) to control authenticated client access. 
# Defaults to true.
#allow_anonymous true

# Control access to the broker using a password file. The file is a
# text file # of lines in the format:
# username:password
# The password (and colon) may be omitted if desired, although this 
# offers very little in the way of security.
#password_file

# Control access to topics on the broker using an access control list
# file. If this parameter is defined then only the topics listed will
# have access.
# Topic access is added with lines of the format:
#
# topic [read|write] <topic>
# 
# The access type is controlled using "read" or "write". This parameter
# is optional - if not given then the access is read/write.
# <topic> can contain the + or # wildcards as in subscriptions.
# 
# The first set of topics are applied to anonymous clients, assuming
# allow_anonymous is true. User specific topic ACLs are added after a 
# user line as follows:
#
# user <username>
#
# The username referred to here is the same as in password_file. It is
# not the clientid.
#
#
# If is also possible to define ACLs based on pattern substitution within the
# topic. The patterns available for substition are:
#
# %c to match the client id of the client
# %u to match the username of the client
#
# The substitution pattern must be the only text for that level of hierarchy.
#
# The form is the same as for the topic keyword, but using pattern as the
# keyword.
# Pattern ACLs apply to all users even if the "user" keyword has previously
# been given.
#
# pattern [read|write] <topic>
#
# Example:
#
# pattern write sensor/%u/data
#
#acl_file

# =================================================================
# Bridges
# =================================================================

# Experimental support for connecting multiple MQTT brokers 
# together.
# Specify multiple sets of connection, address and topic 
# configurations.
# Each connection must have a unique name.
# Only a single address per configuration is currently supported, 
# unlike in rsmb.
# The direction that the topic will be shared can be chosen by 
# specifying out, in or both, where the default value is out. 
# The QoS level of the bridged communication can be specified with the final
# topic option. The default QoS level is 2, to change the QoS the topic
# direction must also be given.
# Multiple topics can be specified per connection, but be careful 
# not to create any loops.
#connection <name>
#address <host>[:<port>]
#topic <topic> [[out | in | both] qos-level]

# Set the client id for this bridge connection. If not defined, 
# this defaults to 'name.hostname' where name is the connection 
# name and hostname is the hostname of this computer.
#clientid

# Set the clean session variable for this bridge.
# When set to true, when the bridge disconnects for any reason, all 
# messages and subscriptions will be cleaned up on the remote 
# broker. Note that with cleansession set to true, there may be a 
# significant amount of retained messages sent when the bridge 
# reconnects after losing its connection.
# When set to false, the subscriptions and messages are kept on the 
# remote broker, and delivered when the bridge reconnects.
#cleansession false

# If set to true, publish notification messages to the local and remote brokers
# giving information about the state of the bridge connection. Retained
# messages are published to the topic $SYS/bridge/connection/<clientid>/state.
# If the message is 1 then the connection is active, or 0 if the connection has
# failed.
#notifications true

# Set the keepalive interval for this bridge connection, in 
# seconds.
#keepalive_interval 60

# Set the start type of the bridge. This controls how the bridge starts and
# can be one of three types: automatic, lazy and once. Note that RSMB provides
# a fourth start type "manual" which isn't currently supported by mosquitto.
#
# "automatic" is the default start type and means that the bridge connection
# will be started automatically when the broker starts and also restarted
# after a short delay (30 seconds) if the connection fails.
#
# Bridges using the "lazy" start type will be started automatically when the
# number of queued messages exceeds the number set with the "threshold"
# parameter. It will be stopped automatically after the time set by the
# "idle_timeout" parameter. Use this start type if you wish the connection to
# only be active when it is needed.
#
# A bridge using the "once" start type will be started automatically when the
# broker starts but will not be restarted if the connection fails.
#start_type automatic

# Set the amount of time a bridge using the lazy start type must be idle before
# it will be stopped. Defaults to 60 seconds.
#idle_timeout 60

# Set the number of messages that need to be queued for a bridge with lazy
# start type to be restarted. Defaults to 10 messages.
# Must be less than max_queued_messages.
#threshold 10

# Set the username to use when connecting to an MQTT v3.1 broker 
# that requires authentication.
#username

# Set the password to use when connecting to an MQTT v3.1 broker 
# that requires authentication. This option is only used if 
# username is also set.
#password

# =================================================================
# External security checks
# =================================================================

# This section defines options for potential use with external
# databases. 
# They are intended to make it easier to add external security
# checks along with WITH_EXT_SECURITY_CHECKS. If you haven't written
# code to support another database and compiled support in, you are
# unlikely to need to change anything here.

# Database hostname.
#db_host

# Database port.
#db_port

# Database name.
#db_name

# Database username.
#db_username

# Database password.
#db_password

# =================================================================
# Unsupported rsmb options - for the future
# =================================================================

#autosave_on_changes
#addresses
#notification_topic
#round_robin

# =================================================================
# rsmb options - unlikely to ever be supported
# =================================================================

#ffdc_output
#max_log_entries
#trace_level
#trace_output
#try_private
MOSQUITO的配置文件 # #更多信息请参见mosquitto.conf(5)。 # #显示默认值,取消注释以进行更改。 # #使用#字符表示注释,但仅当它是 #这一行的第一个字符。 # ================================================================= #一般配置 # ================================================================= #重新发送传出QoS之前等待的时间(秒)=1或 #QoS=2消息。 #重试\u间隔20 #更新$SYS树之间的时间(秒)。 #系统间隔10 #清除的内部消息存储之间的时间(秒) #未引用的消息。较低的值将导致较低的内存 #使用率越高,但处理器时间越长,值越高,性能越好 #相反的效果。 #将值设置为0意味着未引用的消息将被删除 #尽快处理掉。 #仓库清洁间隔10 #将进程id写入文件。默认值是一个空白字符串,表示 #不应写入pid文件。 #如果MOSQUITO为,则应将其设置为/var/run/mosQUITO.pid #在引导时使用init脚本自动运行,并且 #启动停止守护程序或类似程序。 #pid_文件 #以root用户身份运行时,删除此用户及其主用户的权限 #小组。 #留空保留为root,但不建议这样做。 #如果以非root用户身份运行,则此设置无效。 #请注意,在Windows上,这没有效果,因此MOSQUITO应该 #由您希望其运行的用户启动。 #用户莫斯奎托 #每台飞机当前正在飞行的QoS 1和2消息的最大数量 #客户。 #这包括部分通过握手和 #那些正在重试的。默认值为10。设置为0表示否 #最大值。设置为1将保证按顺序交付QoS 1 #和2条信息。 #最多飞行信息10条 #队列中要保留的QoS 1和2消息的最大数量 #高于目前正在飞行的飞机。默认值为100。设置 #为0表示没有最大值(不建议)。 #最大排队消息数100 # ================================================================= #默认侦听器 # ================================================================= #要将默认侦听器绑定到的IP地址/主机名。如果不是 #给定,默认侦听器将不会绑定到特定的 #所有网络接口都可以访问地址和so。 #绑定地址ip地址/主机名 #绑定地址 #用于默认侦听器的端口。 #1883港 #允许的最大客户端连接数。这是 #每个侦听器的设置。 #默认值为-1,这意味着无限连接。 #请注意,其他进程限制意味着无限连接 #这是不可能的。通常为默认的最大数量 #可能的连接数约为1024。 #最大连接数-1 # ================================================================= #额外的听众 # ================================================================= #侦听端口/ip地址组合。通过使用这个变量 #MOSQUITO可以多次监听多个端口。如果 #使用此变量,既不提供绑定_地址也不提供端口, #那么默认侦听器将不会启动。 #必须提供要侦听的端口号。可选地,一个ip地址 #地址或主机名可以作为第二个参数提供。在里面 #在这种情况下,mosquitto将尝试将侦听器绑定到该对象 #地址,从而限制对关联网络的访问,并 #接口。默认情况下,MOSQUITO将侦听所有接口。 #侦听器端口号[ip地址/主机名] #听众 #允许的最大客户端连接数。这是 #每个侦听器的设置。 #默认值为-1,这意味着无限连接。 #请注意,其他进程限制意味着无限连接 #这是不可能的。通常为默认的最大数量 #可能的连接数约为1024。 #最大连接数-1 #可以使用以下命令限制侦听器在主题层次结构中操作 #mount_point选项。这是通过在mount_point字符串前面加前缀来实现的 #连接到此侦听器的任何客户端的所有主题。这只是前缀 #发生在经纪人的内部;客户端将看不到前缀。 #山角 # ================================================================= #坚持 # ================================================================= #如果启用了持久性,请将内存中的数据库保存到磁盘 #每自动保存间隔秒。如果设置为0,则持久性 #只有当MOSQUITO退出时,才会写入数据库。 #请注意,p