niMQTT's usage

niMQTT is an MQTT library designed for the new EthernetInteface lib.

Import libraryniMQTT

An MQTT Client for the new etherNet Interface.

To use it, you only need a callback with the prototype

callback

void callback(const char *topic, const char *message)

Once you have it, you can create your MQTT's client following this prototype:

niMQTT Client prototype

niMQTT(char *server, void (*callback)(const char *, const char*), char *id="mbed", int port=1883, char *username="", char *password="", bool debug=false);

On creating an instance, a Thread will listen and call your callback when necessary, and you can meanwhile:

  • subscribe to a topic:

subscribe

client.sub("topic");
  • unsubscribe:

unsubscribe

client.sub("topic", true);
  • publish a message to a topic:

publish

client.pub("topic", "message");

You can see this example as a starting point:

Import programniMQTT_example

An exemple for the lib niMQTT


4 comments on niMQTT's usage:

14 Aug 2013

hai there,

I tried this program to connect to an MQTT server running on java. But I failed to establish the connection. The serial terminal shows, Connection... Socket connection... Socket connection... Socket connection... ............................. The code is stuck @ do printf("Socket connection...\r\n"); while (socket->connect(server, port) < 0); socket->set_blocking(true, KEEP_ALIVE*500); KEEP_ALIVE / 2 in seconds please help me with this..... Regards.............Bimal

14 Aug 2013

Hi,

Amrita IOT wrote:

The code is stuck @ do printf("Socket connection...\r\n"); while (socket->connect(server, port) < 0); socket->set_blocking(true, KEEP_ALIVE*500); KEEP_ALIVE / 2 in seconds please help me with this..... Regards.............Bimal

well, if your socket can't connect to (server, port), it probably means that your port and/or your ip is not good, or there is a network issue between your server and your mbed.

19 Aug 2013

Guilhem Saure, Thank you for the response. I will check it. But my mbed works well in establishing a TCP connection to a different remote server using example TCP_example and sending some text also. But when I try to connect to the MQTT server @ port 1833, TCP connection is failing........

11 Sep 2013

Are you sure your server is listening on all the interfaces and not only localhost/127.0.0.1?

Please log in to post comments.