All Projects → monstrenyatko → ArduinoMqtt

monstrenyatko / ArduinoMqtt

Licence: MIT License
MQTT client for Arduino

Programming Languages

c
50402 projects - #5 most used programming language
C++
36643 projects - #6 most used programming language

Projects that are alternatives of or similar to ArduinoMqtt

Luamqtt
luamqtt - Pure-lua MQTT v3.1.1 and v5.0 client
Stars: ✭ 58 (+0%)
Mutual labels:  mqtt, internet-of-things, mqtt-client
Hivemq Mqtt Client
HiveMQ MQTT Client is an MQTT 5.0 and MQTT 3.1.1 compatible and feature-rich high-performance Java client library with different API flavours and backpressure support
Stars: ✭ 402 (+593.1%)
Mutual labels:  mqtt, internet-of-things, mqtt-client
Tdm
GUI application to discover and monitor devices flashed with https://github.com/arendst/Sonoff-Tasmota
Stars: ✭ 385 (+563.79%)
Mutual labels:  mqtt, esp8266, mqtt-client
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 (-86.21%)
Mutual labels:  mqtt, esp8266, internet-of-things
Coogleiot
A ESP8266 Library for easy IOT device development
Stars: ✭ 118 (+103.45%)
Mutual labels:  mqtt, esp8266, mqtt-client
Psmqtt
Utility reporting system health and status via MQTT
Stars: ✭ 95 (+63.79%)
Mutual labels:  mqtt, internet-of-things, mqtt-client
server
MyController 2.x server
Stars: ✭ 14 (-75.86%)
Mutual labels:  mqtt, esp8266, internet-of-things
Paho.mqtt.java
Eclipse Paho Java MQTT client library. Paho is an Eclipse IoT project.
Stars: ✭ 1,620 (+2693.1%)
Mutual labels:  mqtt, internet-of-things, mqtt-client
Broadlink Mqtt
MQTT client to control BroadLink devices
Stars: ✭ 169 (+191.38%)
Mutual labels:  mqtt, internet-of-things, mqtt-client
Pjon
PJON (Padded Jittering Operative Network) is an experimental, arduino-compatible, multi-master, multi-media network protocol.
Stars: ✭ 2,615 (+4408.62%)
Mutual labels:  mqtt, esp8266, internet-of-things
thingspeak-esp-examples
ESP8266 / NodeMCU Examples for https://ThingSpeak.com
Stars: ✭ 76 (+31.03%)
Mutual labels:  esp8266, internet-of-things
esp8266-dht22
ESP8266 Lua script running on NodeMCU firmware which sends DHT22 data to web server, with deepsleep mode
Stars: ✭ 14 (-75.86%)
Mutual labels:  esp8266, internet-of-things
Jarolift MQTT
Controlling Jarolift TDEF 433MHZ radio shutters via ESP8266 and CC1101 Transceiver Module in asynchronous mode
Stars: ✭ 43 (-25.86%)
Mutual labels:  esp8266, mqtt-client
Tasmota
Alternative firmware for ESP8266 with easy configuration using webUI, OTA updates, automation using timers or rules, expandability and entirely local control over MQTT, HTTP, Serial or KNX. Full documentation at
Stars: ✭ 16,624 (+28562.07%)
Mutual labels:  mqtt, esp8266
mqtg-bot
MQTT Client Telegram Bot
Stars: ✭ 67 (+15.52%)
Mutual labels:  internet-of-things, mqtt-client
Eduponics-Mini
MicroPython MQTT & code example for Eduponics mini ESP32 learning kit
Stars: ✭ 41 (-29.31%)
Mutual labels:  internet-of-things, mqtt-client
ESP8266 mqtts
Arduino IDE project: send data from DS18B20 temperature sensor to mqtt.flespi.io via MQTT over SSL.
Stars: ✭ 16 (-72.41%)
Mutual labels:  mqtt, esp8266
CODESYS-MQTT
MQTT client library for CODESYS, supporting all QoS
Stars: ✭ 63 (+8.62%)
Mutual labels:  mqtt, mqtt-client
Emqtt
Erlang MQTT v5.0 Client
Stars: ✭ 253 (+336.21%)
Mutual labels:  mqtt, mqtt-client
mqttools
MQTT version 5.0 client and broker using asyncio
Stars: ✭ 44 (-24.14%)
Mutual labels:  mqtt, mqtt-client

ArduinoMqtt

About

MQTT Client library for Arduino based on the Eclipse Paho project. This library bundles the C/C++ MQTTPacket library of the Eclipse Paho project with simple synchronous C++ MQTT Client implementation to get the Arduino like API.

Features

  • Support of MQTT QoS0, QoS1 and simplified QoS2
  • Synchronous implementation. The method yield must be called regularly to allow incoming message processing and to send keep-alive packets in time
  • TCP network communication is out of the library scope. MqttClient::Network object must be provided with read and write implementations
  • System current time and other functions are provided externally by MqttClient::System object to allow easy adaptation to any other environments
  • External logger. MqttClient::Logger object is used to print logs with any convenient way for particular environment
  • No heap memory allocations. All required resources must be provided at the moment of MqttClient construction. It allows the full control and limiting of the used resources externally:
    • MqttClient::Buffer objects are used as send/receive temporary buffers
    • MqttClient::MessageHandlers object is used as storage of subscription callback functions
  • Idle interval calculator (See getIdleInterval method). Could be very useful If you going to put board/radio into low-power mode between data transmissions

Installation

Arduino IDE

Search for ArduinoMqtt using official Library Manager.

PlatformIO

Add ArduinoMqtt to the platformio.ini file to enable automatic installation:

lib_deps = ArduinoMqtt

Usage

See full Arduino example that covers Publish and Subscribe logic.

