客户端<未知>上的Mosquitto docker套接字错误,正在断开连接

时间:2020-02-24 10:05:32

标签: ssl esp8266 mosquitto

我在docker(raspberry pi 4)上设置了mosquitto,使用证书来保护连接。我在mosquitto中没有配置用户名和密码。 我正在使用此代码通过pubsubclient将wemos d1 mini连接到mosquitto。

https://github.com/debsahu/ESP_MQTT_Secure/blob/master/ESP8266_MQTT_SSL/Arduino/ESP8266_PubSubClient_SSL/ESP8266_PubSubClient_SSL.ino

我不使用secrets.h文件,但是我之前尝试过,结果相同。

在插入我的凭据,mqtt数据和ca.crt之后,我得到以下串行输出:

Attempting to connect to SSID: MYSSID ... connected! 
Setting time using SNTP.done! 
Current time: Wed Feb 19 13:53:17 2020 Time: Wed Feb 19 13:53:17 2020 
MQTT connecting ... failed, status code =-2. Try again in 5 seconds. 
MQTT connecting ... failed, status code =-2. Try again in 5 seconds. 
MQTT connecting ... failed, status code =-2. Try again in 5 seconds.

mosquitto给出以下日志:

1582138400: New connection from 192.168.0.8 on port 8883. 
1582138405: Socket error on client <unknown>, disconnecting. 
1582138405: New connection from 192.168.0.8 on port 8883. 
1582138413: New connection from 192.168.0.8 on port 8883. 
1582138418: Socket error on client <unknown>, disconnecting. 
1582138418: New connection from 192.168.0.8 on port 8883. 
1582138424: Socket error on client <unknown>, disconnecting. 
1582138424: New connection from 192.168.0.8 on port 8883. 
1582138429: Socket error on client <unknown>, disconnecting. 
1582138429: New connection from 192.168.0.8 on port 8883. 
1582138434: Socket error on client <unknown>, disconnecting. 
1582138435: New connection from 192.168.0.8 on port 8883.
1582138440: Socket error on client <unknown>, disconnecting. 
1582138440: New connection from 192.168.0.8 on port 8883. 
1582138443: Client <unknown> has exceeded timeout, disconnecting.

至少我得到了最后一行:由于超时而断开连接。 192.168.0.8是wemos D1 mini。 之前,我使用MQTTfx使用ca.crt文件连接到mosquitto,并且工作正常。

我的mosquitto.conf:

allow_anonymous true

port 8883

cafile /ca.crt
keyfile /server.key
certfile /server.crt
tls_version tlsv1.2

persistence true
persistence_location /mosquitto/data/

为什么mosquitto无法识别我在代码中指定的客户端名称? 为什么我不能连接到蚊子?我该如何解决呢?

我也用用户名和密码尝试了同样的结果!

1 个答案:

答案 0 :(得分:0)

我的mosquitto.conf文件现在看起来像这样:

allow_anonymous false
password_file /mosquitto/data/passwordfile.txt


port 8883

cafile /ca.crt
keyfile /server.key
certfile /server.crt
tls_version tlsv1.2

persistence false
persistence_location /mosquitto/data/

log_type all

这是我现在使用的代码,只需插入您的凭据和证书即可:

//Simple boolean to indicate first startup loop
bool startup = false;
        
        
        // Define Your Settings
        
        const char* ssid = "";
        const char* password = "";
        const char* mqtt_server = "";
        const char* mqtt_username = "";
        const char* mqtt_password = "";
        const char* mqtt_clientname = "";
        const int mqtt_port = 8883;
        
        
        //Replace with you issuing certificate authority Base64 format
        //This is also known as the "intermediate" authority that issued
        //your certificate (client.crt)
        static const char digicert[] PROGMEM = R"EOF(
        -----BEGIN CERTIFICATE-----
        
        INSERT YOUR CERT
        
        -----END CERTIFICATE-----
        )EOF";
        
void setup(){     
WiFi.begin(ssid, password);
      Serial.println("Connecting to WiFi.");
      int _try = 0;
      while (WiFi.status() != WL_CONNECTED)
      {
        Serial.print(".");
        delay(500);
        _try++;
    
        // if connection not possible, go to deep-sleep
        //if ( _try >= 10 ) {
        //Serial.println("Can't connect to wifi, go to deep-sleep");
        //ESP.deepSleep(durationSleep * 1e6);
        //}
      }
      Serial.println("Connected to the WiFi network");
      //****
      //Important to set setTrustAnchors to verify certificates
      //setInsecure() will allow the ssl connection without verification
      //****
      //client.setInsecure(); //WARNING Do NOT verify server
    
      client.setTrustAnchors(&cert);
      //NTP is required for CA Cert Validation
      setClock();
    
      //Connect to your MQTT Server and set callback
      mqttclient.setServer(mqtt_server, mqtt_port);

}

我刚从项目中复制了此内容,可能缺少一些行。如果是这样,请告诉我,然后尝试创建一个干净的代码。但总而言之,它应该起作用。