All Projects → plapointe6 → Espmqttclient

plapointe6 / Espmqttclient

Licence: gpl-3.0
Wifi and MQTT handling for ESP8266 and ESP32

Projects that are alternatives of or similar to Espmqttclient

Blinker Doc
blinker中文文档
Stars: ✭ 139 (-17.75%)
Mutual labels:  arduino, esp32, mqtt, iot, esp8266, wifi
Blinker Library
An IoT Solution,Blinker library for embedded hardware. Works with Arduino, ESP8266, ESP32.
Stars: ✭ 1,095 (+547.93%)
Mutual labels:  arduino, esp32, mqtt, iot, esp8266, wifi
Dsckeybusinterface
An Arduino/esp8266/esp32 library to directly interface with DSC security systems.
Stars: ✭ 202 (+19.53%)
Mutual labels:  arduino, esp32, arduino-library, iot, esp8266
Arduinowebsockets
A library for writing modern websockets applications with Arduino (ESP8266 and ESP32)
Stars: ✭ 213 (+26.04%)
Mutual labels:  arduino, esp32, arduino-library, iot, esp8266
Esp32marauder
A suite of WiFi/Bluetooth offensive and defensive tools for the ESP32
Stars: ✭ 233 (+37.87%)
Mutual labels:  arduino, esp32, iot, esp8266, wifi
Blynk Server
Blynk is an Internet of Things Platform aimed to simplify building mobile and web applications for the Internet of Things. Easily connect 400+ hardware models like Arduino, ESP8266, ESP32, Raspberry Pi and similar MCUs and drag-n-drop IOT mobile apps for iOS and Android in 5 minutes
Stars: ✭ 8 (-95.27%)
Mutual labels:  arduino, esp32, mqtt, iot, esp8266
Blynk Library
Blynk library for embedded hardware. Works with Arduino, ESP8266, Raspberry Pi, Intel Edison/Galileo, LinkIt ONE, Particle Core/Photon, Energia, ARM mbed, etc.
Stars: ✭ 3,305 (+1855.62%)
Mutual labels:  arduino, esp32, iot, esp8266, wifi
Espui
A simple web user interface library for ESP32 and ESP8266
Stars: ✭ 330 (+95.27%)
Mutual labels:  arduino, esp32, arduino-library, iot, esp8266
Heatpump
Arduino library to control Mitsubishi Heat Pumps via connector cn105
Stars: ✭ 327 (+93.49%)
Mutual labels:  arduino, arduino-library, mqtt, esp8266, wifi
Esphelper
A library to make using WiFi & MQTT on the ESP8266 easy.
Stars: ✭ 310 (+83.43%)
Mutual labels:  arduino, esp32, mqtt, esp8266, wifi
Arduinojson
📟 JSON library for Arduino and embedded C++. Simple and efficient.
Stars: ✭ 5,456 (+3128.4%)
Mutual labels:  arduino, esp32, arduino-library, iot, esp8266
Bleeper
Library to manage your firmware configurations written in C++
Stars: ✭ 54 (-68.05%)
Mutual labels:  arduino, esp32, iot, esp8266, wifi
Home Assistant Config
My Home Assistant Configuration 🏡🏡
Stars: ✭ 133 (-21.3%)
Mutual labels:  arduino, mqtt, iot, esp8266
Server Go
🎨OpenIoTHub Server[内网穿透和物联网设备管理服务器]
Stars: ✭ 127 (-24.85%)
Mutual labels:  arduino, esp32, iot, esp8266
Pzem004t
Arduino communication library for Peacefair PZEM-004T Energy monitor
Stars: ✭ 165 (-2.37%)
Mutual labels:  arduino, esp32, arduino-library, esp8266
Sming
Sming - Open Source framework for high efficiency native ESP8266 development
Stars: ✭ 1,197 (+608.28%)
Mutual labels:  arduino, iot, esp8266, wifi
Aunit
Unit testing framework for Arduino platforms inspired by ArduinoUnit and Google Test. Used with AUniter or EpoxyDuino for continuous builds.
Stars: ✭ 73 (-56.8%)
Mutual labels:  arduino, esp32, arduino-library, esp8266
Tinygsm
A small Arduino library for GSM modules, that just works
Stars: ✭ 1,186 (+601.78%)
Mutual labels:  arduino, esp32, mqtt, esp8266
Gateway Go
🎁GateWay Client for OpenIoTHub[云易连访问内网端口和设备的网关]
Stars: ✭ 127 (-24.85%)
Mutual labels:  arduino, esp32, iot, esp8266
Esp8266 aliyun mqtt app
基于ESP8266官方SDK快速接入阿里云物联网平台
Stars: ✭ 81 (-52.07%)
Mutual labels:  mqtt, iot, esp8266, wifi

MQTT and Wifi handling for ESP8266 and ESP32

This library is intended to encapsulate the handling of WiFi and MQTT connections of an ESP8266/ESP32. You just need to provide your credentials and it will manage the following things:

  • Connecting to a WiFi network.
  • Connecting to a MQTT broker.
  • Automatically detecting connection lost either from the WiFi client or the MQTT broker and it will retry a connection automatically.
  • Subscribing/unsubscrubing to/from MQTT topics by a friendly callback system.
  • Supports a single occurrence of a '+' or '#' wildcard in subscriptions
  • Provide a callback handling to advise once everything is connected (Wifi and MQTT).
  • Provide a function to enable printing of useful debug information related to MQTT and Wifi connections.
  • Provide some other useful utilities for MQTT and Wifi management.
  • Provide a function to enable an HTTP Update server secured by a password to allow remote update.

