aws / aws-iot-device-sdk-embedded-C

SDK for connecting to AWS IoT from a device using embedded C.
MIT License
976 stars 629 forks source link

Does aws iot broker support will message? #170

Closed chunhuajiang closed 6 years ago

chunhuajiang commented 6 years ago

Hi, I want to use the feature of will message, but it does not work as my expected. My steps:

I checked the MQTT control packet CONNECT & CONNACK at device side, all fields are correct, so I'm doubt whether your broker supports will message.


Attach the control packet:

CONNECT:
10 8e 01 00 04 4d 51 54 54 04 0e 00 0a 00 14 33 52 2d 38 43 38 38 32 42 37 36 39 39 32 31 00 00 00 00 00 00 3e 24 61 77 73 2f 74 68 69 6e 67 73 2f 48 55 42 5f 32 34 37 39 64 32 32 62 33 66 36 35 34 64 38 65 61 33 33 64 34 66 62 62 64 33 33 36 31 30 64 66 2f 73 68 61 64 6f 77 2f 75 70 64 61 74 65 00 2c 7b 22 73 74 61 74 65 22 3a 7b 22 72 65 70 6f 72 74 65 64 22 3a 7b 22 63 6f 6e 6e 65 63 74 65 64 22 3a 22 66 61 6c 73 65 22 7d 7d 7d 

CONNACK:
20 02 00 00

and my analysis of CONNECT packet:

## fixed header
10 // type: CONNECT
b4 01 // remain length 180 

## variable header
00 04 4d 51 54 54 // 0, 4, M, Q, T, T
04 // Protocol Level, 3.1.1
0e // connect flag, 00001110, will qos = 1, will flag = 1, clean session = 1,
00 0a // Keep Alive

## payload
// length of client-id
00 14 
// client id, 3R-8C882B769921
33 52 2d 38 43 38 38 32 42 37 36 39 39 32 31 00 00 00 00 00  

// length of topic
00 3e
// $aws/things/HUB_2479d22b3f654d8ea33d4fbbd33610df/shadow/update
24 61 77 73 2f 74 68 69 6e 67 73 2f 48 55 42 5f 32 34 37 39 64 32 32 62 33 66 36 35 34 64 38 65 61 33 33 64 34 66 62 62 64 33 33 36 31 30 64 66 2f 73 68 61 64 6f 77 2f 75 70 64 61 74 65 

// length of will message
00 2c 
// {"state":{"reported":{"connected":"false"}}}
7b 22 73 74 61 74 65 22 3a 7b 22 72 65 70 6f 72 74 65 64 22 3a 7b 22 63 6f 6e 6e 65 63 74 65 64 22 3a 22 66 61 6c 73 65 22 7d 7d 7d  

Regards & Thanks

gordonwang0 commented 6 years ago

Hi @tidyjiang8,

The AWS IoT broker does support last will and testament.

However, I noticed that you are trying to publish your LWT to $aws/things/HUB_2479d22b3f654d8ea33d4fbbd33610df/shadow/update. Please see the documentation here.

Currently, LWT messages sent to AWS IoT reserved topics (topics that begin with $) are ignored by the AWS IoT Device Shadow service, but are still processed by subscribed clients and by the AWS IoT rules engine. If you want the Device Shadow service to receive LWT messages, register an LWT message to a non-reserved topic and create a rule that republishes the message on the reserved topic.

chunhuajiang commented 6 years ago

Got it, thanks!

chunhuajiang commented 6 years ago

Hi @gordonwang0, The problem was resolved, but I'm curious about why it is designed to do so, could you explain the reason if it's possible?

gordonwang0 commented 6 years ago

Hi @tidyjiang8,

I can't give the exact reason publicly. But it has to do with how our internal systems were designed, and that we haven't seen a compelling case to change it.

chunhuajiang commented 6 years ago

ok, thanks @gordonwang0

dennislwy commented 5 years ago

Hi @tidyjiang8,

The AWS IoT broker does support last will and testament.

However, I noticed that you are trying to publish your LWT to $aws/things/HUB_2479d22b3f654d8ea33d4fbbd33610df/shadow/update. Please see the documentation here.

Currently, LWT messages sent to AWS IoT reserved topics (topics that begin with $) are ignored by the AWS IoT Device Shadow service, but are still processed by subscribed clients and by the AWS IoT rules engine. If you want the Device Shadow service to receive LWT messages, register an LWT message to a non-reserved topic and create a rule that republishes the message on the reserved topic.

Hi @gordonwang0, I am in the same situation (I wish to monitor the connection status of my device), I read the documentation. In other to goes around the limitation, we need to create a rule in Rule engine to re-post the topic back to device shadow.

But what if I want to do this on >1000 device (each device have different 'thingName')? Is not practical to create 1000 rules. In this scenario, how to write 1 rule to handle multiple devices?

gordonwang0 commented 5 years ago

Hi @dennislwy,

You can use wildcards your rule. Something like

{
    "rule": {
    "ruleDisabled": false,
    "sql": "SELECT * FROM 'my/things/+/update'",
    "description": "Turn my/things/ into $aws/things/",
    "actions": [{
        "republish": {
            "topic": "$$aws/things/${topic(3)}/shadow/update",
            "roleArn": "arn:aws:iam::123456789012:role/aws_iot_republish"
        }
    }]
    }
}

(where the + wildcard and topic(3) stand for Thing Name)

yles9056 commented 5 years ago

Hi @gordonwang0, I tried using rule engine to republish last will and update shadow. It worked. But there's one problem. If an inactive client disconnects after connecting to AWS IoT broker for a while, its last will will not be sent immediately. Instead, it will be sent after a long delay(roughly 3 min delay).

I'm guessing this has something to do with how AWS IoT broker works internally.

KyleVest commented 5 years ago

@yles9056 I think your mqtt keep alive time determines the delay.

I have a situation that has 1) my device online and working. 2) connection breaks 3) keep_alive timer is running 4) device comes back online before keep_alive timer expires 5) Even while connected I get LWT message.

Is there a way to cancel the LWT message if I come back online before keep_alive timer expires?