Debug output

Logging is disabled by default. Define MQTT_LOG_ENABLED equal 1 to enable.

If you can't add the define using compiler options (in case of Arduino IDE) just define it before including the library header:

#define MQTT_LOG_ENABLED 1
#include <MqttClient.h>

External Resources

Logger

Provide class implementing the MqttClient::Logger interface. Alternatively just instantiate the MqttClient::LoggerImpl template class that allows direct use of the Arduino HardwareSerial class or any other object with implemented void println(const char*) method:

#define HW_UART_SPEED									57600L
// Setup hardware serial for logging
Serial.begin(HW_UART_SPEED);
while (!Serial);
// Create MqttClient logger
MqttClient::Logger *mqttLogger = new MqttClient::LoggerImpl<HardwareSerial>(Serial);

System resources

Provide class implementing the MqttClient::System interface to allow library access to the system resources.

millis

Used to access the system time. The implementation must return the current time in milliseconds.

Arduino simple implementation:

class System: public MqttClient::System {
public:
	...
	unsigned long millis() const {
		return ::millis();
	}
	...
}

General C++ implementation might be like:

#include <chrono>

class TestSystem: public MqttClient::System {
	...
	unsigned long millis() const {
		return std::chrono::duration_cast<std::chrono::milliseconds>
		(std::chrono::system_clock::now().time_since_epoch()).count();
	}
	...
}
yield [optional]

Used to access the system yield.

Method is called regularly while long waits. Some systems like ESP requires calling of the yield regularly to:

  • maintain WiFi connection
  • reset watchdog timer

ESP8266 simple implementation:

class System: public MqttClient::System {
public:
	...
	void yield(void) {
		::yield();
	}
	...
}

Network

Provide class implementing the MqttClient::Network interface.

Custom helper

Instantiate the MqttClient::NetworkImpl template class that allows usage of any class with implemented methods like:

  • int read(unsigned char* buffer, int len, unsigned long timeoutMs)
  • int write(unsigned char* buffer, int len, unsigned long timeoutMs)

Proposed example assumes that TCP stack is connected using serial interface to Arduino pins 10(RX) and 11(TX). The SoftwareSerial library is used for actual communication:

#define SW_UART_PIN_RX								10
#define SW_UART_PIN_TX								11
#define SW_UART_SPEED								9600L

class Network {
public:
	Network() {
		mNet = new SoftwareSerial(SW_UART_PIN_RX, SW_UART_PIN_TX);
		mNet->begin(SW_UART_SPEED);
	}

	int read(unsigned char* buffer, int len, unsigned long timeoutMs) {
		mNet->setTimeout(timeoutMs);
		return mNet->readBytes((char*) buffer, len);
	}

	int write(unsigned char* buffer, int len, unsigned long timeoutMs) {
		mNet->setTimeout(timeoutMs);
		for (int i = 0; i < len; ++i) {
			mNet->write(buffer[i]);
		}
		mNet->flush();
		return len;
	}

private:
	SoftwareSerial										*mNet;
}

MqttClient::System *mqttSystem = new System;
MqttClient::Network *mqttNetwork = new MqttClient::NetworkImpl<Network>(*network, *mqttSystem);
Arduino Client helper

Another alternative allows using of Arduino Client compatible network classes like EthernetClient or WiFiClient of ESP8266 etc... To have this just instantiate the MqttClient::NetworkClientImpltemplate class:

EthernetClient network;
MqttClient::System *mqttSystem = new System;
MqttClient::Network * mqttNetwork = new MqttClient::NetworkClientImpl<Client>(network, *mqttSystem);

Buffers

Provide two buffers the one for transmitting message and another one for receiving message processing. Buffer is a class implementing the MqttClient::Buffer interface. Use the MqttClient::ArrayBuffer template class to get simplest implementation based on C array:

// Make 128 bytes send buffer
MqttClient::Buffer *mqttSendBuffer = new MqttClient::ArrayBuffer<128>();
// Make 128 bytes receive buffer
MqttClient::Buffer *mqttRecvBuffer = new MqttClient::ArrayBuffer<128>();

Message Handlers storage

Provide class implementing the MqttClient::MessageHandlers interface to keep subscription callback functions.

Simple helper

Instantiate the MqttClient::MessageHandlersImpl template class to get simplest implementation based on C array:

// Allows up to 2 subscriptions simultaneously
MqttClient::MessageHandlers *mqttMessageHandlers = new MqttClient::MessageHandlersImpl<2>();

This implementation does not copy the topic string and keeps only pointer. Please ensure the pointer remains valid while topic is subscribed. This is preferred implementation because of minimal resources requirements.

Static helper

Instantiate the MqttClient::MessageHandlersStaticImpl template class:

// Allows up to 2 subscriptions simultaneously
// Allows up to 64 bytes for topic string including 1 byte for string termination symbol
MqttClient::MessageHandlers *mqttMessageHandlers = new MqttClient::MessageHandlersStaticImpl<2, 64>();

This implementation preallocates buffers to store topic strings in advance. This implementation does copy the topic string. This is preferred implementation when the maximum topic length could be predicted because it avoids the heap allocations at the moment of subscribe call.

Dynamic helper

Instantiate the MqttClient::MessageHandlersDynamicImpl template class:

// Allows up to 2 subscriptions simultaneously
MqttClient::MessageHandlers *mqttMessageHandlers = new MqttClient::MessageHandlersDynamicImpl<2>();

This implementation allocates buffer to store topic string at the moment of subscribe call. This implementation does copy the topic string.

Development

VSCode (PlatformIO)

  1. Install IDE and extension, see official installation guide.
  2. Open the project (File -> Open).
  3. Use the PlatformIO Toolbar or PlatformIO CLI to build the project.

Useful links

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].