Pages: [1]   Go Down
Author Topic: Cosa MQTT client library  (Read 608 times)
0 Members and 1 Guest are viewing this topic.
Sweden
Offline Offline
Sr. Member
****
Karma: 11
Posts: 443
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The second increment of the Cosa MQTT client class is now available. This update will handling incoming publish messages with higher quality of service and correctly respond with acknowledgement messages (PUBACK/PUBREC/PUBCOMP). A new MQTT::Client member function publish_P will allow topic values from program memory buffers.

The typical publish usage pattern is as below:
Code:
  // Start ethernet controller and request network address for hostname
  ethernet.begin_P(HOSTNAME));

  // Start MQTT client with socket and connect to server
  client.begin(ethernet.socket(Socket::TCP));
  client.connect(SERVER, CLIENT);

  // Publish data
  client.publish(TOPIC, VALUE, sizeof(VALUE));
  client.disconnect();
The subscribe and service of incoming publish request usage pattern is:
Code:
class MQTTClient : public MQTT::Client {
public:
  virtual void on_publish(char* topic, void* buf, size_t count);
};

void
MQTTClient::on_publish(char* topic, void* buf, size_t count)
{
  // Handle incoming publish for topic/value
}

MQTTClient client;

void setup()
{
  // Start ethernet controller and request network address for hostname
  ethernet.begin_P(HOSTNAME);

  // Start MQTT client with socket and connect to server
  client.begin(ethernet.socket(Socket::TCP)));
  client.connect(SERVER, CLIENT);

  // Subscribe to a topic
  client.subscribe(TOPIC);
}

void loop()
{
  ...
  // Service incoming publish messages
  client.service());
  ...
}
Two new MQTT client example sketches are available.

1. CosaMQTTtemperature
Reads 1-wire temperature sensor (DS18B20) and publishes to MQTT server.
Example: https://github.com/mikaelpatel/Cosa/blob/master/examples/MQTT/CosaMQTTtemperature/CosaMQTTtemperature.ino

2. CosaMQTThumidity
Reads DHT11 humidity and temperature and publishes on two topics.
Example: https://github.com/mikaelpatel/Cosa/blob/master/examples/MQTT/CosaMQTThumidity/CosaMQTThumidity.ino

Cheers!
Logged

Pages: [1]   Go Up
Jump to: