ExploreEmbedded / Hornbill-Examples

89 stars 171 forks source link

Not receiving MQTT messages when subscribe is done using a non const char #23

Closed SamsTheNerd closed 4 years ago

SamsTheNerd commented 4 years ago

My Code:

` if (device.connect(HOST_ADDRESS, const_cast<char*>(client_id.c_str())) == 0) { Serial.println("Connected To AWS"); delay(1000);

                char getAcceptedTopic[60];
                strcpy(getAcceptedTopic, "Lamps/"); //these ones need to be strcpy to get it started
                strcat(getAcceptedTopic, const_cast<char*>(myLampCred.username.c_str()));
                strcat(getAcceptedTopic, "/rga");

                Serial.print("\ngetAcceptedTopic: ");
                Serial.print(getAcceptedTopic);

                char republishedShadowTopic[60];
                strcpy(republishedShadowTopic, "Lamp/");
                strcat(republishedShadowTopic, const_cast<char*>(myLampCred.username.c_str()));
                strcat(republishedShadowTopic, "/rs");

                if (0==device.subscribe(getAcceptedTopic, mySubCallBackHandler)) {
                //if (0==device.subscribe("Lamps/SamHang/rs", mySubCallBackHandler)) { //try just doing it with the string? 
                Serial.print("Subscribe Successful");
                } else {
                Serial.println("Subscribe Failed, Check The Thing Name and Certificates");
                while(1);
                } 

                if (0==device.subscribe("testchannel", mySubCallBackHandler)) {
                Serial.println("Subscribe Successful");
                } else {
                Serial.println("Subscribe Failed, Check The Thing Name and Certificates");
                while(1);
                } 
                //will need to add in effects/channel topic at some point, but since none of the effects code is set up yet anyways, theres no point in adding a topic now.
                yield();
                Serial.println(republishedShadowTopic);
                if (0 == device.subscribe(republishedShadowTopic, mySubCallBackHandler)){
                    Serial.println("Subscribe Successful");
                } else {
                    Serial.println("Subscribe Failed, Check The THing Name and Certificates");
                    while(1);
                }

                delay(500);
                //send get request
                gettingShadow = true;
                std::string shadowGetTopic = "$aws/things/" + myLampCred.username + "Lamp/shadow/get";
                publishFunc(shadowGetTopic.c_str(), "");
            } else {
                //could add error for this but unless the host address changes and EVERYTHING breaks or wifi isn't connected, then there shouldn't be a problem
                Serial.println("AWS connection failed, check the HOST address");
                while(1);
            }`

I have 3 topics that I subscribe to and two of them are based off of the username, which changes based on device/user. My testchannel and getAcceptedTopic topics receive messages just fine but the republishedShadowTopic does not receive any messages. If i change it from the republishedShadowTopic variable to the string literal "Lamp/user/rs" in the subscribe method, then it works fine. I'm using the AWS IoT console and I can see that all of the messages and sent but the device is just not receiving them. I'm really not sure what this is because it seems like it should work based on the getAcceptedTopic topic working and being declared the same way? I can send the same messages through and have the same results on each topic respectively, so I know that it doesn't have to do with the message contents. Thanks for any help :)

SamsTheNerd commented 4 years ago

Turns out it was due to a scope issue with the republishedShadowTopic variable. It was solved by moving the variable declaration outside of the loop and into the global area. There's more information here if anyone else has this problem