Convert Figma logo to code with AI

knolleary logopubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.

3,810
1,468
3,810
558

Top Related Projects

MQTT library for Arduino

📟 JSON library for Arduino and embedded C++. Simple and efficient.

Quick Overview

PubSubClient is a lightweight MQTT client library for Arduino and ESP8266. It allows Arduino devices to publish messages to and subscribe to topics on an MQTT broker, enabling IoT communication and device management.

Pros

  • Simple and easy to use for beginners
  • Low memory footprint, suitable for resource-constrained devices
  • Supports QoS 0 and 1 for message delivery
  • Compatible with a wide range of Arduino boards and ESP8266

Cons

  • Limited support for advanced MQTT features
  • No built-in SSL/TLS support (requires external libraries)
  • May not be suitable for complex IoT applications requiring extensive functionality
  • Limited documentation for advanced use cases

Code Examples

  1. Connecting to an MQTT broker:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid = "YourWiFiSSID";
const char* password = "YourWiFiPassword";
const char* mqtt_server = "broker.mqtt.com";

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  WiFi.begin(ssid, password);
  client.setServer(mqtt_server, 1883);
  client.connect("ArduinoClient");
}
  1. Publishing a message:
void loop() {
  if (client.connected()) {
    client.publish("sensor/temperature", "25.5");
  }
  delay(5000);
}
  1. Subscribing to a topic and handling incoming messages:
void callback(char* topic, byte* payload, unsigned int length) {
  String message = "";
  for (int i = 0; i < length; i++) {
    message += (char)payload[i];
  }
  Serial.println("Message received: " + message);
}

void setup() {
  // ... (previous setup code)
  client.setCallback(callback);
  client.subscribe("control/led");
}

Getting Started

  1. Install the PubSubClient library in Arduino IDE:

    • Go to Sketch > Include Library > Manage Libraries
    • Search for "PubSubClient" and install it
  2. Include the library and set up the client:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid = "YourWiFiSSID";
const char* password = "YourWiFiPassword";
const char* mqtt_server = "broker.mqtt.com";

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  client.setServer(mqtt_server, 1883);
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
}

void reconnect() {
  while (!client.connected()) {
    if (client.connect("ArduinoClient")) {
      client.subscribe("inTopic");
    } else {
      delay(5000);
    }
  }
}

Competitor Comparisons

MQTT library for Arduino

Pros of arduino-mqtt

  • Supports MQTT 3.1.1 protocol features like QoS 2 and retained messages
  • More flexible API allowing for custom network clients
  • Smaller memory footprint, especially for simple use cases

Cons of arduino-mqtt

  • Less widely adopted compared to PubSubClient
  • Fewer examples and community resources available
  • May require more manual configuration for certain use cases

Code Comparison

PubSubClient:

PubSubClient client(espClient);
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
client.connect("ArduinoClient");
client.publish("outTopic", "hello world");

arduino-mqtt:

MQTTClient client;
client.begin("mqtt_server", 1883, net);
client.onMessage(messageReceived);
client.connect("arduino", "user", "pass");
client.publish("/hello", "world");

Both libraries offer similar basic functionality for MQTT communication on Arduino platforms. PubSubClient is more widely used and has extensive community support, while arduino-mqtt provides a more modern API and supports newer MQTT features. The choice between them depends on specific project requirements and personal preference.

📟 JSON library for Arduino and embedded C++. Simple and efficient.

Pros of ArduinoJson

  • More versatile, handling JSON parsing and serialization for various data types
  • Extensive documentation and examples for different use cases
  • Optimized for memory-constrained devices with customizable memory allocation

Cons of ArduinoJson

  • Larger library size, potentially impacting storage on small devices
  • Steeper learning curve due to more complex API and features
  • Not specifically designed for MQTT communication like PubSubClient

Code Comparison

ArduinoJson:

StaticJsonDocument<200> doc;
doc["sensor"] = "temperature";
doc["value"] = 25.5;
String output;
serializeJson(doc, output);

PubSubClient:

char payload[50];
snprintf(payload, sizeof(payload), "{\"sensor\":\"temperature\",\"value\":25.5}");
client.publish("topic", payload);

ArduinoJson offers a more structured approach to handling JSON data, while PubSubClient focuses on MQTT communication with simpler message handling. ArduinoJson provides better tools for complex JSON operations, but PubSubClient is more lightweight and specific to MQTT protocols. The choice between them depends on the project's requirements for JSON manipulation versus MQTT functionality.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Arduino Client for MQTT

This library provides a client for doing simple publish/subscribe messaging with a server that supports MQTT.

Examples

The library comes with a number of example sketches. See File > Examples > PubSubClient within the Arduino application.

Full API documentation is available here: https://pubsubclient.knolleary.net

Limitations

  • It can only publish QoS 0 messages. It can subscribe at QoS 0 or QoS 1.
  • The maximum message size, including header, is 256 bytes by default. This is configurable via MQTT_MAX_PACKET_SIZE in PubSubClient.h or can be changed by calling PubSubClient::setBufferSize(size).
  • The keepalive interval is set to 15 seconds by default. This is configurable via MQTT_KEEPALIVE in PubSubClient.h or can be changed by calling PubSubClient::setKeepAlive(keepAlive).
  • The client uses MQTT 3.1.1 by default. It can be changed to use MQTT 3.1 by changing value of MQTT_VERSION in PubSubClient.h.

Compatible Hardware

The library uses the Arduino Ethernet Client api for interacting with the underlying network hardware. This means it Just Works with a growing number of boards and shields, including:

  • Arduino Ethernet
  • Arduino Ethernet Shield
  • Arduino YUN – use the included YunClient in place of EthernetClient, and be sure to do a Bridge.begin() first
  • Arduino WiFi Shield - if you want to send packets > 90 bytes with this shield, enable the MQTT_MAX_TRANSFER_SIZE define in PubSubClient.h.
  • Sparkfun WiFly Shield – library
  • TI CC3000 WiFi - library
  • Intel Galileo/Edison
  • ESP8266
  • ESP32

The library cannot currently be used with hardware based on the ENC28J60 chip – such as the Nanode or the Nuelectronics Ethernet Shield. For those, there is an alternative library available.

License

This code is released under the MIT License.