Closed tobiasdierich closed 10 months ago
Could you try again but enable debug logging?
The will message is incoporated into the CONNECT message so this might me malformed somehow (or not formed according AWS standards).
Do you know how to enable debug output?
This is the debug output when it fails to connect:
[ 5][D][esp32-hal-cpu.c:244] setCpuFrequencyMhz(): PLL: 480 / 2 = 240 Mhz, APB: 80000000 Hz
Connecting to Wi-Fi...
[ 51][D][WiFiGeneric.cpp:1035] _eventCallback(): Arduino Event: 0 - WIFI_READY
[WiFi-event] event: 0
[ 148][D][WiFiGeneric.cpp:1035] _eventCallback(): Arduino Event: 2 - STA_START
[WiFi-event] event: 2
E (3557) wifi:Association refused temporarily, comeback time 1024 mSec
[ 3697][D][WiFiGeneric.cpp:1035] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
[ 3697][W][WiFiGeneric.cpp:1057] _eventCallback(): Reason: 203 - ASSOC_FAIL
[ 3700][D][WiFiGeneric.cpp:1077] _eventCallback(): WiFi Reconnect Running
[WiFi-event] event: 5
WiFi lost connection
heap: 216568
[ 6134][D][WiFiGeneric.cpp:1035] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
[ 6134][W][WiFiGeneric.cpp:1057] _eventCallback(): Reason: 201 - NO_AP_FOUND
[ 6138][D][WiFiGeneric.cpp:1081] _eventCallback(): WiFi AutoReconnect Running
[WiFi-event] event: 5
WiFi lost connection
[ 7659][D][WiFiGeneric.cpp:1035] _eventCallback(): Arduino Event: 4 - STA_CONNECTED
[WiFi-event] event: 4
[ 7678][D][WiFiGeneric.cpp:1035] _eventCallback(): Arduino Event: 7 - STA_GOT_IP
[ 7678][D][WiFiGeneric.cpp:1098] _eventCallback(): STA IP: 192.168.178.29, MASK: 255.255.255.0, GW: 192.168.178.1
[WiFi-event] event: 7
WiFi connected
IP address:
192.168.178.29
Connecting to MQTT...
[ 9096][D][ssl_client.cpp:282] start_ssl_client(): Protocol is TLSv1.2 Ciphersuite is TLS-ECDHE-RSA-WITH-AES-128-GCM-SHA256
[ 9096][D][ssl_client.cpp:284] start_ssl_client(): Record expansion is 29
Disconnected from MQTT: 7.
heap: 214912
Connecting to MQTT...
heap: 157096
[ 15243][D][ssl_client.cpp:282] start_ssl_client(): Protocol is TLSv1.2 Ciphersuite is TLS-ECDHE-RSA-WITH-AES-128-GCM-SHA256
[ 15243][D][ssl_client.cpp:284] start_ssl_client(): Record expansion is 29
Disconnected from MQTT: 7.
Connecting to MQTT...
heap: 157388
[ 21345][D][ssl_client.cpp:282] start_ssl_client(): Protocol is TLSv1.2 Ciphersuite is TLS-ECDHE-RSA-WITH-AES-128-GCM-SHA256
[ 21346][D][ssl_client.cpp:284] start_ssl_client(): Record expansion is 29
Disconnected from MQTT: 7.
That looks like the same debug log as the other issue.
Would it help if you do
if (mqttClient.connected()) {
mqttClient.publish("foo/bar/0", 0, false, "test");
}
Just to rule out (or confirm) bugs with the wait_for_connack logic.
I still get the same debug output:
[ 5][D][esp32-hal-cpu.c:244] setCpuFrequencyMhz(): PLL: 480 / 2 = 240 Mhz, APB: 80000000 Hz
Connecting to Wi-Fi...
[ 50][D][WiFiGeneric.cpp:1035] _eventCallback(): Arduino Event: 0 - WIFI_READY
[WiFi-event] event: 0
[ 148][D][WiFiGeneric.cpp:1035] _eventCallback(): Arduino Event: 2 - STA_START
[WiFi-event] event: 2
E (3479) wifi:Association refused temporarily, comeback time 1024 mSec
[ 3658][D][WiFiGeneric.cpp:1035] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
[ 3658][W][WiFiGeneric.cpp:1057] _eventCallback(): Reason: 203 - ASSOC_FAIL
[ 3661][D][WiFiGeneric.cpp:1077] _eventCallback(): WiFi Reconnect Running
[WiFi-event] event: 5
WiFi lost connection
heap: 216856
[ 6095][D][WiFiGeneric.cpp:1035] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
[ 6095][W][WiFiGeneric.cpp:1057] _eventCallback(): Reason: 201 - NO_AP_FOUND
[ 6098][D][WiFiGeneric.cpp:1081] _eventCallback(): WiFi AutoReconnect Running
[WiFi-event] event: 5
WiFi lost connection
[ 7631][D][WiFiGeneric.cpp:1035] _eventCallback(): Arduino Event: 4 - STA_CONNECTED
[WiFi-event] event: 4
[ 7652][D][WiFiGeneric.cpp:1035] _eventCallback(): Arduino Event: 7 - STA_GOT_IP
[ 7652][D][WiFiGeneric.cpp:1098] _eventCallback(): STA IP: 192.168.178.29, MASK: 255.255.255.0, GW: 192.168.178.1
[WiFi-event] event: 7
WiFi connected
IP address:
192.168.178.29
Connecting to MQTT...
[ 9004][D][ssl_client.cpp:282] start_ssl_client(): Protocol is TLSv1.2 Ciphersuite is TLS-ECDHE-RSA-WITH-AES-128-GCM-SHA256
[ 9005][D][ssl_client.cpp:284] start_ssl_client(): Record expansion is 29
Disconnected from MQTT: 7.
heap: 214900
Connecting to MQTT...
[ 15144][D][ssl_client.cpp:282] start_ssl_client(): Protocol is TLSv1.2 Ciphersuite is TLS-ECDHE-RSA-WITH-AES-128-GCM-SHA256
[ 15145][D][ssl_client.cpp:284] start_ssl_client(): Record expansion is 29
Disconnected from MQTT: 7.
heap: 214684
Connecting to MQTT...
heap: 157384
[ 21269][D][ssl_client.cpp:282] start_ssl_client(): Protocol is TLSv1.2 Ciphersuite is TLS-ECDHE-RSA-WITH-AES-128-GCM-SHA256
[ 21270][D][ssl_client.cpp:284] start_ssl_client(): Record expansion is 29
Disconnected from MQTT: 7.
Connecting to MQTT...
heap: 155832
[ 27404][D][ssl_client.cpp:282] start_ssl_client(): Protocol is TLSv1.2 Ciphersuite is TLS-ECDHE-RSA-WITH-AES-128-GCM-SHA256
[ 27405][D][ssl_client.cpp:284] start_ssl_client(): Record expansion is 29
Disconnected from MQTT: 7.
Commenting out the setWill
call I get:
[WiFi-event] event: 7
WiFi connected
IP address:
192.168.178.29
Connecting to MQTT...
heap: 157208
[ 40460][D][ssl_client.cpp:282] start_ssl_client(): Protocol is TLSv1.2 Ciphersuite is TLS-ECDHE-RSA-WITH-AES-128-GCM-SHA256
[ 40461][D][ssl_client.cpp:284] start_ssl_client(): Record expansion is 29
Connected to MQTT.
Session present: 0
Subscribing at QoS 0, packetId: 7
Publishing at QoS 0, packetId:
1
Subscribe acknowledged.
packetId: 7
qos: 0
Publishing message
Publishing message
Publishing message
Publishing message
Publishing message
heap: 169364
Publishing message
Publishing message
Publishing message
Publishing message
Publishing message
The thing with the will is that the library only stores pointers to the topic and payload. But in your case, the data should be long lived.
I'm going to dig in the AWS manuals again. See here: https://docs.aws.amazon.com/iot/latest/developerguide/mqtt.html#mqtt-lwt and here: https://docs.aws.amazon.com/iot/latest/developerguide/connect-policy.html Do you need to enable persistent connections?
For testing purposes I used a "allow all" policy for IoT Core:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "*",
"Resource": "*"
}
]
}
So afaik persistent connections should already be allowed. Is there any way I can help debugging this?
Error code 7 isn't helping. AWS is just closing the TCP connection. It would be helpful is you somehow could find in the AWS logs why it does this.
🤦 I just figured it out. AWS does not support QoS 2 and I set the will to exactly that (see https://docs.aws.amazon.com/iot/latest/developerguide/mqtt.html#mqtt-differences). Changing the QoS to 0 or 1 fixes the issue.
Thanks so much for helping me debug this!
Good to hear. And thanks!
Describe the bug When setting a last will on the mqtt client using
setWill
, the client can not connect to AWS IoT core. Maybe this is related to #52. When trying to connect, the client returns a disconnection due to reason 7. My guess is that when usingsetWill
, theEMC_WAIT_FOR_CONNACK
is ignored and it tries to immediately publish a message which causes AWS to close the connection. If I do not set a last will, connecting and publishing to AWS works fine.Platform: ESP32 TLS: Yes IDE: Arduino IDE for testing Version: 1.4.5
Expected behaviour Can connect to AWS IoT Core MQTT with last will set.
To Reproduce Steps to reproduce the behaviour:
Example code
Output: