EFWob / ESPPubSubClientWrapper

MIT License
3 stars 0 forks source link

Latest changes

20221130

Arduino ESP8266/ESP32 Client for MQTT using PubSubClient-Library

This library provides a client for doing simple publish/subscribe messaging with a server that supports MQTT. It wraps the functionality of the PubSubClient-Library by deriving a wrapper class from PubSubClient class.

The main additions to the base class are:

Basic usage and deviations to PubSubClient

Improved callback handling

By default, you can use only one central callback to react on incoming messages using the subscribe() method. If you subscribe to multiple topics, you have to check each incoming topic and select the appropriate reaction. This is simpilfied with the new on()-methods, which allow to link seperate callback functions to different topics.

There are two variants of the on()-method:

The following will apply for either variant:

Minimalistic Example

The following is a minimalistic example (for ESP8266). In this example two subscriptions are made to the topics "hello" and "world". If message "hello" is received the function callbackHello() will be called, if "world" is received callbackWorld() will be called.

callbackHello() uses the simplified API (the payload is a zero-terminated char string) callbackWorld() uses the standard approach with payload being represented by an uint8_t array and the payload length as additional parameter.

The functionality is the same in both cases (payload will be echoed to Serial monitor).

#include <ESP8266WiFi.h>
#include <ESPPubSubClientWrapper.h>

const char* ssid = "..........";                // Put your WiFi credentials here
const char* password = "..........";
const char* mqtt_server = "broker.mqtt-dashboard.com";

ESPPubSubClientWrapper client(mqtt_server);

/*
This function will be called if topic "hello" is received on MQTT and echo the payload on Serial monitor.
It uses the simplified API with payload being converted to a 0 terminated char pointer (or NULL if no payload was sent)
*/  
void callbackHello(char* topic, char * payload) {
  Serial.println("\r\nMessage ""hello"" received");
  if (payload)
  {
    Serial.printf("Payload-len=%d, Payload=\"%s\"\r\n", strlen(payload), payload);
  }
  else
    Serial.println("Payload is NULL.");
}

/*
This function will be called if topic "world" is received on MQTT and echo the payload on Serial monitor.
It uses the default API with payload as uint_8-array with valid length given by payloadLen (0, if no payload was sent)
*/  
void callbackWorld(char* topic, uint8_t* payload, unsigned int payloadLen) {
  Serial.println("\r\nMessage ""world"" received");
  if (payload)
  {
    char s[payloadLen + 1];
    memcpy(s, payload, payloadLen);
    s[payloadLen] = '\0';
    Serial.printf("Payload-len=%d, Payload=\"%s\"\r\n", payloadLen, s);
  }
  else
    Serial.println("Payload is NULL.");

}

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  client.on("hello", callbackHello);
  client.on("world", callbackWorld);  
}

void loop() {
  client.loop();
}

Limitations

Compatible Hardware

License

This code is released under the MIT License.