Go Down

Topic: Cosa MQTT client library (Read 1 time) previous topic - next topic

kowalski

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: [Select]

  // 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: [Select]

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!

AcmeUK

Oh Great thinks |. I must look at the examples.

OH Dear, Dead links!!!

kowalski

Old stuff. Try https://github.com/mikaelpatel/Cosa/tree/master/examples/IoT/MQTT

Cheers!

Go Up