0

I have a piece of code that is sending an MQTT message.

The message is the same with the only change being changing action between 'on' and 'off'. I can end off as many times as I like, but when I send 'on' it locks (even if it is the first message)

Here is the code

void update_thing(int pin, String thing, String action) {  

  Serial.println(thing + " State Requested to " + action);
  Serial.println(action.c_str());
    if (action == "On") {
      digitalWrite(pin, HIGH);                    // Pin  = 5 V, LED Turns On
    } else {
      digitalWrite(pin, LOW);                     // Pin  = 5 V, LED Turns Off     
    }

  // Generate a new message 
  sprintf(msg, "{\"state\":{\"reported\":{\"%s\": \"%s\"}}}", thing.c_str(), action.c_str());
  if((rc = myClient.publish("$aws/things/myYunLight/shadow/update", msg, strlen(msg), 1, false)) != 0) {
    Serial.println("Publish failed!");
    Serial.println(rc);
  }
}

The issue appears to lie in the msg variable, declared as

char msg[100]; // read-write buffer

and in particular the value fed to action (or even if I remove that and use the text 'on' or 'off'. If I use 'on ' (with a space) it works fine

user2997982
  • 526
  • 5
  • 8
  • Can you explain your last sentence a little more. Are you saying that if you use "ON" instead of the msg variable in the call to publish, that all is OK? Also, can you do a Serial.println(msg), and Serial.println(strlen(msg)) before the publish call? I suspect your msg variable may not be correctly '\0' terminated. Also, if I remember correctly from my yun days, there is a limit on the size of a tcpip packet which could be sent, which is why I gave up on it. – Greycon Jan 28 '16 at 12:46
  • The last sentence was supposed to mean that if I remove the variable substitution for %s in place of the action.c_str() and move the sprint function to the if above and replace it just with text ..\"on\".. or ..\"off\".. it still fails – user2997982 Jan 28 '16 at 12:53
  • Added the debug you asked for, sending 'off' I get (multiple times) `led2 State Requested to Off msg = {"state":{"reported":{"led2": "Off"}}} strlen msg = 38 New Message Recieved: {"reported":{"led2":"Off"}}` then sending on (locks after the first send) `led2 State Requested to On msg = {"state":{"reported":{"led2": "On"}}} strlen msg = 37 New Message Recieved: {"reported":{"led2":"On"}}` – user2997982 Jan 28 '16 at 12:56
  • Are you absolutely sure that the "lock" occurs inside this function? What happens if you remove everything except `digitalWrite(pin, HIGH);`? – molbdnilo Jan 28 '16 at 13:15
  • OK, so if I understand you correctly, when you send on, an MQTT message does indeed get transmitted (Since you show "New Message Received") ? That being the case, you will need to delve into the MQTT client code. Are you using the Arduino IDE, or Eclipse? – Greycon Jan 28 '16 at 13:17
  • Which MQTT Client are you using? I use KNOLeary's PubSub client, and i can't find a signature for the publish method that takes 5 parameters. – Greycon Jan 28 '16 at 13:49
  • OK, the PAHO client, I see it. Just for completeness, could you try 2 more things? 1. Add a Serial.println("Done") at the end of the method, just to prove the hang is in the publish method. Also, could you try setting the QOS (parameter 4) to 0, just in case the PAHO client code is waiting for an ack? – Greycon Jan 28 '16 at 14:06

0 Answers0