Arduino 在 windows 和 ubuntu 上均未连接到 Mosquitto

Arduino does not connect to Mosquitto On both windows and ubuntu

我正在尝试将 ESP8266 连接到我的笔记本电脑和 mosquitto 代理。 windows 和 ubuntu 我都试过了,但是 none 成功了!

这是windows部分:

蚊子经纪人在线:

C:\Program Files\Mosquitto>mosquitto -v
1625556778: mosquitto version 2.0.11 starting
1625556778: Using default config.
1625556778: Starting in local only mode. Connections will only be possible from clients running on this machine.
1625556778: Create a configuration file which defines a listener to allow remote access.
1625556778: For more details see https://mosquitto.org/documentation/authentication-methods/
1625556778: Opening ipv4 listen socket on port 1883.
1625556778: Opening ipv6 listen socket on port 1883.
1625556778: mosquitto version 2.0.11 running

这是arduino代码:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.

const char* ssid = "Laurium";
const char* password = "pass";
//const char* mqtt_server = "192.168.1.35"; I tried this as well
IPAddress mqtt_server = (192, 168, 1, 35);

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  randomSeed(micros());

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  // Switch on the LED if an 1 was received as first character
  if ((char)payload[0] == '1') {
   digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
    // but actually the LED is on; this is because
    // it is active low on the ESP-01)
  } else {
   digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
 }

}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("outTopic", "hello world");
      // ... and resubscribe
      client.subscribe("inTopic");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  Serial.begin(9600);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  long now = millis();
  if (now - lastMsg > 2000) {
    lastMsg = now;
    ++value;
    snprintf (msg, 50, "hello world #%ld", value);
    Serial.print("Publish message: ");
    Serial.println(msg);
    client.publish("outTopic", msg);
  }
}

这是终端上的输出:

WiFi connected
IP address: 
192.168.137.90
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds

我的笔记本本地IP:192.168.1.35

所以它成功连接到WIFI但找不到代理。 我也关注了 This 线程,但没有用。我什至使用新的 TCP 规则在 windows 防火墙中添加了 1883 端口作为入站端口。我还完全禁用了防火墙或切换到默认情况下允许远程客户端的旧版本的 mosquitto 所有无济于事。

ubuntu部分: 这里我稍微修改了代码。我还添加了 ping 命令,这样我就可以检查我的 esp8266 是否可以 ping google?它能否 ping 通目标 mosquitto 服务器 IP? 我在我的 ubuntu vmware 上安装了 mosquito,并在我的主机 windows 中传递了通过 IP config 命令获得的 vmware IP。 我还在 ubuntu 上测试了我的 mosquitto,它在 ubuntu 上与测试发布者-订阅者一起工作正常。 ubuntu 与 windows 不同,固定 mosquitto IP 成功,但 mosquitto 终端没有显示任何内容。

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ESP8266Ping.h>
// Update these with values suitable for your network.

const char* ssid = "Laurium";
const char* password = "pass";
//const char* mqtt_server = "192.168.186.1";

IPAddress mqtt_server = (192, 168, 186, 1);
//IPAddress mqtt_server = (192, 168, 15, 1);


WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  randomSeed(micros());

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  // Switch on the LED if an 1 was received as first character
  if ((char)payload[0] == '1') {
   digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
    // but actually the LED is on; this is because
    // it is active low on the ESP-01)
  } else {
   digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
 }

}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("outTopic", "hello world");
      // ... and resubscribe
      client.subscribe("inTopic");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      bool ret = Ping.ping(mqtt_server);
      Serial.println(ret);
      Serial.println(Ping.ping("www.google.com"));
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  Serial.begin(9600);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop() {

  //IPAddress ip (192, 168, 0, 1); // The remote ip to ping


  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  long now = millis();
  if (now - lastMsg > 2000) {
    lastMsg = now;
    ++value;
    snprintf (msg, 50, "hello world #%ld", value);
    Serial.print("Publish message: ");
    Serial.println(msg);
    client.publish("outTopic", msg);
  }
}

这一次,输出是这样的:

WiFi connected
IP address: 
192.168.137.109
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
1
1

与windows不同,ping mosquitto服务器所在IP成功,但仍未建立任何连接

如果我可以使用任何 windows 或 ubuntu mosquitto 服务器,那很好。

谢谢。

从启动时的输出:

1625556778: Starting in local only mode. Connections will only be possible from clients running on this machine. 
1625556778: Create a configuration file which defines a listener to allow remote access. 
1625556778: For more details see https://mosquitto.org/documentation/authentication-methods/

重要的部分是以仅本地模式启动

从 2.0 版开始,需要明确配置 mosquitto 以接受来自本地主机以外的任何连接,并且需要设置为接受不传递用户名和密码的连接。

您可以将以下内容添加到 mosquitto.conf

allow_anonymous true
listener 1883 0.0.0.0

我在 Ubuntu vmware 上成功 运行 mosquitto。 首先,我将我的 VM 网络桥接并将我的笔记本电脑和我的节点连接到同一个 wifi,这样它们都位于同一个网络上并且可以通信。 其次,我将这两行添加到 mosquitto.conf 文件中:

listener 1883 0.0.0.0
allow_anonymous true

并保存文件。 conf 文件位于 /etc/mosquitto/mosquitto.conf 然后我用sudo service mosquitto stop手动关闭mosquitto并用sudo service mosquitto start重启它。 您可以在另一个终端中使用 netstat -tulpn 来确保您在 127.0.0.1:1883 上有一个 tcp 侦听器。这对我来说很好。 同时我仍然不能在 windows.

上做同样的事情