我已经在docker(树莓派4(上安装了mosquitto,使用证书来保护连接。我没有在mosquito中配置用户名和密码。我正在使用此代码将我的wemos d1 mini连接到具有pubsubclient的mosquito。
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.
mosquito给出以下日志:
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文件连接到mosquito,它工作得很好。
我的蚊子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不认识我在代码中指定的客户名称?为什么我不能连接到蚊子?我该如何解决这个问题?
我也用用户名和密码试过,结果是一样的!
我的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);
}
我刚从一个项目中复制了这个,可能少了一些行。如果是,请告诉我,我会尝试创建一个干净的代码。但总的来说,它应该起作用。