Skip to content

darcyg/arduino-mqtt

 
 

Repository files navigation

arduino-mqtt

paho mqtt library wrapper for arduino

This library bundles the Embedded MQTT C/C++ Client library of the eclipse paho project and adds a thin wrapper to get an Arduino like API.

The first release of the library only supports QoS0 and the basic features to get going. In the next releases more of the features will be available.

This library is an alternative to the pubsubclient library by knolleary which only supports QoS0 and uses a custom protocol implementation.

Download version 1.3.0 of the library.

Compatibility

This library has been only tested on the Arduino Yùn yet. Other boards and shields should work if they properly extend the Client API.

Example

#include <Bridge.h>
#include <YunClient.h>
#include <MQTTClient.h>

YunClient net;
MQTTClient client("connect.shiftr.io", 1883, net);

unsigned long lastMillis = 0;

void setup() {
  Bridge.begin();
  Serial.begin(9600);
  Serial.println("connecting...");
  if (client.connect("arduino", "demo", "demo")) {
    Serial.println("connected!");
    client.subscribe("/another/topic");
    // client.unsubscribe("/another/topic");
  } else {
    Serial.println("not connected!");
  }
}

void loop() {
  client.loop();
  // publish message roughly every second
  if(millis() - lastMillis > 1000) {
    lastMillis = millis();
    client.publish("/topic", "Hello world!");
  }
}

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

Configuration

// this buffer gets allocated two times to hold the outgoing and incomming message
#define MQTT_BUFFER_SIZE 64 // default 128

About

paho mqtt library wrapper for arduino

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C 60.3%
  • C++ 37.0%
  • Other 2.7%