Dependency

The MQTT communication depends on the PubSubClient Library (https://github.com/knolleary/pubsubclient).

Example

#include "EspMQTTClient.h"

EspMQTTClient client(
  "WifiSSID",
  "WifiPassword",
  "192.168.1.100",  // MQTT Broker server ip
  "MQTTUsername",   // Can be omitted if not needed
  "MQTTPassword",   // Can be omitted if not needed
  "TestClient"      // Client name that uniquely identify your device
);

void setup() {}

void onConnectionEstablished() {

  client.subscribe("mytopic/test", [] (const String &payload)  {
    Serial.println(payload);
  });

  client.publish("mytopic/test", "This is a message");
}

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

See "SimpleMQTTClient.ino" for the complete example.

Documentation

Construction

For Wifi and MQTT connection handling (Recommended) :

  EspMQTTClient(
    const char* wifiSsid,
    const char* wifiPassword,
    const char* mqttServerIp,
    const char* mqttUsername,  // Omit this parameter to disable MQTT authentification
    const char* mqttPassword,  // Omit this parameter to disable MQTT authentification
    const char* mqttClientName = "ESP8266",
    const short mqttServerPort = 1883);

MQTT connection handling only :

  EspMQTTClient(
    const char* mqttServerIp,
    const short mqttServerPort,  // It is mandatory here to allow these constructors to be distinct from those with the Wifi handling parameters
    const char* mqttUsername,    // Omit this parameter to disable MQTT authentification
    const char* mqttPassword,    // Omit this parameter to disable MQTT authentification
    const char* mqttClientName = "ESP8266");

Functions

IMPORTANT : Must be called at each loop() of your sketch

void loop();

Basic functions for MQTT communications.

bool publish(const String &topic, const String &payload, bool retain = false);
bool subscribe(const String &topic, MessageReceivedCallback messageReceivedCallback, uint8_t qos = 0);
bool unsubscribe(const String &topic);

Change the maximum packet size that can be sent over MQTT. The default is 128 bytes.

bool setMaxPacketSize(const uint16_t size);

Change the keepalive interval (15 seconds by default)

void setKeepAlive(uint16_t keepAliveSeconds);

Enable the display of usefull debugging messages that will output to serial.

void enableDebuggingMessages(const bool enabled = true);

Enable the web updater. This will host a simple form that will allow firmware upgrade (using, e.g., the .bin file produced by "Export Compiled Binary" in the Arduino IDE's "Sketch" menu). Must be set before the first loop() call.

void enableHTTPWebUpdater(const char* username, const char* password, const char* address = "/");

// this one will set user and password equal to those set for the MQTT connection.
void enableHTTPWebUpdater(const char* address = "/");

Enable last will message. Must be set before the first loop() call.

void enableLastWillMessage(const char* topic, const char* message, const bool retain = false);

Tell the broker to establish a persistent connection. Disabled by default. Must be called before the first loop() execution

void enableMQTTPersistence();

Change the delay between each MQTT reconnection attempt. Default is 15 seconds.

void setMqttReconnectionAttemptDelay(const unsigned int milliseconds);

Change the delay between each Wifi reconnection attempt. Default is 60 seconds.

void setWifiReconnectionAttemptDelay(const unsigned int milliseconds);

Connection status

bool isConnected(); // Return true if everything is connected.
bool isWifiConnected(); // Return true if WiFi is connected.
bool isMqttConnected(); // Return true if MQTT is connected.
bool getConnectionEstablishedCount() // Return the number of time onConnectionEstablished has been called since the beginning.

As ESP8266 does not like to be interrupted too long with the delay() function, this function will allow a delayed execution of a function without interrupting the sketch.

void executeDelayed(const long delay, DelayedExecutionCallback callback);

Some useful getters

const char* getMqttClientName();
const char* getMqttServerIp();
const short getMqttServerPort();

Connection established callback

To allow this library to work, you need to implement the onConnectionEstablished() function in your sketch.

void onConnectionEstablished()
{
  // Here you are sure that everything is connected.
}

In some special cases, like if you want to handle more than one MQTT connection in the same sketch, you can override this callback to another one for the second MQTT client using this function :

void setOnConnectionEstablishedCallback(ConnectionEstablishedCallback callback);

See exemple "twoMQTTClientHandling.ino" for more details.

Subscribing to topics

The function subscribe allow to subscribe to a specific topic.

For exemple, if you want to subscribe to topic test/mytopic, you can do this :

void onTestMessageReceived(const String& message) {
  Serial.print("message received from test/mytopic: " + message);
}

client.subscribe("test/mytopic", onTestMessageReceived);

You can also use lambdas to shorten the code like this :

client.subscribe("test/mytopic", [](const String& message) {
  Serial.print("message received from test/mytopic: " + message;
});

Wildcards

This library also handle MQTT topic wilcards. Most of the time, you will want to see what was the original topic when the callback is called. Here is how to do that.

Exemple : Subscribe to "wildcardtest/#" and display received topic and message to Serial

void onMessageReceived(const String& topic, const String& message) {
  Serial.println(topic + ": " + message);
}

client.subscribe("wildcardtest/#", onMessageReceived);

The same thing with lambdas :

  client.subscribe("wildcardtest/#", [](const String& topic, const String& message) {
    Serial.println(topic + ": " + message);
  });
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].