odelot / aws-mqtt-websockets

Implementation of a middleware to use AWS MQTT service through websockets, aiming the ESP8266 plataform
GNU Lesser General Public License v3.0
231 stars 67 forks source link

Need subscribe example #20

Closed rileyschuit closed 7 years ago

rileyschuit commented 7 years ago

I have trying to figure out how to get the desired shadow from AWS. Publishing the state is no issue.

I think I found the solution here: https://github.com/heskew/aws-sdk-arduino/blob/iot-get-shadow-and-cleanup/src/common/AmazonIOTClient.cpp but I'm failing to put it together. I have attempted to reach out to the author without success.

If we can get the subscribe function in the example (https://github.com/odelot/aws-mqtt-websockets/blob/master/examples/aws-mqtt-websocket-example/aws-mqtt-websocket-example.ino) to return the value from AWS, we have a full solution.

I have reached out to you via twitter to offer compensation for your efforts as well.

Huong-nt commented 7 years ago

You can get the desired shadow from AWS by publishing to the topic $aws/things/myThingName/shadow/get and subscribe topic $aws/things/myThingName/shadow/get/accepted. When you publish a message to the topic $aws/things/myThingName/shadow/get, AWS IoT will publish a response state document to $aws/things/myThingName/shadow/get/accepted.

rileyschuit commented 7 years ago

@Huong-nt This worked! Thank you so much! I will submit another pull request with an example when it functions correctly and is somewhat clean. With the example given, I modded/added:

const char* aws_topic_publish  = "$aws/things/Device1/shadow/get";
const char* aws_topic_subscribe  = "$aws/things/Device1/shadow/get/accepted";

Then modified the subscribe function like so:

//subscribe to a mqtt topic
void subscribe () {
   //subscrip to a topic
    int rc = client->subscribe(aws_topic_subscribe, MQTT::QOS0, messageArrived);
    if (rc != 0) {
      if (DEBUG_PRINT) {
        Serial.print("rc from MQTT subscribe is ");
        Serial.println(rc);
      }
      return;
    }
    if (DEBUG_PRINT) {
      Serial.println("MQTT subscribed");
    }
}

Serial output looks like:

MQTT subscribed
Message 1 arrived: qos 0, retained 0, dup 0, packetid 16383
Payload {"state":{"desired":{"on":false},"reported":{"on":false}},"metadata":{"desired":{"on":{"timestamp":1487734811}},"reported":{"on":{"timestamp":1487734811}}},"version":36,"timestamp":1487735007}