Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More Advanced Publish #33

Merged
merged 1 commit into from
May 17, 2016
Merged

More Advanced Publish #33

merged 1 commit into from
May 17, 2016

Conversation

256dpi
Copy link
Owner

@256dpi 256dpi commented May 5, 2016

This adds the method void publish(MQTTMessage * message); that allows publishing messages using the new MQTTMessage struct. Currently the struct only allows to set the retained flag but may also allow setting the quality of service in later versions.

I didn't want add another normal functions as these are considered short-hand functions and setting the retained flag is more advanced.

Solves #27.

@gibix I created a custom struct rather than using the internal structure to expose a nicer API. In the future we might use the same structure also in the messageReceived callback and maybe add convenience functions like String string() etc.

@gibix
Copy link
Contributor

gibix commented May 5, 2016

Cool! I like the sign of the messageReceived as is, very simple for the user, just define that and it works.
But for expert use we can write a subscribe(topic, *(void)(MQTT:Message)) that within the library we can directly attach to MQTT::MessageHandlers.
I just don't like the use of the MQTT scope in the sketch, is not very "arduino simple style"

@256dpi
Copy link
Owner Author

256dpi commented May 10, 2016

I have the feeling the current signature is a little complicated for amateurs.

So rather than:

void messageReceived(String topic, String payload, char * bytes, unsigned int length) {
  Serial.print("incoming: ");
  Serial.print(topic);
  Serial.print(" - ");
  Serial.print(payload);
  Serial.println();
}

We could have something like this:

void messageReceived(MQTTMessage msg) {
  Serial.print("incoming: ");
  Serial.print(msg.topic());
  Serial.print(" - ");
  Serial.print(msg.payload());
  Serial.println();
}

Then we would also have the ability to add more convenience functions like:

if (msg.topicBeginsWith("/namespace")) {
  // do something
}

@256dpi 256dpi merged commit 2c70573 into master May 17, 2016
@256dpi 256dpi deleted the more-advanced-publish branch May 17, 2016 12:09
@vkynchev
Copy link

vkynchev commented Nov 17, 2016

Hi all! I need to send a message with retained flag, but whatever I try fails and my ESP8266 goes into boot loop.
Can someone please show me how to do that? Thank you!

What have I done so far:
MQTTMessage* message;
message->topic = devices_topic;
message->payload = JSONBuffer;
message->length = sizeof( JSONBuffer ) / sizeof( char );
message->retained = true;
client.publish(message);

@256dpi
Copy link
Owner Author

256dpi commented Nov 18, 2016

You need allocate the message on the stack and then pass a pointer to client.publish().

Here is a corrected example (not tested tough, but should work):

MQTTMessage message; // <- no "*" here
message->topic = devices_topic;
message->payload = JSONBuffer;
message->length = sizeof( JSONBuffer ) / sizeof( char );
message->retained = true;
client.publish(&message); // <- but add "&" here

@vkynchev
Copy link

vkynchev commented Nov 18, 2016

I had to change the "->" to "." for the compiler to not show an error and after that it looks like this

MQTTMessage message;
message.topic = devices_topic;
message.payload = JSONBuffer;
message.length = sizeof( JSONBuffer ) / sizeof( char );
message.retained = true;
client.publish(&message);

The ESP8266 doesn't reboot itself but nothing is sent to the broker.
Can It be problem with the message.length ? Because the JSONBuffer is with fixed length of 256
JSONBuffer and devices_topic are of type char

The other code works because with

client.publish(devices_topic, JSONBuffer);

it sends the message but with no retained flag.

@256dpi
Copy link
Owner Author

256dpi commented Nov 18, 2016

The maximum size for packets being published and received is set by default to 128 bytes. To change that value, you need to download the library manually and change the value in the following file: https://github.com/256dpi/arduino-mqtt/blob/master/src/MQTTClient.h#L5.

You'll need to adjust the buffers sizes.

But, I would recommend that you calculate the properly length for the JSON Buffer and not send the whole buffer as it most likely includes old data from potentially longer JSON objects.

One thing: In contrast to the topic the payload does not handle any null termination.

@vkynchev
Copy link

vkynchev commented Nov 18, 2016

Thanks!
After I adjusted my JSONBuffer to the size of the json object it was under 128 bytes. And the message was sent.

BTW I had read the README.md file several times(including the maximum packet size), but not realized that... 😆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants